Subject: Re: rand48 family of functions
From: "J. Johnston" <[email-masked]>
To: Kazuhiro Fujieda <[email-masked]>
Cc: [email-masked], [email-masked]
Date: Thu, 08 Feb 2001 18:55:55 -0500

Kazuhiro Fujieda wrote:
> 
> I want the rand48 family of functions to compile some UNIX
> applications on the Cygwin environment. I believe these
> functions should be included in newlib. Please apply the
> following patch to the development source of newlib.
> 

Kazuhiro,

  In newlib, we place global static data in a special reentrant structure.
Your patch involves a number of global data arrays.  Could you please
modify your patch to include _r versions of the functions that take
the newlib reentrant structure as the first parameter and use it to reference
the data in question.  The non _r functions just call the _r versions
with the default _REENT pointer.  Check out setenv.c and setenv_r.c
for an example of calling and look at strtok_r.c for an example of using
the reetrant pointer to access global data.

  The added global data elements should be specified in the reent structure
which is defined in libc/include/sys/reent.h.  There is a
struct within there called _reent which is within the union _new.  You can add your 
new variables to the end of that struct.  You will have to modify the _REENT_INIT macro to
initialize your new variables properly.  You shouldn't need a rand48.h file anymore.

  If you can have any questions on making these changes, let me know.

-- Jeff J.

> ChangeLog:
> 2001-02-08  Kazuhiro Fujieda  <[email-masked]>
> 
>         * libc/stdlib/Makefile.am (lib_a_SOURCES): Add rand48 functions.
>         (CHEWOUT_FILES): Add rand48.def.
>         * libc/stdlib/Makefile.am: Add dependencies for rand48 functions.
>         * libc/stdlib/Makefile.in: Regenerated.
>         * libc/include/stdlib.h: Add declarations for rand48 functions.
>         * libc/stdlib/drand48.c: Derived from NetBSD C library.
>         * libc/stdlib/erand48.c: Ditto.
>         * libc/stdlib/jrand48.c: Ditto.
>         * libc/stdlib/lcong48.c: Ditto.
>         * libc/stdlib/lrand48.c: Ditto.
>         * libc/stdlib/mrand48.c: Ditto.
>         * libc/stdlib/nrand48.c: Ditto.
>         * libc/stdlib/rand48.c: Ditto, merged with the newlib style of
>         `rand48.3'.
>         * libc/stdlib/seed48.c: Ditto.
>         * libc/stdlib/srand48.c: Ditto.
> 
> Index: libc/include/stdlib.h
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/include/stdlib.h,v
> retrieving revision 1.7
> diff -u -p -r1.7 stdlib.h
> --- stdlib.h    2000/12/06 11:02:28     1.7
> +++ stdlib.h    2001/02/08 05:00:15
> @@ -114,6 +114,17 @@ char *     _EXFUN(ecvtf,(float,int,int *,int
>  char * _EXFUN(dtoa,(double, int, int, int *, int*, char**));
>  int    _EXFUN(rand_r,(unsigned *__seed));
> 
> +double _EXFUN(drand48,(void));
> +double _EXFUN(erand48,(unsigned short [3]));
> +long   _EXFUN(jrand48,(unsigned short [3]));
> +void   _EXFUN(lcong48,(unsigned short [7]));
> +long   _EXFUN(lrand48,(void));
> +long   _EXFUN(mrand48,(void));
> +long   _EXFUN(nrand48,(unsigned short [3]));
> +unsigned short *
> +       _EXFUN(seed48,(unsigned short [3]));
> +void   _EXFUN(srand48,(long));
> +
>  #ifndef __CYGWIN__
>  _VOID  _EXFUN(cfree,(_PTR));
>  #else
> Index: libc/stdlib/Makefile.am
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/stdlib/Makefile.am,v
> retrieving revision 1.2
> diff -u -p -r1.2 Makefile.am
> --- Makefile.am 2000/12/06 23:50:11     1.2
> +++ Makefile.am 2001/02/08 05:00:15
> @@ -21,6 +21,7 @@ lib_a_SOURCES = \
>         bsearch.c       \
>         calloc.c        \
>         div.c           \
> +       drand48.c       \
>         dtoa.c          \
>         dtoastub.c      \
>         ecvtbuf.c       \
> @@ -28,13 +29,17 @@ lib_a_SOURCES = \
>         environ.c       \
>         envlock.c       \
>         eprintf.c       \
> +       erand48.c       \
>         exit.c          \
>         getenv.c        \
>         getenv_r.c      \
>         getopt.c        \
> +       jrand48.c       \
>         labs.c          \
> +       lcong48.c       \
>         ldiv.c          \
>         ldtoa.c         \
> +       lrand48.c       \
>         malign.c        \
>         malloc.c        \
>         mblen.c         \
> @@ -45,17 +50,22 @@ lib_a_SOURCES = \
>         mbtowc_r.c      \
>         mlock.c         \
>         mprec.c         \
> +       mrand48.c       \
>         msize.c         \
>         mstats.c        \
>         mtrim.c         \
> +       nrand48.c       \
>         putenv.c        \
>         putenv_r.c      \
>         qsort.c         \
>         rand.c          \
> +       rand48.c        \
>         rand_r.c        \
>         realloc.c       \
> +       seed48.c        \
>         setenv.c        \
>         setenv_r.c      \
> +       srand48.c       \
>         strdup.c        \
>         strdup_r.c      \
>         strtod.c        \
> @@ -135,6 +145,7 @@ CHEWOUT_FILES= \
>         mstats.def      \
>         qsort.def       \
>         rand.def        \
> +       rand48.def      \
>         strtod.def      \
>         strtol.def      \
>         strtoul.def     \
> @@ -166,3 +177,14 @@ mbtowc_r.o: mbtowc_r.c mbctype.h
>  mprec.o: mprec.c mprec.h
>  strtod.o: strtod.c mprec.h
>  wctomb_r.o: wctomb_r.c mbctype.h
> +
> +drand48.o: drand48.c rand48.h
> +erand48.o: erand48.c rand48.h
> +jrand48.o: jrand48.c rand48.h
> +lcong48.o: lcong48.c rand48.h
> +lrand48.o: lrand48.c rand48.h
> +mrand48.o: mrand48.c rand48.h
> +nrand48.o: nrand48.c rand48.h
> +rand48.o: rand48.c rand48.h
> +seed48.o: seed48.c rand48.h
> +srand48.o: srand48.c rand48.h
> Index: libc/stdlib/Makefile.in
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/stdlib/Makefile.in,v
> retrieving revision 1.2
> diff -u -p -r1.2 Makefile.in
> --- Makefile.in 2000/12/06 23:50:11     1.2
> +++ Makefile.in 2001/02/08 05:00:15
> @@ -103,6 +103,7 @@ lib_a_SOURCES = \
>         bsearch.c       \
>         calloc.c        \
>         div.c           \
> +       drand48.c       \
>         dtoa.c          \
>         dtoastub.c      \
>         ecvtbuf.c       \
> @@ -110,13 +111,17 @@ lib_a_SOURCES = \
>         environ.c       \
>         envlock.c       \
>         eprintf.c       \
> +       erand48.c       \
>         exit.c          \
>         getenv.c        \
>         getenv_r.c      \
>         getopt.c        \
> +       jrand48.c       \
>         labs.c          \
> +       lcong48.c       \
>         ldiv.c          \
>         ldtoa.c         \
> +       lrand48.c       \
>         malign.c        \
>         malloc.c        \
>         mblen.c         \
> @@ -127,17 +132,22 @@ lib_a_SOURCES = \
>         mbtowc_r.c      \
>         mlock.c         \
>         mprec.c         \
> +       mrand48.c       \
>         msize.c         \
>         mstats.c        \
>         mtrim.c         \
> +       nrand48.c       \
>         putenv.c        \
>         putenv_r.c      \
>         qsort.c         \
>         rand.c          \
> +       rand48.c        \
>         rand_r.c        \
>         realloc.c       \
> +       seed48.c        \
>         setenv.c        \
>         setenv_r.c      \
> +       srand48.c       \
>         strdup.c        \
>         strdup_r.c      \
>         strtod.c        \
> @@ -183,6 +193,7 @@ CHEWOUT_FILES = \
>         mstats.def      \
>         qsort.def       \
>         rand.def        \
> +       rand48.def      \
>         strtod.def      \
>         strtol.def      \
>         strtoul.def     \
> @@ -211,14 +222,15 @@ lib_a_DEPENDENCIES =  mallocr.o freer.o
>  malignr.o vallocr.o pvallocr.o mallinfor.o mallstatsr.o msizer.o \
>  malloptr.o
>  lib_a_OBJECTS =  __adjust.o __exp10.o __ten_mu.o abort.o abs.o assert.o \
> -atexit.o atof.o atoff.o atoi.o atol.o bsearch.o calloc.o div.o dtoa.o \
> -dtoastub.o ecvtbuf.o efgcvt.o environ.o envlock.o eprintf.o exit.o \
> -getenv.o getenv_r.o getopt.o labs.o ldiv.o ldtoa.o malign.o malloc.o \
> -mblen.o mblen_r.o mbstowcs.o mbstowcs_r.o mbtowc.o mbtowc_r.o mlock.o \
> -mprec.o msize.o mstats.o mtrim.o putenv.o putenv_r.o qsort.o rand.o \
> -rand_r.o realloc.o setenv.o setenv_r.o strdup.o strdup_r.o strtod.o \
> -strtol.o strtoul.o system.o valloc.o wcstombs.o wcstombs_r.o wctomb.o \
> -wctomb_r.o
> +atexit.o atof.o atoff.o atoi.o atol.o bsearch.o calloc.o div.o \
> +drand48.o dtoa.o dtoastub.o ecvtbuf.o efgcvt.o environ.o envlock.o \
> +eprintf.o erand48.o exit.o getenv.o getenv_r.o getopt.o jrand48.o \
> +labs.o lcong48.o ldiv.o ldtoa.o lrand48.o malign.o malloc.o mblen.o \
> +mblen_r.o mbstowcs.o mbstowcs_r.o mbtowc.o mbtowc_r.o mlock.o mprec.o \
> +mrand48.o msize.o mstats.o mtrim.o nrand48.o putenv.o putenv_r.o \
> +qsort.o rand.o rand48.o rand_r.o realloc.o seed48.o setenv.o setenv_r.o \
> +srand48.o strdup.o strdup_r.o strtod.o strtol.o strtoul.o system.o \
> +valloc.o wcstombs.o wcstombs_r.o wctomb.o wctomb_r.o
>  CFLAGS = @CFLAGS@
>  COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
>  CCLD = $(CC)
> @@ -446,6 +458,17 @@ mbtowc_r.o: mbtowc_r.c mbctype.h
>  mprec.o: mprec.c mprec.h
>  strtod.o: strtod.c mprec.h
>  wctomb_r.o: wctomb_r.c mbctype.h
> +
> +drand48.o: drand48.c rand48.h
> +erand48.o: erand48.c rand48.h
> +jrand48.o: jrand48.c rand48.h
> +lcong48.o: lcong48.c rand48.h
> +lrand48.o: lrand48.c rand48.h
> +mrand48.o: mrand48.c rand48.h
> +nrand48.o: nrand48.c rand48.h
> +rand48.o: rand48.c rand48.h
> +seed48.o: seed48.c rand48.h
> +srand48.o: srand48.c rand48.h
> 
>  # Tell versions [3.59,3.63) of GNU make to not export all variables.
>  # Otherwise a system limit (for SysV at least) may be exceeded.
> Index: libc/stdlib/drand48.c
> ===================================================================
> RCS file: drand48.c
> diff -N drand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ drand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,20 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +double
> +drand48(void)
> +{
> +       return erand48(__rand48_seed);
> +}
> Index: libc/stdlib/erand48.c
> ===================================================================
> RCS file: erand48.c
> diff -N erand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ erand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,24 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +double
> +erand48(unsigned short xseed[3])
> +{
> +
> +       __dorand48(xseed);
> +       return ldexp((double) xseed[0], -48) +
> +              ldexp((double) xseed[1], -32) +
> +              ldexp((double) xseed[2], -16);
> +}
> Index: libc/stdlib/jrand48.c
> ===================================================================
> RCS file: jrand48.c
> diff -N jrand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ jrand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,22 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +long
> +jrand48(unsigned short xseed[3])
> +{
> +
> +       __dorand48(xseed);
> +       return ((long) xseed[2] << 16) + (long) xseed[1];
> +}
> Index: libc/stdlib/lcong48.c
> ===================================================================
> RCS file: lcong48.c
> diff -N lcong48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ lcong48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,26 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +void
> +lcong48(unsigned short p[7])
> +{
> +       __rand48_seed[0] = p[0];
> +       __rand48_seed[1] = p[1];
> +       __rand48_seed[2] = p[2];
> +       __rand48_mult[0] = p[3];
> +       __rand48_mult[1] = p[4];
> +       __rand48_mult[2] = p[5];
> +       __rand48_add = p[6];
> +}
> Index: libc/stdlib/lrand48.c
> ===================================================================
> RCS file: lrand48.c
> diff -N lrand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ lrand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,22 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +long
> +lrand48(void)
> +{
> +       __dorand48(__rand48_seed);
> +       return (long)((unsigned long) __rand48_seed[2] << 15) +
> +           ((unsigned long) __rand48_seed[1] >> 1);
> +}
> Index: libc/stdlib/mrand48.c
> ===================================================================
> RCS file: mrand48.c
> diff -N mrand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ mrand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,21 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +long
> +mrand48(void)
> +{
> +       __dorand48(__rand48_seed);
> +       return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1];
> +}
> Index: libc/stdlib/nrand48.c
> ===================================================================
> RCS file: nrand48.c
> diff -N nrand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ nrand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,22 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +long
> +nrand48(unsigned short xseed[3])
> +{
> +       __dorand48(xseed);
> +       return (long)((unsigned long) xseed[2] << 15) +
> +           ((unsigned long) xseed[1] >> 1);
> +}
> Index: libc/stdlib/rand48.c
> ===================================================================
> RCS file: rand48.c
> diff -N rand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ rand48.c    Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,188 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +/*
> +FUNCTION
> +   <<drand48>>, <<erand48>>, <<lrand48>>, <<nrand48>>, <<mrand48>>, <<jrand48>>, <<srand48>>, <<seed48>>, <<lcong48>>---pseudo random number generators and initialization routines
> +
> +INDEX
> +       rand48
> +INDEX
> +       drand48
> +INDEX
> +       erand48
> +INDEX
> +       lrand48
> +INDEX
> +       nrand48
> +INDEX
> +       mrand48
> +INDEX
> +       jrand48
> +INDEX
> +       srand48
> +INDEX
> +       seed48
> +INDEX
> +       lcong48
> +
> +ANSI_SYNOPSIS
> +       #include <stdlib.h>
> +       double drand48(void);
> +       double erand48(unsigned short <[xseed]>[3]);
> +       long lrand48(void);
> +       long nrand48(unsigned short <[xseed]>[3]);
> +       long mrand48(void);
> +       long jrand48(unsigned short <[xseed]>[3]);
> +       void srand48(long <[seed]>);
> +       unsigned short *seed48(unsigned short <[xseed]>[3]);
> +       void lcong48(unsigned short <[p]>[7]);
> +
> +TRAD_SYNOPSIS
> +       #include <stdlib.h>
> +       double drand48();
> +
> +       double erand48(<[xseed]>)
> +       unsigned short <[xseed]>[3];
> +
> +       long lrand48();
> +
> +       long nrand48(<[xseed]>)
> +       unsigned short <[xseed]>[3];
> +
> +       long mrand48();
> +
> +       long jrand48(<[xseed]>)
> +       unsigned short <[xseed]>[3];
> +
> +       void srand48(<[seed]>)
> +       long <[seed]>;
> +
> +       unsigned short *seed48(<[xseed]>)
> +       unsigned short <[xseed]>[3];
> +
> +       void lcong48(<[p]>)
> +       unsigned short <[p]>[7];
> +
> +DESCRIPTION
> +The <<rand48>> family of functions generates pseudo-random numbers
> +using a linear congruential algorithm working on integers 48 bits in size.
> +The particular formula employed is
> +r(n+1) = (a * r(n) + c) mod m
> +where the default values are
> +for the multiplicand a = 0xfdeece66d = 25214903917 and
> +the addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48.
> +r(n) is called the seed of the random number generator.
> +
> +For all the six generator routines described next, the first
> +computational step is to perform a single iteration of the algorithm.
> +
> +<<drand48>> and <<erand48>>
> +return values of type double. The full 48 bits of r(n+1) are
> +loaded into the mantissa of the returned value, with the exponent set
> +such that the values produced lie in the interval [0.0, 1.0).
> +
> +<<lrand48>> and <<nrand48>>
> +return values of type long in the range
> +[0, 2**31-1]. The high-order (31) bits of
> +r(n+1) are loaded into the lower bits of the returned value, with
> +the topmost (sign) bit set to zero.
> +
> +<<mrand48>> and <<jrand48>>
> +return values of type long in the range
> +[-2**31, 2**31-1]. The high-order (32) bits of
> +r(n+1) are loaded into the returned value.
> +
> +<<drand48>>, <<lrand48>>, and <<mrand48>>
> +use an internal buffer to store r(n). For these functions
> +the initial value of r(0) = 0x1234abcd330e = 20017429951246.
> +
> +On the other hand, <<erand48>>, <<nrand48>>, and <<jrand48>>
> +use a user-supplied buffer to store the seed r(n),
> +which consists of an array of 3 shorts, where the zeroth member
> +holds the least significant bits.
> +
> +All functions share the same multiplicand and addend.
> +
> +<<srand48>> is used to initialize the internal buffer r(n) of
> +<<drand48>>, <<lrand48>>, and <<mrand48>>
> +such that the 32 bits of the seed value are copied into the upper 32 bits
> +of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
> +Additionally, the constant multiplicand and addend of the algorithm are
> +reset to the default values given above.
> +
> +<<seed48>> also initializes the internal buffer r(n) of
> +<<drand48>>, <<lrand48>>, and <<mrand48>>,
> +but here all 48 bits of the seed can be specified in an array of 3 shorts,
> +where the zeroth member specifies the lowest bits. Again,
> +the constant multiplicand and addend of the algorithm are
> +reset to the default values given above.
> +<<seed48>> returns a pointer to an array of 3 shorts which contains
> +the old seed.
> +This array is statically allocated, thus its contents are lost after
> +each new call to <<seed48>>.
> +
> +Finally, <<lcong48>> allows full control over the multiplicand and
> +addend used in <<drand48>>, <<erand48>>, <<lrand48>>, <<nrand48>>,
> +<<mrand48>, and <<jrand48>>,
> +and the seed used in <<drand48>>, <<lrand48>>, and <<mrand48>>.
> +An array of 7 shorts is passed as parameter; the first three shorts are
> +used to initialize the seed; the second three are used to initialize the
> +multiplicand; and the last short is used to initialize the addend.
> +It is thus not possible to use values greater than 0xffff as the addend.
> +
> +Note that all three methods of seeding the random number generator
> +always also set the multiplicand and addend for any of the six
> +generator calls.
> +
> +For a more powerful random number generator, see <<random>>.
> +
> +PORTABILITY
> +SUS requires these functions.
> +
> +No supporting OS subroutines are required.
> +*/
> +
> +#include "rand48.h"
> +
> +unsigned short __rand48_seed[3] = {
> +       RAND48_SEED_0,
> +       RAND48_SEED_1,
> +       RAND48_SEED_2
> +};
> +unsigned short __rand48_mult[3] = {
> +       RAND48_MULT_0,
> +       RAND48_MULT_1,
> +       RAND48_MULT_2
> +};
> +unsigned short __rand48_add = RAND48_ADD;
> +
> +void
> +__dorand48(unsigned short xseed[3])
> +{
> +       unsigned long accu;
> +       unsigned short temp[2];
> +
> +       accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] +
> +        (unsigned long) __rand48_add;
> +       temp[0] = (unsigned short) accu;        /* lower 16 bits */
> +       accu >>= sizeof(unsigned short) * 8;
> +       accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] +
> +        (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0];
> +       temp[1] = (unsigned short) accu;        /* middle 16 bits */
> +       accu >>= sizeof(unsigned short) * 8;
> +       accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0];
> +       xseed[0] = temp[0];
> +       xseed[1] = temp[1];
> +       xseed[2] = (unsigned short) accu;
> +}
> Index: libc/stdlib/rand48.h
> ===================================================================
> RCS file: rand48.h
> diff -N rand48.h
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ rand48.h    Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,33 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#ifndef _RAND48_H_
> +#define _RAND48_H_
> +
> +#include <math.h>
> +#include <stdlib.h>
> +
> +extern void            _EXFUN(__dorand48,(unsigned short[3]));
> +extern unsigned short  __rand48_seed[3];
> +extern unsigned short  __rand48_mult[3];
> +extern unsigned short  __rand48_add;
> +
> +#define        RAND48_SEED_0   (0x330e)
> +#define        RAND48_SEED_1   (0xabcd)
> +#define        RAND48_SEED_2   (0x1234)
> +#define        RAND48_MULT_0   (0xe66d)
> +#define        RAND48_MULT_1   (0xdeec)
> +#define        RAND48_MULT_2   (0x0005)
> +#define        RAND48_ADD      (0x000b)
> +
> +#endif /* _RAND48_H_ */
> Index: libc/stdlib/seed48.c
> ===================================================================
> RCS file: seed48.c
> diff -N seed48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ seed48.c    Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,32 @@
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +unsigned short *
> +seed48(unsigned short xseed[3])
> +{
> +       static unsigned short sseed[3];
> +
> +       sseed[0] = __rand48_seed[0];
> +       sseed[1] = __rand48_seed[1];
> +       sseed[2] = __rand48_seed[2];
> +       __rand48_seed[0] = xseed[0];
> +       __rand48_seed[1] = xseed[1];
> +       __rand48_seed[2] = xseed[2];
> +       __rand48_mult[0] = RAND48_MULT_0;
> +       __rand48_mult[1] = RAND48_MULT_1;
> +       __rand48_mult[2] = RAND48_MULT_2;
> +       __rand48_add = RAND48_ADD;
> +       return sseed;
> +}
> Index: libc/stdlib/srand48.c
> ===================================================================
> RCS file: srand48.c
> diff -N srand48.c
> --- /dev/null   Tue May  5 13:32:27 1998
> +++ srand48.c   Wed Feb  7 21:00:16 2001
> @@ -0,0 +1,28 @@
> +/*     $NetBSD: srand48.c,v 1.6 2000/01/22 22:19:20 mycroft Exp $      */
> +
> +/*
> + * Copyright (c) 1993 Martin Birgmeier
> + * All rights reserved.
> + *
> + * You may redistribute unmodified or modified versions of this source
> + * code provided that the above copyright notice and this and the
> + * following conditions are retained.
> + *
> + * This software is provided ``as is'', and comes with no warranties
> + * of any kind. I shall in no event be liable for anything that happens
> + * to anyone/anything when using this software.
> + */
> +
> +#include "rand48.h"
> +
> +void
> +srand48(long seed)
> +{
> +       __rand48_seed[0] = RAND48_SEED_0;
> +       __rand48_seed[1] = (unsigned short) seed;
> +       __rand48_seed[2] = (unsigned short) ((unsigned long)seed >> 16);
> +       __rand48_mult[0] = RAND48_MULT_0;
> +       __rand48_mult[1] = RAND48_MULT_1;
> +       __rand48_mult[2] = RAND48_MULT_2;
> +       __rand48_add = RAND48_ADD;
> +}
> Index: libc/stdlib/stdlib.tex
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/stdlib/stdlib.tex,v
> retrieving revision 1.1.1.1
> diff -u -p -r1.1.1.1 stdlib.tex
> --- stdlib.tex  2000/02/17 19:39:48     1.1.1.1
> +++ stdlib.tex  2001/02/08 05:00:16
> @@ -30,6 +30,7 @@ The corresponding declarations are in th
>  * mbtowc::      Minimal multibyte to wide character converter
>  * qsort::       Sort an array
>  * rand::        Pseudo-random numbers
> +* rand48::      Uniformaly distributed pseudo-random numbers
>  * strtod::      String to double or float
>  * strtol::      String to long
>  * strtoul::     String to unsigned long
> @@ -109,6 +110,9 @@ The corresponding declarations are in th
> 
>  @page
>  @include stdlib/rand.def
> +
> +@page
> +@include stdlib/rand48.def
> 
>  @page
>  @include stdlib/strtod.def
> 
> ____
>   | AIST      Kazuhiro Fujieda <[email-masked]>
>   | HOKURIKU  School of Information Science
> o_/ 1990      Japan Advanced Institute of Science and Technology

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple