mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
add compiler-rt fns: udivmodti4, udivti3, umodti3
This commit is contained in:
parent
ea9e1639ca
commit
51bde26842
@ -325,6 +325,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunstfsi.zig" DESTI
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunstfti.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/index.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmodti4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/umodti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")
|
||||
install(FILES "${CMAKE_SOURCE_DIR}/std/special/zigrt.zig" DESTINATION "${ZIG_STD_DEST}/special")
|
||||
|
@ -13,12 +13,3 @@ Any bugs should be solved by trying to duplicate the bug upstream.
|
||||
* If the bug only exists in Zig, something went wrong porting the code,
|
||||
and you can run the C code and Zig code side by side in a debugger
|
||||
to figure out what's happening differently.
|
||||
|
||||
To test Zig's compiler-rt, run this command from the build directory:
|
||||
|
||||
```
|
||||
make install && ./zig test ../std/special/compiler_rt/index.zig --library c
|
||||
```
|
||||
|
||||
The `--library c` argument omits compiler-rt from the generated test program,
|
||||
which prevents duplicate symbol linker errors for all the compiler-rt builtins.
|
||||
|
@ -1,5 +1,7 @@
|
||||
const is_test = @import("builtin").is_test;
|
||||
|
||||
pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) -> fixuint_t {
|
||||
@setDebugSafety(this, true); // TODO
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
const rep_t = switch (fp_t) {
|
||||
f32 => u32,
|
||||
|
@ -11,10 +11,12 @@ comptime {
|
||||
_ = @import("fixunstfsi.zig");
|
||||
_ = @import("fixunstfti.zig");
|
||||
_ = @import("udivti3.zig");
|
||||
_ = @import("udivmodti4.zig");
|
||||
_ = @import("umodti3.zig");
|
||||
}
|
||||
|
||||
const builtin = @import("builtin");
|
||||
const is_test = builtin.is_test;
|
||||
|
||||
const du_int = u64;
|
||||
const di_int = i64;
|
||||
@ -26,17 +28,17 @@ const low = if (builtin.is_big_endian) 1 else 0;
|
||||
const high = 1 - low;
|
||||
|
||||
export fn __udivdi3(a: du_int, b: du_int) -> du_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
return __udivmoddi4(a, b, null);
|
||||
}
|
||||
|
||||
fn du_int_to_udwords(x: du_int) -> udwords {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
return *@ptrCast(&udwords, &x);
|
||||
}
|
||||
|
||||
export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
const n_uword_bits = su_int.bit_count;
|
||||
const n_udword_bits = du_int.bit_count;
|
||||
@ -167,7 +169,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
|
||||
// K X
|
||||
// ---
|
||||
// K K
|
||||
sr = @clz(su_int(d[high])) - @clz(su_int(n[high]));
|
||||
sr = @bitCast(c_uint, c_int(@clz(su_int(d[high]))) - c_int(@clz(su_int(n[high]))));
|
||||
// 0 <= sr <= n_uword_bits - 1 or sr large
|
||||
if (sr > n_uword_bits - 1) {
|
||||
if (maybe_rem) |rem| {
|
||||
@ -208,9 +210,9 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
|
||||
// r.all -= d.all;
|
||||
// carry = 1;
|
||||
// }
|
||||
const s: di_int = (di_int)(*@ptrCast(&du_int, &d[0]) - *@ptrCast(&du_int, &r[0]) - 1) >> (n_udword_bits - 1);
|
||||
const s: di_int = (di_int)(*@ptrCast(&du_int, &d[0]) -% *@ptrCast(&du_int, &r[0]) -% 1) >> (n_udword_bits - 1);
|
||||
carry = su_int(s & 1);
|
||||
*@ptrCast(&du_int, &r[0]) -= *@ptrCast(&du_int, &d[0]) & u64(s);
|
||||
*@ptrCast(&du_int, &r[0]) -= *@ptrCast(&du_int, &d[0]) & @bitCast(u64, s);
|
||||
|
||||
sr -= 1;
|
||||
}
|
||||
@ -222,7 +224,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
|
||||
}
|
||||
|
||||
export fn __umoddi3(a: du_int, b: du_int) -> du_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
var r: du_int = undefined;
|
||||
_ = __udivmoddi4(a, b, &r);
|
||||
@ -274,7 +276,7 @@ export nakedcc fn __aeabi_uidivmod() {
|
||||
}
|
||||
|
||||
export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
const d = __udivsi3(a, b);
|
||||
*rem = su_int(si_int(a) -% (si_int(d) * si_int(b)));
|
||||
@ -286,13 +288,13 @@ export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int {
|
||||
// https://github.com/andrewrk/zig/issues/256
|
||||
|
||||
export fn __aeabi_uidiv(n: su_int, d: su_int) -> su_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
return __udivsi3(n, d);
|
||||
}
|
||||
|
||||
export fn __udivsi3(n: su_int, d: su_int) -> su_int {
|
||||
@setDebugSafety(this, false);
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
const n_uword_bits: c_uint = su_int.bit_count;
|
||||
// special cases
|
||||
@ -300,7 +302,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {
|
||||
return 0; // ?!
|
||||
if (n == 0)
|
||||
return 0;
|
||||
var sr: c_uint = @clz(d) - @clz(n);
|
||||
var sr = @bitCast(c_uint, c_int(@clz(d)) - c_int(@clz(n)));
|
||||
// 0 <= sr <= n_uword_bits - 1 or sr large
|
||||
if (sr > n_uword_bits - 1) // d > r
|
||||
return 0;
|
||||
@ -322,9 +324,9 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {
|
||||
// r.all -= d.all;
|
||||
// carry = 1;
|
||||
// }
|
||||
const s = si_int(d - r - 1) >> si_int(n_uword_bits - 1);
|
||||
const s = si_int(d -% r -% 1) >> si_int(n_uword_bits - 1);
|
||||
carry = su_int(s & 1);
|
||||
r -= d & su_int(s);
|
||||
r -= d & @bitCast(su_int, s);
|
||||
}
|
||||
q = (q << 1) | carry;
|
||||
return q;
|
||||
|
197
std/special/compiler_rt/udivmodti4.zig
Normal file
197
std/special/compiler_rt/udivmodti4.zig
Normal file
@ -0,0 +1,197 @@
|
||||
const builtin = @import("builtin");
|
||||
const is_test = builtin.is_test;
|
||||
|
||||
const low = if (builtin.is_big_endian) 1 else 0;
|
||||
const high = 1 - low;
|
||||
|
||||
export fn __udivmodti4(a: u128, b: u128, maybe_rem: ?&u128) -> u128 {
|
||||
@setDebugSafety(this, is_test);
|
||||
|
||||
const n_udword_bits = u64.bit_count;
|
||||
const n_utword_bits = u128.bit_count;
|
||||
const n = *@ptrCast(&[2]u64, &a); // TODO issue #421
|
||||
const d = *@ptrCast(&[2]u64, &b); // TODO issue #421
|
||||
var q: [2]u64 = undefined;
|
||||
var r: [2]u64 = undefined;
|
||||
var sr: c_uint = undefined;
|
||||
// special cases, X is unknown, K != 0
|
||||
if (n[high] == 0) {
|
||||
if (d[high] == 0) {
|
||||
// 0 X
|
||||
// ---
|
||||
// 0 X
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = n[low] % d[low];
|
||||
}
|
||||
return n[low] / d[low];
|
||||
}
|
||||
// 0 X
|
||||
// ---
|
||||
// K X
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = n[low];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// n[high] != 0
|
||||
if (d[low] == 0) {
|
||||
if (d[high] == 0) {
|
||||
// K X
|
||||
// ---
|
||||
// 0 0
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = n[high] % d[low];
|
||||
}
|
||||
return n[high] / d[low];
|
||||
}
|
||||
// d[high] != 0 */
|
||||
if (n[low] == 0) {
|
||||
// K 0
|
||||
// ---
|
||||
// K 0
|
||||
if (maybe_rem) |rem| {
|
||||
r[high] = n[high] % d[high];
|
||||
r[low] = 0;
|
||||
*rem = *@ptrCast(&u128, &r[0]); // TODO issue #421
|
||||
}
|
||||
return n[high] / d[high];
|
||||
}
|
||||
// K K
|
||||
// ---
|
||||
// K 0
|
||||
if ((d[high] & (d[high] - 1)) == 0) {
|
||||
// d is a power of 2
|
||||
if (maybe_rem) |rem| {
|
||||
r[low] = n[low];
|
||||
r[high] = n[high] & (d[high] - 1);
|
||||
*rem = *@ptrCast(&u128, &r[0]); // TODO issue #421
|
||||
}
|
||||
return n[high] >> @ctz(d[high]);
|
||||
}
|
||||
// K K
|
||||
// ---
|
||||
// K 0
|
||||
sr = @bitCast(c_uint, c_int(@clz(d[high])) - c_int(@clz(n[high])));
|
||||
// 0 <= sr <= n_udword_bits - 2 or sr large
|
||||
if (sr > n_udword_bits - 2) {
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = a;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
sr += 1;
|
||||
// 1 <= sr <= n_udword_bits - 1
|
||||
// q.all = a << (n_utword_bits - sr);
|
||||
q[low] = 0;
|
||||
q[high] = n[low] << (n_udword_bits - sr);
|
||||
// r.all = a >> sr;
|
||||
r[high] = n[high] >> sr;
|
||||
r[low] = (n[high] << (n_udword_bits - sr)) | (n[low] >> sr);
|
||||
} else {
|
||||
// d[low] != 0
|
||||
if (d[high] == 0) {
|
||||
// K X
|
||||
// ---
|
||||
// 0 K
|
||||
if ((d[low] & (d[low] - 1)) == 0) {
|
||||
// if d is a power of 2
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = n[low] & (d[low] - 1);
|
||||
}
|
||||
if (d[low] == 1)
|
||||
return a;
|
||||
sr = @ctz(d[low]);
|
||||
q[high] = n[high] >> sr;
|
||||
q[low] = (n[high] << (n_udword_bits - sr)) | (n[low] >> sr);
|
||||
return *@ptrCast(&u128, &q[0]); // TODO issue #421
|
||||
}
|
||||
// K X
|
||||
// ---
|
||||
// 0 K
|
||||
sr = 1 + n_udword_bits + c_uint(@clz(d[low]))
|
||||
- c_uint(@clz(n[high]));
|
||||
// 2 <= sr <= n_utword_bits - 1
|
||||
// q.all = a << (n_utword_bits - sr);
|
||||
// r.all = a >> sr;
|
||||
if (sr == n_udword_bits) {
|
||||
q[low] = 0;
|
||||
q[high] = n[low];
|
||||
r[high] = 0;
|
||||
r[low] = n[high];
|
||||
} else if (sr < n_udword_bits) {
|
||||
// 2 <= sr <= n_udword_bits - 1
|
||||
q[low] = 0;
|
||||
q[high] = n[low] << (n_udword_bits - sr);
|
||||
r[high] = n[high] >> sr;
|
||||
r[low] = (n[high] << (n_udword_bits - sr)) | (n[low] >> sr);
|
||||
} else {
|
||||
// n_udword_bits + 1 <= sr <= n_utword_bits - 1
|
||||
q[low] = n[low] << (n_utword_bits - sr);
|
||||
q[high] = (n[high] << (n_utword_bits - sr)) |
|
||||
(n[low] >> (sr - n_udword_bits));
|
||||
r[high] = 0;
|
||||
r[low] = n[high] >> (sr - n_udword_bits);
|
||||
}
|
||||
} else {
|
||||
// K X
|
||||
// ---
|
||||
// K K
|
||||
sr = @bitCast(c_uint, c_int(@clz(d[high])) - c_int(@clz(n[high])));
|
||||
// 0 <= sr <= n_udword_bits - 1 or sr large
|
||||
if (sr > n_udword_bits - 1) {
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = a;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
sr += 1;
|
||||
// 1 <= sr <= n_udword_bits
|
||||
// q.all = a << (n_utword_bits - sr);
|
||||
// r.all = a >> sr;
|
||||
q[low] = 0;
|
||||
if (sr == n_udword_bits) {
|
||||
q[high] = n[low];
|
||||
r[high] = 0;
|
||||
r[low] = n[high];
|
||||
} else {
|
||||
r[high] = n[high] >> sr;
|
||||
r[low] = (n[high] << (n_udword_bits - sr)) | (n[low] >> sr);
|
||||
q[high] = n[low] << (n_udword_bits - sr);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Not a special case
|
||||
// q and r are initialized with:
|
||||
// q.all = a << (n_utword_bits - sr);
|
||||
// r.all = a >> sr;
|
||||
// 1 <= sr <= n_utword_bits - 1
|
||||
var carry: u32 = 0;
|
||||
var r_all: u128 = undefined;
|
||||
while (sr > 0) : (sr -= 1) {
|
||||
// r:q = ((r:q) << 1) | carry
|
||||
r[high] = (r[high] << 1) | (r[low] >> (n_udword_bits - 1));
|
||||
r[low] = (r[low] << 1) | (q[high] >> (n_udword_bits - 1));
|
||||
q[high] = (q[high] << 1) | (q[low] >> (n_udword_bits - 1));
|
||||
q[low] = (q[low] << 1) | carry;
|
||||
// carry = 0;
|
||||
// if (r.all >= b)
|
||||
// {
|
||||
// r.all -= b;
|
||||
// carry = 1;
|
||||
// }
|
||||
r_all = *@ptrCast(&u128, &r[0]); // TODO issue #421
|
||||
const s: i128 = i128(b -% r_all -% 1) >> (n_utword_bits - 1);
|
||||
carry = u32(s & 1);
|
||||
r_all -= b & @bitCast(u128, s);
|
||||
r = *@ptrCast(&[2]u64, &r_all); // TODO issue #421
|
||||
}
|
||||
const q_all = ((*@ptrCast(&u128, &q[0])) << 1) | carry; // TODO issue #421
|
||||
if (maybe_rem) |rem| {
|
||||
*rem = r_all;
|
||||
}
|
||||
return q_all;
|
||||
}
|
||||
|
||||
test "import udivmodti4" {
|
||||
_ = @import("udivmodti4_test.zig");
|
||||
}
|
65298
std/special/compiler_rt/udivmodti4_test.zig
Normal file
65298
std/special/compiler_rt/udivmodti4_test.zig
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@
|
||||
const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4;
|
||||
|
||||
export fn __udivti3(a: u128, b: u128) -> u128 {
|
||||
return __udivmodti4(a, b, null);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4;
|
||||
|
||||
export fn __umodti3(a: u128, b: u128) -> u128 {
|
||||
var r: u128 = undefined;
|
||||
_ = __udivmodti4(a, b, &r);
|
||||
return r;
|
||||
}
|
Loading…
Reference in New Issue
Block a user