zig/std/special/compiler_rt/floatunsitf.zig
Shawn Landden 1fdb24827f
breaking changes to all bit manipulation intrinsics
* `@clz`, `@ctz`, `@popCount`, `@bswap`, `@bitreverse` now
   have a type parameter
 * rename @bitreverse to @bitReverse
 * rename @bswap to @byteSwap

Closes #2119
Closes #2120
2019-05-16 16:37:58 -04:00

30 lines
844 B
Zig

const builtin = @import("builtin");
const is_test = builtin.is_test;
const std = @import("std");
pub extern fn __floatunsitf(a: u64) f128 {
@setRuntimeSafety(is_test);
if (a == 0) {
return 0;
}
const mantissa_bits = std.math.floatMantissaBits(f128);
const exponent_bits = std.math.floatExponentBits(f128);
const exponent_bias = (1 << (exponent_bits - 1)) - 1;
const implicit_bit = 1 << mantissa_bits;
const exp = (u64.bit_count - 1) - @clz(u64, a);
const shift = mantissa_bits - @intCast(u7, exp);
// TODO(#1148): @bitCast alignment error
var result align(16) = (@intCast(u128, a) << shift) ^ implicit_bit;
result += (@intCast(u128, exp) + exponent_bias) << mantissa_bits;
return @bitCast(f128, result);
}
test "import floatunsitf" {
_ = @import("floatunsitf_test.zig");
}