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.
This commit is contained in:
Joel Sherrill 2016-03-22 17:50:48 -05:00
parent a0c4772429
commit ecf453f963
7 changed files with 116 additions and 37 deletions

View File

@ -226,6 +226,12 @@ unsigned short *
_VOID _EXFUN(srand48,(long));
_VOID _EXFUN(_srand48_r,(struct _reent *, long));
#endif /* __SVID_VISIBLE || __XSI_VISIBLE */
#if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
char * _EXFUN(initstate,(unsigned, char *, size_t));
long _EXFUN(random,(_VOID));
char * _EXFUN(setstate,(char *));
_VOID _EXFUN(srandom,(unsigned));
#endif
#if __ISO_C_VISIBLE >= 1999
long long _EXFUN(atoll,(const char *__nptr));
#endif

View File

@ -1,17 +0,0 @@
#ifndef __rtems__
static __inline__ long int
random (void)
{
extern int rand(void);
return rand ();
}
static __inline__ void
srandom (unsigned int seed)
{
void srand(unsigned int seed);
srand (seed);
}
#endif

View File

@ -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 \

View File

@ -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

View File

@ -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 */

View File

@ -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
@ -153,6 +154,9 @@ The corresponding declarations are in the header file @file{stdlib.h}.
@page
@include stdlib/rand.def
@page
@include stdlib/random.def
@page
@include stdlib/rand48.def

View File

@ -28,12 +28,6 @@ char *canonicalize_file_name (const char *);
#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
int unsetenv (const char *);
#endif
#if __BSD_VISIBLE || __SVID_SOURCE || __XSI_VISIBLE >= 4
char *initstate (unsigned seed, char *state, size_t size);
long random (void);
char *setstate (const char *state);
void srandom (unsigned);
#endif
#if __XSI_VISIBLE
char *ptsname (int);
int grantpt (int);