This is the mail archive of the libc-hacker@sourceware.org mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix {j,m}rand48{,_r} (BZ #3747)


Hi!

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html
requires:
"The mrand48() and jrand48() functions shall return signed long integers
uniformly distributed over the interval [-2^31,2^31)."

We do that on 32-bit arches, but on 64-bit architectures we return
signed long integers uniformly distributed over the interval
[0,2^32).

Fixed thusly:

2006-12-18  Jakub Jelinek  <jakub@redhat.com>

	[BZ #3747]
	* stdlib/jrand48_r.c (__jrand48_r): Make sure result is in the
	[-2^31 .. 2^31) range.
	* stdlib/tst-rand48.c (main): Fix expected values for 64-bit
	targets.
	* stdlib/tst-rand48-2.c: New test.
	* stdlib/Makefile (tests): Add tst-rand48-2.

--- libc/stdlib/jrand48_r.c.jj	2001-07-06 06:55:41.000000000 +0200
+++ libc/stdlib/jrand48_r.c	2006-12-18 11:46:57.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995, 1997, 1998, 2001 Free Software Foundation, Inc.
+/* Copyright (C) 1995, 1997, 1998, 2001, 2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
 
@@ -30,7 +30,7 @@ __jrand48_r (xsubi, buffer, result)
     return -1;
 
   /* Store the result.  */
-  *result = ((xsubi[2] << 16) | xsubi[1]) & 0xffffffffl;
+  *result = (int32_t) ((xsubi[2] << 16) | xsubi[1]);
 
   return 0;
 }
--- libc/stdlib/Makefile.jj	2006-12-11 21:34:14.000000000 +0100
+++ libc/stdlib/Makefile	2006-12-18 12:22:20.000000000 +0100
@@ -67,7 +67,7 @@ tests		:= tst-strtol tst-strtod testmb t
 		   tst-xpg-basename tst-random tst-random2 tst-bsearch	    \
 		   tst-limits tst-rand48 bug-strtod tst-setcontext	    \
 		   test-a64l tst-qsort tst-system testmb2 bug-strtod2	    \
-		   tst-atof1 tst-atof2 tst-strtod2
+		   tst-atof1 tst-atof2 tst-strtod2 tst-rand48-2
 
 include ../Makeconfig
 
--- libc/stdlib/tst-rand48.c.jj	2001-01-20 04:32:19.000000000 +0100
+++ libc/stdlib/tst-rand48.c	2006-12-18 12:33:59.000000000 +0100
@@ -44,10 +44,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0xa28c1003l)
+  if (l != -0x5d73effdl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0xa28c1003l, l);
+	      __LINE__ - 4, -0x5d73effdl, l);
       result = 1;
     }
 
@@ -60,10 +60,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0x9e88f474l)
+  if (l != -0x61770b8cl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0x9e88f474l, l);
+	      __LINE__ - 4, -0x61770b8cl, l);
       result = 1;
     }
 
@@ -92,10 +92,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0xeb7a1fa3l)
+  if (l != -0x1485e05dl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0xeb7a1fa3l, l);
+	      __LINE__ - 4, -0x1485e05dl, l);
       result = 1;
     }
 
@@ -171,10 +171,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0xa28c1003l)
+  if (l != -0x5d73effdl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0xa28c1003l, l);
+	      __LINE__ - 4, -0x5d73effdl, l);
       result = 1;
     }
 
@@ -187,10 +187,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0x9e88f474l)
+  if (l != -0x61770b8cl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0x9e88f474l, l);
+	      __LINE__ - 4, -0x61770b8cl, l);
       result = 1;
     }
 
@@ -231,10 +231,10 @@ main (void)
     }
 
   l = mrand48 ();
-  if (l != 0xeb7a1fa3l)
+  if (l != -0x1485e05dl)
     {
       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0xeb7a1fa3l, l);
+	      __LINE__ - 4, -0x1485e05dl, l);
       result = 1;
     }
 
@@ -287,10 +287,10 @@ main (void)
     }
 
   l = jrand48 (xs);
-  if (l != 0xf568c7a0l)
+  if (l != -0xa973860l)
     {
       printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
-	      __LINE__ - 4, 0xf568c7a0l, l);
+	      __LINE__ - 4, -0xa973860l, l);
       result = 1;
     }
 
--- libc/stdlib/tst-rand48-2.c.jj	2006-12-18 12:12:01.000000000 +0100
+++ libc/stdlib/tst-rand48-2.c	2006-12-18 12:20:26.000000000 +0100
@@ -0,0 +1,113 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+int
+main (void)
+{
+  time_t t = time (NULL);
+  int i, ret = 0;
+  double d;
+  long int l;
+  struct drand48_data data;
+  unsigned short int buf[3];
+
+  srand48 ((long int) t);
+  for (i = 0; i < 50; i++)
+    if ((d = drand48 ()) < 0.0 || d >= 1.0)
+      {
+        printf ("drand48 %d %g\n", i, d);
+        ret = 1;
+      }
+
+  srand48_r ((long int) t, &data);
+  for (i = 0; i < 50; i++)
+    if (drand48_r (&data, &d) != 0 || d < 0.0 || d >= 1.0)
+      {
+        printf ("drand48_r %d %g\n", i, d);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if ((d = erand48 (buf)) < 0.0 || d >= 1.0)
+      {
+        printf ("erand48 %d %g\n", i, d);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if (erand48_r (buf, &data, &d) != 0 || d < 0.0 || d >= 1.0)
+      {
+        printf ("erand48_r %d %g\n", i, d);
+        ret = 1;
+      }
+
+  srand48 ((long int) t);
+  for (i = 0; i < 50; i++)
+    if ((l = lrand48 ()) < 0 || l > INT32_MAX)
+      {
+        printf ("lrand48 %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  srand48_r ((long int) t, &data);
+  for (i = 0; i < 50; i++)
+    if (lrand48_r (&data, &l) != 0 || l < 0 || l > INT32_MAX)
+      {
+        printf ("lrand48_r %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if ((l = nrand48 (buf)) < 0 || l > INT32_MAX)
+      {
+        printf ("nrand48 %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if (nrand48_r (buf, &data, &l) != 0 || l < 0 || l > INT32_MAX)
+      {
+        printf ("nrand48_r %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  srand48 ((long int) t);
+  for (i = 0; i < 50; i++)
+    if ((l = mrand48 ()) < INT32_MIN || l > INT32_MAX)
+      {
+        printf ("mrand48 %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  srand48_r ((long int) t, &data);
+  for (i = 0; i < 50; i++)
+    if (mrand48_r (&data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
+      {
+        printf ("mrand48_r %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if ((l = jrand48 (buf)) < INT32_MIN || l > INT32_MAX)
+      {
+        printf ("jrand48 %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
+  for (i = 0; i < 50; i++)
+    if (jrand48_r (buf, &data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
+      {
+        printf ("jrand48_r %d %ld\n", i, l);
+        ret = 1;
+      }
+
+  return ret;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]