authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-27 22:45:56+01:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-04-02 23:54:20+02:00
log61161132b63d641657df3f099511e2fa5062d0d1
tree61053d284233594caffbc28c3aa9621d1b82ea64
parentf6ac1a6e05cbd46f646cf09e036921ca282694cf
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

`compiler_rt`: Dissolve `math_utils.zig` into `long_double.zig` & `trig.zig`

The closest namespace the pi/4 constant could belong to is `trig.zig` since it's used across trig function implementations. On the other hand, chucking `long double` bit slicing functions into `trig.zig` seems a little more awkward, so they're put into their own namespace.

8 files changed, 60 insertions(+), 57 deletions(-)

lib/compiler_rt/cos.zig+3-3
......@@ -17,7 +17,7 @@ const trig = @import("trig.zig");
1717const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
1818const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
1919const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
20const utils = @import("math_utils.zig");
20const ld = @import("long_double.zig");
2121
2222comptime {
2323 symbol(&__cosh, "__cosh");
......@@ -124,12 +124,12 @@ pub fn cos(x: f64) callconv(.c) f64 {
124124}
125125
126126fn coslGeneric(comptime T: type, x: T) T {
127 const se = utils.ldSignExponent(x) & 0x7fff;
127 const se = ld.signExponent(x) & 0x7fff;
128128 if (se == 0x7fff) {
129129 return x - x;
130130 }
131131
132 if (@abs(x) < utils.pi_4) {
132 if (@abs(x) < trig.pi_4) {
133133 if (se < 0x3fff - math.floatMantissaBits(T)) {
134134 // raise inexact if x!=0
135135 return 1.0 + x;
lib/compiler_rt/long_double.zig created+37
......@@ -0,0 +1,37 @@
1//! Utilities for dealing with the `long double` type (`f80` or `f128`)
2
3const std = @import("std");
4
5pub const U80 = std.meta.Int(.unsigned, 80);
6
7/// Returns the sign + exponent bits of a `long double`
8pub fn signExponent(x: anytype) u16 {
9 const T = @TypeOf(x);
10 switch (T) {
11 f80 => {
12 const bits: U80 = @bitCast(x);
13 return @intCast(bits >> 64);
14 },
15 f128 => {
16 const bits: u128 = @bitCast(x);
17 return @intCast(bits >> 112);
18 },
19 else => @compileError("`signExponent` supports only `f80` and `f128`, got: " ++ @typeName(T)),
20 }
21}
22
23/// Takes the top 16 bits of a `long double`'s mantissa
24pub fn mantissaTop(x: anytype) u16 {
25 const T = @TypeOf(x);
26 switch (T) {
27 f80 => {
28 const bits: U80 = @bitCast(x);
29 return @intCast((bits >> 48) & 0xFFFF);
30 },
31 f128 => {
32 const bits: u128 = @bitCast(x);
33 return @intCast((bits >> 96) & 0xFFFF);
34 },
35 else => @compileError("`mantissaTop` supports only `f80` and `f128`, got: " ++ @typeName(T)),
36 }
37}
lib/compiler_rt/math_utils.zig deleted-37
......@@ -1,37 +0,0 @@
1const std = @import("std");
2
3pub const U80 = std.meta.Int(.unsigned, 80);
4/// pi divided by 4
5pub const pi_4 = 0.78539816339744830962;
6
7/// Returns the sign + exponent bits of a `long double`
8pub fn ldSignExponent(x: anytype) u16 {
9 const T = @TypeOf(x);
10 switch (T) {
11 f80 => {
12 const bits: U80 = @bitCast(x);
13 return @intCast(bits >> 64);
14 },
15 f128 => {
16 const bits: u128 = @bitCast(x);
17 return @intCast(bits >> 112);
18 },
19 else => @compileError("`ldSignExponent` supports only `f80` and `f128`, got: " ++ @typeName(T)),
20 }
21}
22
23/// Takes the top 16 bits of a `long double`'s mantissa
24pub fn ldMantissaTop(x: anytype) u16 {
25 const T = @TypeOf(x);
26 switch (T) {
27 f80 => {
28 const bits: U80 = @bitCast(x);
29 return @intCast((bits >> 48) & 0xFFFF);
30 },
31 f128 => {
32 const bits: u128 = @bitCast(x);
33 return @intCast((bits >> 96) & 0xFFFF);
34 },
35 else => @compileError("`ldMantissaTop` supports only `f80` and `f128`, got: " ++ @typeName(T)),
36 }
37}
lib/compiler_rt/rem_pio2l.zig+8-8
......@@ -6,7 +6,7 @@
66const std = @import("std");
77const math = std.math;
88
9const utils = @import("math_utils.zig");
9const ld = @import("long_double.zig");
1010const rem_pio2_large = @import("rem_pio2_large.zig").rem_pio2_large;
1111
1212pub fn rem_pio2l(comptime T: type, x: T, y: *[2]T) i32 {
......@@ -34,8 +34,8 @@ pub fn rem_pio2l(comptime T: type, x: T, y: *[2]T) i32 {
3434 const pio2_3: f64 = 6.36831716351370313614e-25; // 0x18a2e037074000.0p-133
3535
3636 fn small(x_val: T) bool {
37 const se = utils.ldSignExponent(x_val);
38 const top = utils.ldMantissaTop(x_val);
37 const se = ld.signExponent(x_val);
38 const top = ld.mantissaTop(x_val);
3939 const lhs = (@as(u32, se & 0x7fff) << 16) | top;
4040 const rhs: u32 = ((0x3fff + 25) << 16) | 0x921f >> 1 | 0x8000;
4141 return lhs < rhs;
......@@ -62,8 +62,8 @@ pub fn rem_pio2l(comptime T: type, x: T, y: *[2]T) i32 {
6262 const pio2_3t: T = -2.5650587247459238361625433492959285e-65;
6363
6464 fn small(x_val: T) bool {
65 const se = utils.ldSignExponent(x_val);
66 const top = utils.ldMantissaTop(x_val);
65 const se = ld.signExponent(x_val);
66 const top = ld.mantissaTop(x_val);
6767 const lhs = (@as(u32, se & 0x7fff) << 16) | top;
6868 const rhs: u32 = ((0x3fff + 45) << 16) | 0x921f;
6969 return lhs < rhs;
......@@ -77,7 +77,7 @@ pub fn rem_pio2l(comptime T: type, x: T, y: *[2]T) i32 {
7777 else => @compileError("rem_pio2l supports only f80 and f128, got: " ++ @typeName(T)),
7878 };
7979
80 const x_se = utils.ldSignExponent(x);
80 const x_se = ld.signExponent(x);
8181 const ex: i32 = @intCast(x_se & 0x7fff);
8282
8383 if (impl.small(x)) {
......@@ -105,14 +105,14 @@ pub fn rem_pio2l(comptime T: type, x: T, y: *[2]T) i32 {
105105
106106 y[0] = r - w;
107107
108 const ey: i32 = @intCast(utils.ldSignExponent(y[0]) & 0x7fff);
108 const ey: i32 = @intCast(ld.signExponent(y[0]) & 0x7fff);
109109 if (ex - ey > impl.round1) {
110110 var t = r;
111111 w = fn_ * impl.pio2_2;
112112 r = t - w;
113113 w = fn_ * impl.pio2_2t - ((t - r) - w);
114114 y[0] = r - w;
115 const ey2: i32 = @intCast(utils.ldSignExponent(y[0]) & 0x7fff);
115 const ey2: i32 = @intCast(ld.signExponent(y[0]) & 0x7fff);
116116 if (ex - ey2 > impl.round2) {
117117 t = r;
118118 w = fn_ * impl.pio2_3;
lib/compiler_rt/sin.zig+3-3
......@@ -17,7 +17,7 @@ const trig = @import("trig.zig");
1717const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
1818const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
1919const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
20const utils = @import("math_utils.zig");
20const ld = @import("long_double.zig");
2121
2222comptime {
2323 symbol(&__sinh, "__sinh");
......@@ -134,12 +134,12 @@ pub fn sin(x: f64) callconv(.c) f64 {
134134}
135135
136136fn sinlGeneric(comptime T: type, x: T) T {
137 const se = utils.ldSignExponent(x) & 0x7fff;
137 const se = ld.signExponent(x) & 0x7fff;
138138 if (se == 0x7fff) {
139139 return x - x;
140140 }
141141
142 if (@abs(x) < utils.pi_4) {
142 if (@abs(x) < trig.pi_4) {
143143 if (se < 0x3fff - (math.floatMantissaBits(T) / 2)) {
144144 // raise inexact if x!=0 and underflow if subnormal
145145 if (compiler_rt.want_float_exceptions) {
lib/compiler_rt/sincos.zig+3-3
......@@ -9,7 +9,7 @@ const trig = @import("trig.zig");
99const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
1010const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
1111const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
12const utils = @import("math_utils.zig");
12const ld = @import("long_double.zig");
1313const compiler_rt = @import("../compiler_rt.zig");
1414const symbol = compiler_rt.symbol;
1515
......@@ -197,7 +197,7 @@ fn sincoslGeneric(comptime T: type, x: T, r_sin: *T, r_cos: *T) void {
197197 @compileError("`sincoslGeneric` implemented only for `f80` and `f128`, got: " ++ @typeName(T));
198198 }
199199
200 const se = utils.ldSignExponent(x) & 0x7fff;
200 const se = ld.signExponent(x) & 0x7fff;
201201 if (se == 0x7fff) {
202202 const result = x - x;
203203 r_sin.* = result;
......@@ -205,7 +205,7 @@ fn sincoslGeneric(comptime T: type, x: T, r_sin: *T, r_cos: *T) void {
205205 return;
206206 }
207207
208 if (@abs(x) < utils.pi_4) {
208 if (@abs(x) < trig.pi_4) {
209209 if (se < 0x3fff - math.floatMantissaBits(T)) {
210210 // raise underflow if subnormal
211211 if (compiler_rt.want_float_exceptions and se == 0) {
lib/compiler_rt/tan.zig+3-3
......@@ -17,7 +17,7 @@ const kernel = @import("trig.zig");
1717const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
1818const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
1919const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
20const utils = @import("math_utils.zig");
20const ld = @import("long_double.zig");
2121
2222const arch = builtin.cpu.arch;
2323const compiler_rt = @import("../compiler_rt.zig");
......@@ -125,12 +125,12 @@ fn tanlGeneric(comptime T: type, x: T) T {
125125 @compileError("`tanlGeneric` implemented only for `f80` and `f128`, got: " ++ T);
126126 }
127127
128 const se = utils.ldSignExponent(x) & 0x7fff;
128 const se = ld.signExponent(x) & 0x7fff;
129129 if (se == 0x7fff) {
130130 return x - x;
131131 }
132132
133 if (@abs(x) < utils.pi_4) {
133 if (@abs(x) < kernel.pi_4) {
134134 if (se < 0x3fff - math.floatMantissaBits(T) / 2) {
135135 if (compiler_rt.want_float_exceptions) {
136136 mem.doNotOptimizeAway(if (se == 0) x * 0x1p-120 else x + 0x1p120);
lib/compiler_rt/trig.zig+3
......@@ -11,6 +11,9 @@
1111// https://git.musl-libc.org/cgit/musl/tree/src/math/__cosl.c
1212// https://git.musl-libc.org/cgit/musl/tree/src/math/__tanl.c
1313
14/// pi divided by 4
15pub const pi_4 = 0.78539816339744830962;
16
1417/// kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
1518/// Input x is assumed to be bounded by ~pi/4 in magnitude.
1619/// Input y is the tail of x.