diff options
author | Joel Sherrill <joel@rtems.org> | 2016-03-22 17:50:48 -0500 |
---|---|---|
committer | Joel Sherrill <joel@rtems.org> | 2016-03-28 22:39:50 -0500 |
commit | ecf453f9635fb278cff4d4bae21a1e249313b817 (patch) | |
tree | 691716a58754527b45dd04f53f36731b12596899 /newlib/libc/stdlib | |
parent | a0c477242996403344a0b3c46e6d4645a3fa672c (diff) | |
download | newlib-ecf453f9635fb278cff4d4bae21a1e249313b817.zip newlib-ecf453f9635fb278cff4d4bae21a1e249313b817.tar.gz newlib-ecf453f9635fb278cff4d4bae21a1e249313b817.tar.bz2 |
Add simple versions of random() and srandom()
Prototypes also added for initstate() and setstate() but they
were not implemented in the shared newlib code.
* newlib/libc/include/cygwin/stdlib.h: Prototypes added.
* winsup/cygwin/include/cygwin/stdlib.h: Prototypes removed.
* newlib/libc/stdlib/random.c: New file.
* newlib/libc/machine/epiphany/machine/stdlib.h: Removed
* newlib/libc/stdlib/Makefile.am: Added random.c.
* newlib/libc/stdlib/stdlib.tex: Added random.def.
* newlib/libc/stdlib/Makefile.in: Regenerated.
Diffstat (limited to 'newlib/libc/stdlib')
-rw-r--r-- | newlib/libc/stdlib/Makefile.am | 2 | ||||
-rw-r--r-- | newlib/libc/stdlib/Makefile.in | 36 | ||||
-rw-r--r-- | newlib/libc/stdlib/random.c | 82 | ||||
-rw-r--r-- | newlib/libc/stdlib/stdlib.tex | 4 |
4 files changed, 110 insertions, 14 deletions
diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index 9deaf9a..2d45d10 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -50,6 +50,7 @@ GENERAL_SOURCES = \ quick_exit.c \ rand.c \ rand_r.c \ + random.c \ realloc.c \ reallocf.c \ sb_charsets.c \ @@ -282,6 +283,7 @@ CHEWOUT_FILES= \ on_exit.def \ rand.def \ rand48.def \ + random.def \ rpmatch.def \ strtod.def \ strtol.def \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index c991cb7..5ab16a7 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -103,15 +103,16 @@ am__objects_2 = lib_a-__adjust.$(OBJEXT) lib_a-__atexit.$(OBJEXT) \ lib_a-mlock.$(OBJEXT) lib_a-mprec.$(OBJEXT) \ lib_a-mstats.$(OBJEXT) lib_a-on_exit_args.$(OBJEXT) \ lib_a-quick_exit.$(OBJEXT) lib_a-rand.$(OBJEXT) \ - lib_a-rand_r.$(OBJEXT) lib_a-realloc.$(OBJEXT) \ - lib_a-reallocf.$(OBJEXT) lib_a-sb_charsets.$(OBJEXT) \ - lib_a-strtod.$(OBJEXT) lib_a-strtodg.$(OBJEXT) \ - lib_a-strtol.$(OBJEXT) lib_a-strtorx.$(OBJEXT) \ - lib_a-strtoul.$(OBJEXT) lib_a-utoa.$(OBJEXT) \ - lib_a-wcstod.$(OBJEXT) lib_a-wcstol.$(OBJEXT) \ - lib_a-wcstoul.$(OBJEXT) lib_a-wcstombs.$(OBJEXT) \ - lib_a-wcstombs_r.$(OBJEXT) lib_a-wctomb.$(OBJEXT) \ - lib_a-wctomb_r.$(OBJEXT) $(am__objects_1) + lib_a-rand_r.$(OBJEXT) lib_a-random.$(OBJEXT) \ + lib_a-realloc.$(OBJEXT) lib_a-reallocf.$(OBJEXT) \ + lib_a-sb_charsets.$(OBJEXT) lib_a-strtod.$(OBJEXT) \ + lib_a-strtodg.$(OBJEXT) lib_a-strtol.$(OBJEXT) \ + lib_a-strtorx.$(OBJEXT) lib_a-strtoul.$(OBJEXT) \ + lib_a-utoa.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \ + lib_a-wcstol.$(OBJEXT) lib_a-wcstoul.$(OBJEXT) \ + lib_a-wcstombs.$(OBJEXT) lib_a-wcstombs_r.$(OBJEXT) \ + lib_a-wctomb.$(OBJEXT) lib_a-wctomb_r.$(OBJEXT) \ + $(am__objects_1) am__objects_3 = lib_a-arc4random.$(OBJEXT) \ lib_a-arc4random_uniform.$(OBJEXT) lib_a-cxa_atexit.$(OBJEXT) \ lib_a-cxa_finalize.$(OBJEXT) lib_a-drand48.$(OBJEXT) \ @@ -159,10 +160,10 @@ am__objects_9 = __adjust.lo __atexit.lo __call_atexit.lo __exp10.lo \ itoa.lo labs.lo ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo \ mbstowcs.lo mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo \ mprec.lo mstats.lo on_exit_args.lo quick_exit.lo rand.lo \ - rand_r.lo realloc.lo reallocf.lo sb_charsets.lo strtod.lo \ - strtodg.lo strtol.lo strtorx.lo strtoul.lo utoa.lo wcstod.lo \ - wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo wctomb.lo \ - wctomb_r.lo $(am__objects_8) + rand_r.lo random.lo realloc.lo reallocf.lo sb_charsets.lo \ + strtod.lo strtodg.lo strtol.lo strtorx.lo strtoul.lo utoa.lo \ + wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo \ + wctomb.lo wctomb_r.lo $(am__objects_8) am__objects_10 = arc4random.lo arc4random_uniform.lo cxa_atexit.lo \ cxa_finalize.lo drand48.lo ecvtbuf.lo efgcvt.lo erand48.lo \ jrand48.lo lcong48.lo lrand48.lo mrand48.lo msize.lo mtrim.lo \ @@ -363,7 +364,7 @@ GENERAL_SOURCES = __adjust.c __atexit.c __call_atexit.c __exp10.c \ gdtoa-hexnan.c getenv.c getenv_r.c itoa.c labs.c ldiv.c \ ldtoa.c malloc.c mblen.c mblen_r.c mbstowcs.c mbstowcs_r.c \ mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c on_exit_args.c \ - quick_exit.c rand.c rand_r.c realloc.c reallocf.c \ + quick_exit.c rand.c rand_r.c random.c realloc.c reallocf.c \ sb_charsets.c strtod.c strtodg.c strtol.c strtorx.c strtoul.c \ utoa.c wcstod.c wcstol.c wcstoul.c wcstombs.c wcstombs_r.c \ wctomb.c wctomb_r.c $(am__append_1) @@ -526,6 +527,7 @@ CHEWOUT_FILES = \ on_exit.def \ rand.def \ rand48.def \ + random.def \ rpmatch.def \ strtod.def \ strtol.def \ @@ -883,6 +885,12 @@ lib_a-rand_r.o: rand_r.c lib_a-rand_r.obj: rand_r.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-rand_r.obj `if test -f 'rand_r.c'; then $(CYGPATH_W) 'rand_r.c'; else $(CYGPATH_W) '$(srcdir)/rand_r.c'; fi` +lib_a-random.o: random.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-random.o `test -f 'random.c' || echo '$(srcdir)/'`random.c + +lib_a-random.obj: random.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-random.obj `if test -f 'random.c'; then $(CYGPATH_W) 'random.c'; else $(CYGPATH_W) '$(srcdir)/random.c'; fi` + lib_a-realloc.o: realloc.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-realloc.o `test -f 'realloc.c' || echo '$(srcdir)/'`realloc.c diff --git a/newlib/libc/stdlib/random.c b/newlib/libc/stdlib/random.c new file mode 100644 index 0000000..de132f3 --- /dev/null +++ b/newlib/libc/stdlib/random.c @@ -0,0 +1,82 @@ +/* +FUNCTION +<<random>>, <<srandom>>---pseudo-random numbers + +INDEX + random +INDEX + srandom + +ANSI_SYNOPSIS + #define _XOPEN_SOURCE 500 + #include <stdlib.h> + long int random(void); + void srandom(unsigned int <[seed]>); + + + +DESCRIPTION +<<random>> returns a different integer each time it is called; each +integer is chosen by an algorithm designed to be unpredictable, so +that you can use <<random>> when you require a random number. +The algorithm depends on a static variable called the ``random seed''; +starting with a given value of the random seed always produces the +same sequence of numbers in successive calls to <<random>>. + +You can set the random seed using <<srandom>>; it does nothing beyond +storing its argument in the static variable used by <<rand>>. You can +exploit this to make the pseudo-random sequence less predictable, if +you wish, by using some other unpredictable value (often the least +significant parts of a time-varying value) as the random seed before +beginning a sequence of calls to <<rand>>; or, if you wish to ensure +(for example, while debugging) that successive runs of your program +use the same ``random'' numbers, you can use <<srandom>> to set the same +random seed at the outset. + +RETURNS +<<random>> returns the next pseudo-random integer in sequence; it is a +number between <<0>> and <<RAND_MAX>> (inclusive). + +<<srandom>> does not return a result. + +NOTES +<<random>> and <<srandom>> are unsafe for multi-threaded applications. + +_XOPEN_SOURCE may be any value >= 500. + +PORTABILITY +<<random>> is required by XSI. This implementation uses the same +algorithm as <<rand>>. + +<<random>> requires no supporting OS subroutines. +*/ + +#ifndef _REENT_ONLY + +#include <stdlib.h> +#include <reent.h> + +void +_DEFUN (srandom, (seed), unsigned int seed) +{ + struct _reent *reent = _REENT; + + _REENT_CHECK_RAND48(reent); + _REENT_RAND_NEXT(reent) = seed; +} + +long int +_DEFUN_VOID (random) +{ + struct _reent *reent = _REENT; + + /* This multiplier was obtained from Knuth, D.E., "The Art of + Computer Programming," Vol 2, Seminumerical Algorithms, Third + Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */ + _REENT_CHECK_RAND48(reent); + _REENT_RAND_NEXT(reent) = + _REENT_RAND_NEXT(reent) * __extension__ 6364136223846793005LL + 1; + return (long int)((_REENT_RAND_NEXT(reent) >> 32) & RAND_MAX); +} + +#endif /* _REENT_ONLY */ diff --git a/newlib/libc/stdlib/stdlib.tex b/newlib/libc/stdlib/stdlib.tex index 53f0b9c..75d80f4 100644 --- a/newlib/libc/stdlib/stdlib.tex +++ b/newlib/libc/stdlib/stdlib.tex @@ -38,6 +38,7 @@ The corresponding declarations are in the header file @file{stdlib.h}. * on_exit:: Request execution of functions at program exit * qsort:: Array sort * rand:: Pseudo-random numbers +* random:: Pseudo-random numbers * rand48:: Uniformly distributed pseudo-random numbers * rpmatch:: Determine whether response is affirmative or negative * strtod:: String to double or float @@ -154,6 +155,9 @@ The corresponding declarations are in the header file @file{stdlib.h}. @include stdlib/rand.def @page +@include stdlib/random.def + +@page @include stdlib/rand48.def @page |