authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-06-21 18:44:08+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-06-24 10:20:56+01:00
log2e0524954405e05d8b2bfa4a15c293f26a4a080d
treead2b8783eaf58a397b6cf87600b48eeb4d39b648
parent10c97c26d964ea70230341468e05fdea533a77a3
signaturelock-open Commit is signed but in an unrecognized format.

compiler_rt: update for new `@bitCast` semantics


4 files changed, 71 insertions(+), 52 deletions(-)

lib/compiler_rt/float_from_int.zig+15-4
......@@ -64,10 +64,21 @@ inline fn limb(limbs: []const u32, index: usize) u32 {
6464pub inline fn floatFromBigInt(comptime T: type, comptime signedness: std.builtin.Signedness, x: []const u32) T {
6565 switch (x.len) {
6666 0 => return 0,
67 inline 1...4 => |limbs_len| return @floatFromInt(@as(
68 @Int(signedness, 32 * limbs_len),
69 @bitCast(x[0..limbs_len].*),
70 )),
67 inline 1...4 => |limbs_len| {
68 const low_to_high: [limbs_len]u32 = switch (@import("builtin").cpu.arch.endian()) {
69 .little => x[0..limbs_len].*,
70 .big => switch (limbs_len) {
71 1 => .{x[0]},
72 2 => .{ x[1], x[0] },
73 3 => .{ x[2], x[1], x[0] },
74 4 => .{ x[3], x[2], x[1], x[0] },
75 else => comptime unreachable,
76 },
77 };
78 const I = @Int(signedness, 32 * limbs_len);
79 const int: I = @bitCast(low_to_high);
80 return @floatFromInt(int);
81 },
7182 else => {},
7283 }
7384
lib/compiler_rt/int_from_float.zig+12-4
......@@ -80,10 +80,18 @@ pub inline fn bigIntFromFloat(comptime signedness: std.builtin.Signedness, resul
8080 switch (result.len) {
8181 0 => return,
8282 inline 1...4 => |limbs_len| {
83 result[0..limbs_len].* = @bitCast(@as(
84 @Int(signedness, 32 * limbs_len),
85 @intFromFloat(a),
86 ));
83 const I = @Int(signedness, 32 * limbs_len);
84 const low_to_high: [limbs_len]u32 = @bitCast(@as(I, @intFromFloat(a)));
85 result[0..limbs_len].* = switch (@import("builtin").cpu.arch.endian()) {
86 .little => low_to_high,
87 .big => switch (limbs_len) {
88 1 => .{low_to_high[0]},
89 2 => .{ low_to_high[1], low_to_high[0] },
90 3 => .{ low_to_high[2], low_to_high[1], low_to_high[0] },
91 4 => .{ low_to_high[3], low_to_high[2], low_to_high[1], low_to_high[0] },
92 else => comptime unreachable,
93 },
94 };
8795 return;
8896 },
8997 else => {},
lib/compiler_rt/limb64.zig+12-6
......@@ -75,7 +75,17 @@ fn asLimbs(v: anytype) Limbs(@TypeOf(v)) {
7575 const int_info = @typeInfo(T).int;
7676 const limb_cnt = comptime limbCount(int_info.bits);
7777 const ET = @Int(int_info.signedness, limb_cnt * 64);
78 return @bitCast(@as(ET, v));
78 const low_to_high: Limbs(T) = @bitCast(@as(ET, v));
79 switch (endian) {
80 .little => return low_to_high,
81 .big => {
82 var swapped: Limbs(T) = undefined;
83 for (low_to_high, 0..) |x, i| {
84 swapped[limb_cnt - i - 1] = x;
85 }
86 return swapped;
87 },
88 }
7989}
8090
8191fn limbWrap(limb: u64, is_signed: bool, bits: u16) u64 {
......@@ -944,11 +954,7 @@ inline fn add3(x: *[3]u64, start: usize, v0: u64) void {
944954
945955fn mulwide(a: u64, b: u64) [2]u64 {
946956 const muldXi = @import("mulXi3.zig").muldXi;
947 const limbs: [2]u64 = @bitCast(muldXi(u64, a, b));
948 return switch (endian) {
949 .little => limbs,
950 .big => .{ limbs[1], limbs[0] },
951 };
957 return @bitCast(muldXi(u64, a, b));
952958}
953959
954960fn __mulo_limb64(out_ptr: [*]u64, a_ptr: [*]const u64, b_ptr: [*]const u64, is_signed: bool, bits: u16) callconv(.c) bool {
lib/compiler_rt/udivmod.zig+32-38
......@@ -61,12 +61,6 @@ pub fn __umodti3(a: u128, b: u128) callconv(.c) u128 {
6161 return r;
6262}
6363
64const lo = switch (builtin.cpu.arch.endian()) {
65 .big => 1,
66 .little => 0,
67};
68const hi = 1 - lo;
69
7064// Let _u1 and _u0 be the high and low limbs of U respectively.
7165// Returns U / v_ and sets r = U % v_.
7266fn divwide_generic(comptime T: type, _u1: T, _u0: T, v_: T, r: *T) T {
......@@ -158,22 +152,22 @@ pub fn udivmod(comptime T: type, a_: T, b_: T, maybe_rem: ?*T) T {
158152 return 0;
159153 }
160154
161 const a: [2]HalfT = @bitCast(a_);
162 const b: [2]HalfT = @bitCast(b_);
155 const a: [2]HalfT = @bitCast(a_); // [0] is low bits, [1] is high bits
156 const b: [2]HalfT = @bitCast(b_); // [0] is low bits, [1] is high bits
163157 var q: [2]HalfT = undefined;
164158 var r: [2]HalfT = undefined;
165159
166160 // When the divisor fits in 64 bits, we can use an optimized path
167 if (b[hi] == 0) {
168 r[hi] = 0;
169 if (a[hi] < b[lo]) {
161 if (b[1] == 0) {
162 r[1] = 0;
163 if (a[1] < b[0]) {
170164 // The result fits in 64 bits
171 q[hi] = 0;
172 q[lo] = divwide(HalfT, a[hi], a[lo], b[lo], &r[lo]);
165 q[1] = 0;
166 q[0] = divwide(HalfT, a[1], a[0], b[0], &r[0]);
173167 } else {
174168 // First, divide with the high part to get the remainder. After that a_hi < b_lo.
175 q[hi] = a[hi] / b[lo];
176 q[lo] = divwide(HalfT, a[hi] % b[lo], a[lo], b[lo], &r[lo]);
169 q[1] = a[1] / b[0];
170 q[0] = divwide(HalfT, a[1] % b[0], a[0], b[0], &r[0]);
177171 }
178172 if (maybe_rem) |rem| {
179173 rem.* = @bitCast(r);
......@@ -181,21 +175,21 @@ pub fn udivmod(comptime T: type, a_: T, b_: T, maybe_rem: ?*T) T {
181175 return @bitCast(q);
182176 }
183177
184 // Large-divisor case: b[hi] != 0, so the quotient fits in one HalfT word.
178 // Large-divisor case: b[1] != 0, so the quotient fits in one HalfT word.
185179 //
186180 // Trial quotient via divwide (Knuth Vol 2, Section 4.3.1):
187181 // Normalize the divisor so its high half has the MSB set, then use divwide
188182 // on the top bits to get a trial quotient that is at most 1 too large.
189183 // This replaces the O(shift) bit-by-bit loop with O(1) operations.
190 const s: Log2Int(HalfT) = @intCast(@clz(b[hi]));
184 const s: Log2Int(HalfT) = @intCast(@clz(b[1]));
191185
192186 if (s == 0) {
193 // b[hi] already has its MSB set, so b >= 2^(T_bits - 1). Since a >= b
187 // b[1] already has its MSB set, so b >= 2^(T_bits - 1). Since a >= b
194188 // (we passed the b_ > a_ check), a >= 2^(T_bits - 1) too, meaning
195 // a[hi] also has its MSB set. Therefore a / b < 2, and the quotient
189 // a[1] also has its MSB set. Therefore a / b < 2, and the quotient
196190 // is exactly 1.
197191 q = @bitCast(@as(T, 0));
198 q[lo] = 1;
192 q[0] = 1;
199193 if (maybe_rem) |rem| {
200194 rem.* = a_ - b_;
201195 }
......@@ -207,12 +201,12 @@ pub fn udivmod(comptime T: type, a_: T, b_: T, maybe_rem: ?*T) T {
207201 std.math.IntFittingRange(0, half_bits),
208202 @intCast(s),
209203 ));
210 const bn_hi: HalfT = (b[hi] << s) | (b[lo] >> sr);
204 const bn_hi: HalfT = (b[1] << s) | (b[0] >> sr);
211205
212206 // Trial numerator: the top (half_bits + s) bits of (a << s), as [a2:a1].
213207 // a2 < bn_hi is guaranteed since a2 < 2^s and bn_hi >= 2^(half_bits - 1).
214 const a2: HalfT = a[hi] >> sr;
215 const a1: HalfT = (a[hi] << s) | (a[lo] >> sr);
208 const a2: HalfT = a[1] >> sr;
209 const a1: HalfT = (a[1] << s) | (a[0] >> sr);
216210
217211 // Trial quotient via divwide: q_hat = floor([a2:a1] / bn_hi).
218212 // By Knuth's theorem (normalized divisor), q <= q_hat <= q + 1.
......@@ -223,42 +217,42 @@ pub fn udivmod(comptime T: type, a_: T, b_: T, maybe_rem: ?*T) T {
223217 // Compute the product using HalfT * HalfT -> T widening multiplications,
224218 // which are native single-instruction ops when HalfT fits in a register
225219 // (e.g. u64 * u64 -> u128 via mulq on x86_64, mul on aarch64).
226 // product = q_hat * [b[hi]:b[lo]] = [p_top : p_mid : p_lo] (3 half-words)
227 const prod_lo: T = @as(T, q_hat) * @as(T, b[lo]);
228 const prod_hi: T = @as(T, q_hat) * @as(T, b[hi]);
220 // product = q_hat * [b[1]:b[0]] = [p_top : p_mid : p_lo] (3 half-words)
221 const prod_lo: T = @as(T, q_hat) * @as(T, b[0]);
222 const prod_hi: T = @as(T, q_hat) * @as(T, b[1]);
229223
230224 const prod_lo_parts: [2]HalfT = @bitCast(prod_lo);
231225 const prod_hi_parts: [2]HalfT = @bitCast(prod_hi);
232226
233 const mid_add = @addWithOverflow(prod_hi_parts[lo], prod_lo_parts[hi]);
227 const mid_add = @addWithOverflow(prod_hi_parts[0], prod_lo_parts[1]);
234228 var p_mid: HalfT = mid_add[0];
235 const p_top: HalfT = prod_hi_parts[hi] +% @as(HalfT, mid_add[1]);
236 var p_lo: HalfT = prod_lo_parts[lo];
229 const p_top: HalfT = prod_hi_parts[1] +% @as(HalfT, mid_add[1]);
230 var p_lo: HalfT = prod_lo_parts[0];
237231
238232 // If product > a, decrement q_hat (at most once, guaranteed by Knuth).
239 if (p_top > 0 or p_mid > a[hi] or (p_mid == a[hi] and p_lo > a[lo])) {
233 if (p_top > 0 or p_mid > a[1] or (p_mid == a[1] and p_lo > a[0])) {
240234 q_hat -= 1;
241235 // Subtract b from the product for correct remainder computation.
242236 // After correction, (q_hat * b) fits in T bits, so borrows into
243237 // p_top cancel it to zero -- we only need [p_mid:p_lo].
244 const sub_lo = @subWithOverflow(p_lo, b[lo]);
238 const sub_lo = @subWithOverflow(p_lo, b[0]);
245239 p_lo = sub_lo[0];
246 const sub_mid = @subWithOverflow(p_mid, b[hi]);
240 const sub_mid = @subWithOverflow(p_mid, b[1]);
247241 const sub_mid2 = @subWithOverflow(sub_mid[0], @as(HalfT, sub_lo[1]));
248242 p_mid = sub_mid2[0];
249243 }
250244
251245 q = @bitCast(@as(T, 0));
252 q[lo] = q_hat;
246 q[0] = q_hat;
253247
254248 if (maybe_rem) |rem| {
255 // remainder = a - q_hat * b = [a[hi]:a[lo]] - [p_mid:p_lo]
249 // remainder = a - q_hat * b = [a[1]:a[0]] - [p_mid:p_lo]
256250 // This subtraction is non-negative since q_hat <= true quotient.
257 const rem_lo = @subWithOverflow(a[lo], p_lo);
258 r[lo] = rem_lo[0];
259 const rem_hi = @subWithOverflow(a[hi], p_mid);
251 const rem_lo = @subWithOverflow(a[0], p_lo);
252 r[0] = rem_lo[0];
253 const rem_hi = @subWithOverflow(a[1], p_mid);
260254 const rem_hi2 = @subWithOverflow(rem_hi[0], @as(HalfT, rem_lo[1]));
261 r[hi] = rem_hi2[0];
255 r[1] = rem_hi2[0];
262256 rem.* = @bitCast(r);
263257 }
264258 return @bitCast(q);