From 09846ceb72c27dfe87f0b5c8d0a6296fc09bbc36 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Wed, 23 Sep 2009 13:46:05 -0400 Subject: PR10632: tapset: randint() function + tests --- tapset/random.stp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tapset/random.stp (limited to 'tapset') diff --git a/tapset/random.stp b/tapset/random.stp new file mode 100644 index 00000000..e0a89d19 --- /dev/null +++ b/tapset/random.stp @@ -0,0 +1,18 @@ +/** @addtogroup library +* The library tapset is a collection of standard functions. +* @{ +*/ + +function randint:long(min:long, max:long) +%{ + + unsigned long difference = (unsigned long)(THIS->max - THIS->min); + + if ( THIS->min >= THIS->max || (THIS->max -THIS->min) > (1024*1024) ){ + CONTEXT->last_error = "either first argument was not strictly less than the second argument, or their difference was greater than (1024*1024)"; + } + THIS->__retvalue = THIS->min + ( _stp_random_pm_u(2147483646) % (difference + 1) ); + +%} + +/** @} */ -- 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. --- tapset/random.stp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'tapset') diff --git a/tapset/random.stp b/tapset/random.stp index e0a89d19..ada94216 100644 --- a/tapset/random.stp +++ b/tapset/random.stp @@ -1,18 +1,14 @@ -/** @addtogroup library -* The library tapset is a collection of standard functions. -* @{ -*/ - -function randint:long(min:long, max:long) +/** + * sfunction randint - Return a random number between [0,n) + * @n: Number past upper limit of range, not larger than 2**20. + */ +function randint:long(n:long) %{ - - unsigned long difference = (unsigned long)(THIS->max - THIS->min); - - if ( THIS->min >= THIS->max || (THIS->max -THIS->min) > (1024*1024) ){ - CONTEXT->last_error = "either first argument was not strictly less than the second argument, or their difference was greater than (1024*1024)"; - } - THIS->__retvalue = THIS->min + ( _stp_random_pm_u(2147483646) % (difference + 1) ); - +#define RANDMAX (1024*1024) + if (THIS->n > RANDMAX) + CONTEXT->last_error = "range too wide"; + else { + THIS->__retvalue = (uint64_t) _stp_random_u((unsigned long) THIS->n); + } +#undef RANDMAX %} - -/** @} */ -- cgit From 243a16e69fa639092a58df581b1aba91269c8fba Mon Sep 17 00:00:00 2001 From: "Frank Ch. Eigler" Date: Wed, 23 Sep 2009 22:36:48 -0400 Subject: PR10632: make randint() unprivileged --- tapset/random.stp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tapset') diff --git a/tapset/random.stp b/tapset/random.stp index ada94216..9b2fdc70 100644 --- a/tapset/random.stp +++ b/tapset/random.stp @@ -3,7 +3,7 @@ * @n: Number past upper limit of range, not larger than 2**20. */ function randint:long(n:long) -%{ +%{ /* unprivileged */ #define RANDMAX (1024*1024) if (THIS->n > RANDMAX) CONTEXT->last_error = "range too wide"; -- cgit