aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2002-08-13 01:31:08 +0000
committerH. Peter Anvin <hpa@zytor.com>2002-08-13 01:31:08 +0000
commitab0aedb11a3d1710460e004facbadfbcf93b8cc9 (patch)
tree5f027104ae0cff9dbcb44870b1c3293041dead0d
parent9c015e5aacb8f0426f994c5861483a7e0598b2a0 (diff)
downloadklibc-ab0aedb11a3d1710460e004facbadfbcf93b8cc9.tar.gz
Add the rand48() functions -- needed by zcipklibc-0.25
-rw-r--r--include/stdlib.h24
-rw-r--r--klibc/Makefile2
-rw-r--r--klibc/include/stdlib.h24
-rw-r--r--klibc/lrand48.c42
-rw-r--r--klibc/seed48.c18
-rw-r--r--klibc/srand48.c16
-rw-r--r--lrand48.c42
-rw-r--r--seed48.c18
-rw-r--r--srand48.c16
9 files changed, 201 insertions, 1 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 24e7029e29120..7acfa03774beb 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -54,4 +54,28 @@ __extern int unsetenv(const char *);
__extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
+
+__extern long mrand48(unsigned short *);
+__extern long jrand48(void);
+__extern long nrand48(unsigned short *);
+__extern long lrand48(void);
+__extern unsigned short *seed48(const unsigned short *);
+__extern void srand48(long);
+
+#define RAND_MAX 0x7fffffff
+static __inline__ int rand(void) {
+ return (int)lrand48();
+}
+static __inline__ void srand(unsigned int __s) {
+ srand48(__s);
+}
+static __inline__ long random(void)
+{
+ return lrand48();
+}
+static __inline__ void srandom(unsigned int __s)
+{
+ srand48(__s);
+}
+
#endif /* _STDLIB_H */
diff --git a/klibc/Makefile b/klibc/Makefile
index 730dd49e928bd..985b80f740914 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -35,7 +35,7 @@ LIBOBJS = vsnprintf.o snprintf.o vsprintf.o sprintf.o \
gethostname.o getdomainname.o getcwd.o seteuid.o setegid.o \
getenv.o setenv.o unsetenv.o getopt.o readdir.o \
time.o fdatasync.o llseek.o select.o nice.o getpriority.o \
- qsort.o \
+ qsort.o lrand48.o srand48.o seed48.o \
inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o
LIB = libc.a
diff --git a/klibc/include/stdlib.h b/klibc/include/stdlib.h
index 24e7029e29120..7acfa03774beb 100644
--- a/klibc/include/stdlib.h
+++ b/klibc/include/stdlib.h
@@ -54,4 +54,28 @@ __extern int unsetenv(const char *);
__extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
+
+__extern long mrand48(unsigned short *);
+__extern long jrand48(void);
+__extern long nrand48(unsigned short *);
+__extern long lrand48(void);
+__extern unsigned short *seed48(const unsigned short *);
+__extern void srand48(long);
+
+#define RAND_MAX 0x7fffffff
+static __inline__ int rand(void) {
+ return (int)lrand48();
+}
+static __inline__ void srand(unsigned int __s) {
+ srand48(__s);
+}
+static __inline__ long random(void)
+{
+ return lrand48();
+}
+static __inline__ void srandom(unsigned int __s)
+{
+ srand48(__s);
+}
+
#endif /* _STDLIB_H */
diff --git a/klibc/lrand48.c b/klibc/lrand48.c
new file mode 100644
index 0000000000000..5405cd55d02dc
--- /dev/null
+++ b/klibc/lrand48.c
@@ -0,0 +1,42 @@
+/*
+ * lrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+unsigned short __rand48_seed[3];
+
+long mrand48(unsigned short xsubi[3])
+{
+ uint64_t x;
+
+ /* The xsubi[] array is littleendian by spec */
+ x = (uint64_t)xsubi[0] +
+ ((uint64_t)xsubi[1] << 16) +
+ ((uint64_t)xsubi[2] << 32);
+
+ x = (0x5deece66dULL * x) + 0xb;
+
+ xsubi[0] = (unsigned short)x;
+ xsubi[1] = (unsigned short)(x >> 16);
+ xsubi[2] = (unsigned short)(x >> 32);
+
+ return (long)(uint32_t)(x >> 16);
+}
+
+long jrand48(void)
+{
+ return mrand48(__rand48_seed);
+}
+
+long nrand48(unsigned short xsubi[3])
+{
+ return mrand48(xsubi) & 0x7fffffff;
+}
+
+long lrand48(void)
+{
+ return nrand48(__rand48_seed);
+}
+
diff --git a/klibc/seed48.c b/klibc/seed48.c
new file mode 100644
index 0000000000000..ba36932e21d3b
--- /dev/null
+++ b/klibc/seed48.c
@@ -0,0 +1,18 @@
+/*
+ * seed48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];
+
+unsigned short *seed48(const unsigned short xsubi[3])
+{
+ static unsigned short oldseed[3];
+ memcpy(oldseed, __rand48_seed, sizeof __rand48_seed);
+ memcpy(__rand48_seed, xsubi, sizeof __rand48_seed);
+
+ return oldseed;
+}
+
diff --git a/klibc/srand48.c b/klibc/srand48.c
new file mode 100644
index 0000000000000..a3df16d95c002
--- /dev/null
+++ b/klibc/srand48.c
@@ -0,0 +1,16 @@
+/*
+ * srand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];
+
+
+void srand48(long seedval)
+{
+ __rand48_seed[0] = 0x330e;
+ __rand48_seed[1] = (unsigned short)seedval;
+ __rand48_seed[2] = (unsigned short)((uint32_t)seedval >> 16);
+}
diff --git a/lrand48.c b/lrand48.c
new file mode 100644
index 0000000000000..5405cd55d02dc
--- /dev/null
+++ b/lrand48.c
@@ -0,0 +1,42 @@
+/*
+ * lrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+unsigned short __rand48_seed[3];
+
+long mrand48(unsigned short xsubi[3])
+{
+ uint64_t x;
+
+ /* The xsubi[] array is littleendian by spec */
+ x = (uint64_t)xsubi[0] +
+ ((uint64_t)xsubi[1] << 16) +
+ ((uint64_t)xsubi[2] << 32);
+
+ x = (0x5deece66dULL * x) + 0xb;
+
+ xsubi[0] = (unsigned short)x;
+ xsubi[1] = (unsigned short)(x >> 16);
+ xsubi[2] = (unsigned short)(x >> 32);
+
+ return (long)(uint32_t)(x >> 16);
+}
+
+long jrand48(void)
+{
+ return mrand48(__rand48_seed);
+}
+
+long nrand48(unsigned short xsubi[3])
+{
+ return mrand48(xsubi) & 0x7fffffff;
+}
+
+long lrand48(void)
+{
+ return nrand48(__rand48_seed);
+}
+
diff --git a/seed48.c b/seed48.c
new file mode 100644
index 0000000000000..ba36932e21d3b
--- /dev/null
+++ b/seed48.c
@@ -0,0 +1,18 @@
+/*
+ * seed48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];
+
+unsigned short *seed48(const unsigned short xsubi[3])
+{
+ static unsigned short oldseed[3];
+ memcpy(oldseed, __rand48_seed, sizeof __rand48_seed);
+ memcpy(__rand48_seed, xsubi, sizeof __rand48_seed);
+
+ return oldseed;
+}
+
diff --git a/srand48.c b/srand48.c
new file mode 100644
index 0000000000000..a3df16d95c002
--- /dev/null
+++ b/srand48.c
@@ -0,0 +1,16 @@
+/*
+ * srand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];
+
+
+void srand48(long seedval)
+{
+ __rand48_seed[0] = 0x330e;
+ __rand48_seed[1] = (unsigned short)seedval;
+ __rand48_seed[2] = (unsigned short)((uint32_t)seedval >> 16);
+}