aboutsummaryrefslogtreecommitdiff
path: root/lib/compiler_rt/trunctfxf2.zig
blob: ddc977943a09fbcb3343e2d7452fc54f9de8052a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const math = @import("std").math;
const common = @import("./common.zig");
const trunc_f80 = @import("./truncf.zig").trunc_f80;

pub const panic = common.panic;

comptime {
    @export(&__trunctfxf2, .{ .name = "__trunctfxf2", .linkage = common.linkage, .visibility = common.visibility });
}

pub fn __trunctfxf2(a: f128) callconv(.c) f80 {
    const src_sig_bits = math.floatMantissaBits(f128);
    const dst_sig_bits = math.floatMantissaBits(f80) - 1; // -1 for the integer bit

    // Various constants whose values follow from the type parameters.
    // Any reasonable optimizer will fold and propagate all of these.
    const src_bits = @typeInfo(f128).float.bits;
    const src_exp_bits = src_bits - src_sig_bits - 1;
    const src_inf_exp = 0x7FFF;

    const src_inf = src_inf_exp << src_sig_bits;
    const src_sign_mask = 1 << (src_sig_bits + src_exp_bits);
    const src_abs_mask = src_sign_mask - 1;
    const round_mask = (1 << (src_sig_bits - dst_sig_bits)) - 1;
    const halfway = 1 << (src_sig_bits - dst_sig_bits - 1);

    // Break a into a sign and representation of the absolute value
    const a_rep = @as(u128, @bitCast(a));
    const a_abs = a_rep & src_abs_mask;
    const sign: u16 = if (a_rep & src_sign_mask != 0) 0x8000 else 0;
    const integer_bit = 1 << 63;

    var res: math.F80 = undefined;

    if (a_abs > src_inf) {
        // a is NaN.
        // Conjure the result by beginning with infinity, setting the qNaN
        // bit and inserting the (truncated) trailing NaN field.
        res.exp = 0x7fff;
        res.fraction = 0x8000000000000000;
        res.fraction |= @as(u64, @truncate(a_abs >> (src_sig_bits - dst_sig_bits)));
    } else {
        // The exponent of a is within the range of normal numbers in the
        // destination format.  We can convert by simply right-shifting with
        // rounding, adding the explicit integer bit, and adjusting the exponent
        res.fraction = @as(u64, @truncate(a_abs >> (src_sig_bits - dst_sig_bits))) | integer_bit;
        res.exp = @truncate(a_abs >> src_sig_bits);

        const round_bits = a_abs & round_mask;
        if (round_bits > halfway) {
            // Round to nearest
            const ov = @addWithOverflow(res.fraction, 1);
            res.fraction = ov[0];
            res.exp += ov[1];
            res.fraction |= @as(u64, ov[1]) << 63; // Restore integer bit after carry
        } else if (round_bits == halfway) {
            // Ties to even
            const ov = @addWithOverflow(res.fraction, res.fraction & 1);
            res.fraction = ov[0];
            res.exp += ov[1];
            res.fraction |= @as(u64, ov[1]) << 63; // Restore integer bit after carry
        }
        if (res.exp == 0) res.fraction &= ~@as(u64, integer_bit); // Remove integer bit for de-normals
    }

    res.exp |= sign;
    return res.toFloat();
}