trinity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 04/12] Remove static from rand_single_bit()
@ 2015-03-06 22:30 tyson.w.smith
  0 siblings, 0 replies; only message in thread
From: tyson.w.smith @ 2015-03-06 22:30 UTC (permalink / raw)
  To: davej; +Cc: trinity, Tyson Smith

From: Tyson Smith <tysmith@motorola.com>

Added a check to verify input value is between 0 and 32 or 64 bits
depending on __WORDSIZE.
---
 include/random.h | 1 +
 random.c         | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/include/random.h b/include/random.h
index f070568..69ccfa3 100644
--- a/include/random.h
+++ b/include/random.h
@@ -24,4 +24,5 @@ unsigned int rand_bool(void);
 unsigned int rand32(void);
 u64 rand64(void);
 unsigned int rand_range(unsigned int min, unsigned int max);
+unsigned long rand_single_bit(unsigned char size);
 unsigned long set_rand_bitmask(unsigned int num, const unsigned long *values);
diff --git a/random.c b/random.c
index adee25e..063912f 100644
--- a/random.c
+++ b/random.c
@@ -69,8 +69,14 @@ unsigned int rand_bool(void)
 	return rand() % 2;
 }
 
-static unsigned int rand_single_bit(unsigned char size)
+/*
+ * Pick a random power of two between 2^0 and 2^(__WORDSIZE-1)
+ */
+unsigned long rand_single_bit(unsigned char size)
 {
+	if (size > __WORDSIZE)
+		size = __WORDSIZE;
+
 	return (1UL << (rand() % size));
 }
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-03-06 22:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-06 22:30 [PATCH 04/12] Remove static from rand_single_bit() tyson.w.smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).