2 prim::{PrimFloat, PrimUInt},
3 traits::{Compare, Context, ConvertFrom, ConvertTo, Float, Make, Select},
6 // TODO: change `sin`/`cos`/`sin_pi`/`cos_pi` implementations to be
7 // in terms of `sin_cos_tau` aka. `(sin(2 * pi * x), cos(2 * pi * x))`
10 #![allow(clippy::excessive_precision)]
13 /// coefficients of taylor series for `sin(pi * x)` centered at `0`
17 /// sinpi: bfloat(taylor(sin(%pi*x),x,0,19))$
18 /// for i: 1 step 2 thru 19 do
19 /// printf(true, "pub(crate) const SINPI_KERNEL_TAYLOR_~d: f64 = ~a;~%", i, ssubst("e", "b", string(coeff(sinpi, x, i))))$
21 pub(crate) const SINPI_KERNEL_TAYLOR_1: f64 =
22 3.1415926535897932384626433832795028841971693993751e0;
23 pub(crate) const SINPI_KERNEL_TAYLOR_3: f64 =
24 -5.1677127800499700292460525111835658670375480943142e0;
25 pub(crate) const SINPI_KERNEL_TAYLOR_5: f64 =
26 2.550164039877345443856177583695296720669172555234e0;
27 pub(crate) const SINPI_KERNEL_TAYLOR_7: f64 =
28 -5.9926452932079207688773938354604004601536358636814e-1;
29 pub(crate) const SINPI_KERNEL_TAYLOR_9: f64 =
30 8.2145886611128228798802365523698344807837460797753e-2;
31 pub(crate) const SINPI_KERNEL_TAYLOR_11: f64 =
32 -7.370430945714350777259089957290781501211638236021e-3;
33 pub(crate) const SINPI_KERNEL_TAYLOR_13: f64 =
34 4.6630280576761256442062891447027174382819981361599e-4;
35 pub(crate) const SINPI_KERNEL_TAYLOR_15: f64 =
36 -2.1915353447830215827384652057094188859248708765956e-5;
37 pub(crate) const SINPI_KERNEL_TAYLOR_17: f64 =
38 7.9520540014755127847832068624575890327682459384282e-7;
39 pub(crate) const SINPI_KERNEL_TAYLOR_19: f64 =
40 -2.2948428997269873110203872385571587856074785581088e-8;
42 /// coefficients of taylor series for `cos(pi * x)` centered at `0`
46 /// cospi: bfloat(taylor(cos(%pi*x),x,0,18))$
47 /// for i: 0 step 2 thru 18 do
48 /// printf(true, "pub(crate) const COSPI_KERNEL_TAYLOR_~d: f64 = ~a;~%", i, ssubst("e", "b", string(coeff(cospi, x, i))))$
50 pub(crate) const COSPI_KERNEL_TAYLOR_0: f64 = 1.0e0;
51 pub(crate) const COSPI_KERNEL_TAYLOR_2: f64 =
52 -4.9348022005446793094172454999380755676568497036204e0;
53 pub(crate) const COSPI_KERNEL_TAYLOR_4: f64 =
54 4.0587121264167682181850138620293796354053160696952e0;
55 pub(crate) const COSPI_KERNEL_TAYLOR_6: f64 =
56 -1.3352627688545894958753047828505831928711354556681e0;
57 pub(crate) const COSPI_KERNEL_TAYLOR_8: f64 =
58 2.3533063035889320454187935277546542154506893530856e-1;
59 pub(crate) const COSPI_KERNEL_TAYLOR_10: f64 =
60 -2.5806891390014060012598294252898849657186441048147e-2;
61 pub(crate) const COSPI_KERNEL_TAYLOR_12: f64 =
62 1.9295743094039230479033455636859576401684718150003e-3;
63 pub(crate) const COSPI_KERNEL_TAYLOR_14: f64 =
64 -1.0463810492484570711801672835223932761029733149091e-4;
65 pub(crate) const COSPI_KERNEL_TAYLOR_16: f64 =
66 4.3030695870329470072978237149669233008960901556009e-6;
67 pub(crate) const COSPI_KERNEL_TAYLOR_18: f64 =
68 -1.387895246221377211446808750399309343777037849978e-7;
71 /// computes `sin(pi * x)` for `-0.25 <= x <= 0.25`
72 /// not guaranteed to give correct sign for zero result
73 /// has an error of up to 2ULP
74 pub fn sin_pi_kernel_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
76 let mut v: Ctx::VecF16 = ctx.make(consts::SINPI_KERNEL_TAYLOR_5.to());
77 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_3.to()));
78 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_1.to()));
82 /// computes `cos(pi * x)` for `-0.25 <= x <= 0.25`
83 /// has an error of up to 2ULP
84 pub fn cos_pi_kernel_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
86 let mut v: Ctx::VecF16 = ctx.make(consts::COSPI_KERNEL_TAYLOR_4.to());
87 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_2.to()));
88 v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_0.to()))
91 /// computes `sin(pi * x)` for `-0.25 <= x <= 0.25`
92 /// not guaranteed to give correct sign for zero result
93 /// has an error of up to 2ULP
94 pub fn sin_pi_kernel_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
96 let mut v: Ctx::VecF32 = ctx.make(consts::SINPI_KERNEL_TAYLOR_9.to());
97 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_7.to()));
98 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_5.to()));
99 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_3.to()));
100 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_1.to()));
104 /// computes `cos(pi * x)` for `-0.25 <= x <= 0.25`
105 /// has an error of up to 2ULP
106 pub fn cos_pi_kernel_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
108 let mut v: Ctx::VecF32 = ctx.make(consts::COSPI_KERNEL_TAYLOR_8.to());
109 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_6.to()));
110 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_4.to()));
111 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_2.to()));
112 v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_0.to()))
115 /// computes `sin(pi * x)` for `-0.25 <= x <= 0.25`
116 /// not guaranteed to give correct sign for zero result
117 /// has an error of up to 2ULP
118 pub fn sin_pi_kernel_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
120 let mut v: Ctx::VecF64 = ctx.make(consts::SINPI_KERNEL_TAYLOR_15.to());
121 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_13.to()));
122 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_11.to()));
123 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_9.to()));
124 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_7.to()));
125 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_5.to()));
126 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_3.to()));
127 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_1.to()));
131 /// computes `cos(pi * x)` for `-0.25 <= x <= 0.25`
132 /// has an error of up to 2ULP
133 pub fn cos_pi_kernel_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
135 let mut v: Ctx::VecF64 = ctx.make(consts::COSPI_KERNEL_TAYLOR_16.to());
136 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_14.to()));
137 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_12.to()));
138 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_10.to()));
139 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_8.to()));
140 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_6.to()));
141 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_4.to()));
142 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_2.to()));
143 v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_0.to()))
146 /// computes `(sin(pi * x), cos(pi * x))`
147 /// not guaranteed to give correct sign for zero results
148 /// inherits error from `sin_pi_kernel` and `cos_pi_kernel`
149 pub fn sin_cos_pi_impl<
151 VecF: Float<PrimFloat = PrimF> + Make<Context = Ctx>,
152 PrimF: PrimFloat<BitsType = PrimU>,
154 SinPiKernel: FnOnce(Ctx, VecF) -> VecF,
155 CosPiKernel: FnOnce(Ctx, VecF) -> VecF,
159 sin_pi_kernel: SinPiKernel,
160 cos_pi_kernel: CosPiKernel,
162 let two_f: VecF = ctx.make(2.0.to());
163 let one_half: VecF = ctx.make(0.5.to());
164 let max_contiguous_integer: VecF = ctx.make(PrimF::max_contiguous_integer());
165 // if `x` is finite and bigger than `max_contiguous_integer`, then x is an even integer
166 let in_range = x.abs().lt(max_contiguous_integer); // use `lt` so nans are counted as out-of-range
167 let is_finite = x.is_finite();
168 let nan: VecF = ctx.make(f32::NAN.to());
169 let zero_f: VecF = ctx.make(0.to());
170 let one_f: VecF = ctx.make(1.to());
171 let zero_i: VecF::SignedBitsType = ctx.make(0.to());
172 let one_i: VecF::SignedBitsType = ctx.make(1.to());
173 let two_i: VecF::SignedBitsType = ctx.make(2.to());
174 let out_of_range_sin = is_finite.select(zero_f, nan);
175 let out_of_range_cos = is_finite.select(one_f, nan);
176 let xi = (x * two_f).round();
177 let xk = x - xi * one_half;
178 let sk = sin_pi_kernel(ctx, xk);
179 let ck = cos_pi_kernel(ctx, xk);
180 let xi = VecF::SignedBitsType::cvt_from(xi);
181 let bit_0_clear = (xi & one_i).eq(zero_i);
182 let st = bit_0_clear.select(sk, ck);
183 let ct = bit_0_clear.select(ck, sk);
184 let s = (xi & two_i).eq(zero_i).select(st, -st);
185 let c = ((xi + one_i) & two_i).eq(zero_i).select(ct, -ct);
187 in_range.select(s, out_of_range_sin),
188 in_range.select(c, out_of_range_cos),
192 /// computes `(sin(pi * x), cos(pi * x))`
193 /// not guaranteed to give correct sign for zero results
194 /// has an error of up to 2ULP
195 pub fn sin_cos_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> (Ctx::VecF16, Ctx::VecF16) {
196 sin_cos_pi_impl(ctx, x, sin_pi_kernel_f16, cos_pi_kernel_f16)
199 /// computes `sin(pi * x)`
200 /// not guaranteed to give correct sign for zero results
201 /// has an error of up to 2ULP
202 pub fn sin_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
203 sin_cos_pi_f16(ctx, x).0
206 /// computes `cos(pi * x)`
207 /// not guaranteed to give correct sign for zero results
208 /// has an error of up to 2ULP
209 pub fn cos_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
210 sin_cos_pi_f16(ctx, x).1
213 /// computes `(sin(pi * x), cos(pi * x))`
214 /// not guaranteed to give correct sign for zero results
215 /// has an error of up to 2ULP
216 pub fn sin_cos_pi_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> (Ctx::VecF32, Ctx::VecF32) {
217 sin_cos_pi_impl(ctx, x, sin_pi_kernel_f32, cos_pi_kernel_f32)
220 /// computes `sin(pi * x)`
221 /// not guaranteed to give correct sign for zero results
222 /// has an error of up to 2ULP
223 pub fn sin_pi_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
224 sin_cos_pi_f32(ctx, x).0
227 /// computes `cos(pi * x)`
228 /// not guaranteed to give correct sign for zero results
229 /// has an error of up to 2ULP
230 pub fn cos_pi_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
231 sin_cos_pi_f32(ctx, x).1
234 /// computes `(sin(pi * x), cos(pi * x))`
235 /// not guaranteed to give correct sign for zero results
236 /// has an error of up to 2ULP
237 pub fn sin_cos_pi_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> (Ctx::VecF64, Ctx::VecF64) {
238 sin_cos_pi_impl(ctx, x, sin_pi_kernel_f64, cos_pi_kernel_f64)
241 /// computes `sin(pi * x)`
242 /// not guaranteed to give correct sign for zero results
243 /// has an error of up to 2ULP
244 pub fn sin_pi_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
245 sin_cos_pi_f64(ctx, x).0
248 /// computes `cos(pi * x)`
249 /// not guaranteed to give correct sign for zero results
250 /// has an error of up to 2ULP
251 pub fn cos_pi_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
252 sin_cos_pi_f64(ctx, x).1
255 /// computes `tan(pi * x)`
256 /// error inherited from `sin_pi / cos_pi`
257 pub fn tan_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
258 let (sin, cos) = sin_cos_pi_f16(ctx, x);
262 /// computes `tan(pi * x)`
263 /// error inherited from `sin_pi / cos_pi`
264 pub fn tan_pi_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
265 let (sin, cos) = sin_cos_pi_f32(ctx, x);
269 /// computes `tan(pi * x)`
270 /// error inherited from `sin_pi / cos_pi`
271 pub fn tan_pi_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
272 let (sin, cos) = sin_cos_pi_f64(ctx, x);
281 scalar::{Scalar, Value},
285 struct CheckUlpCallbackArg<F, I> {
293 fn check_ulp<T: PrimFloat>(
295 is_ok: impl Fn(CheckUlpCallbackArg<T, u64>) -> bool,
296 fn_f16: impl Fn(T) -> T,
297 fn_reference: impl Fn(f64) -> f64,
299 let x_f64: f64 = x.to();
300 let expected_f64 = fn_reference(x_f64);
301 let expected: T = expected_f64.to();
302 let result = fn_f16(x);
303 if result == expected {
306 if result.is_nan() && expected.is_nan() {
309 let expected_bits: i64 = expected.to_bits().to();
310 let result_bits: i64 = result.to_bits().to();
311 let distance_in_ulp = (expected_bits - result_bits).unsigned_abs();
313 && !expected.is_nan()
314 && is_ok(CheckUlpCallbackArg {
325 x = {x:?} {x_bits:#X}, \
326 result = {result:?} {result_bits:#X}, \
327 expected = {expected:?} {expected_bits:#X}, \
328 distance_in_ulp = {distance_in_ulp}",
330 x_bits = x.to_bits(),
332 result_bits = result.to_bits(),
334 expected_bits = expected.to_bits(),
335 distance_in_ulp = distance_in_ulp,
341 not(feature = "f16"),
342 should_panic(expected = "f16 feature is not enabled")
344 fn test_sin_pi_kernel_f16() {
348 |arg| arg.distance_in_ulp <= if arg.expected == 0.to() { 0 } else { 2 },
349 |x| sin_pi_kernel_f16(Scalar, Value(x)).0,
350 |x| (f64::consts::PI * x).sin(),
353 let quarter = F16::to_bits(0.25f32.to());
354 for bits in (0..=quarter).rev() {
355 check(F16::from_bits(bits));
356 check(-F16::from_bits(bits));
362 not(feature = "f16"),
363 should_panic(expected = "f16 feature is not enabled")
365 fn test_cos_pi_kernel_f16() {
369 |arg| arg.distance_in_ulp <= 2 && arg.result <= 1.to(),
370 |x| cos_pi_kernel_f16(Scalar, Value(x)).0,
371 |x| (f64::consts::PI * x).cos(),
374 let quarter = F16::to_bits(0.25f32.to());
375 for bits in (0..=quarter).rev() {
376 check(F16::from_bits(bits));
377 check(-F16::from_bits(bits));
382 #[cfg(feature = "full_tests")]
383 fn test_sin_pi_kernel_f32() {
387 |arg| arg.distance_in_ulp <= if arg.expected == 0. { 0 } else { 2 },
388 |x| sin_pi_kernel_f32(Scalar, Value(x)).0,
389 |x| (f64::consts::PI * x).sin(),
392 let quarter = 0.25f32.to_bits();
393 for bits in (0..=quarter).rev() {
394 check(f32::from_bits(bits));
395 check(-f32::from_bits(bits));
400 #[cfg(feature = "full_tests")]
401 fn test_cos_pi_kernel_f32() {
405 |arg| arg.distance_in_ulp <= 2 && arg.result <= 1.,
406 |x| cos_pi_kernel_f32(Scalar, Value(x)).0,
407 |x| (f64::consts::PI * x).cos(),
410 let quarter = 0.25f32.to_bits();
411 for bits in (0..=quarter).rev() {
412 check(f32::from_bits(bits));
413 check(-f32::from_bits(bits));
418 #[cfg(feature = "full_tests")]
419 fn test_sin_pi_kernel_f64() {
423 sin_cos_pi_check_ulp_callback,
424 |x| sin_pi_kernel_f64(Scalar, Value(x)).0,
425 |x| reference_sin_cos_pi_f64(x).0,
428 let quarter = 0.25f32.to_bits();
429 for bits in (0..=quarter).rev().step_by(1 << 5) {
430 check(f32::from_bits(bits) as f64);
431 check(-f32::from_bits(bits) as f64);
436 #[cfg(feature = "full_tests")]
437 fn test_cos_pi_kernel_f64() {
441 sin_cos_pi_check_ulp_callback,
442 |x| cos_pi_kernel_f64(Scalar, Value(x)).0,
443 |x| reference_sin_cos_pi_f64(x).1,
446 let quarter = 0.25f32.to_bits();
447 for bits in (0..=quarter).rev().step_by(1 << 5) {
448 check(f32::from_bits(bits) as f64);
449 check(-f32::from_bits(bits) as f64);
453 fn sin_cos_pi_check_ulp_callback<F: PrimFloat>(arg: CheckUlpCallbackArg<F, u64>) -> bool {
454 if arg.x % 0.5.to() == 0.0.to() {
455 arg.distance_in_ulp == 0
457 arg.distance_in_ulp <= 2 && arg.result.abs() <= 1.to()
463 not(feature = "f16"),
464 should_panic(expected = "f16 feature is not enabled")
466 fn test_sin_pi_f16() {
467 for bits in 0..=u16::MAX {
469 F16::from_bits(bits),
470 sin_cos_pi_check_ulp_callback,
471 |x| sin_pi_f16(Scalar, Value(x)).0,
472 |x| (f64::consts::PI * x).sin(),
479 not(feature = "f16"),
480 should_panic(expected = "f16 feature is not enabled")
482 fn test_cos_pi_f16() {
483 for bits in 0..=u16::MAX {
485 F16::from_bits(bits),
486 sin_cos_pi_check_ulp_callback,
487 |x| cos_pi_f16(Scalar, Value(x)).0,
488 |x| (f64::consts::PI * x).cos(),
493 fn reference_sin_cos_pi_f32(mut v: f64) -> (f64, f64) {
495 return (f64::NAN, f64::NAN);
500 } else if v <= -1.0 {
504 let part = v.round() as i32;
506 v *= f64::consts::PI / 2.0;
507 let (sin, cos) = v.sin_cos();
514 _ => panic!("not implemented: part={}", part),
518 fn reference_sin_cos_pi_f64(mut v: f64) -> (f64, f64) {
520 use rug::{float::Constant, Float};
522 return (f64::NAN, f64::NAN);
527 } else if v <= -1.0 {
531 let part = v.round() as i32;
534 let mut v = Float::with_val(precision, v);
535 let pi = Float::with_val(precision, Constant::Pi);
538 let cos = pi_2; // just a temp var, value is ignored
539 let (sin, cos) = v.sin_cos(cos);
540 let sin: f64 = sin.cast();
541 let cos: f64 = cos.cast();
548 _ => panic!("not implemented: part={}", part),
552 macro_rules! test_reference_sin_cos_pi_test_cases {
553 ($case:expr, $ty:ident) => {
554 $case($ty::NAN, $ty::NAN, $ty::NAN);
555 $case($ty::INFINITY, $ty::NAN, $ty::NAN);
556 $case(-$ty::INFINITY, $ty::NAN, $ty::NAN);
560 0.38268343236508977172845998403039886676134456248563,
561 0.92387953251128675612818318939678828682241662586364,
565 0.70710678118654752440084436210484903928483593768847,
566 0.70710678118654752440084436210484903928483593768847,
570 0.92387953251128675612818318939678828682241662586364,
571 0.38268343236508977172845998403039886676134456248563,
573 $case(-3.5, 1., -0.);
576 0.92387953251128675612818318939678828682241662586364,
577 -0.38268343236508977172845998403039886676134456248563,
581 0.70710678118654752440084436210484903928483593768847,
582 -0.70710678118654752440084436210484903928483593768847,
586 0.38268343236508977172845998403039886676134456248563,
587 -0.92387953251128675612818318939678828682241662586364,
589 $case(-3., -0., -1.);
592 -0.38268343236508977172845998403039886676134456248563,
593 -0.92387953251128675612818318939678828682241662586364,
597 -0.70710678118654752440084436210484903928483593768847,
598 -0.70710678118654752440084436210484903928483593768847,
602 -0.92387953251128675612818318939678828682241662586364,
603 -0.38268343236508977172845998403039886676134456248563,
605 $case(-2.5, -1., 0.);
608 -0.92387953251128675612818318939678828682241662586364,
609 0.38268343236508977172845998403039886676134456248563,
613 -0.70710678118654752440084436210484903928483593768847,
614 0.70710678118654752440084436210484903928483593768847,
618 -0.38268343236508977172845998403039886676134456248563,
619 0.92387953251128675612818318939678828682241662586364,
624 0.38268343236508977172845998403039886676134456248563,
625 0.92387953251128675612818318939678828682241662586364,
629 0.70710678118654752440084436210484903928483593768847,
630 0.70710678118654752440084436210484903928483593768847,
634 0.92387953251128675612818318939678828682241662586364,
635 0.38268343236508977172845998403039886676134456248563,
637 $case(-1.5, 1., -0.);
640 0.92387953251128675612818318939678828682241662586364,
641 -0.38268343236508977172845998403039886676134456248563,
645 0.70710678118654752440084436210484903928483593768847,
646 -0.70710678118654752440084436210484903928483593768847,
650 0.38268343236508977172845998403039886676134456248563,
651 -0.92387953251128675612818318939678828682241662586364,
653 $case(-1., -0., -1.);
656 -0.38268343236508977172845998403039886676134456248563,
657 -0.92387953251128675612818318939678828682241662586364,
661 -0.70710678118654752440084436210484903928483593768847,
662 -0.70710678118654752440084436210484903928483593768847,
666 -0.92387953251128675612818318939678828682241662586364,
667 -0.38268343236508977172845998403039886676134456248563,
669 $case(-0.5, -1., 0.);
672 -0.92387953251128675612818318939678828682241662586364,
673 0.38268343236508977172845998403039886676134456248563,
677 -0.70710678118654752440084436210484903928483593768847,
678 0.70710678118654752440084436210484903928483593768847,
682 -0.38268343236508977172845998403039886676134456248563,
683 0.92387953251128675612818318939678828682241662586364,
688 0.38268343236508977172845998403039886676134456248563,
689 0.92387953251128675612818318939678828682241662586364,
693 0.70710678118654752440084436210484903928483593768847,
694 0.70710678118654752440084436210484903928483593768847,
698 0.92387953251128675612818318939678828682241662586364,
699 0.38268343236508977172845998403039886676134456248563,
704 0.92387953251128675612818318939678828682241662586364,
705 -0.38268343236508977172845998403039886676134456248563,
709 0.70710678118654752440084436210484903928483593768847,
710 -0.70710678118654752440084436210484903928483593768847,
714 0.38268343236508977172845998403039886676134456248563,
715 -0.92387953251128675612818318939678828682241662586364,
720 -0.38268343236508977172845998403039886676134456248563,
721 -0.92387953251128675612818318939678828682241662586364,
725 -0.70710678118654752440084436210484903928483593768847,
726 -0.70710678118654752440084436210484903928483593768847,
730 -0.92387953251128675612818318939678828682241662586364,
731 -0.38268343236508977172845998403039886676134456248563,
733 $case(1.5, -1., -0.);
736 -0.92387953251128675612818318939678828682241662586364,
737 0.38268343236508977172845998403039886676134456248563,
741 -0.70710678118654752440084436210484903928483593768847,
742 0.70710678118654752440084436210484903928483593768847,
746 -0.38268343236508977172845998403039886676134456248563,
747 0.92387953251128675612818318939678828682241662586364,
752 0.38268343236508977172845998403039886676134456248563,
753 0.92387953251128675612818318939678828682241662586364,
757 0.70710678118654752440084436210484903928483593768847,
758 0.70710678118654752440084436210484903928483593768847,
762 0.92387953251128675612818318939678828682241662586364,
763 0.38268343236508977172845998403039886676134456248563,
768 0.92387953251128675612818318939678828682241662586364,
769 -0.38268343236508977172845998403039886676134456248563,
773 0.70710678118654752440084436210484903928483593768847,
774 -0.70710678118654752440084436210484903928483593768847,
778 0.38268343236508977172845998403039886676134456248563,
779 -0.92387953251128675612818318939678828682241662586364,
784 -0.38268343236508977172845998403039886676134456248563,
785 -0.92387953251128675612818318939678828682241662586364,
789 -0.70710678118654752440084436210484903928483593768847,
790 -0.70710678118654752440084436210484903928483593768847,
794 -0.92387953251128675612818318939678828682241662586364,
795 -0.38268343236508977172845998403039886676134456248563,
797 $case(3.5, -1., -0.);
800 -0.92387953251128675612818318939678828682241662586364,
801 0.38268343236508977172845998403039886676134456248563,
805 -0.70710678118654752440084436210484903928483593768847,
806 0.70710678118654752440084436210484903928483593768847,
810 -0.38268343236508977172845998403039886676134456248563,
811 0.92387953251128675612818318939678828682241662586364,
818 fn test_reference_sin_cos_pi_f32() {
819 fn approx_same(a: f32, b: f32) -> bool {
820 if a.is_finite() && b.is_finite() {
823 a == b || (a.is_nan() && b.is_nan())
827 fn case(x: f32, expected_sin: f32, expected_cos: f32) {
828 let (ref_sin, ref_cos) = reference_sin_cos_pi_f32(x as f64);
830 approx_same(ref_sin as f32, expected_sin)
831 && approx_same(ref_cos as f32, expected_cos),
832 "case failed: x={x}, expected_sin={expected_sin}, expected_cos={expected_cos}, ref_sin={ref_sin}, ref_cos={ref_cos}",
834 expected_sin=expected_sin,
835 expected_cos=expected_cos,
840 test_reference_sin_cos_pi_test_cases!(case, f32);
844 fn test_reference_sin_cos_pi_f64() {
845 fn same(a: f64, b: f64) -> bool {
846 if a.is_finite() && b.is_finite() {
849 a == b || (a.is_nan() && b.is_nan())
853 fn case(x: f64, expected_sin: f64, expected_cos: f64) {
854 let (ref_sin, ref_cos) = reference_sin_cos_pi_f64(x);
856 same(ref_sin, expected_sin) && same(ref_cos, expected_cos),
857 "case failed: x={x}, expected_sin={expected_sin}, expected_cos={expected_cos}, ref_sin={ref_sin}, ref_cos={ref_cos}",
859 expected_sin=expected_sin,
860 expected_cos=expected_cos,
865 test_reference_sin_cos_pi_test_cases!(case, f64);
869 #[cfg(feature = "full_tests")]
870 fn test_sin_pi_f32() {
871 for bits in 0..=u32::MAX {
873 f32::from_bits(bits),
874 sin_cos_pi_check_ulp_callback,
875 |x| sin_pi_f32(Scalar, Value(x)).0,
876 |x| reference_sin_cos_pi_f32(x).0,
882 #[cfg(feature = "full_tests")]
883 fn test_cos_pi_f32() {
884 for bits in 0..=u32::MAX {
886 f32::from_bits(bits),
887 sin_cos_pi_check_ulp_callback,
888 |x| cos_pi_f32(Scalar, Value(x)).0,
889 |x| reference_sin_cos_pi_f32(x).1,
895 #[cfg(feature = "full_tests")]
896 fn test_sin_pi_f64() {
897 for bits in (0..=u32::MAX).step_by(1 << 7) {
899 f32::from_bits(bits) as f64,
900 sin_cos_pi_check_ulp_callback,
901 |x| sin_pi_f64(Scalar, Value(x)).0,
902 |x| reference_sin_cos_pi_f64(x).0,
908 #[cfg(feature = "full_tests")]
909 fn test_cos_pi_f64() {
910 for bits in (0..=u32::MAX).step_by(1 << 7) {
912 f32::from_bits(bits) as f64,
913 sin_cos_pi_check_ulp_callback,
914 |x| cos_pi_f64(Scalar, Value(x)).0,
915 |x| reference_sin_cos_pi_f64(x).1,