compiler_rt: fix TODOs in udivmod.zig

This commit is contained in:
Eric Joldasov 2022-11-15 20:40:32 +06:00 committed by Veikka Tuominen
parent eed82ca287
commit 684264908e
3 changed files with 12 additions and 12 deletions

View file

@ -18,8 +18,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
const SignedDoubleInt = std.meta.Int(.signed, double_int_bits); const SignedDoubleInt = std.meta.Int(.signed, double_int_bits);
const Log2SingleInt = std.math.Log2Int(SingleInt); const Log2SingleInt = std.math.Log2Int(SingleInt);
const n = @ptrCast(*const [2]SingleInt, &a).*; // TODO issue #421 const n = @bitCast([2]SingleInt, a);
const d = @ptrCast(*const [2]SingleInt, &b).*; // TODO issue #421 const d = @bitCast([2]SingleInt, b);
var q: [2]SingleInt = undefined; var q: [2]SingleInt = undefined;
var r: [2]SingleInt = undefined; var r: [2]SingleInt = undefined;
var sr: c_uint = undefined; var sr: c_uint = undefined;
@ -61,7 +61,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
if (maybe_rem) |rem| { if (maybe_rem) |rem| {
r[high] = n[high] % d[high]; r[high] = n[high] % d[high];
r[low] = 0; r[low] = 0;
rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 rem.* = @bitCast(DoubleInt, r);
} }
return n[high] / d[high]; return n[high] / d[high];
} }
@ -73,7 +73,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
if (maybe_rem) |rem| { if (maybe_rem) |rem| {
r[low] = n[low]; r[low] = n[low];
r[high] = n[high] & (d[high] - 1); r[high] = n[high] & (d[high] - 1);
rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 rem.* = @bitCast(DoubleInt, r);
} }
return n[high] >> @intCast(Log2SingleInt, @ctz(d[high])); return n[high] >> @intCast(Log2SingleInt, @ctz(d[high]));
} }
@ -113,7 +113,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
sr = @ctz(d[low]); sr = @ctz(d[low]);
q[high] = n[high] >> @intCast(Log2SingleInt, sr); q[high] = n[high] >> @intCast(Log2SingleInt, sr);
q[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr)); q[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421 return @bitCast(DoubleInt, q);
} }
// K X // K X
// --- // ---
@ -187,13 +187,13 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// r.all -= b; // r.all -= b;
// carry = 1; // carry = 1;
// } // }
r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 r_all = @bitCast(DoubleInt, r);
const s: SignedDoubleInt = @bitCast(SignedDoubleInt, b -% r_all -% 1) >> (double_int_bits - 1); const s: SignedDoubleInt = @bitCast(SignedDoubleInt, b -% r_all -% 1) >> (double_int_bits - 1);
carry = @intCast(u32, s & 1); carry = @intCast(u32, s & 1);
r_all -= b & @bitCast(DoubleInt, s); r_all -= b & @bitCast(DoubleInt, s);
r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421 r = @bitCast([2]SingleInt, r_all);
} }
const q_all = ((@ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421 const q_all = (@bitCast(DoubleInt, q) << 1) | carry;
if (maybe_rem) |rem| { if (maybe_rem) |rem| {
rem.* = r_all; rem.* = r_all;
} }

View file

@ -6,8 +6,8 @@ const testing = @import("std").testing;
fn test__udivmoddi4(a: u64, b: u64, expected_q: u64, expected_r: u64) !void { fn test__udivmoddi4(a: u64, b: u64, expected_q: u64, expected_r: u64) !void {
var r: u64 = undefined; var r: u64 = undefined;
const q = __udivmoddi4(a, b, &r); const q = __udivmoddi4(a, b, &r);
try testing.expect(q == expected_q); try testing.expectEqual(expected_q, q);
try testing.expect(r == expected_r); try testing.expectEqual(expected_r, r);
} }
test "udivmoddi4" { test "udivmoddi4" {

View file

@ -6,8 +6,8 @@ const testing = @import("std").testing;
fn test__udivmodti4(a: u128, b: u128, expected_q: u128, expected_r: u128) !void { fn test__udivmodti4(a: u128, b: u128, expected_q: u128, expected_r: u128) !void {
var r: u128 = undefined; var r: u128 = undefined;
const q = __udivmodti4(a, b, &r); const q = __udivmodti4(a, b, &r);
try testing.expect(q == expected_q); try testing.expectEqual(expected_q, q);
try testing.expect(r == expected_r); try testing.expectEqual(expected_r, r);
} }
test "udivmodti4" { test "udivmodti4" {