add sin_pi_f16, cos_pi_f16, and sin_cos_pi_f16
[vector-math.git] / src / algorithms / trig_pi.rs
1 use crate::{
2 f16::F16,
3 ieee754::FloatEncoding,
4 traits::{Compare, Context, ConvertFrom, ConvertTo, Float, Select},
5 };
6
7 mod consts {
8 #![allow(clippy::excessive_precision)]
9
10 /// coefficients of taylor series for `sin(pi * x)` centered at `0`
11 /// generated using:
12 /// ```maxima,text
13 /// fpprec:50$
14 /// sinpi: bfloat(taylor(sin(%pi*x),x,0,19))$
15 /// for i: 1 step 2 thru 19 do
16 /// printf(true, "pub(crate) const SINPI_KERNEL_TAYLOR_~d: f64 = ~a;~%", i, ssubst("e", "b", string(coeff(sinpi, x, i))))$
17 /// ```
18 pub(crate) const SINPI_KERNEL_TAYLOR_1: f64 =
19 3.1415926535897932384626433832795028841971693993751e0;
20 pub(crate) const SINPI_KERNEL_TAYLOR_3: f64 =
21 -5.1677127800499700292460525111835658670375480943142e0;
22 pub(crate) const SINPI_KERNEL_TAYLOR_5: f64 =
23 2.550164039877345443856177583695296720669172555234e0;
24 pub(crate) const SINPI_KERNEL_TAYLOR_7: f64 =
25 -5.9926452932079207688773938354604004601536358636814e-1;
26 pub(crate) const SINPI_KERNEL_TAYLOR_9: f64 =
27 8.2145886611128228798802365523698344807837460797753e-2;
28 pub(crate) const SINPI_KERNEL_TAYLOR_11: f64 =
29 -7.370430945714350777259089957290781501211638236021e-3;
30 pub(crate) const SINPI_KERNEL_TAYLOR_13: f64 =
31 4.6630280576761256442062891447027174382819981361599e-4;
32 pub(crate) const SINPI_KERNEL_TAYLOR_15: f64 =
33 -2.1915353447830215827384652057094188859248708765956e-5;
34 pub(crate) const SINPI_KERNEL_TAYLOR_17: f64 =
35 7.9520540014755127847832068624575890327682459384282e-7;
36 pub(crate) const SINPI_KERNEL_TAYLOR_19: f64 =
37 -2.2948428997269873110203872385571587856074785581088e-8;
38
39 /// coefficients of taylor series for `cos(pi * x)` centered at `0`
40 /// generated using:
41 /// ```maxima,text
42 /// fpprec:50$
43 /// cospi: bfloat(taylor(cos(%pi*x),x,0,18))$
44 /// for i: 0 step 2 thru 18 do
45 /// printf(true, "pub(crate) const COSPI_KERNEL_TAYLOR_~d: f64 = ~a;~%", i, ssubst("e", "b", string(coeff(cospi, x, i))))$
46 /// ```
47 pub(crate) const COSPI_KERNEL_TAYLOR_0: f64 = 1.0e0;
48 pub(crate) const COSPI_KERNEL_TAYLOR_2: f64 =
49 -4.9348022005446793094172454999380755676568497036204e0;
50 pub(crate) const COSPI_KERNEL_TAYLOR_4: f64 =
51 4.0587121264167682181850138620293796354053160696952e0;
52 pub(crate) const COSPI_KERNEL_TAYLOR_6: f64 =
53 -1.3352627688545894958753047828505831928711354556681e0;
54 pub(crate) const COSPI_KERNEL_TAYLOR_8: f64 =
55 2.3533063035889320454187935277546542154506893530856e-1;
56 pub(crate) const COSPI_KERNEL_TAYLOR_10: f64 =
57 -2.5806891390014060012598294252898849657186441048147e-2;
58 pub(crate) const COSPI_KERNEL_TAYLOR_12: f64 =
59 1.9295743094039230479033455636859576401684718150003e-3;
60 pub(crate) const COSPI_KERNEL_TAYLOR_14: f64 =
61 -1.0463810492484570711801672835223932761029733149091e-4;
62 pub(crate) const COSPI_KERNEL_TAYLOR_16: f64 =
63 4.3030695870329470072978237149669233008960901556009e-6;
64 pub(crate) const COSPI_KERNEL_TAYLOR_18: f64 =
65 -1.387895246221377211446808750399309343777037849978e-7;
66 }
67
68 /// computes `sin(pi * x)` for `-0.25 <= x <= 0.25`
69 /// not guaranteed to give correct sign for zero result
70 /// has an error of up to 2ULP
71 pub fn sin_pi_kernel_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
72 let x_sq = x * x;
73 let mut v: Ctx::VecF16 = ctx.make(consts::SINPI_KERNEL_TAYLOR_5.to());
74 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_3.to()));
75 v = v.mul_add_fast(x_sq, ctx.make(consts::SINPI_KERNEL_TAYLOR_1.to()));
76 v * x
77 }
78
79 /// computes `cos(pi * x)` for `-0.25 <= x <= 0.25`
80 /// has an error of up to 2ULP
81 pub fn cos_pi_kernel_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
82 let x_sq = x * x;
83 let mut v: Ctx::VecF16 = ctx.make(consts::COSPI_KERNEL_TAYLOR_4.to());
84 v = v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_2.to()));
85 v.mul_add_fast(x_sq, ctx.make(consts::COSPI_KERNEL_TAYLOR_0.to()))
86 }
87
88 /// computes `(sin(pi * x), cos(pi * x))`
89 /// not guaranteed to give correct sign for zero results
90 /// has an error of up to 2ULP
91 pub fn sin_cos_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> (Ctx::VecF16, Ctx::VecF16) {
92 let two_f16: Ctx::VecF16 = ctx.make(2.0.to());
93 let one_half: Ctx::VecF16 = ctx.make(0.5.to());
94 let max_contiguous_integer: Ctx::VecF16 =
95 ctx.make((1u16 << (F16::MANTISSA_FIELD_WIDTH + 1)).to());
96 // if `x` is finite and bigger than `max_contiguous_integer`, then x is an even integer
97 let in_range = x.abs().lt(max_contiguous_integer); // use `lt` so nans are counted as out-of-range
98 let is_finite = x.is_finite();
99 let nan: Ctx::VecF16 = ctx.make(f32::NAN.to());
100 let zero_f16: Ctx::VecF16 = ctx.make(0.to());
101 let one_f16: Ctx::VecF16 = ctx.make(1.to());
102 let zero_i16: Ctx::VecI16 = ctx.make(0.to());
103 let one_i16: Ctx::VecI16 = ctx.make(1.to());
104 let two_i16: Ctx::VecI16 = ctx.make(2.to());
105 let out_of_range_sin = is_finite.select(zero_f16, nan);
106 let out_of_range_cos = is_finite.select(one_f16, nan);
107 let xi = (x * two_f16).round();
108 let xk = x - xi * one_half;
109 let sk = sin_pi_kernel_f16(ctx, xk);
110 let ck = cos_pi_kernel_f16(ctx, xk);
111 let xi = Ctx::VecI16::cvt_from(xi);
112 let bit_0_clear = (xi & one_i16).eq(zero_i16);
113 let st = bit_0_clear.select(sk, ck);
114 let ct = bit_0_clear.select(ck, sk);
115 let s = (xi & two_i16).eq(zero_i16).select(st, -st);
116 let c = ((xi + one_i16) & two_i16).eq(zero_i16).select(ct, -ct);
117 (
118 in_range.select(s, out_of_range_sin),
119 in_range.select(c, out_of_range_cos),
120 )
121 }
122
123 /// computes `sin(pi * x)`
124 /// not guaranteed to give correct sign for zero results
125 /// has an error of up to 2ULP
126 pub fn sin_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
127 sin_cos_pi_f16(ctx, x).0
128 }
129
130 /// computes `cos(pi * x)`
131 /// not guaranteed to give correct sign for zero results
132 /// has an error of up to 2ULP
133 pub fn cos_pi_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
134 sin_cos_pi_f16(ctx, x).1
135 }
136
137 #[cfg(test)]
138 mod tests {
139 use super::*;
140 use crate::{
141 f16::F16,
142 scalar::{Scalar, Value},
143 };
144 use std::f64;
145
146 struct CheckUlpCallbackArg<F, I> {
147 distance_in_ulp: I,
148 x: F,
149 expected: F,
150 result: F,
151 }
152
153 #[track_caller]
154 fn check_ulp_f16(
155 x: F16,
156 is_ok: impl Fn(CheckUlpCallbackArg<F16, u32>) -> bool,
157 fn_f16: impl Fn(F16) -> F16,
158 fn_f64: impl Fn(f64) -> f64,
159 ) {
160 let x_f64: f64 = x.to();
161 let expected_f64 = fn_f64(x_f64);
162 let expected: F16 = expected_f64.to();
163 let result = fn_f16(x);
164 if result == expected {
165 return;
166 }
167 if result.is_nan() && expected.is_nan() {
168 return;
169 }
170 let distance_in_ulp = (expected.to_bits() as i32 - result.to_bits() as i32).unsigned_abs();
171 if !result.is_nan()
172 && !expected.is_nan()
173 && is_ok(CheckUlpCallbackArg {
174 distance_in_ulp,
175 x,
176 expected,
177 result,
178 })
179 {
180 return;
181 }
182 panic!(
183 "error is too big: \
184 x = {x:?} {x_bits:#X}, \
185 result = {result:?} {result_bits:#X}, \
186 expected = {expected:?} {expected_bits:#X}, \
187 distance_in_ulp = {distance_in_ulp}",
188 x = x,
189 x_bits = x.to_bits(),
190 result = result,
191 result_bits = result.to_bits(),
192 expected = expected,
193 expected_bits = expected.to_bits(),
194 distance_in_ulp = distance_in_ulp,
195 );
196 }
197
198 #[test]
199 #[cfg_attr(
200 not(feature = "f16"),
201 should_panic(expected = "f16 feature is not enabled")
202 )]
203 fn test_sin_pi_kernel_f16() {
204 let check = |x| {
205 check_ulp_f16(
206 x,
207 |arg| arg.distance_in_ulp <= if arg.expected == 0.to() { 0 } else { 2 },
208 |x| sin_pi_kernel_f16(Scalar, Value(x)).0,
209 |x| (f64::consts::PI * x).sin(),
210 )
211 };
212 let quarter = F16::to_bits(0.25f32.to());
213 for bits in (0..=quarter).rev() {
214 check(F16::from_bits(bits));
215 check(-F16::from_bits(bits));
216 }
217 }
218
219 #[test]
220 #[cfg_attr(
221 not(feature = "f16"),
222 should_panic(expected = "f16 feature is not enabled")
223 )]
224 fn test_cos_pi_kernel_f16() {
225 let check = |x| {
226 check_ulp_f16(
227 x,
228 |arg| arg.distance_in_ulp <= 2 && arg.result <= 1.to(),
229 |x| cos_pi_kernel_f16(Scalar, Value(x)).0,
230 |x| (f64::consts::PI * x).cos(),
231 )
232 };
233 let quarter = F16::to_bits(0.25f32.to());
234 for bits in (0..=quarter).rev() {
235 check(F16::from_bits(bits));
236 check(-F16::from_bits(bits));
237 }
238 }
239
240 fn sin_cos_pi_check_ulp_callback_f16(arg: CheckUlpCallbackArg<F16, u32>) -> bool {
241 if f32::cvt_from(arg.x) % 0.5 == 0.0 {
242 arg.distance_in_ulp == 0
243 } else {
244 arg.distance_in_ulp <= 2 && arg.result.abs() <= 1.to()
245 }
246 }
247
248 #[test]
249 #[cfg_attr(
250 not(feature = "f16"),
251 should_panic(expected = "f16 feature is not enabled")
252 )]
253 fn test_sin_pi_f16() {
254 for bits in 0..=u16::MAX {
255 check_ulp_f16(
256 F16::from_bits(bits),
257 sin_cos_pi_check_ulp_callback_f16,
258 |x| sin_pi_f16(Scalar, Value(x)).0,
259 |x| (f64::consts::PI * x).sin(),
260 );
261 }
262 }
263
264 #[test]
265 #[cfg_attr(
266 not(feature = "f16"),
267 should_panic(expected = "f16 feature is not enabled")
268 )]
269 fn test_cos_pi_f16() {
270 for bits in 0..=u16::MAX {
271 check_ulp_f16(
272 F16::from_bits(bits),
273 sin_cos_pi_check_ulp_callback_f16,
274 |x| cos_pi_f16(Scalar, Value(x)).0,
275 |x| (f64::consts::PI * x).cos(),
276 );
277 }
278 }
279 }