This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add simple versions of random() and srandom()


Prototypes also added for initstate() and setstate() but they
were not implemented.
---
 newlib/libc/include/stdlib.h                  |  6 ++
 newlib/libc/machine/epiphany/machine/stdlib.h | 17 ------
 newlib/libc/stdlib/Makefile.am                |  1 +
 newlib/libc/stdlib/Makefile.in                | 35 ++++++-----
 newlib/libc/stdlib/random.c                   | 86 +++++++++++++++++++++++++++
 5 files changed, 114 insertions(+), 31 deletions(-)
 delete mode 100644 newlib/libc/machine/epiphany/machine/stdlib.h
 create mode 100644 newlib/libc/stdlib/random.c

diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h
index 7d4ae76..e368df1 100644
--- a/newlib/libc/include/stdlib.h
+++ b/newlib/libc/include/stdlib.h
@@ -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
diff --git a/newlib/libc/machine/epiphany/machine/stdlib.h b/newlib/libc/machine/epiphany/machine/stdlib.h
deleted file mode 100644
index 7ede4dd..0000000
--- a/newlib/libc/machine/epiphany/machine/stdlib.h
+++ /dev/null
@@ -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
diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am
index 9deaf9a..a59df68 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	\
diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in
index c991cb7..004da65 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)
@@ -883,6 +884,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..27ee5af
--- /dev/null
+++ b/newlib/libc/stdlib/random.c
@@ -0,0 +1,86 @@
+/*
+FUNCTION
+<<random>>, <<srandom>>---pseudo-random numbers
+
+INDEX
+	random
+INDEX
+	srandom
+
+ANSI_SYNOPSIS
+	#include <stdlib.h>
+	long int random(void);
+	void srandom(unsigned int <[seed]>);
+
+TRAD_SYNOPSIS
+	#include <stdlib.h>
+	long int rand();
+
+	void srandon(<[seed]>)
+	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.
+
+
+PORTABILITY
+<<random>> is required by POSIX. 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 */
-- 
1.8.3.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]