diff --git a/lib/compiler_rt/comparef.zig b/lib/compiler_rt/comparef.zig index 512eba0594..76f04f430a 100644 --- a/lib/compiler_rt/comparef.zig +++ b/lib/compiler_rt/comparef.zig @@ -61,8 +61,8 @@ pub inline fn cmpf2(comptime T: type, comptime RT: type, a: T, b: T) RT { } pub inline fn cmp_f80(comptime RT: type, a: f80, b: f80) RT { - const a_rep = std.math.break_f80(a); - const b_rep = std.math.break_f80(b); + const a_rep = std.math.F80.fromFloat(a); + const b_rep = std.math.F80.fromFloat(b); const sig_bits = std.math.floatMantissaBits(f80); const int_bit = 0x8000000000000000; const sign_bit = 0x8000; diff --git a/lib/compiler_rt/extendf.zig b/lib/compiler_rt/extendf.zig index 5c7c2fecde..6e546b182c 100644 --- a/lib/compiler_rt/extendf.zig +++ b/lib/compiler_rt/extendf.zig @@ -131,7 +131,7 @@ pub inline fn extend_f80(comptime src_t: type, a: std.meta.Int(.unsigned, @typeI } dst.exp |= sign; - return std.math.make_f80(dst); + return dst.toFloat(); } test { diff --git a/lib/compiler_rt/extendxftf2.zig b/lib/compiler_rt/extendxftf2.zig index 53de08e686..55324be2b4 100644 --- a/lib/compiler_rt/extendxftf2.zig +++ b/lib/compiler_rt/extendxftf2.zig @@ -18,7 +18,7 @@ fn __extendxftf2(a: f80) callconv(.C) f128 { const dst_min_normal = @as(u128, 1) << dst_sig_bits; // Break a into a sign and representation of the absolute value - var a_rep = std.math.break_f80(a); + var a_rep = std.math.F80.fromFloat(a); const sign = a_rep.exp & 0x8000; a_rep.exp &= 0x7FFF; var abs_result: u128 = undefined; diff --git a/lib/compiler_rt/subxf3.zig b/lib/compiler_rt/subxf3.zig index 815bc1f78f..9dc7625b1e 100644 --- a/lib/compiler_rt/subxf3.zig +++ b/lib/compiler_rt/subxf3.zig @@ -8,8 +8,8 @@ comptime { } fn __subxf3(a: f80, b: f80) callconv(.C) f80 { - var b_rep = std.math.break_f80(b); + var b_rep = std.math.F80.fromFloat(b); b_rep.exp ^= 0x8000; - const neg_b = std.math.make_f80(b_rep); + const neg_b = b_rep.toFloat(); return a + neg_b; } diff --git a/lib/compiler_rt/truncf.zig b/lib/compiler_rt/truncf.zig index d8b7c6b682..5c116811dc 100644 --- a/lib/compiler_rt/truncf.zig +++ b/lib/compiler_rt/truncf.zig @@ -121,7 +121,7 @@ pub inline fn trunc_f80(comptime dst_t: type, a: f80) dst_t { const dst_nan_mask = dst_qnan - 1; // Break a into a sign and representation of the absolute value - var a_rep = std.math.break_f80(a); + var a_rep = std.math.F80.fromFloat(a); const sign = a_rep.exp & 0x8000; a_rep.exp &= 0x7FFF; a_rep.fraction &= 0x7FFFFFFFFFFFFFFF; diff --git a/lib/compiler_rt/trunctfxf2.zig b/lib/compiler_rt/trunctfxf2.zig index b7c2b1cb1d..be78200a16 100644 --- a/lib/compiler_rt/trunctfxf2.zig +++ b/lib/compiler_rt/trunctfxf2.zig @@ -64,5 +64,5 @@ pub fn __trunctfxf2(a: f128) callconv(.C) f80 { } res.exp |= sign; - return math.make_f80(res); + return res.toFloat(); } diff --git a/lib/std/math.zig b/lib/std/math.zig index 67782bf93b..1e7858aaa9 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1720,21 +1720,21 @@ pub fn comptimeMod(num: anytype, comptime denom: comptime_int) IntFittingRange(0 pub const F80 = struct { fraction: u64, exp: u16, + + pub fn toFloat(self: F80) f80 { + const int = (@as(u80, self.exp) << 64) | self.fraction; + return @as(f80, @bitCast(int)); + } + + pub fn fromFloat(x: f80) F80 { + const int = @as(u80, @bitCast(x)); + return .{ + .fraction = @as(u64, @truncate(int)), + .exp = @as(u16, @truncate(int >> 64)), + }; + } }; -pub fn make_f80(repr: F80) f80 { - const int = (@as(u80, repr.exp) << 64) | repr.fraction; - return @as(f80, @bitCast(int)); -} - -pub fn break_f80(x: f80) F80 { - const int = @as(u80, @bitCast(x)); - return .{ - .fraction = @as(u64, @truncate(int)), - .exp = @as(u16, @truncate(int >> 64)), - }; -} - /// Returns -1, 0, or 1. /// Supports integer and float types and vectors of integer and float types. /// Unsigned integer types will always return 0 or 1. diff --git a/lib/std/math/nextafter.zig b/lib/std/math/nextafter.zig index 12418b5a1a..399be6cd11 100644 --- a/lib/std/math/nextafter.zig +++ b/lib/std/math/nextafter.zig @@ -61,7 +61,7 @@ fn nextAfterFloat(comptime T: type, x: T, y: T) T { const integer_bit_mask = 1 << math.floatFractionalBits(f80); const exponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1; - var x_parts = math.break_f80(x); + var x_parts = math.F80.fromFloat(x); // Bitwise increment/decrement the fractional part while also taking care to update the // exponent if we overflow the fractional part. This might flip the integer bit; this is @@ -88,7 +88,7 @@ fn nextAfterFloat(comptime T: type, x: T, y: T) T { // set to cleared (if the old value was normal) or remained cleared (if the old value was // subnormal), both of which are the outcomes we want. - return math.make_f80(x_parts); + return x_parts.toFloat(); } else { const Bits = std.meta.Int(.unsigned, @bitSizeOf(T)); var x_bits: Bits = @bitCast(x);