From: tyson.w.smith@gmail.com
To: davej@codemonkey.org.uk
Cc: trinity@vger.kernel.org, Tyson Smith <tysmith@motorola.com>
Subject: [PATCH 04/12] Remove static from rand_single_bit()
Date: Fri, 6 Mar 2015 14:30:39 -0800 [thread overview]
Message-ID: <1425681039-28465-1-git-send-email-tyson.w.smith@gmail.com> (raw)
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
reply other threads:[~2015-03-06 22:30 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1425681039-28465-1-git-send-email-tyson.w.smith@gmail.com \
--to=tyson.w.smith@gmail.com \
--cc=davej@codemonkey.org.uk \
--cc=trinity@vger.kernel.org \
--cc=tysmith@motorola.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).