authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-14 19:16:17+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-14 23:48:07+01:00
log4df6119335513af5b8d7dc0f6421b42cf13c3654
tree6c8f2b89ca3d743f6b9a6bd1250438a47135ecab
parent0f35174807ca7a058920f25312f1579c0d6e91c4

feat(libzigc): avoid division/multiplication in `drand48` and `erand48`


1 files changed, 2 insertions(+), 2 deletions(-)

lib/c/stdlib/drand48.zig+2-2
......@@ -34,7 +34,7 @@ fn erand48(xsubi: *[3]c_ushort) callconv(.c) f64 {
3434 const next_xi = separate_lcg.next();
3535
3636 xsubi.* = .{ @truncate(next_xi & 0xFFFF), @truncate((next_xi >> 16) & 0xFFFF), @truncate((next_xi >> 32) & 0xFFFF) };
37 return @as(f64, next_xi) / @as(f64, std.math.maxInt(u48));
37 return @as(f64, @bitCast(0x3ff0000000000000 | (@as(u64, next_xi) << 4))) - 1.0;
3838}
3939
4040fn jrand48(xsubi: *[3]c_ushort) callconv(.c) c_long {
......@@ -58,7 +58,7 @@ fn nrand48(xsubi: *[3]c_ushort) callconv(.c) c_long {
5858}
5959
6060fn drand48() callconv(.c) f64 {
61 return 2e-48 * @as(f64, lcg.next());
61 return @as(f64, @bitCast(0x3ff0000000000000 | (@as(u64, lcg.next()) << 4))) - 1.0;
6262}
6363
6464fn lrand48() callconv(.c) c_long {