zig/lib/std/special/compiler_rt/floatditf.zig
Andrew Kelley d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00

39 lines
1.1 KiB
Zig

const builtin = @import("builtin");
const is_test = builtin.is_test;
const std = @import("std");
const maxInt = std.math.maxInt;
const significandBits = 112;
const exponentBias = 16383;
const implicitBit = (@as(u128, 1) << significandBits);
pub fn __floatditf(arg: i64) callconv(.C) f128 {
@setRuntimeSafety(is_test);
if (arg == 0)
return 0.0;
// All other cases begin by extracting the sign and absolute value of a
var sign: u128 = 0;
var aAbs = @bitCast(u64, arg);
if (arg < 0) {
sign = 1 << 127;
aAbs = ~@bitCast(u64, arg) + 1;
}
// Exponent of (fp_t)a is the width of abs(a).
const exponent = 63 - @clz(u64, aAbs);
var result: u128 = undefined;
// Shift a into the significand field, rounding if it is a right-shift
const shift = significandBits - exponent;
result = @as(u128, aAbs) << shift ^ implicitBit;
result += (@as(u128, exponent) + exponentBias) << significandBits;
return @bitCast(f128, result | sign);
}
test "import floatditf" {
_ = @import("floatditf_test.zig");
}