compiler_rt: Add floatuntisf
This commit is contained in:
parent
379950f81d
commit
c32b2e45ef
59
std/special/compiler_rt/floatuntisf.zig
Normal file
59
std/special/compiler_rt/floatuntisf.zig
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
const builtin = @import("builtin");
|
||||||
|
const is_test = builtin.is_test;
|
||||||
|
|
||||||
|
const FLT_MANT_DIG = 24;
|
||||||
|
|
||||||
|
pub extern fn __floatuntisf(arg: u128) f32 {
|
||||||
|
@setRuntimeSafety(is_test);
|
||||||
|
|
||||||
|
if (arg == 0)
|
||||||
|
return 0.0;
|
||||||
|
|
||||||
|
var a = arg;
|
||||||
|
const N: u32 = @sizeOf(u128) * 8;
|
||||||
|
const sd = @bitCast(i32, N -% @clz(a)); // number of significant digits
|
||||||
|
var e: i32 = sd -% 1; // exponent
|
||||||
|
if (sd > FLT_MANT_DIG) {
|
||||||
|
// start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
|
||||||
|
// finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
|
||||||
|
// 12345678901234567890123456
|
||||||
|
// 1 = msb 1 bit
|
||||||
|
// P = bit FLT_MANT_DIG-1 bits to the right of 1
|
||||||
|
// Q = bit FLT_MANT_DIG bits to the right of 1
|
||||||
|
// R = "or" of all bits to the right of Q
|
||||||
|
switch (sd) {
|
||||||
|
FLT_MANT_DIG + 1 => {
|
||||||
|
a <<= 1;
|
||||||
|
},
|
||||||
|
FLT_MANT_DIG + 2 => {},
|
||||||
|
else => {
|
||||||
|
const shift_amt = @bitCast(i32, N +% (FLT_MANT_DIG + 2)) -% sd;
|
||||||
|
const shift_amt_u7 = @intCast(u7, shift_amt);
|
||||||
|
a = (a >> @intCast(u7, sd -% (FLT_MANT_DIG + 2))) |
|
||||||
|
@boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// finish
|
||||||
|
a |= @boolToInt((a & 4) != 0); // Or P into R
|
||||||
|
a +%= 1; // round - this step may add a significant bit
|
||||||
|
a >>= 2; // dump Q and R
|
||||||
|
// a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
|
||||||
|
if ((a & (u128(1) << FLT_MANT_DIG)) != 0) {
|
||||||
|
a >>= 1;
|
||||||
|
e +%= 1;
|
||||||
|
}
|
||||||
|
// a is now rounded to FLT_MANT_DIG bits
|
||||||
|
} else {
|
||||||
|
a <<= @intCast(u7, FLT_MANT_DIG -% sd);
|
||||||
|
// a is now rounded to FLT_MANT_DIG bits
|
||||||
|
}
|
||||||
|
|
||||||
|
const high = @bitCast(u32, (e +% 127) << 23); // exponent
|
||||||
|
const low = @truncate(u32, a) & 0x007fffff; // mantissa
|
||||||
|
|
||||||
|
return @bitCast(f32, high | low);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "import floatuntisf" {
|
||||||
|
_ = @import("floatuntisf_test.zig");
|
||||||
|
}
|
72
std/special/compiler_rt/floatuntisf_test.zig
Normal file
72
std/special/compiler_rt/floatuntisf_test.zig
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
const __floatuntisf = @import("floatuntisf.zig").__floatuntisf;
|
||||||
|
const assert = @import("std").debug.assert;
|
||||||
|
|
||||||
|
fn test__floatuntisf(a: u128, expected: f32) void {
|
||||||
|
const x = __floatuntisf(a);
|
||||||
|
assert(x == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "floatuntisf" {
|
||||||
|
test__floatuntisf(0, 0.0);
|
||||||
|
|
||||||
|
test__floatuntisf(1, 1.0);
|
||||||
|
test__floatuntisf(2, 2.0);
|
||||||
|
test__floatuntisf(20, 20.0);
|
||||||
|
|
||||||
|
test__floatuntisf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
|
||||||
|
test__floatuntisf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
|
||||||
|
|
||||||
|
test__floatuntisf(make_ti(0x8000008000000000, 0), 0x1.000001p+127);
|
||||||
|
test__floatuntisf(make_ti(0x8000000000000800, 0), 0x1.0p+127);
|
||||||
|
test__floatuntisf(make_ti(0x8000010000000000, 0), 0x1.000002p+127);
|
||||||
|
|
||||||
|
test__floatuntisf(make_ti(0x8000000000000000, 0), 0x1.000000p+127);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72EA000000, 0x1.FEDCBA8p+50);
|
||||||
|
test__floatuntisf(0x0007FB72EB000000, 0x1.FEDCBACp+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72EC000000, 0x1.FEDCBBp+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72E6000000, 0x1.FEDCB98p+50);
|
||||||
|
test__floatuntisf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E4000000, 0x1.FEDCB9p+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0xFFFFFFFFFFFFFFFE, 0x1p+64);
|
||||||
|
test__floatuntisf(0xFFFFFFFFFFFFFFFF, 0x1p+64);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72EA000000, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72EB000000, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72EBFFFFFF, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72EC000000, 0x1.FEDCBCp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E8000001, 0x1.FEDCBAp+50);
|
||||||
|
|
||||||
|
test__floatuntisf(0x0007FB72E6000000, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E7000000, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E7FFFFFF, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E4000001, 0x1.FEDCBAp+50);
|
||||||
|
test__floatuntisf(0x0007FB72E4000000, 0x1.FEDCB8p+50);
|
||||||
|
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCB90000000000001), 0x1.FEDCBAp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBA0000000000000), 0x1.FEDCBAp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBAFFFFFFFFFFFFF), 0x1.FEDCBAp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBB0000000000000), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBB0000000000001), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBBFFFFFFFFFFFFF), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBC0000000000000), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBC0000000000001), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBD0000000000000), 0x1.FEDCBCp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBD0000000000001), 0x1.FEDCBEp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBDFFFFFFFFFFFFF), 0x1.FEDCBEp+76);
|
||||||
|
test__floatuntisf(make_ti(0x0000000000001FED, 0xCBE0000000000000), 0x1.FEDCBEp+76);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_ti(high: u64, low: u64) u128 {
|
||||||
|
var result: u128 = high;
|
||||||
|
result <<= 64;
|
||||||
|
result |= low;
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user