commit b99dc12ecece60caecf05d4b41495f6979f944d0 (tree)
parent 9d1a39c50ffeca1e42ed5f1b75a05d92eec869db
Author: Andrew Kelley <andrew@ziglang.org>
Date: Wed, 14 Jan 2026 19:10:00 +0100
Merge pull request 'libc: use common `rand` and `*rand48` implementations' (#30826) from GasInfinity/zig:libc-rand into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30826
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Diffstat:
18 files changed, 209 insertions(+), 140 deletions(-)
diff --git a/lib/c/stdlib.zig b/lib/c/stdlib.zig
@@ -7,6 +7,9 @@ const ldiv_t = std.c.ldiv_t;
const lldiv_t = std.c.lldiv_t;
comptime {
+ _ = @import("stdlib/rand.zig");
+ _ = @import("stdlib/drand48.zig");
+
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
// Functions specific to musl and wasi-libc.
@export(&abs, .{ .name = "abs", .linkage = common.linkage, .visibility = common.visibility });
diff --git a/lib/c/stdlib/drand48.zig b/lib/c/stdlib/drand48.zig
@@ -0,0 +1,147 @@
+//! drand48 functions are based off a 48-bit lcg prng: https://pubs.opengroup.org/onlinepubs/9799919799/functions/drand48.html
+
+const std = @import("std");
+const common = @import("../common.zig");
+const builtin = @import("builtin");
+const Lcg = std.Random.lcg.Wrapping(u48);
+
+comptime {
+ if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
+ @export(&erand48, .{ .name = "erand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&jrand48, .{ .name = "jrand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&nrand48, .{ .name = "nrand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&drand48, .{ .name = "drand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&lrand48, .{ .name = "lrand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&mrand48, .{ .name = "mrand48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&lcong48, .{ .name = "lcong48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&seed48, .{ .name = "seed48", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&srand48, .{ .name = "srand48", .linkage = common.linkage, .visibility = common.visibility });
+ }
+}
+
+// NOTE: all "magic" numbers and tests are extracted and adapted from the source above
+
+const default_multiplier = 0x5DEECE66D;
+const default_addend = 0xB;
+
+var lcg: Lcg = .init(0, default_multiplier, default_addend);
+var seed48_xi: [3]c_ushort = undefined;
+
+fn erand48(xsubi: *[3]c_ushort) callconv(.c) f64 {
+ const xi = @as(u48, @as(u16, @truncate(xsubi[0])) | (@as(u48, @as(u16, @truncate(xsubi[1])))) << 16) | (@as(u48, @as(u16, @truncate(xsubi[2]))) << 32);
+
+ var separate_lcg: Lcg = .init(xi, lcg.a, lcg.c);
+ const next_xi = separate_lcg.next();
+
+ xsubi.* = .{ @truncate(next_xi & 0xFFFF), @truncate((next_xi >> 16) & 0xFFFF), @truncate((next_xi >> 32) & 0xFFFF) };
+ return @as(f64, next_xi) / @as(f64, std.math.maxInt(u48));
+}
+
+fn jrand48(xsubi: *[3]c_ushort) callconv(.c) c_long {
+ const xi = @as(u48, @as(u16, @truncate(xsubi[0])) | (@as(u48, @as(u16, @truncate(xsubi[1])))) << 16) | (@as(u48, @as(u16, @truncate(xsubi[2]))) << 32);
+
+ var separate_lcg: Lcg = .init(xi, lcg.a, lcg.c);
+ const next_xi = separate_lcg.next();
+
+ xsubi.* = .{ @truncate(next_xi & 0xFFFF), @truncate((next_xi >> 16) & 0xFFFF), @truncate((next_xi >> 32) & 0xFFFF) };
+ return @as(i32, @bitCast(@as(u32, @truncate(next_xi >> 16))));
+}
+
+fn nrand48(xsubi: *[3]c_ushort) callconv(.c) c_long {
+ const xi = @as(u48, @as(u16, @truncate(xsubi[0])) | (@as(u48, @as(u16, @truncate(xsubi[1])))) << 16) | (@as(u48, @as(u16, @truncate(xsubi[2]))) << 32);
+
+ var separate_lcg: Lcg = .init(xi, lcg.a, lcg.c);
+ const next_xi = separate_lcg.next();
+
+ xsubi.* = .{ @truncate(next_xi & 0xFFFF), @truncate((next_xi >> 16) & 0xFFFF), @truncate((next_xi >> 32) & 0xFFFF) };
+ return @intCast(next_xi >> 17); // a c_long is always at least 32-bits, this is never UB
+}
+
+fn drand48() callconv(.c) f64 {
+ return 2e-48 * @as(f64, lcg.next());
+}
+
+fn lrand48() callconv(.c) c_long {
+ return @intCast(lcg.next() >> 17);
+}
+
+fn mrand48() callconv(.c) c_long {
+ return @as(i32, @bitCast(@as(u32, @truncate(lcg.next() >> 16))));
+}
+
+// 0..3 is `Xi`, 3..6 is `a`, 6 is `c`
+// first low 16-bits, then mid, then high.
+fn lcong48(param: *[7]c_ushort) callconv(.c) void {
+ lcg.xi = (@as(u48, @as(u16, @truncate(param[0]))) | (@as(u48, @as(u16, @truncate(param[1])))) << 16) | (@as(u48, @as(u16, @truncate(param[2]))) << 32);
+ lcg.a = (@as(u48, @as(u16, @truncate(param[3]))) | (@as(u48, @as(u16, @truncate(param[4])))) << 16) | (@as(u48, @as(u16, @truncate(param[5]))) << 32);
+ lcg.c = @as(u16, @truncate(param[6]));
+}
+
+fn seed48(seed16v: *[3]c_ushort) callconv(.c) *[3]c_ushort {
+ seed48_xi = .{ @truncate(lcg.xi & 0xFFFF), @truncate((lcg.xi >> 16) & 0xFFFF), @truncate((lcg.xi >> 32) & 0xFFFF) };
+ const xi = (@as(u48, @as(u16, @truncate(seed16v[0]))) | (@as(u48, @as(u16, @truncate(seed16v[1])))) << 16) | (@as(u48, @as(u16, @truncate(seed16v[2]))) << 32);
+ lcg = .init(xi, default_multiplier, default_addend);
+ return &seed48_xi;
+}
+
+fn srand48(seedval: c_long) callconv(.c) void {
+ const xi = (@as(u32, @truncate(@as(c_ulong, @bitCast(seedval)))) << 16) | 0x330E;
+ lcg = .init(xi, default_multiplier, default_addend);
+}
+
+test erand48 {
+ var xsubi: [3]c_ushort = .{ 37174, 64810, 11603 };
+
+ try std.testing.expectApproxEqAbs(0.8965, erand48(&xsubi), 0.0005);
+ try std.testing.expectEqualSlices(c_ushort, &.{ 22537, 47966, 58735 }, &xsubi);
+
+ try std.testing.expectApproxEqAbs(0.3375, erand48(&xsubi), 0.0005);
+ try std.testing.expectEqualSlices(c_ushort, &.{ 37344, 32911, 22119 }, &xsubi);
+
+ try std.testing.expectApproxEqAbs(0.6475, erand48(&xsubi), 0.0005);
+ try std.testing.expectEqualSlices(c_ushort, &.{ 23659, 29872, 42445 }, &xsubi);
+
+ try std.testing.expectApproxEqAbs(0.5005, erand48(&xsubi), 0.0005);
+ try std.testing.expectEqualSlices(c_ushort, &.{ 31642, 7875, 32802 }, &xsubi);
+
+ try std.testing.expectApproxEqAbs(0.5065, erand48(&xsubi), 0.0005);
+ try std.testing.expectEqualSlices(c_ushort, &.{ 64669, 14399, 33170 }, &xsubi);
+}
+
+test jrand48 {
+ var xsubi: [3]c_ushort = .{ 25175, 11052, 45015 };
+
+ try std.testing.expectEqual(1699503220, jrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 2326, 23668, 25932 }, &xsubi);
+
+ try std.testing.expectEqual(-992276007, jrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 41577, 4569, 50395 }, &xsubi);
+
+ try std.testing.expectEqual(-19535776, jrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 31936, 59488, 65237 }, &xsubi);
+
+ try std.testing.expectEqual(79438377, jrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 40395, 8745, 1212 }, &xsubi);
+
+ try std.testing.expectEqual(-1258917728, jrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 37242, 28832, 46326 }, &xsubi);
+}
+
+test nrand48 {
+ var xsubi: [3]c_ushort = .{ 546, 33817, 23389 };
+
+ try std.testing.expectEqual(914920692, nrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 29829, 10728, 27921 }, &xsubi);
+
+ try std.testing.expectEqual(754104482, nrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 6828, 28997, 23013 }, &xsubi);
+
+ try std.testing.expectEqual(609453945, nrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 58183, 3826, 18599 }, &xsubi);
+
+ try std.testing.expectEqual(1878644360, nrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 36678, 44304, 57331 }, &xsubi);
+
+ try std.testing.expectEqual(2114923686, nrand48(&xsubi));
+ try std.testing.expectEqualSlices(c_ushort, &.{ 58585, 22861, 64542 }, &xsubi);
+}
diff --git a/lib/c/stdlib/rand.zig b/lib/c/stdlib/rand.zig
@@ -0,0 +1,30 @@
+const std = @import("std");
+const common = @import("../common.zig");
+const builtin = @import("builtin");
+
+comptime {
+ if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
+ @export(&rand, .{ .name = "rand", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&srand, .{ .name = "srand", .linkage = common.linkage, .visibility = common.visibility });
+ @export(&rand_r, .{ .name = "rand_r", .linkage = common.linkage, .visibility = common.visibility });
+ }
+}
+
+// NOTE: The PRNG used for `rand` is unspecified, so it can be any!
+var rand_state: std.Random.SplitMix64 = .init(1);
+
+fn rand_r(seed: *c_uint) callconv(.c) c_int {
+ var mix: std.Random.SplitMix64 = .init(seed.*);
+ defer seed.* = @truncate(mix.s);
+
+ // Every bundled libc defines RAND_MAX as `std.math.maxInt(u31)` (except windows where it is `std.math.maxInt(u15)`)
+ return @as(u31, @truncate(mix.next() >> 33));
+}
+
+fn srand(seed: c_uint) callconv(.c) void {
+ rand_state = .init(seed);
+}
+
+fn rand() callconv(.c) c_int {
+ return @as(u31, @truncate(rand_state.next() >> 33));
+}
diff --git a/lib/libc/musl/src/prng/__rand48_step.c b/lib/libc/musl/src/prng/__rand48_step.c
@@ -1,14 +0,0 @@
-#include <stdint.h>
-#include "rand48.h"
-
-uint64_t __rand48_step(unsigned short *xi, unsigned short *lc)
-{
- uint64_t a, x;
- x = xi[0] | xi[1]+0U<<16 | xi[2]+0ULL<<32;
- a = lc[0] | lc[1]+0U<<16 | lc[2]+0ULL<<32;
- x = a*x + lc[3];
- xi[0] = x;
- xi[1] = x>>16;
- xi[2] = x>>32;
- return x & 0xffffffffffffull;
-}
diff --git a/lib/libc/musl/src/prng/__seed48.c b/lib/libc/musl/src/prng/__seed48.c
@@ -1,3 +0,0 @@
-#include "rand48.h"
-
-unsigned short __seed48[7] = { 0, 0, 0, 0xe66d, 0xdeec, 0x5, 0xb };
diff --git a/lib/libc/musl/src/prng/drand48.c b/lib/libc/musl/src/prng/drand48.c
@@ -1,17 +0,0 @@
-#include <stdlib.h>
-#include <inttypes.h>
-#include "rand48.h"
-
-double erand48(unsigned short s[3])
-{
- union {
- uint64_t u;
- double f;
- } x = { 0x3ff0000000000000ULL | __rand48_step(s, __seed48+3)<<4 };
- return x.f - 1.0;
-}
-
-double drand48(void)
-{
- return erand48(__seed48);
-}
diff --git a/lib/libc/musl/src/prng/lcong48.c b/lib/libc/musl/src/prng/lcong48.c
@@ -1,8 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "rand48.h"
-
-void lcong48(unsigned short p[7])
-{
- memcpy(__seed48, p, sizeof __seed48);
-}
diff --git a/lib/libc/musl/src/prng/lrand48.c b/lib/libc/musl/src/prng/lrand48.c
@@ -1,13 +0,0 @@
-#include <stdlib.h>
-#include <inttypes.h>
-#include "rand48.h"
-
-long nrand48(unsigned short s[3])
-{
- return __rand48_step(s, __seed48+3) >> 17;
-}
-
-long lrand48(void)
-{
- return nrand48(__seed48);
-}
diff --git a/lib/libc/musl/src/prng/mrand48.c b/lib/libc/musl/src/prng/mrand48.c
@@ -1,13 +0,0 @@
-#include <stdlib.h>
-#include <inttypes.h>
-#include "rand48.h"
-
-long jrand48(unsigned short s[3])
-{
- return (int32_t)(__rand48_step(s, __seed48+3) >> 16);
-}
-
-long mrand48(void)
-{
- return jrand48(__seed48);
-}
diff --git a/lib/libc/musl/src/prng/rand.c b/lib/libc/musl/src/prng/rand.c
@@ -1,15 +0,0 @@
-#include <stdlib.h>
-#include <stdint.h>
-
-static uint64_t seed;
-
-void srand(unsigned s)
-{
- seed = s-1;
-}
-
-int rand(void)
-{
- seed = 6364136223846793005ULL*seed + 1;
- return seed>>33;
-}
diff --git a/lib/libc/musl/src/prng/rand48.h b/lib/libc/musl/src/prng/rand48.h
@@ -1,5 +0,0 @@
-#include <stdint.h>
-#include <features.h>
-
-hidden uint64_t __rand48_step(unsigned short *xi, unsigned short *lc);
-extern hidden unsigned short __seed48[7];
diff --git a/lib/libc/musl/src/prng/rand_r.c b/lib/libc/musl/src/prng/rand_r.c
@@ -1,15 +0,0 @@
-#include <stdlib.h>
-
-static unsigned temper(unsigned x)
-{
- x ^= x>>11;
- x ^= x<<7 & 0x9D2C5680;
- x ^= x<<15 & 0xEFC60000;
- x ^= x>>18;
- return x;
-}
-
-int rand_r(unsigned *seed)
-{
- return temper(*seed = *seed * 1103515245 + 12345)/2;
-}
diff --git a/lib/libc/musl/src/prng/seed48.c b/lib/libc/musl/src/prng/seed48.c
@@ -1,11 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "rand48.h"
-
-unsigned short *seed48(unsigned short *s)
-{
- static unsigned short p[3];
- memcpy(p, __seed48, sizeof p);
- memcpy(__seed48, s, sizeof p);
- return p;
-}
diff --git a/lib/libc/musl/src/prng/srand48.c b/lib/libc/musl/src/prng/srand48.c
@@ -1,6 +0,0 @@
-#include <stdlib.h>
-
-void srand48(long seed)
-{
- seed48((unsigned short [3]){ 0x330e, seed, seed>>16 });
-}
diff --git a/lib/std/Random.zig b/lib/std/Random.zig
@@ -26,6 +26,7 @@ pub const Sfc64 = @import("Random/Sfc64.zig");
pub const RomuTrio = @import("Random/RomuTrio.zig");
pub const SplitMix64 = @import("Random/SplitMix64.zig");
pub const ziggurat = @import("Random/ziggurat.zig");
+pub const lcg = @import("Random/lcg.zig");
/// Any comparison of this field may result in illegal behavior, since it may be set to
/// `undefined` in cases where the random implementation does not have any associated
diff --git a/lib/std/Random/lcg.zig b/lib/std/Random/lcg.zig
@@ -0,0 +1,28 @@
+//! Linear congruential generator
+//!
+//! X(n+1) = (a * Xn + c) mod m
+//!
+//! PRNG
+
+const std = @import("std");
+
+/// Linear congruent generator where the modulo is `std.math.maxInt(T)`,
+/// wrapping over the integer.
+pub fn Wrapping(comptime T: type) type {
+ return struct {
+ xi: T,
+ a: T,
+ c: T,
+
+ pub fn init(xi: T, a: T, c: T) LcgSelf {
+ return .{ .xi = xi, .a = a, .c = c };
+ }
+
+ pub fn next(lcg: *LcgSelf) T {
+ lcg.xi = (lcg.a *% lcg.xi) +% lcg.c;
+ return lcg.xi;
+ }
+
+ const LcgSelf = @This();
+ };
+}
diff --git a/src/libs/musl.zig b/src/libs/musl.zig
@@ -1305,17 +1305,7 @@ const src_files = [_][]const u8{
"musl/src/passwd/putgrent.c",
"musl/src/passwd/putpwent.c",
"musl/src/passwd/putspent.c",
- "musl/src/prng/drand48.c",
- "musl/src/prng/lcong48.c",
- "musl/src/prng/lrand48.c",
- "musl/src/prng/mrand48.c",
- "musl/src/prng/__rand48_step.c",
- "musl/src/prng/rand.c",
"musl/src/prng/random.c",
- "musl/src/prng/rand_r.c",
- "musl/src/prng/__seed48.c",
- "musl/src/prng/seed48.c",
- "musl/src/prng/srand48.c",
"musl/src/process/aarch64/vfork.s",
"musl/src/process/arm/vfork.s",
"musl/src/process/execl.c",
diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig
@@ -885,16 +885,6 @@ const libc_top_half_src_files = [_][]const u8{
"musl/src/network/inet_pton.c",
"musl/src/network/ntohl.c",
"musl/src/network/ntohs.c",
- "musl/src/prng/drand48.c",
- "musl/src/prng/lcong48.c",
- "musl/src/prng/lrand48.c",
- "musl/src/prng/mrand48.c",
- "musl/src/prng/__rand48_step.c",
- "musl/src/prng/rand.c",
- "musl/src/prng/rand_r.c",
- "musl/src/prng/__seed48.c",
- "musl/src/prng/seed48.c",
- "musl/src/prng/srand48.c",
"musl/src/regex/fnmatch.c",
"musl/src/regex/regerror.c",
"musl/src/search/hsearch.c",