From dc0c46f5f74a9e0468236b31c8d9364eb24c3624 Mon Sep 17 00:00:00 2001 From: Charley Wang Date: Wed, 23 Sep 2009 14:00:33 -0400 Subject: PR1062: runtime function * arith.c (_stp_random_pm_u): New function. --- runtime/arith.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'runtime/arith.c') diff --git a/runtime/arith.c b/runtime/arith.c index d1d0da29..4b0e6c9e 100644 --- a/runtime/arith.c +++ b/runtime/arith.c @@ -106,6 +106,28 @@ static int _stp_random_pm (int n) #endif /* _STP_TEST_ */ +#ifndef _STP_TEST_ +/** Return a random integer between 0 and n - 1. + * @param n how far from zero to go. Make it positive but less than a million or so. + */ +static int _stp_random_pm_u (int n) +{ + static unsigned long seed; + static int initialized_p = 0; + + if (unlikely (! initialized_p)) { + seed = (unsigned long) jiffies; + initialized_p = 1; + } + + /* from glibc rand man page */ + seed = seed * 1103515245 + 12345; + + return (seed % n); +} +#endif /* _STP_TEST_ */ + + #if defined (__i386__) || defined (__arm__) /* 64-bit division functions extracted from libgcc */ -- cgit From d098276239dd9f2e1ad031b48519a5a21779c369 Mon Sep 17 00:00:00 2001 From: "Frank Ch. Eigler" Date: Wed, 23 Sep 2009 16:41:40 -0400 Subject: PR10632: simplify randint() implementation * tapset/random.stp (randint): Make it 1-arity (imply min=0). Document with kerneldoc. * doc/Systemtap_Tapset_Reference/tapsets.tmpl: Extract the docs. * runtime/arith.c (_stp_random_pm_u): Rename without _pm. (_stp_random_pm): Rewrite in terms of ..._u. * testsuite/random.stp: Adapt & simplify. --- runtime/arith.c | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) (limited to 'runtime/arith.c') diff --git a/runtime/arith.c b/runtime/arith.c index 4b0e6c9e..4c818a76 100644 --- a/runtime/arith.c +++ b/runtime/arith.c @@ -84,48 +84,34 @@ static int64_t _stp_mod64 (const char **error, int64_t x, int64_t y) } -#ifndef _STP_TEST_ -/** Return a random integer between -n and n. +/** Return a random integer between 0 and n - 1. * @param n how far from zero to go. Make it positive but less than a million or so. */ -static int _stp_random_pm (int n) +static unsigned long _stp_random_u (unsigned long n) { static unsigned long seed; static int initialized_p = 0; - + if (unlikely (! initialized_p)) { seed = (unsigned long) jiffies; initialized_p = 1; } - + /* from glibc rand man page */ seed = seed * 1103515245 + 12345; - - return (seed % (2*n+1)-n); + + return (n == 0 ? 0 : seed % n); } -#endif /* _STP_TEST_ */ -#ifndef _STP_TEST_ -/** Return a random integer between 0 and n - 1. +/** Return a random integer between -n and n. * @param n how far from zero to go. Make it positive but less than a million or so. */ -static int _stp_random_pm_u (int n) +static int _stp_random_pm (unsigned n) { - static unsigned long seed; - static int initialized_p = 0; - - if (unlikely (! initialized_p)) { - seed = (unsigned long) jiffies; - initialized_p = 1; - } - - /* from glibc rand man page */ - seed = seed * 1103515245 + 12345; - - return (seed % n); + return -(int)n + (int)_stp_random_u (2*n + 1); } -#endif /* _STP_TEST_ */ + #if defined (__i386__) || defined (__arm__) -- cgit