authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-27 22:37:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-27 22:37:07-07:00
logf7f03c699d2dee4c3eec2108e159bbe300e24c6f
treea8fda028d8aae82b9fd2fd8488b6e6e11643402e
parent0b2ed45f5fd69cb5f3afb3118726b17859bda0f5

compiler-rt: provide actual sincos implementations


3 files changed, 264 insertions(+), 49 deletions(-)

lib/std/special/compiler_rt/cos.zig+17-17
......@@ -2,7 +2,7 @@ const std = @import("std");
22const math = std.math;
33const expect = std.testing.expect;
44
5const kernel = @import("trig.zig");
5const trig = @import("trig.zig");
66const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
77const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
88
......@@ -28,27 +28,27 @@ pub fn cosf(x: f32) callconv(.C) f32 {
2828 math.doNotOptimizeAway(x + 0x1p120);
2929 return 1.0;
3030 }
31 return kernel.__cosdf(x);
31 return trig.__cosdf(x);
3232 }
3333 if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
3434 if (ix > 0x4016cbe3) { // |x| ~> 3*pi/4
35 return -kernel.__cosdf(if (sign) x + c2pio2 else x - c2pio2);
35 return -trig.__cosdf(if (sign) x + c2pio2 else x - c2pio2);
3636 } else {
3737 if (sign) {
38 return kernel.__sindf(x + c1pio2);
38 return trig.__sindf(x + c1pio2);
3939 } else {
40 return kernel.__sindf(c1pio2 - x);
40 return trig.__sindf(c1pio2 - x);
4141 }
4242 }
4343 }
4444 if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
4545 if (ix > 0x40afeddf) { // |x| ~> 7*pi/4
46 return kernel.__cosdf(if (sign) x + c4pio2 else x - c4pio2);
46 return trig.__cosdf(if (sign) x + c4pio2 else x - c4pio2);
4747 } else {
4848 if (sign) {
49 return kernel.__sindf(-x - c3pio2);
49 return trig.__sindf(-x - c3pio2);
5050 } else {
51 return kernel.__sindf(x - c3pio2);
51 return trig.__sindf(x - c3pio2);
5252 }
5353 }
5454 }
......@@ -61,10 +61,10 @@ pub fn cosf(x: f32) callconv(.C) f32 {
6161 var y: f64 = undefined;
6262 const n = rem_pio2f(x, &y);
6363 return switch (n & 3) {
64 0 => kernel.__cosdf(y),
65 1 => kernel.__sindf(-y),
66 2 => -kernel.__cosdf(y),
67 else => kernel.__sindf(y),
64 0 => trig.__cosdf(y),
65 1 => trig.__sindf(-y),
66 2 => -trig.__cosdf(y),
67 else => trig.__sindf(y),
6868 };
6969}
7070
......@@ -79,7 +79,7 @@ pub fn cos(x: f64) callconv(.C) f64 {
7979 math.doNotOptimizeAway(x + 0x1p120);
8080 return 1.0;
8181 }
82 return kernel.__cos(x, 0);
82 return trig.__cos(x, 0);
8383 }
8484
8585 // cos(Inf or NaN) is NaN
......@@ -90,10 +90,10 @@ pub fn cos(x: f64) callconv(.C) f64 {
9090 var y: [2]f64 = undefined;
9191 const n = rem_pio2(x, &y);
9292 return switch (n & 3) {
93 0 => kernel.__cos(y[0], y[1]),
94 1 => -kernel.__sin(y[0], y[1], 1),
95 2 => -kernel.__cos(y[0], y[1]),
96 else => kernel.__sin(y[0], y[1], 1),
93 0 => trig.__cos(y[0], y[1]),
94 1 => -trig.__sin(y[0], y[1], 1),
95 2 => -trig.__cos(y[0], y[1]),
96 else => trig.__sin(y[0], y[1], 1),
9797 };
9898}
9999
lib/std/special/compiler_rt/sin.zig+17-17
......@@ -8,7 +8,7 @@ const std = @import("std");
88const math = std.math;
99const expect = std.testing.expect;
1010
11const kernel = @import("trig.zig");
11const trig = @import("trig.zig");
1212const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
1313const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
1414
......@@ -34,27 +34,27 @@ pub fn sinf(x: f32) callconv(.C) f32 {
3434 math.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
3535 return x;
3636 }
37 return kernel.__sindf(x);
37 return trig.__sindf(x);
3838 }
3939 if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
4040 if (ix <= 0x4016cbe3) { // |x| ~<= 3pi/4
4141 if (sign) {
42 return -kernel.__cosdf(x + s1pio2);
42 return -trig.__cosdf(x + s1pio2);
4343 } else {
44 return kernel.__cosdf(x - s1pio2);
44 return trig.__cosdf(x - s1pio2);
4545 }
4646 }
47 return kernel.__sindf(if (sign) -(x + s2pio2) else -(x - s2pio2));
47 return trig.__sindf(if (sign) -(x + s2pio2) else -(x - s2pio2));
4848 }
4949 if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
5050 if (ix <= 0x40afeddf) { // |x| ~<= 7*pi/4
5151 if (sign) {
52 return kernel.__cosdf(x + s3pio2);
52 return trig.__cosdf(x + s3pio2);
5353 } else {
54 return -kernel.__cosdf(x - s3pio2);
54 return -trig.__cosdf(x - s3pio2);
5555 }
5656 }
57 return kernel.__sindf(if (sign) x + s4pio2 else x - s4pio2);
57 return trig.__sindf(if (sign) x + s4pio2 else x - s4pio2);
5858 }
5959
6060 // sin(Inf or NaN) is NaN
......@@ -65,10 +65,10 @@ pub fn sinf(x: f32) callconv(.C) f32 {
6565 var y: f64 = undefined;
6666 const n = rem_pio2f(x, &y);
6767 return switch (n & 3) {
68 0 => kernel.__sindf(y),
69 1 => kernel.__cosdf(y),
70 2 => kernel.__sindf(-y),
71 else => -kernel.__cosdf(y),
68 0 => trig.__sindf(y),
69 1 => trig.__cosdf(y),
70 2 => trig.__sindf(-y),
71 else => -trig.__cosdf(y),
7272 };
7373}
7474
......@@ -83,7 +83,7 @@ pub fn sin(x: f64) callconv(.C) f64 {
8383 math.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
8484 return x;
8585 }
86 return kernel.__sin(x, 0.0, 0);
86 return trig.__sin(x, 0.0, 0);
8787 }
8888
8989 // sin(Inf or NaN) is NaN
......@@ -94,10 +94,10 @@ pub fn sin(x: f64) callconv(.C) f64 {
9494 var y: [2]f64 = undefined;
9595 const n = rem_pio2(x, &y);
9696 return switch (n & 3) {
97 0 => kernel.__sin(y[0], y[1], 1),
98 1 => kernel.__cos(y[0], y[1]),
99 2 => -kernel.__sin(y[0], y[1], 1),
100 else => -kernel.__cos(y[0], y[1]),
97 0 => trig.__sin(y[0], y[1], 1),
98 1 => trig.__cos(y[0], y[1]),
99 2 => -trig.__sin(y[0], y[1], 1),
100 else => -trig.__cos(y[0], y[1]),
101101 };
102102}
103103
lib/std/special/compiler_rt/sincos.zig+230-15
......@@ -1,27 +1,242 @@
1const std = @import("std");
2const math = std.math;
13const sin = @import("sin.zig");
24const cos = @import("cos.zig");
5const trig = @import("trig.zig");
6const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
7const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
38
4pub fn __sincosh(a: f16, r_sin: *f16, r_cos: *f16) callconv(.C) void {
5 r_sin.* = sin.__sinh(a);
6 r_cos.* = cos.__cosh(a);
9pub fn __sincosh(x: f16, r_sin: *f16, r_cos: *f16) callconv(.C) void {
10 // TODO: more efficient implementation
11 var big_sin: f32 = undefined;
12 var big_cos: f32 = undefined;
13 sincosf(x, &big_sin, &big_cos);
14 r_sin.* = @floatCast(f16, big_sin);
15 r_cos.* = @floatCast(f16, big_cos);
716}
817
9pub fn sincosf(a: f32, r_sin: *f32, r_cos: *f32) callconv(.C) void {
10 r_sin.* = sin.sinf(a);
11 r_cos.* = cos.cosf(a);
18pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.C) void {
19 const sc1pio2: f64 = 1.0 * math.pi / 2.0; // 0x3FF921FB, 0x54442D18
20 const sc2pio2: f64 = 2.0 * math.pi / 2.0; // 0x400921FB, 0x54442D18
21 const sc3pio2: f64 = 3.0 * math.pi / 2.0; // 0x4012D97C, 0x7F3321D2
22 const sc4pio2: f64 = 4.0 * math.pi / 2.0; // 0x401921FB, 0x54442D18
23
24 const pre_ix = @bitCast(u32, x);
25 const sign = pre_ix >> 31 != 0;
26 const ix = pre_ix & 0x7fffffff;
27
28 // |x| ~<= pi/4
29 if (ix <= 0x3f490fda) {
30 // |x| < 2**-12
31 if (ix < 0x39800000) {
32 // raise inexact if x!=0 and underflow if subnormal
33 math.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
34 r_sin.* = x;
35 r_cos.* = 1.0;
36 return;
37 }
38 r_sin.* = trig.__sindf(x);
39 r_cos.* = trig.__cosdf(x);
40 return;
41 }
42
43 // |x| ~<= 5*pi/4
44 if (ix <= 0x407b53d1) {
45 // |x| ~<= 3pi/4
46 if (ix <= 0x4016cbe3) {
47 if (sign) {
48 r_sin.* = -trig.__cosdf(x + sc1pio2);
49 r_cos.* = trig.__sindf(x + sc1pio2);
50 } else {
51 r_sin.* = trig.__cosdf(sc1pio2 - x);
52 r_cos.* = trig.__sindf(sc1pio2 - x);
53 }
54 return;
55 }
56 // -sin(x+c) is not correct if x+c could be 0: -0 vs +0
57 r_sin.* = -trig.__sindf(if (sign) x + sc2pio2 else x - sc2pio2);
58 r_cos.* = -trig.__cosdf(if (sign) x + sc2pio2 else x - sc2pio2);
59 return;
60 }
61
62 // |x| ~<= 9*pi/4
63 if (ix <= 0x40e231d5) {
64 // |x| ~<= 7*pi/4
65 if (ix <= 0x40afeddf) {
66 if (sign) {
67 r_sin.* = trig.__cosdf(x + sc3pio2);
68 r_cos.* = -trig.__sindf(x + sc3pio2);
69 } else {
70 r_sin.* = -trig.__cosdf(x - sc3pio2);
71 r_cos.* = trig.__sindf(x - sc3pio2);
72 }
73 return;
74 }
75 r_sin.* = trig.__sindf(if (sign) x + sc4pio2 else x - sc4pio2);
76 r_cos.* = trig.__cosdf(if (sign) x + sc4pio2 else x - sc4pio2);
77 return;
78 }
79
80 // sin(Inf or NaN) is NaN
81 if (ix >= 0x7f800000) {
82 const result = x - x;
83 r_sin.* = result;
84 r_cos.* = result;
85 return;
86 }
87
88 // general argument reduction needed
89 var y: f64 = undefined;
90 const n = rem_pio2f(x, &y);
91 const s = trig.__sindf(y);
92 const c = trig.__cosdf(y);
93 switch (n & 3) {
94 0 => {
95 r_sin.* = s;
96 r_cos.* = c;
97 },
98 1 => {
99 r_sin.* = c;
100 r_cos.* = -s;
101 },
102 2 => {
103 r_sin.* = -s;
104 r_cos.* = -c;
105 },
106 else => {
107 r_sin.* = -c;
108 r_cos.* = s;
109 },
110 }
12111}
13112
14pub fn sincos(a: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {
15 r_sin.* = sin.sin(a);
16 r_cos.* = cos.cos(a);
113pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {
114 const ix = @truncate(u32, @bitCast(u64, x) >> 32) & 0x7fffffff;
115
116 // |x| ~< pi/4
117 if (ix <= 0x3fe921fb) {
118 // if |x| < 2**-27 * sqrt(2)
119 if (ix < 0x3e46a09e) {
120 // raise inexact if x != 0 and underflow if subnormal
121 math.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
122 r_sin.* = x;
123 r_cos.* = 1.0;
124 return;
125 }
126 r_sin.* = trig.__sin(x, 0.0, 0);
127 r_cos.* = trig.__cos(x, 0.0);
128 return;
129 }
130
131 // sincos(Inf or NaN) is NaN
132 if (ix >= 0x7ff00000) {
133 const result = x - x;
134 r_sin.* = result;
135 r_cos.* = result;
136 return;
137 }
138
139 // argument reduction needed
140 var y: [2]f64 = undefined;
141 const n = rem_pio2(x, &y);
142 const s = trig.__sin(y[0], y[1], 1);
143 const c = trig.__cos(y[0], y[1]);
144 switch (n & 3) {
145 0 => {
146 r_sin.* = s;
147 r_cos.* = c;
148 },
149 1 => {
150 r_sin.* = c;
151 r_cos.* = -s;
152 },
153 2 => {
154 r_sin.* = -s;
155 r_cos.* = -c;
156 },
157 else => {
158 r_sin.* = -c;
159 r_cos.* = s;
160 },
161 }
162}
163
164pub fn __sincosx(x: f80, r_sin: *f80, r_cos: *f80) callconv(.C) void {
165 // TODO: more efficient implementation
166 //return sincos_generic(f80, x, r_sin, r_cos);
167 var big_sin: f128 = undefined;
168 var big_cos: f128 = undefined;
169 sincosq(x, &big_sin, &big_cos);
170 r_sin.* = @floatCast(f80, big_sin);
171 r_cos.* = @floatCast(f80, big_cos);
17172}
18173
19pub fn __sincosx(a: f80, r_sin: *f80, r_cos: *f80) callconv(.C) void {
20 r_sin.* = sin.__sinx(a);
21 r_cos.* = cos.__cosx(a);
174pub fn sincosq(x: f128, r_sin: *f128, r_cos: *f128) callconv(.C) void {
175 // TODO: more correct implementation
176 //return sincos_generic(f128, x, r_sin, r_cos);
177 var small_sin: f64 = undefined;
178 var small_cos: f64 = undefined;
179 sincos(@floatCast(f64, x), &small_sin, &small_cos);
180 r_sin.* = small_sin;
181 r_cos.* = small_cos;
22182}
23183
24pub fn sincosq(a: f128, r_sin: *f128, r_cos: *f128) callconv(.C) void {
25 r_sin.* = sin.sinq(a);
26 r_cos.* = cos.cosq(a);
184const rem_pio2_generic = @compileError("TODO");
185
186/// Ported from musl sincosl.c. Needs the following dependencies to be complete:
187/// * rem_pio2_generic ported from __rem_pio2l.c
188/// * trig.sin_generic ported from __sinl.c
189/// * trig.cos_generic ported from __cosl.c
190inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void {
191 const sc1pio4: F = 1.0 * math.pi / 4.0;
192 const bits = @typeInfo(F).Float.bits;
193 const I = std.meta.Int(.unsigned, bits);
194 const ix = @bitCast(I, x) & (math.maxInt(I) >> 1);
195 const se = @truncate(u16, ix >> (bits - 16));
196
197 if (se == 0x7fff) {
198 const result = x - x;
199 r_sin.* = result;
200 r_cos.* = result;
201 return;
202 }
203
204 if (@bitCast(F, ix) < sc1pio4) {
205 if (se < 0x3fff - math.floatFractionalBits(F) - 1) {
206 // raise underflow if subnormal
207 if (se == 0) {
208 math.doNotOptimizeAway(x * 0x1p-120);
209 }
210 r_sin.* = x;
211 // raise inexact if x!=0
212 r_cos.* = 1.0 + x;
213 return;
214 }
215 r_sin.* = trig.sin_generic(F, x, 0, 0);
216 r_cos.* = trig.cos_generic(F, x, 0);
217 return;
218 }
219
220 var y: [2]F = undefined;
221 const n = rem_pio2_generic(F, x, &y);
222 const s = trig.sin_generic(F, y[0], y[1], 1);
223 const c = trig.cos_generic(F, y[0], y[1]);
224 switch (n & 3) {
225 0 => {
226 r_sin.* = s;
227 r_cos.* = c;
228 },
229 1 => {
230 r_sin.* = c;
231 r_cos.* = -s;
232 },
233 2 => {
234 r_sin.* = -s;
235 r_cos.* = -c;
236 },
237 else => {
238 r_sin.* = -c;
239 r_cos.* = s;
240 },
241 }
27242}