diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 7391adb4b..879423d77 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,29 @@ +2001-02-14 Kazuhiro Fujieda + + * libc/include/stdlib.h: Add declarations of rand48 functions and + their reentrant versions. + * libc/include/sys/reent.h: Move macros from rand48.h. Add + struct _rand48 for shared parameters of rand48 functions. + (struct _reent): Add a variable _r48 of struct _rand48. + (_REENT_INIT): Add _r48 initialization. + * 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/stdlib/drand48.c (drand48, _drand48_r): Derived from the + NetBSD C library. + * libc/stdlib/erand48.c (erand48, _erand48_r): Ditto. + * libc/stdlib/jrand48.c (jrand48, _jrand48_r): Ditto. + * libc/stdlib/lcong48.c (lcong48, _lcong48_r): Ditto. + * libc/stdlib/lrand48.c (lrand48, _lrand48_r): Ditto. + * libc/stdlib/mrand48.c (mrand48, _mrand48_r): Ditto. + * libc/stdlib/nrand48.c (nrand48, _nrand48_r): Ditto. + * libc/stdlib/seed48.c (seed48, _seed48_r): Ditto. + * libc/stdlib/srand48.c (srand48, _srand48_r): Ditto. + * libc/stdlib/rand48.c (__dorand48): Ditto. + * libc/stdlib/rand48.h: Ditto, and modify declarations of global + parameters into macros referring them in the reentrant structure. + 2001-02-12 Jeff Johnston * libc/include/sys/stat.h: Add mknod for Cygwin now that diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h index 46cb2e970..b05510847 100644 --- a/newlib/libc/include/stdlib.h +++ b/newlib/libc/include/stdlib.h @@ -114,6 +114,27 @@ 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(_drand48_r,(struct _reent *)); +double _EXFUN(erand48,(unsigned short [3])); +double _EXFUN(_erand48_r,(struct _reent *, unsigned short [3])); +long _EXFUN(jrand48,(unsigned short [3])); +long _EXFUN(_jrand48_r,(struct _reent *, unsigned short [3])); +_VOID _EXFUN(lcong48,(unsigned short [7])); +_VOID _EXFUN(_lcong48_r,(struct _reent *, unsigned short [7])); +long _EXFUN(lrand48,(_VOID)); +long _EXFUN(_lrand48_r,(struct _reent *)); +long _EXFUN(mrand48,(_VOID)); +long _EXFUN(_mrand48_r,(struct _reent *)); +long _EXFUN(nrand48,(unsigned short [3])); +long _EXFUN(_nrand48_r,(struct _reent *, unsigned short [3])); +unsigned short * + _EXFUN(seed48,(unsigned short [3])); +unsigned short * + _EXFUN(_seed48_r,(struct _reent *, unsigned short [3])); +_VOID _EXFUN(srand48,(long)); +_VOID _EXFUN(_srand48_r,(struct _reent *, long)); + #ifndef __CYGWIN__ _VOID _EXFUN(cfree,(_PTR)); #else diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h index 760638676..8a0fb7f12 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -135,6 +135,33 @@ struct __sFILE { struct _reent *_data; }; +/* + * rand48 family support + * + * 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. + */ +#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) +struct _rand48 { + unsigned short _seed[3]; + unsigned short _mult[3]; + unsigned short _add; +}; + /* * struct _reent * @@ -183,7 +210,7 @@ struct _reent struct tm _localtime_buf; int _gamma_signgam; __extension__ unsigned long long _rand_next; - + struct _rand48 _r48; } _reent; /* Two next two fields were once used by malloc. They are no longer used. They are used to preserve the space used before so as to @@ -213,7 +240,9 @@ struct _reent #define _REENT_INIT(var) \ { 0, &var.__sf[0], &var.__sf[1], &var.__sf[2], 0, "", 0, "C", \ 0, NULL, NULL, 0, NULL, NULL, 0, NULL, { {0, NULL, "", \ - { 0,0,0,0,0,0,0,0}, 0, 1} } } + { 0,0,0,0,0,0,0,0}, 0, 1, \ + {{_RAND48_SEED_0, _RAND48_SEED_1, _RAND48_SEED_2}, \ + {_RAND48_MULT_0, _RAND48_MULT_1, _RAND48_MULT_2}, _RAND48_ADD}} } } /* * All references to struct _reent are via this pointer. diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index e6d60dbc3..2c44d2cc9 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -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,13 @@ 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 diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index 8274e6c88..6ca4b0f0b 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -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 reallocr.o callocr.o cfreer.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,16 @@ 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. diff --git a/newlib/libc/stdlib/drand48.c b/newlib/libc/stdlib/drand48.c new file mode 100644 index 000000000..8f9c4a269 --- /dev/null +++ b/newlib/libc/stdlib/drand48.c @@ -0,0 +1,29 @@ +/* + * 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 +_DEFUN (_drand48_r, (r), + struct _reent *r) +{ + return _erand48_r(r, __rand48_seed); +} + +#ifndef _REENT_ONLY +double +_DEFUN_VOID (drand48) +{ + return _drand48_r (_REENT); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/erand48.c b/newlib/libc/stdlib/erand48.c new file mode 100644 index 000000000..26b39fb5b --- /dev/null +++ b/newlib/libc/stdlib/erand48.c @@ -0,0 +1,34 @@ +/* + * 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 +_DEFUN (_erand48_r, (r, xseed), + struct _reent *r _AND + unsigned short xseed[3]) +{ + __dorand48(r, xseed); + return ldexp((double) xseed[0], -48) + + ldexp((double) xseed[1], -32) + + ldexp((double) xseed[2], -16); +} + +#ifndef _REENT_ONLY +double +_DEFUN (erand48, (xseed), + unsigned short xseed[3]) +{ + return _erand48_r (_REENT, xseed); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/jrand48.c b/newlib/libc/stdlib/jrand48.c new file mode 100644 index 000000000..9e2f92cb6 --- /dev/null +++ b/newlib/libc/stdlib/jrand48.c @@ -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" + +long +_DEFUN (_jrand48_r, (r, xseed), + struct _reent *r _AND + unsigned short xseed[3]) +{ + __dorand48(r, xseed); + return ((long) xseed[2] << 16) + (long) xseed[1]; +} + +#ifndef _REENT_ONLY +long +_DEFUN (jrand48, (xseed), + unsigned short xseed[3]) +{ + return _jrand48_r (_REENT, xseed); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/lcong48.c b/newlib/libc/stdlib/lcong48.c new file mode 100644 index 000000000..6dae4e906 --- /dev/null +++ b/newlib/libc/stdlib/lcong48.c @@ -0,0 +1,37 @@ +/* + * 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 +_DEFUN (_lcong48_r, (r, p), + struct _reent *r _AND + 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]; +} + +#ifndef _REENT_ONLY +_VOID +_DEFUN (lcong48, (p), + unsigned short p[7]) +{ + _lcong48_r (_REENT, p); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/lrand48.c b/newlib/libc/stdlib/lrand48.c new file mode 100644 index 000000000..2e850f224 --- /dev/null +++ b/newlib/libc/stdlib/lrand48.c @@ -0,0 +1,31 @@ +/* + * 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 +_DEFUN (_lrand48_r, (r), + struct _reent *r) +{ + __dorand48(r, __rand48_seed); + return (long)((unsigned long) __rand48_seed[2] << 15) + + ((unsigned long) __rand48_seed[1] >> 1); +} + +#ifndef _REENT_ONLY +long +_DEFUN_VOID (lrand48) +{ + return _lrand48_r (_REENT); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/mrand48.c b/newlib/libc/stdlib/mrand48.c new file mode 100644 index 000000000..d515b3a80 --- /dev/null +++ b/newlib/libc/stdlib/mrand48.c @@ -0,0 +1,30 @@ +/* + * 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 +_DEFUN (_mrand48_r, (r), + struct _reent *r) +{ + __dorand48(r, __rand48_seed); + return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; +} + +#ifndef _REENT_ONLY +long +_DEFUN_VOID (mrand48) +{ + return _mrand48_r (_REENT); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/nrand48.c b/newlib/libc/stdlib/nrand48.c new file mode 100644 index 000000000..39e9fb13c --- /dev/null +++ b/newlib/libc/stdlib/nrand48.c @@ -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. + */ + +#include "rand48.h" + +long +_DEFUN (_nrand48_r, (r, xseed), + struct _reent *r _AND + unsigned short xseed[3]) +{ + __dorand48 (r, xseed); + return (long)((unsigned long) xseed[2] << 15) + + ((unsigned long) xseed[1] >> 1); +} + +#ifndef _REENT_ONLY +long +_DEFUN (nrand48, (xseed), + unsigned short xseed[3]) +{ + return _nrand48_r (_REENT, xseed); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/rand48.c b/newlib/libc/stdlib/rand48.c new file mode 100644 index 000000000..c65af12cd --- /dev/null +++ b/newlib/libc/stdlib/rand48.c @@ -0,0 +1,178 @@ +/* + * 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 + <>, <>, <>, <>, <>, <>, <>, <>, <>, <> ---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 + 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 + 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 <> 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. + +<> and <> +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]. + +<> and <> +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. + +<> and <> +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. + +<>, <>, and <> +use an internal buffer to store r(n). For these functions +the initial value of r(0) = 0x1234abcd330e = 20017429951246. + +On the other hand, <>, <>, and <> +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. + +<> is used to initialize the internal buffer r(n) of +<>, <>, and <> +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. + +<> also initializes the internal buffer r(n) of +<>, <>, and <>, +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. +<> 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 <>. + +Finally, <> allows full control over the multiplicand and +addend used in <>, <>, <>, <>, +<>, and <>, +and the seed used in <>, <>, and <>. +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 <>. + +PORTABILITY +SUS requires these functions. + +No supporting OS subroutines are required. +*/ + +#include "rand48.h" + +void +_DEFUN (__dorand48, (r, xseed), + struct _reent *r _AND + 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; +} diff --git a/newlib/libc/stdlib/rand48.h b/newlib/libc/stdlib/rand48.h new file mode 100644 index 000000000..c1446d683 --- /dev/null +++ b/newlib/libc/stdlib/rand48.h @@ -0,0 +1,36 @@ +/* + * 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 +#include + +extern void _EXFUN(__dorand48,(struct _reent *r, unsigned short[3])); +#define __rand48_seed (r->_new._reent._r48._seed) +#define __rand48_mult (r->_new._reent._r48._mult) +#define __rand48_add (r->_new._reent._r48._add) + +#if 0 +/* following values are defined in */ +#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 + +#endif /* _RAND48_H_ */ diff --git a/newlib/libc/stdlib/seed48.c b/newlib/libc/stdlib/seed48.c new file mode 100644 index 000000000..3f2cb5061 --- /dev/null +++ b/newlib/libc/stdlib/seed48.c @@ -0,0 +1,43 @@ +/* + * 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 * +_DEFUN (_seed48_r, (r, xseed), + struct _reent *r _AND + 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; +} + +#ifndef _REENT_ONLY +unsigned short * +_DEFUN (seed48, (xseed), + unsigned short xseed[3]) +{ + return _seed48_r (_REENT, xseed); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/srand48.c b/newlib/libc/stdlib/srand48.c new file mode 100644 index 000000000..8850540e3 --- /dev/null +++ b/newlib/libc/stdlib/srand48.c @@ -0,0 +1,37 @@ +/* + * 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 +_DEFUN (_srand48_r, (r, seed), + struct _reent *r _AND + 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; +} + +#ifndef _REENT_ONLY +_VOID +_DEFUN (srand48, (seed), + long seed) +{ + _srand48_r (_REENT, seed); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdlib/stdlib.tex b/newlib/libc/stdlib/stdlib.tex index 7a0d723d3..fc1643603 100644 --- a/newlib/libc/stdlib/stdlib.tex +++ b/newlib/libc/stdlib/stdlib.tex @@ -30,6 +30,7 @@ The corresponding declarations are in the header file @file{stdlib.h}. * 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 @@ -110,6 +111,9 @@ The corresponding declarations are in the header file @file{stdlib.h}. @page @include stdlib/rand.def +@page +@include stdlib/rand48.def + @page @include stdlib/strtod.def