mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
compiler_rt: fix TODOs in udivmod.zig
This commit is contained in:
parent
eed82ca287
commit
684264908e
@ -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 Log2SingleInt = std.math.Log2Int(SingleInt);
|
||||
|
||||
const n = @ptrCast(*const [2]SingleInt, &a).*; // TODO issue #421
|
||||
const d = @ptrCast(*const [2]SingleInt, &b).*; // TODO issue #421
|
||||
const n = @bitCast([2]SingleInt, a);
|
||||
const d = @bitCast([2]SingleInt, b);
|
||||
var q: [2]SingleInt = undefined;
|
||||
var r: [2]SingleInt = 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| {
|
||||
r[high] = n[high] % d[high];
|
||||
r[low] = 0;
|
||||
rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
|
||||
rem.* = @bitCast(DoubleInt, r);
|
||||
}
|
||||
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| {
|
||||
r[low] = n[low];
|
||||
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]));
|
||||
}
|
||||
@ -113,7 +113,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
|
||||
sr = @ctz(d[low]);
|
||||
q[high] = n[high] >> @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
|
||||
// ---
|
||||
@ -187,13 +187,13 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
|
||||
// r.all -= b;
|
||||
// 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);
|
||||
carry = @intCast(u32, s & 1);
|
||||
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| {
|
||||
rem.* = r_all;
|
||||
}
|
||||
|
||||
@ -6,8 +6,8 @@ const testing = @import("std").testing;
|
||||
fn test__udivmoddi4(a: u64, b: u64, expected_q: u64, expected_r: u64) !void {
|
||||
var r: u64 = undefined;
|
||||
const q = __udivmoddi4(a, b, &r);
|
||||
try testing.expect(q == expected_q);
|
||||
try testing.expect(r == expected_r);
|
||||
try testing.expectEqual(expected_q, q);
|
||||
try testing.expectEqual(expected_r, r);
|
||||
}
|
||||
|
||||
test "udivmoddi4" {
|
||||
|
||||
@ -6,8 +6,8 @@ const testing = @import("std").testing;
|
||||
fn test__udivmodti4(a: u128, b: u128, expected_q: u128, expected_r: u128) !void {
|
||||
var r: u128 = undefined;
|
||||
const q = __udivmodti4(a, b, &r);
|
||||
try testing.expect(q == expected_q);
|
||||
try testing.expect(r == expected_r);
|
||||
try testing.expectEqual(expected_q, q);
|
||||
try testing.expectEqual(expected_r, r);
|
||||
}
|
||||
|
||||
test "udivmodti4" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user