Rename rand64() to _rand64()

This commit is contained in:
Justine Tunney 2022-10-10 04:12:06 -07:00
parent c424352a0a
commit 7ae556463a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 141 additions and 139 deletions

View file

@ -402,6 +402,15 @@ $(SRCS): \
libc/integral/lp64arg.inc \ libc/integral/lp64arg.inc \
libc/integral/lp64.inc libc/integral/lp64.inc
.PHONY: toolchain
toolchain: o/cosmopolitan.h \
o/$(MODE)/ape/public/ape.lds \
o/$(MODE)/libc/crt/crt.o \
o/$(MODE)/ape/ape.o \
o/$(MODE)/ape/ape-copy-self.o \
o/$(MODE)/ape/ape-no-modify-self.o \
o/$(MODE)/cosmopolitan.a
# UNSPECIFIED PREREQUISITES TUTORIAL # UNSPECIFIED PREREQUISITES TUTORIAL
# #
# A build rule must exist for all files that make needs to consider in # A build rule must exist for all files that make needs to consider in

View file

@ -9,19 +9,12 @@
#endif #endif
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/nt/nt/process.h"
#include "libc/mem/gc.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
dontinline void dostuff(const char *s) { dontinline void dostuff(const char *s) {
int i, us; int i, us;
srand(rand64()); /* seeds rand() w/ intel rdrnd, auxv, etc. */ srand(_rand64()); /* seeds rand() w/ intel rdrnd, auxv, etc. */
for (i = 0; i < 5; ++i) { for (i = 0; i < 5; ++i) {
us = rand() % 500000; us = rand() % 500000;
usleep(us); usleep(us);

View file

@ -102,7 +102,7 @@ uint64_t rngset64(void) {
static unsigned i; static unsigned i;
static uint64_t s; static uint64_t s;
if (!i) { if (!i) {
s = rand64(); s = _rand64();
i = (s + 1) & (511); i = (s + 1) & (511);
} }
return MarsagliaXorshift64(&s); return MarsagliaXorshift64(&s);
@ -215,7 +215,7 @@ const struct Function {
{"moby", moby}, // {"moby", moby}, //
{"mt19937", _mt19937}, // {"mt19937", _mt19937}, //
{"python", pythonx2}, // {"python", pythonx2}, //
{"rand64", rand64}, // {"rand64", _rand64}, //
{"rdrand", rdrand}, // {"rdrand", rdrand}, //
{"rdrnd", rdrand}, // {"rdrnd", rdrand}, //
{"rdseed", rdseed}, // {"rdseed", rdseed}, //

View file

@ -86,7 +86,7 @@ int tmpfd(void) {
*p++ = '.'; *p++ = '.';
} }
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
x = rand64(); x = _rand64();
for (j = 0; j < 6; ++j) { for (j = 0; j < 6; ++j) {
p[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[x % 36]; p[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[x % 36];
x /= 36; x /= 36;

View file

@ -63,7 +63,7 @@ int ResolveDns(const struct ResolvConf *resolvconf, int af, const char *name,
if (!resolvconf->nameservers.i) return 0; if (!resolvconf->nameservers.i) return 0;
bzero(&h, sizeof(h)); bzero(&h, sizeof(h));
rc = ebadmsg(); rc = ebadmsg();
h.id = rand64(); h.id = _rand64();
h.bf1 = 1; /* recursion desired */ h.bf1 = 1; /* recursion desired */
h.qdcount = 1; h.qdcount = 1;
q.qname = name; q.qname = name;

View file

@ -24,17 +24,17 @@
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include "libc/intrin/bits.h"
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/dns/consts.h" #include "libc/dns/consts.h"
#include "libc/dns/dns.h" #include "libc/dns/dns.h"
#include "libc/dns/dnsheader.h" #include "libc/dns/dnsheader.h"
#include "libc/dns/dnsquestion.h" #include "libc/dns/dnsquestion.h"
#include "libc/dns/resolvconf.h" #include "libc/dns/resolvconf.h"
#include "libc/intrin/bits.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/sock/sock.h" #include "libc/sock/sock.h"
#include "libc/stdio/rand.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/af.h" #include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/ipproto.h"
@ -65,7 +65,7 @@ int ResolveDnsReverse(const struct ResolvConf *resolvconf, int af,
if (!resolvconf->nameservers.i) return 0; if (!resolvconf->nameservers.i) return 0;
bzero(&h, sizeof(h)); bzero(&h, sizeof(h));
rc = ebadmsg(); rc = ebadmsg();
h.id = rand64(); h.id = _rand64();
h.bf1 = 1; /* recursion desired */ h.bf1 = 1; /* recursion desired */
h.qdcount = 1; h.qdcount = 1;
q.qname = name; q.qname = name;

View file

@ -29,7 +29,7 @@
* tracked by a global variable which is updated at fork(). The only * tracked by a global variable which is updated at fork(). The only
* exception is when the process is vfork()'d in which case a system * exception is when the process is vfork()'d in which case a system
* call shall be issued. This optimization helps make functions like * call shall be issued. This optimization helps make functions like
* rand64() fork-safe, however it could lead to race conditions in * _rand64() fork-safe, however it could lead to race conditions in
* programs that mix fork() with threads. In that case, apps should * programs that mix fork() with threads. In that case, apps should
* consider using `sys_getpid().ax` instead to force a system call. * consider using `sys_getpid().ax` instead to force a system call.
* *

View file

@ -46,7 +46,7 @@ static struct {
* @threadsafe * @threadsafe
* @vforksafe * @vforksafe
*/ */
uint64_t rand64(void) { uint64_t _rand64(void) {
void *p; void *p;
uint128_t s; uint128_t s;
if (__threaded) pthread_spin_lock(&g_rand64.lock); if (__threaded) pthread_spin_lock(&g_rand64.lock);

View file

@ -29,7 +29,7 @@
* double x = _real1(lemur64()); // make float on [0,1]-interval * double x = _real1(lemur64()); // make float on [0,1]-interval
* *
* If you want a fast pseudorandom number generator that seeds itself * If you want a fast pseudorandom number generator that seeds itself
* automatically on startup and fork() then consider rand64(). If you * automatically on startup and fork(), then consider _rand64. If you
* want true random data then consider rdseed, rdrand, and getrandom. * want true random data then consider rdseed, rdrand, and getrandom.
* *
* @return 64 bits of pseudorandom data * @return 64 bits of pseudorandom data
@ -37,7 +37,7 @@
* @note this function takes at minimum 1 cycle * @note this function takes at minimum 1 cycle
* @note this function passes bigcrush and practrand * @note this function passes bigcrush and practrand
* @note this function is not intended for cryptography * @note this function is not intended for cryptography
* @see rand64(), rngset(), _real1(), _real2(), _real3() * @see _rand64(), rngset(), _real1(), _real2(), _real3()
*/ */
uint64_t lemur64(void) { uint64_t lemur64(void) {
static uint128_t s = 2131259787901769494; static uint128_t s = 2131259787901769494;

View file

@ -42,7 +42,7 @@ char *mkdtemp(char *template) {
int i, j, n; int i, j, n;
if ((n = strlen(template)) >= 6 && !memcmp(template + n - 6, "XXXXXX", 6)) { if ((n = strlen(template)) >= 6 && !memcmp(template + n - 6, "XXXXXX", 6)) {
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
x = rand64(); x = _rand64();
for (j = 0; j < 6; ++j) { for (j = 0; j < 6; ++j) {
template[n - 6 + j] = "0123456789abcdefghijklmnopqrstuvwxyz"[x % 36]; template[n - 6 + j] = "0123456789abcdefghijklmnopqrstuvwxyz"[x % 36];
x /= 36; x /= 36;

View file

@ -77,7 +77,7 @@ static uint64_t g_mkostemps_reseed;
dontdiscard int mkostempsm(char *template, int suffixlen, unsigned flags, dontdiscard int mkostempsm(char *template, int suffixlen, unsigned flags,
int mode) { int mode) {
int fd; int fd;
if (g_mkostemps_reseed++ % RESEED == 0) g_mkostemps_rand = rand64(); if (g_mkostemps_reseed++ % RESEED == 0) g_mkostemps_rand = _rand64();
fd = mkostempsmi(template, suffixlen, flags, &g_mkostemps_rand, mode, open); fd = mkostempsmi(template, suffixlen, flags, &g_mkostemps_rand, mode, open);
STRACE("mkostempsm([%#s], %'d, %#x, %#o) → %d% m", template, suffixlen, flags, STRACE("mkostempsm([%#s], %'d, %#x, %#o) → %d% m", template, suffixlen, flags,
mode, fd); mode, fd);

View file

@ -35,7 +35,7 @@
* *
* @note this function does well on bigcrush and practrand * @note this function does well on bigcrush and practrand
* @note this function is not intended for cryptography * @note this function is not intended for cryptography
* @see lemur64(), rand64(), rdrand() * @see lemur64(), _rand64(), rdrand()
*/ */
int rand(void) { int rand(void) {
return KnuthLinearCongruentialGenerator(&g_rando) >> 33; return KnuthLinearCongruentialGenerator(&g_rando) >> 33;

View file

@ -23,7 +23,7 @@ long random(void);
void srandom(unsigned); void srandom(unsigned);
uint64_t lemur64(void); uint64_t lemur64(void);
uint64_t rand64(void); uint64_t _rand64(void);
uint64_t vigna(void); uint64_t vigna(void);
uint64_t vigna_r(uint64_t[hasatleast 1]); uint64_t vigna_r(uint64_t[hasatleast 1]);
void svigna(uint64_t); void svigna(uint64_t);

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/asmflag.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/intrin/asmflag.h"
#include "libc/nexgen32e/x86feature.h" #include "libc/nexgen32e/x86feature.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/sysv/consts/grnd.h" #include "libc/sysv/consts/grnd.h"
@ -38,7 +38,7 @@ static dontinline uint64_t rdrand_failover(void) {
r = 0; r = 0;
f = 0; f = 0;
} else { } else {
return rand64(); return _rand64();
} }
} }
} }
@ -59,7 +59,7 @@ static dontinline uint64_t rdrand_failover(void) {
* @note this function could block a nontrivial time on old computers * @note this function could block a nontrivial time on old computers
* @note this function is indeed intended for cryptography * @note this function is indeed intended for cryptography
* @note this function takes around 300 cycles * @note this function takes around 300 cycles
* @see rngset(), rdseed(), rand64() * @see rngset(), rdseed(), _rand64()
* @asyncsignalsafe * @asyncsignalsafe
* @vforksafe * @vforksafe
*/ */

View file

@ -34,7 +34,7 @@
* @note this function could block a nontrivial time on old computers * @note this function could block a nontrivial time on old computers
* @note this function is indeed intended for cryptography * @note this function is indeed intended for cryptography
* @note this function takes around 800 cycles * @note this function takes around 800 cycles
* @see rngset(), rdrand(), rand64() * @see rngset(), rdrand(), _rand64()
* @asyncsignalsafe * @asyncsignalsafe
* @vforksafe * @vforksafe
*/ */

View file

@ -46,7 +46,7 @@ static uint64_t g_vigna;
* rngset(buf, sizeof(buf), vigna, 0); * rngset(buf, sizeof(buf), vigna, 0);
* *
* If you want a fast pseudorandom number generator that seeds itself * If you want a fast pseudorandom number generator that seeds itself
* automatically on startup and fork() then consider rand64(). If you * automatically on startup and fork(), then consider _rand64. If you
* want true random data then consider rdseed, rdrand, and getrandom. * want true random data then consider rdseed, rdrand, and getrandom.
* *
* @return 64 bits of pseudorandom data * @return 64 bits of pseudorandom data

View file

@ -22,8 +22,8 @@
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/nexgen32e/x86feature.h" #include "libc/nexgen32e/x86feature.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/buffer.internal.h" #include "libc/runtime/buffer.internal.h"
#include "libc/stdio/rand.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"
@ -146,7 +146,7 @@ TEST(scalevolume, testSmallestScale_justSaturates) {
// - ~200ns w/ -O3 -march=skylake MODE=rel // - ~200ns w/ -O3 -march=skylake MODE=rel
void randomizeaudio(void) { void randomizeaudio(void) {
rngset(pcm, sizeof(pcm), rand64, -1); rngset(pcm, sizeof(pcm), _rand64, -1);
} }
void scalevolume_pure(int amt) { void scalevolume_pure(int amt) {

View file

@ -27,7 +27,7 @@ char p[16];
struct TtyCursor c; struct TtyCursor c;
void SetUp(void) { void SetUp(void) {
rngset(p, sizeof(p), rand64, -1); rngset(p, sizeof(p), _rand64, -1);
} }
TEST(ttymove, sameCoord_doesNothing) { TEST(ttymove, sameCoord_doesNothing) {

View file

@ -95,7 +95,7 @@ int Worker(void *p, int tid) {
int i, rc, fd; int i, rc, fd;
for (i = 0; i < 64; ++i) { for (i = 0; i < 64; ++i) {
ASSERT_NE(-1, (fd = open("/zip/libc/testlib/hyperion.txt", O_RDONLY))); ASSERT_NE(-1, (fd = open("/zip/libc/testlib/hyperion.txt", O_RDONLY)));
usleep(rand64() % 100); usleep(_rand64() % 100);
for (;;) { for (;;) {
rc = read(fd, buf, 64); rc = read(fd, buf, 64);
if (rc != -1) { if (rc != -1) {

View file

@ -101,7 +101,7 @@ TEST(setrlimit, testFileSizeLimit) {
firstnonnull(getenv("TMPDIR"), "/tmp"), firstnonnull(getenv("TMPDIR"), "/tmp"),
firstnonnull(program_invocation_short_name, "unknown"), getpid()); firstnonnull(program_invocation_short_name, "unknown"), getpid());
ASSERT_NE(-1, (fd = open(tmpname, O_RDWR | O_CREAT | O_TRUNC, 0644))); ASSERT_NE(-1, (fd = open(tmpname, O_RDWR | O_CREAT | O_TRUNC, 0644)));
rngset(junkdata, 512, rand64, -1); rngset(junkdata, 512, _rand64, -1);
for (i = 0; i < 5 * 1024 * 1024 / 512; ++i) { for (i = 0; i < 5 * 1024 * 1024 / 512; ++i) {
ASSERT_EQ(512, write(fd, junkdata, 512)); ASSERT_EQ(512, write(fd, junkdata, 512));
} }
@ -143,7 +143,7 @@ TEST(setrlimit, testMemoryLimit) {
ASSERT_EQ(ENOMEM, errno); ASSERT_EQ(ENOMEM, errno);
_exit(0); _exit(0);
} }
rngset(p, PAGESIZE, rand64, -1); rngset(p, PAGESIZE, _rand64, -1);
} }
_exit(1); _exit(1);
} }
@ -171,7 +171,7 @@ TEST(setrlimit, testVirtualMemoryLimit) {
ASSERT_EQ(ENOMEM, errno); ASSERT_EQ(ENOMEM, errno);
_exit(0); _exit(0);
} }
rngset(p, PAGESIZE, rand64, -1); rngset(p, PAGESIZE, _rand64, -1);
} }
_exit(1); _exit(1);
} }
@ -201,7 +201,7 @@ TEST(setrlimit, testDataMemoryLimit) {
ASSERT_EQ(ENOMEM, errno); ASSERT_EQ(ENOMEM, errno);
_exit(0); _exit(0);
} }
rngset(p, PAGESIZE, rand64, -1); rngset(p, PAGESIZE, _rand64, -1);
} }
_exit(1); _exit(1);
} }

View file

@ -18,9 +18,9 @@
*/ */
#include "libc/dns/dns.h" #include "libc/dns/dns.h"
#include "libc/dns/dnsheader.h" #include "libc/dns/dnsheader.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"
@ -39,8 +39,8 @@ TEST(SerializeDnsHeader, test) {
TEST(SerializeDnsHeader, fuzzSymmetry) { TEST(SerializeDnsHeader, fuzzSymmetry) {
uint8_t buf[12]; uint8_t buf[12];
struct DnsHeader in, out; struct DnsHeader in, out;
rngset(&in, sizeof(in), rand64, -1); rngset(&in, sizeof(in), _rand64, -1);
rngset(&out, sizeof(out), rand64, -1); rngset(&out, sizeof(out), _rand64, -1);
SerializeDnsHeader(buf, &in); SerializeDnsHeader(buf, &in);
DeserializeDnsHeader(&out, buf); DeserializeDnsHeader(&out, buf);
ASSERT_EQ(0, memcmp(&in, &out, 12), "%#.*s\n\t%#.*s", 12, in, 12, buf); ASSERT_EQ(0, memcmp(&in, &out, 12), "%#.*s\n\t%#.*s", 12, in, 12, buf);

View file

@ -101,10 +101,10 @@
#include "libc/intrin/pxor.h" #include "libc/intrin/pxor.h"
#include "libc/limits.h" #include "libc/limits.h"
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/mem/gc.internal.h"
#include "libc/nexgen32e/kcpuids.h" #include "libc/nexgen32e/kcpuids.h"
#include "libc/stdio/lcg.internal.h" #include "libc/stdio/lcg.internal.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
@ -2105,8 +2105,8 @@ TEST(pdep, fuzz) {
int i; int i;
uint64_t x, y; uint64_t x, y;
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
x = rand64(); x = _rand64();
y = rand64(); y = _rand64();
ASSERT_EQ(pdep(x, y), (pdep)(x, y)); ASSERT_EQ(pdep(x, y), (pdep)(x, y));
} }
} }
@ -2115,8 +2115,8 @@ TEST(pext, fuzz) {
int i; int i;
uint64_t x, y; uint64_t x, y;
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
x = rand64(); x = _rand64();
y = rand64(); y = _rand64();
ASSERT_EQ(pext(x, y), (pext)(x, y)); ASSERT_EQ(pext(x, y), (pext)(x, y));
} }
} }

View file

@ -16,9 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
@ -65,7 +65,7 @@ TEST(memcmp, fuzz) {
int i, o, n, g; int i, o, n, g;
char a[256], b[256]; char a[256], b[256];
for (i = 0; i < 100000; ++i) { for (i = 0; i < 100000; ++i) {
rngset(a, sizeof(a), rand64, -1); rngset(a, sizeof(a), _rand64, -1);
memcpy(b, a, sizeof(a)); memcpy(b, a, sizeof(a));
if (rand() & 1) { if (rand() & 1) {
a[rand() % sizeof(a)] += rand(); a[rand() % sizeof(a)] += rand();

View file

@ -44,7 +44,7 @@ void OnChld(int sig) {
} }
dontinline void Generate(int i) { dontinline void Generate(int i) {
A[i] = rand64(); A[i] = _rand64();
} }
int Thrasher(void *arg, int tid) { int Thrasher(void *arg, int tid) {
@ -56,11 +56,11 @@ int Thrasher(void *arg, int tid) {
return 0; return 0;
} }
TEST(rand64, testLcg_doesntProduceIdenticalValues) { TEST(_rand64, testLcg_doesntProduceIdenticalValues) {
int i, j; int i, j;
bzero(A, sizeof(A)); bzero(A, sizeof(A));
for (i = 0; i < ARRAYLEN(A); ++i) { for (i = 0; i < ARRAYLEN(A); ++i) {
A[i] = rand64(); A[i] = _rand64();
} }
for (i = 0; i < ARRAYLEN(A); ++i) { for (i = 0; i < ARRAYLEN(A); ++i) {
EXPECT_NE(0, A[i], "i=%d", i); EXPECT_NE(0, A[i], "i=%d", i);
@ -71,7 +71,7 @@ TEST(rand64, testLcg_doesntProduceIdenticalValues) {
} }
} }
TEST(rand64, testThreadSafety_doesntProduceIdenticalValues) { TEST(_rand64, testThreadSafety_doesntProduceIdenticalValues) {
int i, j, rc, ws; int i, j, rc, ws;
sigset_t ss, oldss; sigset_t ss, oldss;
struct sigaction oldsa; struct sigaction oldsa;

View file

@ -114,7 +114,7 @@ TEST(strlen, fuzz) {
char *b; char *b;
size_t n, n1, n2; size_t n, n1, n2;
for (n = 2; n < 1026; ++n) { for (n = 2; n < 1026; ++n) {
b = rngset(calloc(1, n), n - 1, rand64, -1); b = rngset(calloc(1, n), n - 1, _rand64, -1);
n1 = strlen(b); n1 = strlen(b);
n2 = strlen_pure(b); n2 = strlen_pure(b);
ASSERT_EQ(n1, n2, "%#.*s", n, b); ASSERT_EQ(n1, n2, "%#.*s", n, b);

View file

@ -101,10 +101,10 @@ static int CompareInt(const void *a, const void *b) {
BENCH(djbsort, bench) { BENCH(djbsort, bench) {
n = 256; n = 256;
a = _gc(memalign(32, n * 4)); a = _gc(memalign(32, n * 4));
EZBENCH2("insertionsort[255]", rngset(a, n * 4, rand64, -1), EZBENCH2("insertionsort[255]", rngset(a, n * 4, _rand64, -1),
insertionsort(a, n)); insertionsort(a, n));
EZBENCH2("djbsort[255]", rngset(a, n * 4, rand64, -1), djbsort(a, n)); EZBENCH2("djbsort[255]", rngset(a, n * 4, _rand64, -1), djbsort(a, n));
EZBENCH2("_intsort[255]", rngset(a, n * 4, rand64, -1), _intsort(a, n)); EZBENCH2("_intsort[255]", rngset(a, n * 4, _rand64, -1), _intsort(a, n));
EZBENCH2("qsort[255]", rngset(a, n * 4, rand64, -1), EZBENCH2("qsort[255]", rngset(a, n * 4, _rand64, -1),
qsort(a, n, sizeof(int), CompareInt)); qsort(a, n, sizeof(int), CompareInt));
} }

View file

@ -42,7 +42,7 @@ TEST(MemMove, overlapping) {
for (i = 0; i < N; i += S) { for (i = 0; i < N; i += S) {
for (j = 0; j < N; j += S) { for (j = 0; j < N; j += S) {
for (n = MIN(N - i, N - j) + 1; n--;) { for (n = MIN(N - i, N - j) + 1; n--;) {
b0 = rngset(malloc(N), N, rand64, -1); b0 = rngset(malloc(N), N, _rand64, -1);
b1 = memcpy(malloc(N), b0, N); b1 = memcpy(malloc(N), b0, N);
b2 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N);
ASSERT_EQ(b1 + j, memmove(b1 + j, b1 + i, n)); ASSERT_EQ(b1 + j, memmove(b1 + j, b1 + i, n));
@ -66,7 +66,7 @@ TEST(MemCpy, overlapping) {
for (j = 0; j < N; j += S) { for (j = 0; j < N; j += S) {
for (n = MIN(N - i, N - j) + 1; n--;) { for (n = MIN(N - i, N - j) + 1; n--;) {
if (j <= i) { if (j <= i) {
b0 = rngset(malloc(N), N, rand64, -1); b0 = rngset(malloc(N), N, _rand64, -1);
b1 = memcpy(malloc(N), b0, N); b1 = memcpy(malloc(N), b0, N);
b2 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N);
ASSERT_EQ(b1 + j, memcpy(b1 + j, b1 + i, n)); ASSERT_EQ(b1 + j, memcpy(b1 + j, b1 + i, n));
@ -90,7 +90,7 @@ TEST(MemMove, overlappingDirect) {
for (i = 0; i < N; i += S) { for (i = 0; i < N; i += S) {
for (j = 0; j < N; j += S) { for (j = 0; j < N; j += S) {
for (n = MIN(N - i, N - j) + 1; n--;) { for (n = MIN(N - i, N - j) + 1; n--;) {
b0 = rngset(malloc(N), N, rand64, -1); b0 = rngset(malloc(N), N, _rand64, -1);
b1 = memcpy(malloc(N), b0, N); b1 = memcpy(malloc(N), b0, N);
b2 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N);
ASSERT_EQ(b1 + j, (memmove)(b1 + j, b1 + i, n)); ASSERT_EQ(b1 + j, (memmove)(b1 + j, b1 + i, n));

View file

@ -16,10 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/nexgen32e/nexgen32e.h" #include "libc/nexgen32e/nexgen32e.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"
@ -56,7 +56,7 @@ BENCH(strtolower, bench) {
EZBENCH2( EZBENCH2(
"strtolower", "strtolower",
{ {
rngset(data, size, rand64, -1); rngset(data, size, _rand64, -1);
data[size - 1] = 0; data[size - 1] = 0;
}, },
strtolower(data)); strtolower(data));

View file

@ -144,7 +144,7 @@ void MeatyReadWriteTest(void) {
char *mem, *buf; char *mem, *buf;
n = 8 * 1024 * 1024; n = 8 * 1024 * 1024;
buf = gc(malloc(n)); buf = gc(malloc(n));
mem = rngset(gc(malloc(n)), n, rand64, -1); mem = rngset(gc(malloc(n)), n, _rand64, -1);
ASSERT_NE(NULL, (f = fopen(PATH, "wb"))); ASSERT_NE(NULL, (f = fopen(PATH, "wb")));
setbuffer(f, gc(malloc(4 * 1000 * 1000)), 4 * 1000 * 1000); setbuffer(f, gc(malloc(4 * 1000 * 1000)), 4 * 1000 * 1000);
EXPECT_EQ(n, fwrite(mem, 1, n, f)); EXPECT_EQ(n, fwrite(mem, 1, n, f));

View file

@ -180,7 +180,7 @@ uint64_t Rand64LowByte(void) {
uint64_t x; uint64_t x;
for (x = i = 0; i < 8; ++i) { for (x = i = 0; i < 8; ++i) {
x <<= 8; x <<= 8;
x |= rand64() & 255; x |= _rand64() & 255;
} }
return x; return x;
} }
@ -203,7 +203,7 @@ static const struct RandomFunction {
{"SixthEditionLowByte", SixthEditionLowByte, false}, // {"SixthEditionLowByte", SixthEditionLowByte, false}, //
{"SeventhEditionLowByte", SeventhEditionLowByte, false}, // {"SeventhEditionLowByte", SeventhEditionLowByte, false}, //
{"KnuthLcg", KnuthLcg, false}, // {"KnuthLcg", KnuthLcg, false}, //
{"rand64", rand64, true}, // {"rand64", _rand64, true}, //
{"Rand64LowByte", Rand64LowByte, true}, // {"Rand64LowByte", Rand64LowByte, true}, //
{"GetRandom", GetRandom, true}, // {"GetRandom", GetRandom, true}, //
}; };

View file

@ -16,14 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/bits.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h" #include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/sysv/consts/grnd.h" #include "libc/sysv/consts/grnd.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
@ -157,14 +157,14 @@ TEST(mt19937, test) {
BENCH(mt19937, bench8) { BENCH(mt19937, bench8) {
volatile uint64_t x; volatile uint64_t x;
EZBENCH2("lemur64", donothing, x = lemur64()); EZBENCH2("lemur64", donothing, x = lemur64());
EZBENCH2("rand64", donothing, x = rand64()); EZBENCH2("_rand64", donothing, x = _rand64());
EZBENCH2("vigna", donothing, x = vigna()); EZBENCH2("vigna", donothing, x = vigna());
EZBENCH2("vigna_r", donothing, vigna_r(&x)); EZBENCH2("vigna_r", donothing, vigna_r(&x));
EZBENCH2("xorshift", donothing, x = xorshift()); EZBENCH2("xorshift", donothing, x = xorshift());
EZBENCH2("knuth", donothing, x = knuth()); EZBENCH2("knuth", donothing, x = knuth());
EZBENCH2("random", donothing, x = urandom()); EZBENCH2("random", donothing, x = urandom());
EZBENCH2("mt19937", donothing, x = _mt19937()); EZBENCH2("mt19937", donothing, x = _mt19937());
EZBENCH2("rand64char", donothing, x = rand64()); EZBENCH2("rand64char", donothing, x = _rand64());
size_t i = 0; size_t i = 0;
volatile uint8_t *p = gc(malloc(3 * 2048 * 2 * 8)); volatile uint8_t *p = gc(malloc(3 * 2048 * 2 * 8));
EZBENCH3("rdrand", 2048, donothing, p[i++] = rdrand()); EZBENCH3("rdrand", 2048, donothing, p[i++] = rdrand());
@ -174,7 +174,7 @@ BENCH(mt19937, bench8) {
BENCH(mt19937, bench32k) { BENCH(mt19937, bench32k) {
volatile char *p = gc(malloc(32768)); volatile char *p = gc(malloc(32768));
EZBENCH_N("rngset(rand64,-1)", 32768, rngset(p, 32768, rand64, -1)); EZBENCH_N("rngset(_rand64,-1)", 32768, rngset(p, 32768, _rand64, -1));
EZBENCH_N("rngset(rdseed,512)", 32768, rngset(p, 32768, rdseed, 512)); EZBENCH_N("rngset(rdseed,512)", 32768, rngset(p, 32768, rdseed, 512));
EZBENCH_N("ctrdrbg+rdseed [blk]", 32768, ctrdrbg1(p, 32768)); EZBENCH_N("ctrdrbg+rdseed [blk]", 32768, ctrdrbg1(p, 32768));
EZBENCH_N("getrandom [block]", 32768, GetRandom(p, 32768)); EZBENCH_N("getrandom [block]", 32768, GetRandom(p, 32768));
@ -183,7 +183,7 @@ BENCH(mt19937, bench32k) {
EZBENCH_N("knuth [word]", 32768, knutha(p, 32768)); EZBENCH_N("knuth [word]", 32768, knutha(p, 32768));
EZBENCH_N("random [word]", 32768, rngset(p, 32768, urandom, 0)); EZBENCH_N("random [word]", 32768, rngset(p, 32768, urandom, 0));
EZBENCH_N("mt19937 [word]", 32768, rngset(p, 32768, _mt19937, 0)); EZBENCH_N("mt19937 [word]", 32768, rngset(p, 32768, _mt19937, 0));
EZBENCH_N("rand64 [word]", 32768, rngset(p, 32768, rand64, 0)); EZBENCH_N("_rand64 [word]", 32768, rngset(p, 32768, _rand64, 0));
EZBENCH_N("rdrand [word]", 32768, rngset(p, 32768, rdrand, 0)); EZBENCH_N("rdrand [word]", 32768, rngset(p, 32768, rdrand, 0));
EZBENCH_N("rdseed [word]", 32768, rngset(p, 32768, rdseed, 0)); EZBENCH_N("rdseed [word]", 32768, rngset(p, 32768, rdseed, 0));
} }

View file

@ -39,16 +39,16 @@ TEST(rand003, srandSmokeTest) {
} }
TEST(rand005, rand64SmokeTest) { TEST(rand005, rand64SmokeTest) {
ASSERT_TRUE(rand64() != rand64() || rand64() != rand64()); ASSERT_TRUE(_rand64() != _rand64() || _rand64() != _rand64());
} }
TEST(rand64, test) { TEST(_rand64, test) {
char *p; char *p;
size_t i; size_t i;
uint64_t x; uint64_t x;
p = memcpy(malloc(kHyperionSize), kHyperion, kHyperionSize); p = memcpy(malloc(kHyperionSize), kHyperion, kHyperionSize);
for (i = 0; i < kHyperionSize / 8; ++i) { for (i = 0; i < kHyperionSize / 8; ++i) {
x = rand64(); x = _rand64();
WRITE64LE(p + i * 8, x); WRITE64LE(p + i * 8, x);
} }
free(p); free(p);

View file

@ -68,7 +68,7 @@ TEST(BLAKE2B256Test, ABC) {
BENCH(blake2, bench) { BENCH(blake2, bench) {
char fun[256]; char fun[256];
rngset(fun, 256, rand64, -1); rngset(fun, 256, _rand64, -1);
EZBENCH_N("blake2b256", 0, EZBLAKE2B256(0, 0)); EZBENCH_N("blake2b256", 0, EZBLAKE2B256(0, 0));
EZBENCH_N("blake2b256", 8, EZBLAKE2B256("helloooo", 8)); EZBENCH_N("blake2b256", 8, EZBLAKE2B256("helloooo", 8));
EZBENCH_N("blake2b256", 31, EZBLAKE2B256(fun, 31)); EZBENCH_N("blake2b256", 31, EZBLAKE2B256(fun, 31));

View file

@ -16,9 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
@ -30,7 +30,7 @@ TEST(bzero, test) {
a = gc(malloc(128)); a = gc(malloc(128));
b = gc(malloc(128)); b = gc(malloc(128));
for (n = 0; n < 128; ++n) { for (n = 0; n < 128; ++n) {
rngset(a, 128, rand64, -1); rngset(a, 128, _rand64, -1);
memcpy(b, a, 128); memcpy(b, a, 128);
bzero(a, n); bzero(a, n);
memset(b, 0, n); memset(b, 0, n);

View file

@ -96,7 +96,7 @@ TEST(highwayhash64, test) {
BENCH(highwayhash64, newbench) { BENCH(highwayhash64, newbench) {
char fun[256]; char fun[256];
rngset(fun, 256, rand64, -1); rngset(fun, 256, _rand64, -1);
EZBENCH_N("highwayhash64", 0, HighwayHash64(0, 0, kTestKey1)); EZBENCH_N("highwayhash64", 0, HighwayHash64(0, 0, kTestKey1));
EZBENCH_N("highwayhash64", 8, HighwayHash64("helloooo", 8, kTestKey1)); EZBENCH_N("highwayhash64", 8, HighwayHash64("helloooo", 8, kTestKey1));
EZBENCH_N("highwayhash64", 31, HighwayHash64(fun, 31, kTestKey1)); EZBENCH_N("highwayhash64", 31, HighwayHash64(fun, 31, kTestKey1));

View file

@ -56,7 +56,7 @@ TEST(memccpy, memcpy) {
b1 = calloc(1, n); b1 = calloc(1, n);
b2 = calloc(1, n); b2 = calloc(1, n);
b3 = calloc(1, n); b3 = calloc(1, n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
e1 = memccpy_pure(b2, b1, 31337, n); e1 = memccpy_pure(b2, b1, 31337, n);
e2 = memccpy(b3, b1, 31337, n); e2 = memccpy(b3, b1, 31337, n);
n1 = e1 ? e1 - b2 : n; n1 = e1 ? e1 - b2 : n;

View file

@ -30,8 +30,8 @@ TEST(memcpy, test) {
for (unsigned n = 0; n < 1026; ++n) { for (unsigned n = 0; n < 1026; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1, memcpy(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, b2); ASSERT_EQ(b1, memcpy(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, b2);
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -40,8 +40,8 @@ TEST(memcpy, test) {
for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) { for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1, memcpy(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, b2); ASSERT_EQ(b1, memcpy(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, b2);
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -54,8 +54,8 @@ TEST(mempcpy, test) {
for (unsigned n = 0; n < 1026; ++n) { for (unsigned n = 0; n < 1026; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1 + n, mempcpy(b1, b2, n)); ASSERT_EQ(b1 + n, mempcpy(b1, b2, n));
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -64,8 +64,8 @@ TEST(mempcpy, test) {
for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) { for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1 + n, mempcpy(b1, b2, n)); ASSERT_EQ(b1 + n, mempcpy(b1, b2, n));
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -78,8 +78,8 @@ TEST(memcpy, direct) {
for (unsigned n = 0; n < 1026; ++n) { for (unsigned n = 0; n < 1026; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1, (memcpy)(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, ASSERT_EQ(b1, (memcpy)(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n,
b2); b2);
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
@ -89,8 +89,8 @@ TEST(memcpy, direct) {
for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) { for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1, (memcpy)(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n, ASSERT_EQ(b1, (memcpy)(b1, b2, n), "%ld\n\t%#.*s\n\t%#.*s", n, n, b1, n,
b2); b2);
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
@ -104,8 +104,8 @@ TEST(mempcpy, direct) {
for (unsigned n = 0; n < 1026; ++n) { for (unsigned n = 0; n < 1026; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1 + n, (mempcpy)(b1, b2, n)); ASSERT_EQ(b1 + n, (mempcpy)(b1, b2, n));
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -114,8 +114,8 @@ TEST(mempcpy, direct) {
for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) { for (unsigned n = kHalfCache3 - 1; n < kHalfCache3 + 2; ++n) {
b1 = malloc(n); b1 = malloc(n);
b2 = malloc(n); b2 = malloc(n);
rngset(b1, n, rand64, -1); rngset(b1, n, _rand64, -1);
rngset(b2, n, rand64, -1); rngset(b2, n, _rand64, -1);
ASSERT_EQ(b1 + n, (mempcpy)(b1, b2, n)); ASSERT_EQ(b1 + n, (mempcpy)(b1, b2, n));
ASSERT_EQ(0, memcmp(b1, b2, n)); ASSERT_EQ(0, memcmp(b1, b2, n));
free(b2); free(b2);
@ -162,8 +162,8 @@ TEST(memcpy, testBackwardsOverlap3) {
#define B(F, N) \ #define B(F, N) \
do { \ do { \
char *d = rngset(malloc(N), N, rand64, -1); \ char *d = rngset(malloc(N), N, _rand64, -1); \
char *s = rngset(malloc(N), N, rand64, -1); \ char *s = rngset(malloc(N), N, _rand64, -1); \
EZBENCH2(#F " " #N, donothing, \ EZBENCH2(#F " " #N, donothing, \
EXPROPRIATE(F(VEIL("r", d), VEIL("r", s), N))); \ EXPROPRIATE(F(VEIL("r", d), VEIL("r", s), N))); \
free(d); \ free(d); \

View file

@ -118,7 +118,7 @@ TEST(memchr, fuzz) {
p = malloc(64); p = malloc(64);
for (i = -2; i < 257; ++i) { for (i = -2; i < 257; ++i) {
for (j = 0; j < 17; ++j) { for (j = 0; j < 17; ++j) {
rngset(p, 64, rand64, -1); rngset(p, 64, _rand64, -1);
ASSERT_EQ(memchr(p + j, i, 64 - j), memchr_pure(p + j, i, 64 - j)); ASSERT_EQ(memchr(p + j, i, 64 - j), memchr_pure(p + j, i, 64 - j));
} }
} }
@ -139,7 +139,7 @@ TEST(strchrnul, fuzz) {
p = calloc(1, 64); p = calloc(1, 64);
for (i = -2; i < 257; ++i) { for (i = -2; i < 257; ++i) {
for (j = 0; j < 17; ++j) { for (j = 0; j < 17; ++j) {
rngset(p, 63, rand64, -1); rngset(p, 63, _rand64, -1);
ASSERT_EQ(strchrnul(p + j, i), strchrnul_pure(p + j, i)); ASSERT_EQ(strchrnul(p + j, i), strchrnul_pure(p + j, i));
} }
} }
@ -159,7 +159,7 @@ TEST(rawmemchr, fuzz) {
p = malloc(64); p = malloc(64);
for (i = -2; i < 257; ++i) { for (i = -2; i < 257; ++i) {
for (j = 0; j < 17; ++j) { for (j = 0; j < 17; ++j) {
rngset(p, 63, rand64, -1); rngset(p, 63, _rand64, -1);
p[63] = i; p[63] = i;
ASSERT_EQ(rawmemchr(p + j, i), rawmemchr_pure(p + j, i)); ASSERT_EQ(rawmemchr(p + j, i), rawmemchr_pure(p + j, i));
} }

View file

@ -20,10 +20,10 @@
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/intrin/bits.h" #include "libc/intrin/bits.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/nexgen32e/cachesize.h" #include "libc/nexgen32e/cachesize.h"
#include "libc/nexgen32e/x86feature.h" #include "libc/nexgen32e/x86feature.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
@ -506,7 +506,7 @@ dontinline int strcasecmp_pure(const char *a, const char *b) {
} }
char *randomize_buf2str(size_t size, char data[size]) { char *randomize_buf2str(size_t size, char data[size]) {
rngset(data, size, rand64, -1); rngset(data, size, _rand64, -1);
data[size - 1] = '\0'; data[size - 1] = '\0';
return data; return data;
} }

View file

@ -16,9 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/ezbench.h" #include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h" #include "libc/testlib/hyperion.h"
@ -75,7 +75,7 @@ TEST(EncodeBase64, testOom_returnsNullAndSetsSizeToZero) {
TEST(Base64, RoundTrip) { TEST(Base64, RoundTrip) {
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
n = rand() % 32; n = rand() % 32;
rngset(b, n, rand64, -1); rngset(b, n, _rand64, -1);
p = EncodeBase64(b, n, &m); p = EncodeBase64(b, n, &m);
q = DecodeBase64(p, m, &m); q = DecodeBase64(p, m, &m);
ASSERT_EQ(n, m); ASSERT_EQ(n, m);
@ -88,7 +88,7 @@ TEST(Base64, RoundTrip) {
TEST(Base64, Fuzz) { TEST(Base64, Fuzz) {
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
n = rand() % 32; n = rand() % 32;
rngset(b, n, rand64, -1); rngset(b, n, _rand64, -1);
free(DecodeBase64(p, m, 0)); free(DecodeBase64(p, m, 0));
} }
} }

View file

@ -65,12 +65,12 @@ uint64_t rng[12];
mbedtls_ecp_group grp; mbedtls_ecp_group grp;
int GetEntropy(void *c, unsigned char *p, size_t n) { int GetEntropy(void *c, unsigned char *p, size_t n) {
rngset(p, n, rand64, -1); rngset(p, n, _rand64, -1);
return 0; return 0;
} }
void SetUp(void) { void SetUp(void) {
rngset(rng, sizeof(rng), rand64, -1); rngset(rng, sizeof(rng), _rand64, -1);
} }
#ifdef MBEDTLS_SELF_TEST #ifdef MBEDTLS_SELF_TEST
@ -169,7 +169,7 @@ BENCH(p256, bench) {
mbedtls_ecp_group_init(&grp); mbedtls_ecp_group_init(&grp);
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1); mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
mbedtls_mpi x = {1, 8, _gc(calloc(8, 8))}; mbedtls_mpi x = {1, 8, _gc(calloc(8, 8))};
rngset(x.p, 8 * 8, rand64, -1); rngset(x.p, 8 * 8, _rand64, -1);
EZBENCH2("P-256 modulus MbedTLS MPI lib", donothing, P256_MPI(&x)); EZBENCH2("P-256 modulus MbedTLS MPI lib", donothing, P256_MPI(&x));
EZBENCH2("P-256 modulus Justine rewrite", donothing, P256_JUSTINE(&x)); EZBENCH2("P-256 modulus Justine rewrite", donothing, P256_JUSTINE(&x));
mbedtls_ecp_group_free(&grp); mbedtls_ecp_group_free(&grp);
@ -704,7 +704,7 @@ TEST(ShiftRightAvx, test1) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[1] = {rand64()}; uint64_t mem[1] = {_rand64()};
uint64_t want[1]; uint64_t want[1];
uint64_t got[1]; uint64_t got[1];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -719,7 +719,7 @@ TEST(ShiftRightAvx, test2) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[2] = {rand64(), rand64()}; uint64_t mem[2] = {_rand64(), _rand64()};
uint64_t want[2]; uint64_t want[2];
uint64_t got[2]; uint64_t got[2];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -735,7 +735,7 @@ TEST(ShiftRightAvx, test3) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[3] = {rand64(), rand64(), rand64()}; uint64_t mem[3] = {_rand64(), _rand64(), _rand64()};
uint64_t want[3]; uint64_t want[3];
uint64_t got[3]; uint64_t got[3];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -752,7 +752,7 @@ TEST(ShiftRightAvx, test4) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[4] = {rand64(), rand64(), rand64(), rand64()}; uint64_t mem[4] = {_rand64(), _rand64(), _rand64(), _rand64()};
uint64_t want[4]; uint64_t want[4];
uint64_t got[4]; uint64_t got[4];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -770,8 +770,8 @@ TEST(ShiftRightAvx, test8) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[8] = {rand64(), rand64(), rand64(), rand64(), uint64_t mem[8] = {_rand64(), _rand64(), _rand64(), _rand64(),
rand64(), rand64(), rand64(), rand64()}; _rand64(), _rand64(), _rand64(), _rand64()};
uint64_t want[8]; uint64_t want[8];
uint64_t got[8]; uint64_t got[8];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -793,8 +793,8 @@ TEST(ShiftRightAvx, test9) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
int i; int i;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
uint64_t mem[9] = {rand64(), rand64(), rand64(), rand64(), rand64(), uint64_t mem[9] = {_rand64(), _rand64(), _rand64(), _rand64(), _rand64(),
rand64(), rand64(), rand64(), rand64()}; _rand64(), _rand64(), _rand64(), _rand64()};
uint64_t want[9]; uint64_t want[9];
uint64_t got[9]; uint64_t got[9];
memcpy(want, mem, sizeof(mem)); memcpy(want, mem, sizeof(mem));
@ -811,7 +811,7 @@ TEST(ShiftRightAvx, test9) {
BENCH(ShiftRight, bench) { BENCH(ShiftRight, bench) {
if (!X86_HAVE(AVX)) return; if (!X86_HAVE(AVX)) return;
uint64_t x[64]; uint64_t x[64];
rngset(x, sizeof(x), rand64, -1); rngset(x, sizeof(x), _rand64, -1);
EZBENCH2("ShiftRight", donothing, ShiftRight(x, 64, 1)); EZBENCH2("ShiftRight", donothing, ShiftRight(x, 64, 1));
EZBENCH2("ShiftRightAvx", donothing, ShiftRightAvx(x, 64, 1)); EZBENCH2("ShiftRightAvx", donothing, ShiftRightAvx(x, 64, 1));
EZBENCH2("ShiftRightPure", donothing, ShiftRightPure(x, 64, 1)); EZBENCH2("ShiftRightPure", donothing, ShiftRightPure(x, 64, 1));
@ -819,7 +819,7 @@ BENCH(ShiftRight, bench) {
BENCH(Zeroize, bench) { BENCH(Zeroize, bench) {
uint64_t x[64]; uint64_t x[64];
rngset(x, sizeof(x), rand64, -1); rngset(x, sizeof(x), _rand64, -1);
EZBENCH2("memset (64)", donothing, memset(x, 0, sizeof(x))); EZBENCH2("memset (64)", donothing, memset(x, 0, sizeof(x)));
EZBENCH2("Zeroize (64)", donothing, mbedtls_platform_zeroize(x, 64)); EZBENCH2("Zeroize (64)", donothing, mbedtls_platform_zeroize(x, 64));
} }

View file

@ -92,9 +92,9 @@ void *Worker(void *arg) {
for (int j = 0; j < 50; ++j) { for (int j = 0; j < 50; ++j) {
ASSERT_EQ(SQLITE_OK, DbExec(db, "BEGIN TRANSACTION")); ASSERT_EQ(SQLITE_OK, DbExec(db, "BEGIN TRANSACTION"));
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
ASSERT_EQ(SQLITE_OK, sqlite3_bind_int64(stmt[0], 1, rand64())); ASSERT_EQ(SQLITE_OK, sqlite3_bind_int64(stmt[0], 1, _rand64()));
ASSERT_EQ(SQLITE_OK, sqlite3_bind_text( ASSERT_EQ(SQLITE_OK, sqlite3_bind_text(
stmt[0], 2, kNames[rand64() % ARRAYLEN(kNames)], stmt[0], 2, kNames[_rand64() % ARRAYLEN(kNames)],
-1, SQLITE_TRANSIENT)); -1, SQLITE_TRANSIENT));
ASSERT_EQ(SQLITE_DONE, DbStep(stmt[0])); ASSERT_EQ(SQLITE_DONE, DbStep(stmt[0]));
ASSERT_EQ(SQLITE_OK, sqlite3_reset(stmt[0])); ASSERT_EQ(SQLITE_OK, sqlite3_reset(stmt[0]));

View file

@ -83,7 +83,7 @@ static int init_mparams(void) {
} }
else else
#endif /* USE_DEV_RANDOM */ #endif /* USE_DEV_RANDOM */
magic = (size_t)(rand64() ^ (size_t)0x55555555U); magic = (size_t)(_rand64() ^ (size_t)0x55555555U);
magic |= (size_t)8U; /* ensure nonzero */ magic |= (size_t)8U; /* ensure nonzero */
magic &= ~(size_t)7U; /* improve chances of fault for bad values */ magic &= ~(size_t)7U; /* improve chances of fault for bad values */
/* Until memory modes commonly available, use volatile-write */ /* Until memory modes commonly available, use volatile-write */

View file

@ -47,8 +47,8 @@ TEST(everest, tinierVersionBehavesTheSame) {
size_t i; size_t i;
uint8_t secret[32], bpoint[32], public[2][32]; uint8_t secret[32], bpoint[32], public[2][32];
for (i = 0; i < 500; ++i) { for (i = 0; i < 500; ++i) {
rngset(secret, sizeof(secret), rand64, -1); rngset(secret, sizeof(secret), _rand64, -1);
rngset(bpoint, sizeof(bpoint), rand64, -1); rngset(bpoint, sizeof(bpoint), _rand64, -1);
Hacl_Curve25519_crypto_scalarmult(public[0], secret, bpoint); Hacl_Curve25519_crypto_scalarmult(public[0], secret, bpoint);
curve25519(public[1], secret, bpoint); curve25519(public[1], secret, bpoint);
ASSERT_EQ(0, memcmp(public[0], public[1], sizeof(public[0]))); ASSERT_EQ(0, memcmp(public[0], public[1], sizeof(public[0])));
@ -70,8 +70,8 @@ TEST(everest, tinierVersionBehavesTheSame) {
BENCH(everest, bench) { BENCH(everest, bench) {
uint8_t secret[32], bpoint[32], public[32]; uint8_t secret[32], bpoint[32], public[32];
rngset(secret, sizeof(secret), rand64, -1); rngset(secret, sizeof(secret), _rand64, -1);
rngset(bpoint, sizeof(bpoint), rand64, -1); rngset(bpoint, sizeof(bpoint), _rand64, -1);
EZBENCH2("everest", donothing, EZBENCH2("everest", donothing,
Hacl_Curve25519_crypto_scalarmult(public, secret, bpoint)); Hacl_Curve25519_crypto_scalarmult(public, secret, bpoint));
EZBENCH2("mariana", donothing, curve25519(public, secret, bpoint)); EZBENCH2("mariana", donothing, curve25519(public, secret, bpoint));

View file

@ -32,7 +32,7 @@
int ecp_mod_p384_old(mbedtls_mpi *); int ecp_mod_p384_old(mbedtls_mpi *);
int GetEntropy(void *c, unsigned char *p, size_t n) { int GetEntropy(void *c, unsigned char *p, size_t n) {
rngset(p, n, rand64, -1); rngset(p, n, _rand64, -1);
return 0; return 0;
} }

View file

@ -3709,7 +3709,7 @@ void test_ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
* hundreds of milliseconds of latency, which we can't have in our pure * hundreds of milliseconds of latency, which we can't have in our pure
* testing infrastructure. * testing infrastructure.
*/ */
for( i = block_size; i < buflen; i += max( 1, rand64() & 31 ) ) for( i = block_size; i < buflen; i += max( 1, _rand64() & 31 ) )
{ {
mbedtls_test_set_step( i ); mbedtls_test_set_step( i );
/* Restore correct pre-encryption record */ /* Restore correct pre-encryption record */
@ -3747,7 +3747,7 @@ void test_ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
* hundreds of milliseconds of latency, which we can't have in our pure * hundreds of milliseconds of latency, which we can't have in our pure
* testing infrastructure. * testing infrastructure.
*/ */
for( i = padlen; i <= pad_max_len; i += max( 1, rand64() & 31 ) ) for( i = padlen; i <= pad_max_len; i += max( 1, _rand64() & 31 ) )
{ {
mbedtls_test_set_step( i ); mbedtls_test_set_step( i );
/* Restore correct pre-encryption record */ /* Restore correct pre-encryption record */

View file

@ -98,4 +98,4 @@ M(1, f, "fpclassify", Fpclassify, fpclassify(x),
"nan=0,inf=1,zero=2,subnorm=3,normal=4") "nan=0,inf=1,zero=2,subnorm=3,normal=4")
M(0, i, "rand", Rand, rand(), "deterministic random number") M(0, i, "rand", Rand, rand(), "deterministic random number")
M(0, i, "rand64", Rand64, rand64(), "64-bit random number") M(0, i, "rand64", _Rand64, _rand64(), "64-bit random number")

View file

@ -70,7 +70,7 @@ void UdpClient(void) {
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))); CHECK_NE(-1, (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)));
CHECK_NE(-1, connect(sock, &addr, addrsize)); CHECK_NE(-1, connect(sock, &addr, addrsize));
for (;;) { for (;;) {
rngset(buf, sizeof(buf), rand64, -1); rngset(buf, sizeof(buf), _rand64, -1);
CHECK_NE(-1, write(sock, &addr, addrsize)); CHECK_NE(-1, write(sock, &addr, addrsize));
} }
} }
@ -106,7 +106,7 @@ void TcpClient(void) {
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))); CHECK_NE(-1, (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)));
CHECK_NE(-1, connect(sock, &addr, addrsize)); CHECK_NE(-1, connect(sock, &addr, addrsize));
for (;;) { for (;;) {
rngset(buf, sizeof(buf), rand64, -1); rngset(buf, sizeof(buf), _rand64, -1);
CHECK_NE(-1, write(sock, buf, sizeof(buf))); CHECK_NE(-1, write(sock, buf, sizeof(buf)));
CHECK_NE(-1, read(sock, buf, sizeof(buf))); CHECK_NE(-1, read(sock, buf, sizeof(buf)));
} }

View file

@ -150,7 +150,7 @@ int LuaLemur64(lua_State *L) {
} }
int LuaRand64(lua_State *L) { int LuaRand64(lua_State *L) {
return LuaRand(L, rand64); return LuaRand(L, _rand64);
} }
int LuaRdrand(lua_State *L) { int LuaRdrand(lua_State *L) {

View file

@ -2611,7 +2611,7 @@ static char *ServeAssetCompressed(struct Asset *a) {
dg.i = 0; dg.i = 0;
dg.c = 0; dg.c = 0;
if (usingssl) { if (usingssl) {
dg.z = 512 + (rand64() & 1023); dg.z = 512 + (_rand64() & 1023);
} else { } else {
dg.z = 65536; dg.z = 65536;
} }

View file

@ -16,19 +16,19 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/safemacros.internal.h"
#include "libc/fmt/conv.h" #include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h" #include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h" #include "libc/limits.h"
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/log/log.h" #include "libc/log/log.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/math.h" #include "libc/math.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/lcg.internal.h" #include "libc/stdio/lcg.internal.h"
#include "libc/stdio/rand.h" #include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/ex.h" #include "libc/sysv/consts/ex.h"
@ -184,7 +184,7 @@ void GetOpts(int argc, char *argv[]) {
want_double = true; want_double = true;
break; break;
case 'g': case 'g':
rand_ = rand64; rand_ = _rand64;
break; break;
case 'N': case 'N':
name_ = optarg; name_ = optarg;

View file

@ -414,8 +414,8 @@ static void OnMouseLeftDrag(long y, long x) {
save_x = x; save_x = x;
y = top + (y << (zoom + !!zoom)); y = top + (y << (zoom + !!zoom));
x = left + (x << zoom); x = left + (x << zoom);
y += rand64() & ((1ul << (zoom + !!zoom)) - 1); y += _rand64() & ((1ul << (zoom + !!zoom)) - 1);
x += rand64() & ((1ul << zoom) - 1); x += _rand64() & ((1ul << zoom) - 1);
if (y < 0 || y >= byn) return; if (y < 0 || y >= byn) return;
if (x < 0 || x >= bxn) return; if (x < 0 || x >= bxn) return;
if (erase) { if (erase) {
@ -819,7 +819,7 @@ static void Rando1(void) {
long i, n; long i, n;
n = (byn * bxn) >> 6; n = (byn * bxn) >> 6;
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
board[i] = rand64(); board[i] = _rand64();
} }
} }