summaryrefslogtreecommitdiff
path: root/libc/stdlib
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-03-13 23:38:40 +0000
committerEric Andersen <andersen@codepoet.org>2002-03-13 23:38:40 +0000
commit94e5ab630be58fa6d008fbfa735492bd6ce2a568 (patch)
treed95f3ae872c4a15c253d3e8f126891a923ab71da /libc/stdlib
parente3d8cc9c9912c12576f2623bec382ca96f36bb9a (diff)
Merge in an ugly pile of rand* functions from glibc. :(
Diffstat (limited to 'libc/stdlib')
-rw-r--r--libc/stdlib/Makefile7
-rw-r--r--libc/stdlib/drand48-iter.c59
-rw-r--r--libc/stdlib/drand48.c32
-rw-r--r--libc/stdlib/drand48_r.c27
-rw-r--r--libc/stdlib/erand48.c32
-rw-r--r--libc/stdlib/erand48_r.c48
-rw-r--r--libc/stdlib/jrand48.c32
-rw-r--r--libc/stdlib/jrand48_r.c35
-rw-r--r--libc/stdlib/lrand48.c32
-rw-r--r--libc/stdlib/lrand48_r.c29
-rw-r--r--libc/stdlib/mrand48.c32
-rw-r--r--libc/stdlib/mrand48_r.c29
-rw-r--r--libc/stdlib/nrand48.c32
-rw-r--r--libc/stdlib/nrand48_r.c38
-rw-r--r--libc/stdlib/rand_r.c48
-rw-r--r--libc/stdlib/srand48.c28
-rw-r--r--libc/stdlib/srand48_r.c40
17 files changed, 578 insertions, 2 deletions
diff --git a/libc/stdlib/Makefile b/libc/stdlib/Makefile
index 63f2298e9..32c24a0d2 100644
--- a/libc/stdlib/Makefile
+++ b/libc/stdlib/Makefile
@@ -38,8 +38,11 @@ MSRC2=atexit.c
MOBJ2=atexit.o on_exit.o __exit_handler.o exit.o
CSRC = abort.c getenv.c mktemp.c qsort.c realpath.c bsearch.c \
- mkstemp.c putenv.c rand.c random.c setenv.c system.c div.c ldiv.c \
- getpt.c ptsname.c grantpt.c unlockpt.c gcvt.c
+ mkstemp.c putenv.c rand.c random.c setenv.c system.c div.c \
+ ldiv.c getpt.c ptsname.c grantpt.c unlockpt.c gcvt.c
+CSRC+= drand48.c drand48-iter.c drand48_r.c erand48.c erand48_r.c \
+ jrand48.c jrand48_r.c lrand48.c lrand48_r.c mrand48.c mrand48_r.c \
+ nrand48.c nrand48_r.c rand_r.c srand48.c srand48_r.c
ifeq ($(HAS_FLOATING_POINT),true)
CSRC += strtod.c
endif
diff --git a/libc/stdlib/drand48-iter.c b/libc/stdlib/drand48-iter.c
new file mode 100644
index 000000000..7ccc2fbc9
--- /dev/null
+++ b/libc/stdlib/drand48-iter.c
@@ -0,0 +1,59 @@
+/* Copyright (C) 1995, 1996, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+/* Global state for non-reentrant functions. */
+struct drand48_data __libc_drand48_data;
+
+
+int
+__drand48_iterate (xsubi, buffer)
+ unsigned short int xsubi[3];
+ struct drand48_data *buffer;
+{
+ uint64_t X;
+ uint64_t result;
+
+ /* Initialize buffer, if not yet done. */
+ if (unlikely(!buffer->__init))
+ {
+ buffer->__a = 0x5deece66dull;
+ buffer->__c = 0xb;
+ buffer->__init = 1;
+ }
+
+ /* Do the real work. We choose a data type which contains at least
+ 48 bits. Because we compute the modulus it does not care how
+ many bits really are computed. */
+
+ X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
+
+ result = X * buffer->__a + buffer->__c;
+
+ xsubi[0] = result & 0xffff;
+ xsubi[1] = (result >> 16) & 0xffff;
+ xsubi[2] = (result >> 32) & 0xffff;
+
+ return 0;
+}
diff --git a/libc/stdlib/drand48.c b/libc/stdlib/drand48.c
new file mode 100644
index 000000000..d18ff3f08
--- /dev/null
+++ b/libc/stdlib/drand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+double drand48 (void)
+{
+ double result;
+
+ erand48_r (__libc_drand48_data.__x, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/drand48_r.c b/libc/stdlib/drand48_r.c
new file mode 100644
index 000000000..b6c055c1f
--- /dev/null
+++ b/libc/stdlib/drand48_r.c
@@ -0,0 +1,27 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <errno.h>
+#include <math.h>
+#include <stdlib.h>
+
+int drand48_r (struct drand48_data *buffer, double *result)
+{
+ return erand48_r (buffer->__x, buffer, result);
+}
diff --git a/libc/stdlib/erand48.c b/libc/stdlib/erand48.c
new file mode 100644
index 000000000..6d4c72683
--- /dev/null
+++ b/libc/stdlib/erand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+double erand48 (unsigned short int xsubi[3])
+{
+ double result;
+
+ (void) erand48_r (xsubi, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/erand48_r.c b/libc/stdlib/erand48_r.c
new file mode 100644
index 000000000..fb48304fa
--- /dev/null
+++ b/libc/stdlib/erand48_r.c
@@ -0,0 +1,48 @@
+/* Copyright (C) 1995, 1997, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <ieee754.h>
+#include <stdlib.h>
+#include <limits.h>
+
+
+int erand48_r (xsubi, buffer, result)
+ unsigned short int xsubi[3];
+ struct drand48_data *buffer;
+ double *result;
+{
+ union ieee754_double temp;
+
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Construct a positive double with the 48 random bits distributed over
+ its fractional part so the resulting FP number is [0.0,1.0). */
+
+ temp.ieee.negative = 0;
+ temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
+ temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
+ temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
+
+ /* Please note the lower 4 bits of mantissa1 are always 0. */
+ *result = temp.d - 1.0;
+
+ return 0;
+}
diff --git a/libc/stdlib/jrand48.c b/libc/stdlib/jrand48.c
new file mode 100644
index 000000000..1106f1f9f
--- /dev/null
+++ b/libc/stdlib/jrand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+long int jrand48 (unsigned short int xsubi[3])
+{
+ long int result;
+
+ (void) jrand48_r (xsubi, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/jrand48_r.c b/libc/stdlib/jrand48_r.c
new file mode 100644
index 000000000..8ea3d09bd
--- /dev/null
+++ b/libc/stdlib/jrand48_r.c
@@ -0,0 +1,35 @@
+/* Copyright (C) 1995, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+int jrand48_r (xsubi, buffer, result)
+ unsigned short int xsubi[3];
+ struct drand48_data *buffer;
+ long int *result;
+{
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Store the result. */
+ *result = ((xsubi[2] << 16) | xsubi[1]) & 0xffffffffl;
+
+ return 0;
+}
diff --git a/libc/stdlib/lrand48.c b/libc/stdlib/lrand48.c
new file mode 100644
index 000000000..c6396c047
--- /dev/null
+++ b/libc/stdlib/lrand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+long int lrand48 (void)
+{
+ long int result;
+
+ nrand48_r (__libc_drand48_data.__x, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/lrand48_r.c b/libc/stdlib/lrand48_r.c
new file mode 100644
index 000000000..c2851efd7
--- /dev/null
+++ b/libc/stdlib/lrand48_r.c
@@ -0,0 +1,29 @@
+/* Copyright (C) 1995, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+int lrand48_r (struct drand48_data *buffer, long int *result)
+{
+ /* Be generous for the arguments, detect some errors. */
+ if (buffer == NULL)
+ return -1;
+
+ return nrand48_r (buffer->__x, buffer, result);
+}
diff --git a/libc/stdlib/mrand48.c b/libc/stdlib/mrand48.c
new file mode 100644
index 000000000..a732603e5
--- /dev/null
+++ b/libc/stdlib/mrand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+long int mrand48 (void)
+{
+ long int result;
+
+ jrand48_r (__libc_drand48_data.__x, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/mrand48_r.c b/libc/stdlib/mrand48_r.c
new file mode 100644
index 000000000..74351a059
--- /dev/null
+++ b/libc/stdlib/mrand48_r.c
@@ -0,0 +1,29 @@
+/* Copyright (C) 1995, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+int mrand48_r (struct drand48_data *buffer, long int *result)
+{
+ /* Be generous for the arguments, detect some errors. */
+ if (buffer == NULL)
+ return -1;
+
+ return jrand48_r (buffer->__x, buffer, result);
+}
diff --git a/libc/stdlib/nrand48.c b/libc/stdlib/nrand48.c
new file mode 100644
index 000000000..585bfe7fd
--- /dev/null
+++ b/libc/stdlib/nrand48.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+long int nrand48 (unsigned short int xsubi[3])
+{
+ long int result;
+
+ nrand48_r (xsubi, &__libc_drand48_data, &result);
+
+ return result;
+}
diff --git a/libc/stdlib/nrand48_r.c b/libc/stdlib/nrand48_r.c
new file mode 100644
index 000000000..dd2eed8f1
--- /dev/null
+++ b/libc/stdlib/nrand48_r.c
@@ -0,0 +1,38 @@
+/* Copyright (C) 1995, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+int nrand48_r (xsubi, buffer, result)
+ unsigned short int xsubi[3];
+ struct drand48_data *buffer;
+ long int *result;
+{
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Store the result. */
+ if (sizeof (unsigned short int) == 2)
+ *result = xsubi[2] << 15 | xsubi[1] >> 1;
+ else
+ *result = xsubi[2] >> 1;
+
+ return 0;
+}
diff --git a/libc/stdlib/rand_r.c b/libc/stdlib/rand_r.c
new file mode 100644
index 000000000..610044083
--- /dev/null
+++ b/libc/stdlib/rand_r.c
@@ -0,0 +1,48 @@
+/* Reentrant random function frm POSIX.1c.
+ Copyright (C) 1996, 1999 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@cygnus.com <mailto:drepper@cygnus.com>>, 1996.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+
+/* This algorithm is mentioned in the ISO C standard, here extended
+ for 32 bits. */
+int rand_r (unsigned int *seed)
+{
+ unsigned int next = *seed;
+ int result;
+
+ next *= 1103515245;
+ next += 12345;
+ result = (unsigned int) (next / 65536) % 2048;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int) (next / 65536) % 1024;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int) (next / 65536) % 1024;
+
+ *seed = next;
+
+ return result;
+}
diff --git a/libc/stdlib/srand48.c b/libc/stdlib/srand48.c
new file mode 100644
index 000000000..a172d07a1
--- /dev/null
+++ b/libc/stdlib/srand48.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+
+/* Global state for non-reentrant functions. Defined in drand48-iter.c. */
+extern struct drand48_data __libc_drand48_data;
+
+void srand48 (long seedval)
+{
+ srand48_r (seedval, &__libc_drand48_data);
+}
diff --git a/libc/stdlib/srand48_r.c b/libc/stdlib/srand48_r.c
new file mode 100644
index 000000000..c0fa38e90
--- /dev/null
+++ b/libc/stdlib/srand48_r.c
@@ -0,0 +1,40 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdlib.h>
+#include <limits.h>
+
+int srand48_r (seedval, buffer)
+ long int seedval;
+ struct drand48_data *buffer;
+{
+ /* The standards say we only have 32 bits. */
+ if (sizeof (long int) > 4)
+ seedval &= 0xffffffffl;
+
+ buffer->__x[2] = seedval >> 16;
+ buffer->__x[1] = seedval & 0xffffl;
+ buffer->__x[0] = 0x330e;
+
+ buffer->__a = 0x5deece66dull;
+ buffer->__c = 0xb;
+ buffer->__init = 1;
+
+ return 0;
+}