authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-04-02 21:51:02+02:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-04-02 23:54:20+02:00
logad10c7600793547393848528e7d4a608d9fde59e
tree09df3c266a372562c615bca323aca308dac29523
parent61161132b63d641657df3f099511e2fa5062d0d1
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

`compiler_rt`: Stop using dunders for trig functions names

Additionally, use `std.math.pi / 4.0` for `pi/4` in `trig.zig` instead of hardcoding the value.

5 files changed, 146 insertions(+), 145 deletions(-)

lib/compiler_rt/cos.zig+35-35
......@@ -20,11 +20,11 @@ const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
2020const ld = @import("long_double.zig");
2121
2222comptime {
23 symbol(&__cosh, "__cosh");
23 symbol(&cosh, "__cosh");
2424 symbol(&cosl, "__cosl");
2525 symbol(&cosf, "cosf");
2626 symbol(&cos, "cos");
27 symbol(&__cosx, "__cosx");
27 symbol(&cosx, "__cosx");
2828 if (compiler_rt.want_ppc_abi) {
2929 symbol(&cosq, "cosf128");
3030 }
......@@ -32,7 +32,7 @@ comptime {
3232 symbol(&cosl, "cosl");
3333}
3434
35pub fn __cosh(a: f16) callconv(.c) f16 {
35pub fn cosh(a: f16) callconv(.c) f16 {
3636 // TODO: more efficient implementation
3737 return @floatCast(cosf(a));
3838}
......@@ -54,27 +54,27 @@ pub fn cosf(x: f32) callconv(.c) f32 {
5454 if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
5555 return 1.0;
5656 }
57 return trig.__cosdf(x);
57 return trig.cosdf(x);
5858 }
5959 if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
6060 if (ix > 0x4016cbe3) { // |x| ~> 3*pi/4
61 return -trig.__cosdf(if (sign) x + c2pio2 else x - c2pio2);
61 return -trig.cosdf(if (sign) x + c2pio2 else x - c2pio2);
6262 } else {
6363 if (sign) {
64 return trig.__sindf(x + c1pio2);
64 return trig.sindf(x + c1pio2);
6565 } else {
66 return trig.__sindf(c1pio2 - x);
66 return trig.sindf(c1pio2 - x);
6767 }
6868 }
6969 }
7070 if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
7171 if (ix > 0x40afeddf) { // |x| ~> 7*pi/4
72 return trig.__cosdf(if (sign) x + c4pio2 else x - c4pio2);
72 return trig.cosdf(if (sign) x + c4pio2 else x - c4pio2);
7373 } else {
7474 if (sign) {
75 return trig.__sindf(-x - c3pio2);
75 return trig.sindf(-x - c3pio2);
7676 } else {
77 return trig.__sindf(x - c3pio2);
77 return trig.sindf(x - c3pio2);
7878 }
7979 }
8080 }
......@@ -87,10 +87,10 @@ pub fn cosf(x: f32) callconv(.c) f32 {
8787 var y: f64 = undefined;
8888 const n = rem_pio2f(x, &y);
8989 return switch (n & 3) {
90 0 => trig.__cosdf(y),
91 1 => trig.__sindf(-y),
92 2 => -trig.__cosdf(y),
93 else => trig.__sindf(y),
90 0 => trig.cosdf(y),
91 1 => trig.sindf(-y),
92 2 => -trig.cosdf(y),
93 else => trig.sindf(y),
9494 };
9595}
9696
......@@ -105,7 +105,7 @@ pub fn cos(x: f64) callconv(.c) f64 {
105105 if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
106106 return 1.0;
107107 }
108 return trig.__cos(x, 0);
108 return trig.cos(x, 0);
109109 }
110110
111111 // cos(Inf or NaN) is NaN
......@@ -116,10 +116,10 @@ pub fn cos(x: f64) callconv(.c) f64 {
116116 var y: [2]f64 = undefined;
117117 const n = rem_pio2(x, &y);
118118 return switch (n & 3) {
119 0 => trig.__cos(y[0], y[1]),
120 1 => -trig.__sin(y[0], y[1], 1),
121 2 => -trig.__cos(y[0], y[1]),
122 else => trig.__sin(y[0], y[1], 1),
119 0 => trig.cos(y[0], y[1]),
120 1 => -trig.sin(y[0], y[1], 1),
121 2 => -trig.cos(y[0], y[1]),
122 else => trig.sin(y[0], y[1], 1),
123123 };
124124}
125125
......@@ -134,20 +134,20 @@ fn coslGeneric(comptime T: type, x: T) T {
134134 // raise inexact if x!=0
135135 return 1.0 + x;
136136 }
137 return trig.__cosl(T, x, 0.0);
137 return trig.cosl(T, x, 0.0);
138138 }
139139
140140 var y: [2]T = undefined;
141141 const n = rem_pio2l(T, x, &y);
142142 return switch (n & 3) {
143 0 => trig.__cosl(T, y[0], y[1]),
144 1 => -trig.__sinl(T, y[0], y[1], 1),
145 2 => -trig.__cosl(T, y[0], y[1]),
146 else => trig.__sinl(T, y[0], y[1], 1),
143 0 => trig.cosl(T, y[0], y[1]),
144 1 => -trig.sinl(T, y[0], y[1], 1),
145 2 => -trig.cosl(T, y[0], y[1]),
146 else => trig.sinl(T, y[0], y[1], 1),
147147 };
148148}
149149
150pub fn __cosx(x: f80) callconv(.c) f80 {
150pub fn cosx(x: f80) callconv(.c) f80 {
151151 return coslGeneric(f80, x);
152152}
153153
......@@ -157,10 +157,10 @@ pub fn cosq(x: f128) callconv(.c) f128 {
157157
158158pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {
159159 switch (@typeInfo(c_longdouble).float.bits) {
160 16 => return __cosh(x),
160 16 => return cosh(x),
161161 32 => return cosf(x),
162162 64 => return cos(x),
163 80 => return __cosx(x),
163 80 => return cosx(x),
164164 128 => return cosq(x),
165165 else => @compileError("unreachable"),
166166 }
......@@ -170,7 +170,7 @@ fn testCosSpecial(comptime T: type) !void {
170170 const f = switch (T) {
171171 f32 => cosf,
172172 f64 => cos,
173 f80 => __cosx,
173 f80 => cosx,
174174 f128 => cosq,
175175 else => @compileError("unimplemented"),
176176 };
......@@ -214,13 +214,13 @@ test "cos64.special" {
214214
215215test "cos80.normal" {
216216 const epsilon = math.floatEps(f80);
217 try expectApproxEqAbs(@as(f80, 1.0), __cosx(0.0), epsilon);
218 try expectApproxEqAbs(@as(f80, 0.98006657784124163112419651674816888), __cosx(0.2), epsilon);
219 try expectApproxEqAbs(@as(f80, 0.62762309833608037003563995939286067), __cosx(0.8923), epsilon);
220 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), __cosx(1.5), epsilon);
221 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), __cosx(-1.5), epsilon);
222 try expectApproxEqAbs(@as(f80, 0.9691317730707771246), __cosx(37.45), epsilon);
223 try expectApproxEqAbs(@as(f80, 0.4008006809354834001), __cosx(89.123), epsilon);
217 try expectApproxEqAbs(@as(f80, 1.0), cosx(0.0), epsilon);
218 try expectApproxEqAbs(@as(f80, 0.98006657784124163112419651674816888), cosx(0.2), epsilon);
219 try expectApproxEqAbs(@as(f80, 0.62762309833608037003563995939286067), cosx(0.8923), epsilon);
220 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), cosx(1.5), epsilon);
221 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), cosx(-1.5), epsilon);
222 try expectApproxEqAbs(@as(f80, 0.9691317730707771246), cosx(37.45), epsilon);
223 try expectApproxEqAbs(@as(f80, 0.4008006809354834001), cosx(89.123), epsilon);
224224}
225225
226226test "cos80.special" {
lib/compiler_rt/sin.zig+35-35
......@@ -20,11 +20,11 @@ const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
2020const ld = @import("long_double.zig");
2121
2222comptime {
23 symbol(&__sinh, "__sinh");
23 symbol(&sinh, "__sinh");
2424 symbol(&sinl, "__sinl");
2525 symbol(&sinf, "sinf");
2626 symbol(&sin, "sin");
27 symbol(&__sinx, "__sinx");
27 symbol(&sinx, "__sinx");
2828 if (compiler_rt.want_ppc_abi) {
2929 symbol(&sinq, "sinf128");
3030 }
......@@ -32,7 +32,7 @@ comptime {
3232 symbol(&sinl, "sinl");
3333}
3434
35pub fn __sinh(x: f16) callconv(.c) f16 {
35pub fn sinh(x: f16) callconv(.c) f16 {
3636 // TODO: more efficient implementation
3737 return @floatCast(sinf(x));
3838}
......@@ -60,27 +60,27 @@ pub fn sinf(x: f32) callconv(.c) f32 {
6060 }
6161 return x;
6262 }
63 return trig.__sindf(x);
63 return trig.sindf(x);
6464 }
6565 if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
6666 if (ix <= 0x4016cbe3) { // |x| ~<= 3pi/4
6767 if (sign) {
68 return -trig.__cosdf(x + s1pio2);
68 return -trig.cosdf(x + s1pio2);
6969 } else {
70 return trig.__cosdf(x - s1pio2);
70 return trig.cosdf(x - s1pio2);
7171 }
7272 }
73 return trig.__sindf(if (sign) -(x + s2pio2) else -(x - s2pio2));
73 return trig.sindf(if (sign) -(x + s2pio2) else -(x - s2pio2));
7474 }
7575 if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
7676 if (ix <= 0x40afeddf) { // |x| ~<= 7*pi/4
7777 if (sign) {
78 return trig.__cosdf(x + s3pio2);
78 return trig.cosdf(x + s3pio2);
7979 } else {
80 return -trig.__cosdf(x - s3pio2);
80 return -trig.cosdf(x - s3pio2);
8181 }
8282 }
83 return trig.__sindf(if (sign) x + s4pio2 else x - s4pio2);
83 return trig.sindf(if (sign) x + s4pio2 else x - s4pio2);
8484 }
8585
8686 // sin(Inf or NaN) is NaN
......@@ -91,10 +91,10 @@ pub fn sinf(x: f32) callconv(.c) f32 {
9191 var y: f64 = undefined;
9292 const n = rem_pio2f(x, &y);
9393 return switch (n & 3) {
94 0 => trig.__sindf(y),
95 1 => trig.__cosdf(y),
96 2 => trig.__sindf(-y),
97 else => -trig.__cosdf(y),
94 0 => trig.sindf(y),
95 1 => trig.cosdf(y),
96 2 => trig.sindf(-y),
97 else => -trig.cosdf(y),
9898 };
9999}
100100
......@@ -115,7 +115,7 @@ pub fn sin(x: f64) callconv(.c) f64 {
115115 }
116116 return x;
117117 }
118 return trig.__sin(x, 0.0, 0);
118 return trig.sin(x, 0.0, 0);
119119 }
120120
121121 // sin(Inf or NaN) is NaN
......@@ -126,10 +126,10 @@ pub fn sin(x: f64) callconv(.c) f64 {
126126 var y: [2]f64 = undefined;
127127 const n = rem_pio2(x, &y);
128128 return switch (n & 3) {
129 0 => trig.__sin(y[0], y[1], 1),
130 1 => trig.__cos(y[0], y[1]),
131 2 => -trig.__sin(y[0], y[1], 1),
132 else => -trig.__cos(y[0], y[1]),
129 0 => trig.sin(y[0], y[1], 1),
130 1 => trig.cos(y[0], y[1]),
131 2 => -trig.sin(y[0], y[1], 1),
132 else => -trig.cos(y[0], y[1]),
133133 };
134134}
135135
......@@ -147,20 +147,20 @@ fn sinlGeneric(comptime T: type, x: T) T {
147147 }
148148 return x;
149149 }
150 return trig.__sinl(T, x, 0.0, 0);
150 return trig.sinl(T, x, 0.0, 0);
151151 }
152152
153153 var y: [2]T = undefined;
154154 const n = rem_pio2l(T, x, &y);
155155 return switch (n & 3) {
156 0 => trig.__sinl(T, y[0], y[1], 1),
157 1 => trig.__cosl(T, y[0], y[1]),
158 2 => -trig.__sinl(T, y[0], y[1], 1),
159 else => -trig.__cosl(T, y[0], y[1]),
156 0 => trig.sinl(T, y[0], y[1], 1),
157 1 => trig.cosl(T, y[0], y[1]),
158 2 => -trig.sinl(T, y[0], y[1], 1),
159 else => -trig.cosl(T, y[0], y[1]),
160160 };
161161}
162162
163pub fn __sinx(x: f80) callconv(.c) f80 {
163pub fn sinx(x: f80) callconv(.c) f80 {
164164 return sinlGeneric(f80, x);
165165}
166166
......@@ -170,10 +170,10 @@ pub fn sinq(x: f128) callconv(.c) f128 {
170170
171171pub fn sinl(x: c_longdouble) callconv(.c) c_longdouble {
172172 switch (@typeInfo(c_longdouble).float.bits) {
173 16 => return __sinh(x),
173 16 => return sinh(x),
174174 32 => return sinf(x),
175175 64 => return sin(x),
176 80 => return __sinx(x),
176 80 => return sinx(x),
177177 128 => return sinq(x),
178178 else => @compileError("unreachable"),
179179 }
......@@ -183,7 +183,7 @@ fn testSinSpecial(comptime T: type) !void {
183183 const f = switch (T) {
184184 f32 => sinf,
185185 f64 => sin,
186 f80 => __sinx,
186 f80 => sinx,
187187 f128 => sinq,
188188 else => @compileError("unimplemented"),
189189 };
......@@ -227,13 +227,13 @@ test "sin64.special" {
227227
228228test "sin80.normal" {
229229 const epsilon = math.floatEps(f80);
230 try expectApproxEqAbs(@as(f80, 0.0), __sinx(0.0), epsilon);
231 try expectApproxEqAbs(@as(f80, 0.19866933079506121545941262711838975), __sinx(0.2), epsilon);
232 try expectApproxEqAbs(@as(f80, 0.77851733855773487830689285621486050), __sinx(0.8923), epsilon);
233 try expectApproxEqAbs(@as(f80, 0.99749498660405443094172337114148732), __sinx(1.5), epsilon);
234 try expectApproxEqAbs(@as(f80, -0.99749498660405443094172337114148732), __sinx(-1.5), epsilon);
235 try expectApproxEqAbs(@as(f80, -0.24654331551411356504), __sinx(37.45), epsilon);
236 try expectApproxEqAbs(@as(f80, 0.91616527666226951006), __sinx(89.123), epsilon);
230 try expectApproxEqAbs(@as(f80, 0.0), sinx(0.0), epsilon);
231 try expectApproxEqAbs(@as(f80, 0.19866933079506121545941262711838975), sinx(0.2), epsilon);
232 try expectApproxEqAbs(@as(f80, 0.77851733855773487830689285621486050), sinx(0.8923), epsilon);
233 try expectApproxEqAbs(@as(f80, 0.99749498660405443094172337114148732), sinx(1.5), epsilon);
234 try expectApproxEqAbs(@as(f80, -0.99749498660405443094172337114148732), sinx(-1.5), epsilon);
235 try expectApproxEqAbs(@as(f80, -0.24654331551411356504), sinx(37.45), epsilon);
236 try expectApproxEqAbs(@as(f80, 0.91616527666226951006), sinx(89.123), epsilon);
237237}
238238
239239test "sin80.special" {
lib/compiler_rt/sincos.zig+38-38
......@@ -14,10 +14,10 @@ const compiler_rt = @import("../compiler_rt.zig");
1414const symbol = compiler_rt.symbol;
1515
1616comptime {
17 symbol(&__sincosh, "__sincosh");
17 symbol(&sincosh, "__sincosh");
1818 symbol(&sincosf, "sincosf");
1919 symbol(&sincos, "sincos");
20 symbol(&__sincosx, "__sincosx");
20 symbol(&sincosx, "__sincosx");
2121 if (compiler_rt.want_ppc_abi) {
2222 symbol(&sincosq, "sincosf128");
2323 }
......@@ -25,7 +25,7 @@ comptime {
2525 symbol(&sincosl, "sincosl");
2626}
2727
28pub fn __sincosh(x: f16, r_sin: *f16, r_cos: *f16) callconv(.c) void {
28pub fn sincosh(x: f16, r_sin: *f16, r_cos: *f16) callconv(.c) void {
2929 // TODO: more efficient implementation
3030 var big_sin: f32 = undefined;
3131 var big_cos: f32 = undefined;
......@@ -60,8 +60,8 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.c) void {
6060 r_cos.* = 1.0;
6161 return;
6262 }
63 r_sin.* = trig.__sindf(x);
64 r_cos.* = trig.__cosdf(x);
63 r_sin.* = trig.sindf(x);
64 r_cos.* = trig.cosdf(x);
6565 return;
6666 }
6767
......@@ -70,17 +70,17 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.c) void {
7070 // |x| ~<= 3pi/4
7171 if (ix <= 0x4016cbe3) {
7272 if (sign) {
73 r_sin.* = -trig.__cosdf(x + sc1pio2);
74 r_cos.* = trig.__sindf(x + sc1pio2);
73 r_sin.* = -trig.cosdf(x + sc1pio2);
74 r_cos.* = trig.sindf(x + sc1pio2);
7575 } else {
76 r_sin.* = trig.__cosdf(sc1pio2 - x);
77 r_cos.* = trig.__sindf(sc1pio2 - x);
76 r_sin.* = trig.cosdf(sc1pio2 - x);
77 r_cos.* = trig.sindf(sc1pio2 - x);
7878 }
7979 return;
8080 }
8181 // -sin(x+c) is not correct if x+c could be 0: -0 vs +0
82 r_sin.* = -trig.__sindf(if (sign) x + sc2pio2 else x - sc2pio2);
83 r_cos.* = -trig.__cosdf(if (sign) x + sc2pio2 else x - sc2pio2);
82 r_sin.* = -trig.sindf(if (sign) x + sc2pio2 else x - sc2pio2);
83 r_cos.* = -trig.cosdf(if (sign) x + sc2pio2 else x - sc2pio2);
8484 return;
8585 }
8686
......@@ -89,16 +89,16 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.c) void {
8989 // |x| ~<= 7*pi/4
9090 if (ix <= 0x40afeddf) {
9191 if (sign) {
92 r_sin.* = trig.__cosdf(x + sc3pio2);
93 r_cos.* = -trig.__sindf(x + sc3pio2);
92 r_sin.* = trig.cosdf(x + sc3pio2);
93 r_cos.* = -trig.sindf(x + sc3pio2);
9494 } else {
95 r_sin.* = -trig.__cosdf(x - sc3pio2);
96 r_cos.* = trig.__sindf(x - sc3pio2);
95 r_sin.* = -trig.cosdf(x - sc3pio2);
96 r_cos.* = trig.sindf(x - sc3pio2);
9797 }
9898 return;
9999 }
100 r_sin.* = trig.__sindf(if (sign) x + sc4pio2 else x - sc4pio2);
101 r_cos.* = trig.__cosdf(if (sign) x + sc4pio2 else x - sc4pio2);
100 r_sin.* = trig.sindf(if (sign) x + sc4pio2 else x - sc4pio2);
101 r_cos.* = trig.cosdf(if (sign) x + sc4pio2 else x - sc4pio2);
102102 return;
103103 }
104104
......@@ -113,8 +113,8 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.c) void {
113113 // general argument reduction needed
114114 var y: f64 = undefined;
115115 const n = rem_pio2f(x, &y);
116 const s = trig.__sindf(y);
117 const c = trig.__cosdf(y);
116 const s = trig.sindf(y);
117 const c = trig.cosdf(y);
118118 switch (n & 3) {
119119 0 => {
120120 r_sin.* = s;
......@@ -154,8 +154,8 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.c) void {
154154 r_cos.* = 1.0;
155155 return;
156156 }
157 r_sin.* = trig.__sin(x, 0.0, 0);
158 r_cos.* = trig.__cos(x, 0.0);
157 r_sin.* = trig.sin(x, 0.0, 0);
158 r_cos.* = trig.cos(x, 0.0);
159159 return;
160160 }
161161
......@@ -170,8 +170,8 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.c) void {
170170 // argument reduction needed
171171 var y: [2]f64 = undefined;
172172 const n = rem_pio2(x, &y);
173 const s = trig.__sin(y[0], y[1], 1);
174 const c = trig.__cos(y[0], y[1]);
173 const s = trig.sin(y[0], y[1], 1);
174 const c = trig.cos(y[0], y[1]);
175175 switch (n & 3) {
176176 0 => {
177177 r_sin.* = s;
......@@ -216,15 +216,15 @@ fn sincoslGeneric(comptime T: type, x: T, r_sin: *T, r_cos: *T) void {
216216 r_cos.* = 1.0 + x;
217217 return;
218218 }
219 r_sin.* = trig.__sinl(T, x, 0.0, 0);
220 r_cos.* = trig.__cosl(T, x, 0.0);
219 r_sin.* = trig.sinl(T, x, 0.0, 0);
220 r_cos.* = trig.cosl(T, x, 0.0);
221221 return;
222222 }
223223
224224 var y: [2]T = undefined;
225225 const n = rem_pio2l(T, x, &y);
226 const s = trig.__sinl(T, y[0], y[1], 1);
227 const c = trig.__cosl(T, y[0], y[1]);
226 const s = trig.sinl(T, y[0], y[1], 1);
227 const c = trig.cosl(T, y[0], y[1]);
228228 switch (n & 3) {
229229 0 => {
230230 r_sin.* = s;
......@@ -245,7 +245,7 @@ fn sincoslGeneric(comptime T: type, x: T, r_sin: *T, r_cos: *T) void {
245245 }
246246}
247247
248pub fn __sincosx(x: f80, r_sin: *f80, r_cos: *f80) callconv(.c) void {
248pub fn sincosx(x: f80, r_sin: *f80, r_cos: *f80) callconv(.c) void {
249249 return sincoslGeneric(f80, x, r_sin, r_cos);
250250}
251251
......@@ -255,10 +255,10 @@ pub fn sincosq(x: f128, r_sin: *f128, r_cos: *f128) callconv(.c) void {
255255
256256pub fn sincosl(x: c_longdouble, r_sin: *c_longdouble, r_cos: *c_longdouble) callconv(.c) void {
257257 switch (@typeInfo(c_longdouble).float.bits) {
258 16 => return __sincosh(x, r_sin, r_cos),
258 16 => return sincosh(x, r_sin, r_cos),
259259 32 => return sincosf(x, r_sin, r_cos),
260260 64 => return sincos(x, r_sin, r_cos),
261 80 => return __sincosx(x, r_sin, r_cos),
261 80 => return sincosx(x, r_sin, r_cos),
262262 128 => return sincosq(x, r_sin, r_cos),
263263 else => @compileError("unreachable"),
264264 }
......@@ -268,7 +268,7 @@ fn testSincosSpecial(comptime T: type) !void {
268268 const f = switch (T) {
269269 f32 => sincosf,
270270 f64 => sincos,
271 f80 => __sincosx,
271 f80 => sincosx,
272272 f128 => sincosq,
273273 else => @compileError("unimplemented"),
274274 };
......@@ -378,31 +378,31 @@ test "sincos80.normal" {
378378 var s: f80 = undefined;
379379 var c: f80 = undefined;
380380
381 __sincosx(0.0, &s, &c);
381 sincosx(0.0, &s, &c);
382382 try expectApproxEqAbs(@as(f80, 0.0), s, epsilon);
383383 try expectApproxEqAbs(@as(f80, 1.0), c, epsilon);
384384
385 __sincosx(0.2, &s, &c);
385 sincosx(0.2, &s, &c);
386386 try expectApproxEqAbs(@as(f80, 0.19866933079506121545941262711838975), s, epsilon);
387387 try expectApproxEqAbs(@as(f80, 0.98006657784124163112419651674816888), c, epsilon);
388388
389 __sincosx(0.8923, &s, &c);
389 sincosx(0.8923, &s, &c);
390390 try expectApproxEqAbs(@as(f80, 0.77851733855773487830689285621486050), s, epsilon);
391391 try expectApproxEqAbs(@as(f80, 0.62762309833608037003563995939286067), c, epsilon);
392392
393 __sincosx(1.5, &s, &c);
393 sincosx(1.5, &s, &c);
394394 try expectApproxEqAbs(@as(f80, 0.99749498660405443094172337114148732), s, epsilon);
395395 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), c, epsilon);
396396
397 __sincosx(-1.5, &s, &c);
397 sincosx(-1.5, &s, &c);
398398 try expectApproxEqAbs(@as(f80, -0.99749498660405443094172337114148732), s, epsilon);
399399 try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), c, epsilon);
400400
401 __sincosx(37.45, &s, &c);
401 sincosx(37.45, &s, &c);
402402 try expectApproxEqAbs(@as(f80, -0.24654331551411356504), s, epsilon);
403403 try expectApproxEqAbs(@as(f80, 0.9691317730707771246), c, epsilon);
404404
405 __sincosx(89.123, &s, &c);
405 sincosx(89.123, &s, &c);
406406 try expectApproxEqAbs(@as(f80, 0.91616527666226951006), s, epsilon);
407407 try expectApproxEqAbs(@as(f80, 0.4008006809354834001), c, epsilon);
408408}
lib/compiler_rt/tan.zig+23-23
......@@ -24,10 +24,10 @@ const compiler_rt = @import("../compiler_rt.zig");
2424const symbol = @import("../compiler_rt.zig").symbol;
2525
2626comptime {
27 symbol(&__tanh, "__tanh");
27 symbol(&tanh, "__tanh");
2828 symbol(&tanf, "tanf");
2929 symbol(&tan, "tan");
30 symbol(&__tanx, "__tanx");
30 symbol(&tanx, "__tanx");
3131 if (compiler_rt.want_ppc_abi) {
3232 symbol(&tanq, "tanf128");
3333 }
......@@ -35,7 +35,7 @@ comptime {
3535 symbol(&tanl, "tanl");
3636}
3737
38pub fn __tanh(x: f16) callconv(.c) f16 {
38pub fn tanh(x: f16) callconv(.c) f16 {
3939 // TODO: more efficient implementation
4040 return @floatCast(tanf(x));
4141}
......@@ -63,20 +63,20 @@ pub fn tanf(x: f32) callconv(.c) f32 {
6363 }
6464 return x;
6565 }
66 return kernel.__tandf(x, false);
66 return kernel.tandf(x, false);
6767 }
6868 if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
6969 if (ix <= 0x4016cbe3) { // |x| ~<= 3pi/4
70 return kernel.__tandf((if (sign) x + t1pio2 else x - t1pio2), true);
70 return kernel.tandf((if (sign) x + t1pio2 else x - t1pio2), true);
7171 } else {
72 return kernel.__tandf((if (sign) x + t2pio2 else x - t2pio2), false);
72 return kernel.tandf((if (sign) x + t2pio2 else x - t2pio2), false);
7373 }
7474 }
7575 if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
7676 if (ix <= 0x40afeddf) { // |x| ~<= 7*pi/4
77 return kernel.__tandf((if (sign) x + t3pio2 else x - t3pio2), true);
77 return kernel.tandf((if (sign) x + t3pio2 else x - t3pio2), true);
7878 } else {
79 return kernel.__tandf((if (sign) x + t4pio2 else x - t4pio2), false);
79 return kernel.tandf((if (sign) x + t4pio2 else x - t4pio2), false);
8080 }
8181 }
8282
......@@ -87,7 +87,7 @@ pub fn tanf(x: f32) callconv(.c) f32 {
8787
8888 var y: f64 = undefined;
8989 const n = rem_pio2f(x, &y);
90 return kernel.__tandf(y, n & 1 != 0);
90 return kernel.tandf(y, n & 1 != 0);
9191}
9292
9393pub fn tan(x: f64) callconv(.c) f64 {
......@@ -107,7 +107,7 @@ pub fn tan(x: f64) callconv(.c) f64 {
107107 }
108108 return x;
109109 }
110 return kernel.__tan(x, 0.0, false);
110 return kernel.tan(x, 0.0, false);
111111 }
112112
113113 // tan(Inf or NaN) is NaN
......@@ -117,7 +117,7 @@ pub fn tan(x: f64) callconv(.c) f64 {
117117
118118 var y: [2]f64 = undefined;
119119 const n = rem_pio2(x, &y);
120 return kernel.__tan(y[0], y[1], n & 1 != 0);
120 return kernel.tan(y[0], y[1], n & 1 != 0);
121121}
122122
123123fn tanlGeneric(comptime T: type, x: T) T {
......@@ -137,15 +137,15 @@ fn tanlGeneric(comptime T: type, x: T) T {
137137 }
138138 return x;
139139 }
140 return kernel.__tanl(T, x, 0.0, 0);
140 return kernel.tanl(T, x, 0.0, 0);
141141 }
142142
143143 var y: [2]T = undefined;
144144 const n = rem_pio2l(T, x, &y);
145 return kernel.__tanl(T, y[0], y[1], n & 1);
145 return kernel.tanl(T, y[0], y[1], n & 1);
146146}
147147
148pub fn __tanx(x: f80) callconv(.c) f80 {
148pub fn tanx(x: f80) callconv(.c) f80 {
149149 return tanlGeneric(f80, x);
150150}
151151
......@@ -155,10 +155,10 @@ pub fn tanq(x: f128) callconv(.c) f128 {
155155
156156pub fn tanl(x: c_longdouble) callconv(.c) c_longdouble {
157157 switch (@typeInfo(c_longdouble).float.bits) {
158 16 => return __tanh(x),
158 16 => return tanh(x),
159159 32 => return tanf(x),
160160 64 => return tan(x),
161 80 => return __tanx(x),
161 80 => return tanx(x),
162162 128 => return tanq(x),
163163 else => @compileError("unreachable"),
164164 }
......@@ -184,7 +184,7 @@ fn testTanSpecial(comptime T: type) !void {
184184 const f = switch (T) {
185185 f32 => tanf,
186186 f64 => tan,
187 f80 => __tanx,
187 f80 => tanx,
188188 f128 => tanq,
189189 else => @compileError("unimplemented"),
190190 };
......@@ -207,12 +207,12 @@ test "tan64.normal" {
207207test "tan80.normal" {
208208 const epsilon = math.floatEps(f80);
209209
210 try expectApproxEqAbs(@as(f80, 0.0), __tanx(0.0), epsilon);
211 try expectApproxEqAbs(@as(f80, 0.2027100355086724833213582716475345), __tanx(0.2), epsilon);
212 try expectApproxEqAbs(@as(f80, 1.2404217445497097995561220131857544), __tanx(0.8923), epsilon);
213 try expectApproxEqAbs(@as(f80, 14.10141994717171938764), __tanx(1.5), epsilon);
214 try expectApproxEqAbs(@as(f80, -0.25439607116885656232), __tanx(37.45), epsilon);
215 try expectApproxEqAbs(@as(f80, 2.2858376251355320963), __tanx(89.123), epsilon);
210 try expectApproxEqAbs(@as(f80, 0.0), tanx(0.0), epsilon);
211 try expectApproxEqAbs(@as(f80, 0.2027100355086724833213582716475345), tanx(0.2), epsilon);
212 try expectApproxEqAbs(@as(f80, 1.2404217445497097995561220131857544), tanx(0.8923), epsilon);
213 try expectApproxEqAbs(@as(f80, 14.10141994717171938764), tanx(1.5), epsilon);
214 try expectApproxEqAbs(@as(f80, -0.25439607116885656232), tanx(37.45), epsilon);
215 try expectApproxEqAbs(@as(f80, 2.2858376251355320963), tanx(89.123), epsilon);
216216}
217217
218218test "tan128.normal" {
lib/compiler_rt/trig.zig+15-14
......@@ -11,8 +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;
14const std = @import("std");
15
16pub const pi_4 = std.math.pi / 4.0;
1617
1718/// kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
1819/// Input x is assumed to be bounded by ~pi/4 in magnitude.
......@@ -49,7 +50,7 @@ pub const pi_4 = 0.78539816339744830962;
4950/// expression for cos(). Retention happens in all cases tested
5051/// under FreeBSD, so don't pessimize things by forcibly clipping
5152/// any extra precision in w.
52pub fn __cos(x: f64, y: f64) f64 {
53pub fn cos(x: f64, y: f64) f64 {
5354 const C1 = 4.16666666666666019037e-02; // 0x3FA55555, 0x5555554C
5455 const C2 = -1.38888888888741095749e-03; // 0xBF56C16C, 0x16C15177
5556 const C3 = 2.48015872894767294178e-05; // 0x3EFA01A0, 0x19CB1590
......@@ -65,7 +66,7 @@ pub fn __cos(x: f64, y: f64) f64 {
6566 return w + (((1.0 - w) - hz) + (z * r - x * y));
6667}
6768
68pub fn __cosdf(x: f64) f32 {
69pub fn cosdf(x: f64) f32 {
6970 // |cos(x) - c(x)| < 2**-34.1 (~[-5.37e-11, 5.295e-11]).
7071 const C0 = -0x1ffffffd0c5e81.0p-54; // -0.499999997251031003120
7172 const C1 = 0x155553e1053a42.0p-57; // 0.0416666233237390631894
......@@ -79,7 +80,7 @@ pub fn __cosdf(x: f64) f32 {
7980 return @floatCast(((1.0 + z * C0) + w * C1) + (w * z) * r);
8081}
8182
82pub fn __cosl(comptime T: type, x: T, y: T) T {
83pub fn cosl(comptime T: type, x: T, y: T) T {
8384 const impl = switch (T) {
8485 f80 => struct {
8586 const C1: T = 0.0416666666666666666136;
......@@ -115,7 +116,7 @@ pub fn __cosl(comptime T: type, x: T, y: T) T {
115116 z * (C7 + z * (C8 + z * (C9 + z * (C10 + z * C11))))))))));
116117 }
117118 },
118 else => @compileError("__cosl supports only f80 and f128, got: " ++ @typeName(T)),
119 else => @compileError("cosl supports only f80 and f128, got: " ++ @typeName(T)),
119120 };
120121
121122 const z = x * x;
......@@ -152,7 +153,7 @@ pub fn __cosl(comptime T: type, x: T, y: T) T {
152153/// r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
153154/// then 3 2
154155/// sin(x) = x + (S1*x + (x *(r-y/2)+y))
155pub fn __sin(x: f64, y: f64, iy: i32) f64 {
156pub fn sin(x: f64, y: f64, iy: i32) f64 {
156157 const S1 = -1.66666666666666324348e-01; // 0xBFC55555, 0x55555549
157158 const S2 = 8.33333333332248946124e-03; // 0x3F811111, 0x1110F8A6
158159 const S3 = -1.98412698298579493134e-04; // 0xBF2A01A0, 0x19C161D5
......@@ -171,7 +172,7 @@ pub fn __sin(x: f64, y: f64, iy: i32) f64 {
171172 }
172173}
173174
174pub fn __sinl(comptime T: type, x: T, y: T, iy: i32) T {
175pub fn sinl(comptime T: type, x: T, y: T, iy: i32) T {
175176 const impl = switch (T) {
176177 f80 => struct {
177178 const S1: T = -0.166666666666666666671;
......@@ -209,7 +210,7 @@ pub fn __sinl(comptime T: type, x: T, y: T, iy: i32) T {
209210 z * (S9 + z * (S10 + z * (S11 + z * S12)))))))));
210211 }
211212 },
212 else => @compileError("__sinl supports only f80 and f128, got: " ++ @typeName(T)),
213 else => @compileError("sinl supports only f80 and f128, got: " ++ @typeName(T)),
213214 };
214215
215216 const z = x * x;
......@@ -223,7 +224,7 @@ pub fn __sinl(comptime T: type, x: T, y: T, iy: i32) T {
223224 return x - ((z * (0.5 * y - v * r) - y) - v * impl.S1);
224225}
225226
226pub fn __sindf(x: f64) f32 {
227pub fn sindf(x: f64) f32 {
227228 // |sin(x)/x - s(x)| < 2**-37.5 (~[-4.89e-12, 4.824e-12]).
228229 const S1 = -0x15555554cbac77.0p-55; // -0.166666666416265235595
229230 const S2 = 0x111110896efbb2.0p-59; // 0.0083333293858894631756
......@@ -270,7 +271,7 @@ pub fn __sindf(x: f64) f32 {
270271/// 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
271272/// tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
272273/// = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
273pub fn __tan(x_: f64, y_: f64, odd: bool) f64 {
274pub fn tan(x_: f64, y_: f64, odd: bool) f64 {
274275 var x = x_;
275276 var y = y_;
276277
......@@ -343,7 +344,7 @@ pub fn __tan(x_: f64, y_: f64, odd: bool) f64 {
343344 return a0 + a * (1.0 + a0 * w0 + a0 * v);
344345}
345346
346pub fn __tandf(x: f64, odd: bool) f32 {
347pub fn tandf(x: f64, odd: bool) f32 {
347348 // |tan(x)/x - t(x)| < 2**-25.5 (~[-2e-08, 2e-08]).
348349 const T = [_]f64{
349350 0x15554d3418c99f.0p-54, // 0.333331395030791399758
......@@ -376,7 +377,7 @@ pub fn __tandf(x: f64, odd: bool) f32 {
376377 return @floatCast(if (odd) -1.0 / r0 else r0);
377378}
378379
379pub fn __tanl(comptime T: type, x_: T, y_: T, odd: i32) T {
380pub fn tanl(comptime T: type, x_: T, y_: T, odd: i32) T {
380381 var x = x_;
381382 var y = y_;
382383 const impl = switch (T) {
......@@ -456,7 +457,7 @@ pub fn __tanl(comptime T: type, x_: T, y_: T, odd: i32) T {
456457 w * (T47 + w * (T51 + w * T55)))))))))));
457458 }
458459 },
459 else => @compileError("__tanl supports only f80 and f128, got: " ++ @typeName(T)),
460 else => @compileError("tanl supports only f80 and f128, got: " ++ @typeName(T)),
460461 };
461462
462463 const big = @abs(x) >= 0.67434;