authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-13 22:14:07+01:00
committergravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-14 12:47:58+01:00
logad3828db45b7830d5953e7c728c227e78f574cf9
tree187aefc2f705e6c0c9ec0d8c1ff2b0c94de86e7c
parent9d1a39c50ffeca1e42ed5f1b75a05d92eec869db
signaturebadge-check Signed by SSH key SHA256:p3IHbr0lyK2ekfDC1Zi7dOEV/9T6lGghNawhl5sBnM4

feat(libzigc): use common `rand` implementations

* also remove their musl implementation

6 files changed, 32 insertions(+), 34 deletions(-)

lib/c/stdlib.zig+2
......@@ -7,6 +7,8 @@ const ldiv_t = std.c.ldiv_t;
77const lldiv_t = std.c.lldiv_t;
88
99comptime {
10 _ = @import("stdlib/rand.zig");
11
1012 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
1113 // Functions specific to musl and wasi-libc.
1214 @export(&abs, .{ .name = "abs", .linkage = common.linkage, .visibility = common.visibility });
lib/c/stdlib/rand.zig created+30
......@@ -0,0 +1,30 @@
1const std = @import("std");
2const common = @import("../common.zig");
3const builtin = @import("builtin");
4
5comptime {
6 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
7 @export(&rand, .{ .name = "rand", .linkage = common.linkage, .visibility = common.visibility });
8 @export(&srand, .{ .name = "srand", .linkage = common.linkage, .visibility = common.visibility });
9 @export(&rand_r, .{ .name = "rand_r", .linkage = common.linkage, .visibility = common.visibility });
10 }
11}
12
13// NOTE: The PRNG used for `rand` is unspecified, so it can be any!
14var rand_state: std.Random.SplitMix64 = .init(1);
15
16fn rand_r(seed: *c_uint) callconv(.c) c_int {
17 var mix: std.Random.SplitMix64 = .init(seed.*);
18 defer seed.* = @truncate(mix.s);
19
20 // Every bundled libc defines RAND_MAX as `std.math.maxInt(u31)` (except windows where it is `std.math.maxInt(u15)`)
21 return @as(u31, @truncate(mix.next() >> 33));
22}
23
24fn srand(seed: c_uint) callconv(.c) void {
25 rand_state = .init(seed);
26}
27
28fn rand() callconv(.c) c_int {
29 return @as(u31, @truncate(rand_state.next() >> 33));
30}
lib/libc/musl/src/prng/rand.c deleted-15
......@@ -1,15 +0,0 @@
1#include <stdlib.h>
2#include <stdint.h>
3
4static uint64_t seed;
5
6void srand(unsigned s)
7{
8 seed = s-1;
9}
10
11int rand(void)
12{
13 seed = 6364136223846793005ULL*seed + 1;
14 return seed>>33;
15}
lib/libc/musl/src/prng/rand_r.c deleted-15
......@@ -1,15 +0,0 @@
1#include <stdlib.h>
2
3static unsigned temper(unsigned x)
4{
5 x ^= x>>11;
6 x ^= x<<7 & 0x9D2C5680;
7 x ^= x<<15 & 0xEFC60000;
8 x ^= x>>18;
9 return x;
10}
11
12int rand_r(unsigned *seed)
13{
14 return temper(*seed = *seed * 1103515245 + 12345)/2;
15}
src/libs/musl.zig-2
......@@ -1310,9 +1310,7 @@ const src_files = [_][]const u8{
13101310 "musl/src/prng/lrand48.c",
13111311 "musl/src/prng/mrand48.c",
13121312 "musl/src/prng/__rand48_step.c",
1313 "musl/src/prng/rand.c",
13141313 "musl/src/prng/random.c",
1315 "musl/src/prng/rand_r.c",
13161314 "musl/src/prng/__seed48.c",
13171315 "musl/src/prng/seed48.c",
13181316 "musl/src/prng/srand48.c",
src/libs/wasi_libc.zig-2
......@@ -890,8 +890,6 @@ const libc_top_half_src_files = [_][]const u8{
890890 "musl/src/prng/lrand48.c",
891891 "musl/src/prng/mrand48.c",
892892 "musl/src/prng/__rand48_step.c",
893 "musl/src/prng/rand.c",
894 "musl/src/prng/rand_r.c",
895893 "musl/src/prng/__seed48.c",
896894 "musl/src/prng/seed48.c",
897895 "musl/src/prng/srand48.c",