aboutsummaryrefslogtreecommitdiff
path: root/lib/std/special/compiler_rt/floatuntisf.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-09-26 01:54:45 -0400
committerGitHub <noreply@github.com>2019-09-26 01:54:45 -0400
commit68bb3945708c43109c48bda3664176307d45b62c (patch)
treeafb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/special/compiler_rt/floatuntisf.zig
parent6128bc728d1e1024a178c16c2149f5b1a167a013 (diff)
parent4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff)
downloadzig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz
zig-68bb3945708c43109c48bda3664176307d45b62c.zip
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/special/compiler_rt/floatuntisf.zig')
-rw-r--r--lib/std/special/compiler_rt/floatuntisf.zig61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/std/special/compiler_rt/floatuntisf.zig b/lib/std/special/compiler_rt/floatuntisf.zig
new file mode 100644
index 0000000000..1b42b267d9
--- /dev/null
+++ b/lib/std/special/compiler_rt/floatuntisf.zig
@@ -0,0 +1,61 @@
+const builtin = @import("builtin");
+const is_test = builtin.is_test;
+const std = @import("std");
+const maxInt = std.math.maxInt;
+
+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(u128, 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(maxInt(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");
+}