Coverage Report

Created: 2026-08-13 08:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.30/src/sincospi.rs
Line
Count
Source
1
/*
2
 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3
 * //
4
 * // Redistribution and use in source and binary forms, with or without modification,
5
 * // are permitted provided that the following conditions are met:
6
 * //
7
 * // 1.  Redistributions of source code must retain the above copyright notice, this
8
 * // list of conditions and the following disclaimer.
9
 * //
10
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11
 * // this list of conditions and the following disclaimer in the documentation
12
 * // and/or other materials provided with the distribution.
13
 * //
14
 * // 3.  Neither the name of the copyright holder nor the names of its
15
 * // contributors may be used to endorse or promote products derived from
16
 * // this software without specific prior written permission.
17
 * //
18
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
use crate::common::{dd_fmla, dyad_fmla, f_fmla, is_odd_integer};
30
use crate::double_double::DoubleDouble;
31
use crate::polyeval::{f_polyeval3, f_polyeval4};
32
use crate::rounding::CpuRound;
33
use crate::sin::SinCos;
34
use crate::sincospi_tables::SINPI_K_PI_OVER_64;
35
36
/**
37
Cospi(x) on [0; 0.000244140625]
38
39
Generated by Sollya:
40
```text
41
d = [0, 0.000244140625];
42
f_cos = cos(y*pi);
43
Q = fpminimax(f_cos, [|0, 2, 4, 6, 8, 10|], [|107...|], d, relative, floating);
44
```
45
46
See ./notes/cospi_zero_dd.sollya
47
**/
48
#[cold]
49
#[inline(always)]
50
0
fn as_cospi_zero<B: SinCosPiBackend>(x: f64, backend: &B) -> f64 {
51
    const C: [(u64, u64); 5] = [
52
        (0xbcb692b71366cc04, 0xc013bd3cc9be45de),
53
        (0xbcb32b33fb803bd5, 0x40103c1f081b5ac4),
54
        (0xbc9f5b752e98b088, 0xbff55d3c7e3cbff9),
55
        (0x3c30023d540b9350, 0x3fce1f506446cb66),
56
        (0x3c1a5d47937787d2, 0xbf8a9b062a36ba1c),
57
    ];
58
0
    let x2 = backend.exact_mult(x, x);
59
0
    let mut p = backend.quick_mul_add(
60
0
        x2,
61
0
        DoubleDouble::from_bit_pair(C[3]),
62
0
        DoubleDouble::from_bit_pair(C[3]),
63
    );
64
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[2]));
65
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[1]));
66
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[0]));
67
0
    p = backend.mul_add_f64(x2, p, 1.);
68
0
    p.to_f64()
69
0
}
Unexecuted instantiation: pxfm::sincospi::as_cospi_zero::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::as_cospi_zero::<pxfm::sincospi::GenSinCosPiBackend>
70
71
/**
72
Sinpi on range [0.0, 0.03515625]
73
74
Generated poly by Sollya:
75
```text
76
d = [0, 0.03515625];
77
78
f_sin = sin(y*pi)/y;
79
Q = fpminimax(f_sin, [|0, 2, 4, 6, 8, 10|], [|107...|], d, relative, floating);
80
```
81
See ./notes/sinpi_zero_dd.sollya
82
**/
83
#[cold]
84
#[inline(always)]
85
0
fn as_sinpi_zero<B: SinCosPiBackend>(x: f64, backend: &B) -> f64 {
86
    const C: [(u64, u64); 6] = [
87
        (0x3ca1a626311d9056, 0x400921fb54442d18),
88
        (0x3cb055f12c462211, 0xc014abbce625be53),
89
        (0xbc9789ea63534250, 0x400466bc6775aae1),
90
        (0xbc78b86de6962184, 0xbfe32d2cce62874e),
91
        (0x3c4eddf7cd887302, 0x3fb507833e2b781f),
92
        (0x3bf180c9d4af2894, 0xbf7e2ea4e143707e),
93
    ];
94
0
    let x2 = backend.exact_mult(x, x);
95
0
    let mut p = backend.quick_mul_add(
96
0
        x2,
97
0
        DoubleDouble::from_bit_pair(C[5]),
98
0
        DoubleDouble::from_bit_pair(C[4]),
99
    );
100
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[3]));
101
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[2]));
102
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[1]));
103
0
    p = backend.quick_mul_add(x2, p, DoubleDouble::from_bit_pair(C[0]));
104
0
    p = backend.quick_mult_f64(p, x);
105
0
    p.to_f64()
106
0
}
Unexecuted instantiation: pxfm::sincospi::as_sinpi_zero::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::as_sinpi_zero::<pxfm::sincospi::GenSinCosPiBackend>
107
108
// Return k and y, where
109
// k = round(x * 64 / pi) and y = (x * 64 / pi) - k.
110
#[inline]
111
0
pub(crate) fn reduce_pi_64(x: f64) -> (f64, i64) {
112
0
    let kd = (x * 64.).cpu_round();
113
0
    let y = dd_fmla(kd, -1. / 64., x);
114
0
    (y, unsafe {
115
0
        kd.to_int_unchecked::<i64>() // indeterminate values is always filtered out before this call, as well only lowest bits are used
116
0
    })
117
0
}
118
119
// Return k and y, where
120
// k = round(x * 64 / pi) and y = (x * 64 / pi) - k.
121
#[inline(always)]
122
#[allow(unused)]
123
0
pub(crate) fn reduce_pi_64_fma(x: f64) -> (f64, i64) {
124
0
    let kd = (x * 64.).round();
125
0
    let y = f64::mul_add(kd, -1. / 64., x);
126
0
    (y, unsafe {
127
0
        kd.to_int_unchecked::<i64>() // indeterminate values is always filtered out before this call, as well only lowest bits are used
128
0
    })
129
0
}
130
131
pub(crate) trait SinCosPiBackend {
132
    fn fma(&self, x: f64, y: f64, z: f64) -> f64;
133
    fn dd_fma(&self, x: f64, y: f64, z: f64) -> f64;
134
    fn dyad_fma(&self, x: f64, y: f64, z: f64) -> f64;
135
    fn polyeval3(&self, x: f64, a0: f64, a1: f64, a2: f64) -> f64;
136
    fn arg_reduce_pi_64(&self, x: f64) -> (f64, i64);
137
    fn quick_mult_f64(&self, x: DoubleDouble, y: f64) -> DoubleDouble;
138
    fn quick_mult(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble;
139
    fn odd_integer(&self, x: f64) -> bool;
140
    fn div(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble;
141
    fn mul_add_f64(&self, a: DoubleDouble, b: DoubleDouble, c: f64) -> DoubleDouble;
142
    fn quick_mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble;
143
    fn mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble;
144
    fn exact_mult(&self, x: f64, y: f64) -> DoubleDouble;
145
}
146
147
pub(crate) struct GenSinCosPiBackend {}
148
149
impl SinCosPiBackend for GenSinCosPiBackend {
150
    #[inline(always)]
151
0
    fn fma(&self, x: f64, y: f64, z: f64) -> f64 {
152
0
        f_fmla(x, y, z)
153
0
    }
154
    #[inline(always)]
155
0
    fn dd_fma(&self, x: f64, y: f64, z: f64) -> f64 {
156
0
        dd_fmla(x, y, z)
157
0
    }
158
    #[inline(always)]
159
0
    fn dyad_fma(&self, x: f64, y: f64, z: f64) -> f64 {
160
0
        dyad_fmla(x, y, z)
161
0
    }
162
    #[inline(always)]
163
0
    fn polyeval3(&self, x: f64, a0: f64, a1: f64, a2: f64) -> f64 {
164
        use crate::polyeval::f_polyeval3;
165
0
        f_polyeval3(x, a0, a1, a2)
166
0
    }
167
    #[inline(always)]
168
0
    fn arg_reduce_pi_64(&self, x: f64) -> (f64, i64) {
169
0
        reduce_pi_64(x)
170
0
    }
171
    #[inline(always)]
172
0
    fn quick_mult_f64(&self, x: DoubleDouble, y: f64) -> DoubleDouble {
173
0
        DoubleDouble::quick_mult_f64(x, y)
174
0
    }
175
    #[inline(always)]
176
0
    fn quick_mult(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble {
177
0
        DoubleDouble::quick_mult(x, y)
178
0
    }
179
180
    #[inline(always)]
181
0
    fn odd_integer(&self, x: f64) -> bool {
182
0
        is_odd_integer(x)
183
0
    }
184
185
    #[inline(always)]
186
0
    fn div(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble {
187
0
        DoubleDouble::div(x, y)
188
0
    }
189
190
    #[inline(always)]
191
0
    fn mul_add_f64(&self, a: DoubleDouble, b: DoubleDouble, c: f64) -> DoubleDouble {
192
0
        DoubleDouble::mul_add_f64(a, b, c)
193
0
    }
194
195
    #[inline(always)]
196
0
    fn quick_mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble {
197
0
        DoubleDouble::quick_mul_add(a, b, c)
198
0
    }
199
200
    #[inline(always)]
201
0
    fn mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble {
202
0
        DoubleDouble::mul_add(a, b, c)
203
0
    }
204
205
    #[inline(always)]
206
0
    fn exact_mult(&self, x: f64, y: f64) -> DoubleDouble {
207
0
        DoubleDouble::from_exact_mult(x, y)
208
0
    }
209
}
210
211
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
212
pub(crate) struct FmaSinCosPiBackend {}
213
214
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
215
impl SinCosPiBackend for FmaSinCosPiBackend {
216
    #[inline(always)]
217
0
    fn fma(&self, x: f64, y: f64, z: f64) -> f64 {
218
0
        f64::mul_add(x, y, z)
219
0
    }
220
    #[inline(always)]
221
0
    fn dd_fma(&self, x: f64, y: f64, z: f64) -> f64 {
222
0
        f64::mul_add(x, y, z)
223
0
    }
224
    #[inline(always)]
225
0
    fn dyad_fma(&self, x: f64, y: f64, z: f64) -> f64 {
226
0
        f64::mul_add(x, y, z)
227
0
    }
228
    #[inline(always)]
229
0
    fn polyeval3(&self, x: f64, a0: f64, a1: f64, a2: f64) -> f64 {
230
        use crate::polyeval::d_polyeval3;
231
0
        d_polyeval3(x, a0, a1, a2)
232
0
    }
233
    #[inline(always)]
234
0
    fn arg_reduce_pi_64(&self, x: f64) -> (f64, i64) {
235
0
        reduce_pi_64_fma(x)
236
0
    }
237
    #[inline(always)]
238
0
    fn quick_mult_f64(&self, x: DoubleDouble, y: f64) -> DoubleDouble {
239
0
        DoubleDouble::quick_mult_f64_fma(x, y)
240
0
    }
241
    #[inline(always)]
242
0
    fn quick_mult(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble {
243
0
        DoubleDouble::quick_mult_fma(x, y)
244
0
    }
245
246
    #[inline(always)]
247
0
    fn odd_integer(&self, x: f64) -> bool {
248
0
        is_odd_integer(x)
249
0
    }
250
251
    #[inline(always)]
252
0
    fn div(&self, x: DoubleDouble, y: DoubleDouble) -> DoubleDouble {
253
0
        DoubleDouble::div_fma(x, y)
254
0
    }
255
256
    #[inline(always)]
257
0
    fn mul_add_f64(&self, a: DoubleDouble, b: DoubleDouble, c: f64) -> DoubleDouble {
258
0
        DoubleDouble::mul_add_f64_fma(a, b, c)
259
0
    }
260
261
    #[inline(always)]
262
0
    fn quick_mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble {
263
0
        DoubleDouble::quick_mul_add_fma(a, b, c)
264
0
    }
265
266
    #[inline(always)]
267
0
    fn mul_add(&self, a: DoubleDouble, b: DoubleDouble, c: DoubleDouble) -> DoubleDouble {
268
0
        DoubleDouble::mul_add_fma(a, b, c)
269
0
    }
270
271
    #[inline(always)]
272
0
    fn exact_mult(&self, x: f64, y: f64) -> DoubleDouble {
273
0
        DoubleDouble::from_exact_mult_fma(x, y)
274
0
    }
275
}
276
277
#[inline(always)]
278
0
pub(crate) fn sincospi_eval<B: SinCosPiBackend>(x: f64, backend: &B) -> SinCos {
279
0
    let x2 = x * x;
280
    /*
281
        sinpi(pi*x) poly generated by Sollya:
282
        d = [0, 0.0078128];
283
        f_sin = sin(y*pi)/y;
284
        Q = fpminimax(f_sin, [|0, 2, 4, 6|], [|107, D...|], d, relative, floating);
285
        See ./notes/sinpi.sollya
286
    */
287
0
    let sin_lop = backend.polyeval3(
288
0
        x2,
289
0
        f64::from_bits(0xc014abbce625be4d),
290
0
        f64::from_bits(0x400466bc6767f259),
291
0
        f64::from_bits(0xbfe32d176b0b3baf),
292
0
    ) * x2;
293
    // We're splitting polynomial in two parts, since first term dominates
294
    // we compute: (a0_lo + a0_hi) * x + x * (a1 * x^2 + a2 + x^4) ...
295
0
    let sin_lo = backend.dd_fma(f64::from_bits(0x3ca1a5c04563817a), x, sin_lop * x);
296
0
    let sin_hi = x * f64::from_bits(0x400921fb54442d18);
297
298
    /*
299
       cospi(pi*x) poly generated by Sollya:
300
       d = [0, 0.015625];
301
       f_cos = cos(y*pi);
302
       Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|107, D...|], d, relative, floating);
303
       See ./notes/cospi.sollya
304
    */
305
0
    let p = backend.polyeval3(
306
0
        x2,
307
0
        f64::from_bits(0xc013bd3cc9be45cf),
308
0
        f64::from_bits(0x40103c1f08085ad1),
309
0
        f64::from_bits(0xbff55d1e43463fc3),
310
    );
311
312
    // We're splitting polynomial in two parts, since first term dominates
313
    // we compute: (a0_lo + a0_hi) + (a1 * x^2 + a2 + x^4)...
314
0
    let cos_lo = backend.dd_fma(p, x2, f64::from_bits(0xbbdf72adefec0800));
315
0
    let cos_hi = f64::from_bits(0x3ff0000000000000);
316
317
0
    let err = backend.fma(
318
0
        x2,
319
0
        f64::from_bits(0x3cb0000000000000), // 2^-52
320
0
        f64::from_bits(0x3c40000000000000), // 2^-59
321
    );
322
0
    SinCos {
323
0
        v_sin: DoubleDouble::from_exact_add(sin_hi, sin_lo),
324
0
        v_cos: DoubleDouble::from_exact_add(cos_hi, cos_lo),
325
0
        err,
326
0
    }
327
0
}
Unexecuted instantiation: pxfm::sincospi::sincospi_eval::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sincospi_eval::<pxfm::sincospi::GenSinCosPiBackend>
328
329
#[inline(always)]
330
0
pub(crate) fn sincospi_eval_dd<B: SinCosPiBackend>(x: f64, backend: &B) -> SinCos {
331
0
    let x2 = backend.exact_mult(x, x);
332
    // Sin coeffs
333
    // poly sin(pi*x) generated by Sollya:
334
    // d = [0, 0.0078128];
335
    // f_sin = sin(y*pi)/y;
336
    // Q = fpminimax(f_sin, [|0, 2, 4, 6, 8|], [|107...|], d, relative, floating);
337
    // see ./notes/sinpi_dd.sollya
338
    const SC: [(u64, u64); 5] = [
339
        (0x3ca1a626330ccf19, 0x400921fb54442d18),
340
        (0x3cb05540f6323de9, 0xc014abbce625be53),
341
        (0xbc9050fdd1229756, 0x400466bc6775aadf),
342
        (0xbc780d406f3472e8, 0xbfe32d2cce5a7bf1),
343
        (0x3c4cfcf8b6b817f2, 0x3fb5077069d8a182),
344
    ];
345
346
0
    let mut sin_y = backend.quick_mul_add(
347
0
        x2,
348
0
        DoubleDouble::from_bit_pair(SC[4]),
349
0
        DoubleDouble::from_bit_pair(SC[3]),
350
    );
351
0
    sin_y = backend.quick_mul_add(x2, sin_y, DoubleDouble::from_bit_pair(SC[2]));
352
0
    sin_y = backend.quick_mul_add(x2, sin_y, DoubleDouble::from_bit_pair(SC[1]));
353
0
    sin_y = backend.quick_mul_add(x2, sin_y, DoubleDouble::from_bit_pair(SC[0]));
354
0
    sin_y = backend.quick_mult_f64(sin_y, x);
355
356
    // Cos coeffs
357
    // d = [0, 0.0078128];
358
    // f_cos = cos(y*pi);
359
    // Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|107...|], d, relative, floating);
360
    // See ./notes/cospi_dd.sollya
361
    const CC: [(u64, u64); 5] = [
362
        (0xbaaa70a580000000, 0x3ff0000000000000),
363
        (0xbcb69211d8dd1237, 0xc013bd3cc9be45de),
364
        (0xbcbd96cfd637eeb7, 0x40103c1f081b5abf),
365
        (0x3c994d75c577f029, 0xbff55d3c7e2e4ba5),
366
        (0xbc5c542d998a4e48, 0x3fce1f2f5f747411),
367
    ];
368
369
0
    let mut cos_y = backend.quick_mul_add(
370
0
        x2,
371
0
        DoubleDouble::from_bit_pair(CC[4]),
372
0
        DoubleDouble::from_bit_pair(CC[3]),
373
    );
374
0
    cos_y = backend.quick_mul_add(x2, cos_y, DoubleDouble::from_bit_pair(CC[2]));
375
0
    cos_y = backend.quick_mul_add(x2, cos_y, DoubleDouble::from_bit_pair(CC[1]));
376
0
    cos_y = backend.quick_mul_add(x2, cos_y, DoubleDouble::from_bit_pair(CC[0]));
377
0
    SinCos {
378
0
        v_sin: sin_y,
379
0
        v_cos: cos_y,
380
0
        err: 0.,
381
0
    }
382
0
}
Unexecuted instantiation: pxfm::sincospi::sincospi_eval_dd::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sincospi_eval_dd::<pxfm::sincospi::GenSinCosPiBackend>
383
384
#[cold]
385
#[inline(always)]
386
0
fn sinpi_dd<B: SinCosPiBackend>(
387
0
    x: f64,
388
0
    sin_k: DoubleDouble,
389
0
    cos_k: DoubleDouble,
390
0
    backend: &B,
391
0
) -> f64 {
392
0
    let r_sincos = sincospi_eval_dd(x, backend);
393
0
    let cos_k_sin_y = backend.quick_mult(cos_k, r_sincos.v_sin);
394
0
    let rr = backend.mul_add(sin_k, r_sincos.v_cos, cos_k_sin_y);
395
0
    rr.to_f64()
396
0
}
Unexecuted instantiation: pxfm::sincospi::sinpi_dd::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sinpi_dd::<pxfm::sincospi::GenSinCosPiBackend>
397
398
#[cold]
399
#[inline(always)]
400
0
fn sincospi_dd<B: SinCosPiBackend>(
401
0
    x: f64,
402
0
    sin_sin_k: DoubleDouble,
403
0
    sin_cos_k: DoubleDouble,
404
0
    cos_sin_k: DoubleDouble,
405
0
    cos_cos_k: DoubleDouble,
406
0
    backend: &B,
407
0
) -> (f64, f64) {
408
0
    let r_sincos = sincospi_eval_dd(x, backend);
409
410
0
    let cos_k_sin_y = backend.quick_mult(sin_cos_k, r_sincos.v_sin);
411
0
    let rr_sin = backend.mul_add(sin_sin_k, r_sincos.v_cos, cos_k_sin_y);
412
413
0
    let cos_k_sin_y = backend.quick_mult(cos_cos_k, r_sincos.v_sin);
414
0
    let rr_cos = backend.mul_add(cos_sin_k, r_sincos.v_cos, cos_k_sin_y);
415
416
0
    (rr_sin.to_f64(), rr_cos.to_f64())
417
0
}
Unexecuted instantiation: pxfm::sincospi::sincospi_dd::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sincospi_dd::<pxfm::sincospi::GenSinCosPiBackend>
418
419
// [sincospi_eval] gives precision around 2^-66 what is not enough for DD case this method gives 2^-84
420
#[inline]
421
0
fn sincospi_eval_extended(x: f64) -> SinCos {
422
0
    let x2 = DoubleDouble::from_exact_mult(x, x);
423
    /*
424
        sinpi(pi*x) poly generated by Sollya:
425
        d = [0, 0.0078128];
426
        f_sin = sin(y*pi)/y;
427
        Q = fpminimax(f_sin, [|0, 2, 4, 6, 8|], [|107, 107, D...|], d, relative, floating);
428
        See ./notes/sinpi.sollya
429
    */
430
0
    let sin_lop = f_polyeval3(
431
0
        x2.hi,
432
0
        f64::from_bits(0x400466bc67763662),
433
0
        f64::from_bits(0xbfe32d2cce5aad86),
434
0
        f64::from_bits(0x3fb5077099a1f35b),
435
    );
436
0
    let mut v_sin = DoubleDouble::mul_f64_add(
437
0
        x2,
438
0
        sin_lop,
439
0
        DoubleDouble::from_bit_pair((0x3cb0553d6ee5e8ec, 0xc014abbce625be53)),
440
    );
441
0
    v_sin = DoubleDouble::mul_add(
442
0
        x2,
443
0
        v_sin,
444
0
        DoubleDouble::from_bit_pair((0x3ca1a626330dd130, 0x400921fb54442d18)),
445
0
    );
446
0
    v_sin = DoubleDouble::quick_mult_f64(v_sin, x);
447
448
    /*
449
       cospi(pi*x) poly generated by Sollya:
450
       d = [0, 0.015625];
451
       f_cos = cos(y*pi);
452
       Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|107, 107, D...|], d, relative, floating);
453
       See ./notes/cospi_fast_dd.sollya
454
    */
455
0
    let p = f_polyeval3(
456
0
        x2.hi,
457
0
        f64::from_bits(0x40103c1f081b5abf),
458
0
        f64::from_bits(0xbff55d3c7e2edd89),
459
0
        f64::from_bits(0x3fce1f2fd9d79484),
460
    );
461
462
0
    let mut v_cos = DoubleDouble::mul_f64_add(
463
0
        x2,
464
0
        p,
465
0
        DoubleDouble::from_bit_pair((0xbcb69236a9b3ed73, 0xc013bd3cc9be45de)),
466
    );
467
0
    v_cos = DoubleDouble::mul_add_f64(x2, v_cos, f64::from_bits(0x3ff0000000000000));
468
469
0
    SinCos {
470
0
        v_sin: DoubleDouble::from_exact_add(v_sin.hi, v_sin.lo),
471
0
        v_cos: DoubleDouble::from_exact_add(v_cos.hi, v_cos.lo),
472
0
        err: 0.,
473
0
    }
474
0
}
475
476
0
pub(crate) fn f_fast_sinpi_dd(x: f64) -> DoubleDouble {
477
0
    let ix = x.to_bits();
478
0
    let ax = ix & 0x7fff_ffff_ffff_ffff;
479
0
    if ax == 0 {
480
0
        return DoubleDouble::new(0., 0.);
481
0
    }
482
0
    let e: i32 = (ax >> 52) as i32;
483
0
    let m0 = (ix & 0x000fffffffffffff) | (1u64 << 52);
484
0
    let sgn: i64 = (ix as i64) >> 63;
485
0
    let m = ((m0 as i64) ^ sgn).wrapping_sub(sgn);
486
0
    let mut s: i32 = 1063i32.wrapping_sub(e);
487
0
    if s < 0 {
488
0
        s = -s - 1;
489
0
        if s > 10 {
490
0
            return DoubleDouble::new(0., f64::copysign(0.0, x));
491
0
        }
492
0
        let iq: u64 = (m as u64).wrapping_shl(s as u32);
493
0
        if (iq & 2047) == 0 {
494
0
            return DoubleDouble::new(0., f64::copysign(0.0, x));
495
0
        }
496
0
    }
497
498
0
    if ax <= 0x3fa2000000000000u64 {
499
        // |x| <= 0.03515625
500
        const PI: DoubleDouble = DoubleDouble::new(
501
            f64::from_bits(0x3ca1a62633145c07),
502
            f64::from_bits(0x400921fb54442d18),
503
        );
504
505
0
        if ax < 0x3c90000000000000 {
506
            // for x near zero, sinpi(x) = pi*x + O(x^3), thus worst cases are those
507
            // of the function pi*x, and if x is a worst case, then 2*x is another
508
            // one in the next binade. For this reason, worst cases are only included
509
            // for the binade [2^-1022, 2^-1021). For larger binades,
510
            // up to [2^-54,2^-53), worst cases should be deduced by multiplying
511
            // by some power of 2.
512
0
            if ax < 0x0350000000000000 {
513
0
                let t = x * f64::from_bits(0x4690000000000000);
514
0
                let z = DoubleDouble::quick_mult_f64(PI, t);
515
0
                let r = z.to_f64();
516
0
                let rs = r * f64::from_bits(0x3950000000000000);
517
0
                let rt = rs * f64::from_bits(0x4690000000000000);
518
0
                return DoubleDouble::new(
519
                    0.,
520
0
                    dyad_fmla((z.hi - rt) + z.lo, f64::from_bits(0x3950000000000000), rs),
521
                );
522
0
            }
523
0
            let z = DoubleDouble::quick_mult_f64(PI, x);
524
0
            return z;
525
0
        }
526
527
        /*
528
           Poly generated by Sollya:
529
           d = [0, 0.03515625];
530
           f_sin = sin(y*pi)/y;
531
           Q = fpminimax(f_sin, [|0, 2, 4, 6, 8, 10|], [|107, 107, D...|], d, relative, floating);
532
533
           See ./notes/sinpi_zero_fast_dd.sollya
534
        */
535
        const C: [u64; 4] = [
536
            0xbfe32d2cce62bd85,
537
            0x3fb50783487eb73d,
538
            0xbf7e3074f120ad1f,
539
            0x3f3e8d9011340e5a,
540
        ];
541
542
0
        let x2 = DoubleDouble::from_exact_mult(x, x);
543
544
        const C_PI: DoubleDouble =
545
            DoubleDouble::from_bit_pair((0x3ca1a626331457a4, 0x400921fb54442d18));
546
547
0
        let p = f_polyeval4(
548
0
            x2.hi,
549
0
            f64::from_bits(C[0]),
550
0
            f64::from_bits(C[1]),
551
0
            f64::from_bits(C[2]),
552
0
            f64::from_bits(C[3]),
553
        );
554
0
        let mut r = DoubleDouble::mul_f64_add(
555
0
            x2,
556
0
            p,
557
0
            DoubleDouble::from_bit_pair((0xbc96dd7ae221e58c, 0x400466bc6775aae2)),
558
        );
559
0
        r = DoubleDouble::mul_add(
560
0
            x2,
561
0
            r,
562
0
            DoubleDouble::from_bit_pair((0x3cb05511c8a6c478, 0xc014abbce625be53)),
563
0
        );
564
0
        r = DoubleDouble::mul_add(r, x2, C_PI);
565
0
        r = DoubleDouble::quick_mult_f64(r, x);
566
0
        let k = DoubleDouble::from_exact_add(r.hi, r.lo);
567
0
        return k;
568
0
    }
569
570
0
    let si = e.wrapping_sub(1011);
571
0
    if si >= 0 && (m0.wrapping_shl(si.wrapping_add(1) as u32)) == 0 {
572
        // x is integer or half-integer
573
0
        if (m0.wrapping_shl(si as u32)) == 0 {
574
0
            return DoubleDouble::new(0., f64::copysign(0.0, x)); // x is integer
575
0
        }
576
0
        let t = (m0.wrapping_shl((si - 1) as u32)) >> 63;
577
        // t = 0 if |x| = 1/2 mod 2, t = 1 if |x| = 3/2 mod 2
578
0
        return DoubleDouble::new(
579
            0.,
580
0
            if t == 0 {
581
0
                f64::copysign(1.0, x)
582
            } else {
583
0
                -f64::copysign(1.0, x)
584
            },
585
        );
586
0
    }
587
588
0
    let (y, k) = reduce_pi_64(x);
589
590
    // // cos(k * pi/64) = sin(k * pi/64 + pi/2) = sin((k + 32) * pi/64).
591
0
    let sin_k = DoubleDouble::from_bit_pair(SINPI_K_PI_OVER_64[((k as u64) & 127) as usize]);
592
0
    let cos_k = DoubleDouble::from_bit_pair(
593
0
        SINPI_K_PI_OVER_64[((k as u64).wrapping_add(32) & 127) as usize],
594
    );
595
596
0
    let r_sincos = sincospi_eval_extended(y);
597
598
0
    let sin_k_cos_y = DoubleDouble::quick_mult(sin_k, r_sincos.v_cos);
599
0
    let cos_k_sin_y = DoubleDouble::quick_mult(cos_k, r_sincos.v_sin);
600
601
    // sin_k_cos_y is always >> cos_k_sin_y
602
0
    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
603
0
    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
604
0
    DoubleDouble::from_exact_add(rr.hi, rr.lo)
605
0
}
606
607
#[inline(always)]
608
0
fn sinpi_gen_impl<B: SinCosPiBackend>(x: f64, backend: B) -> f64 {
609
0
    let ix = x.to_bits();
610
0
    let ax = ix & 0x7fff_ffff_ffff_ffff;
611
0
    if ax == 0 {
612
0
        return x;
613
0
    }
614
0
    let e: i32 = (ax >> 52) as i32;
615
0
    let m0 = (ix & 0x000fffffffffffff) | (1u64 << 52);
616
0
    let sgn: i64 = (ix as i64) >> 63;
617
0
    let m = ((m0 as i64) ^ sgn).wrapping_sub(sgn);
618
0
    let mut s: i32 = 1063i32.wrapping_sub(e);
619
0
    if s < 0 {
620
0
        if e == 0x7ff {
621
0
            if (ix << 12) == 0 {
622
0
                return f64::NAN;
623
0
            }
624
0
            return x + x; // case x=NaN
625
0
        }
626
0
        s = -s - 1;
627
0
        if s > 10 {
628
0
            return f64::copysign(0.0, x);
629
0
        }
630
0
        let iq: u64 = (m as u64).wrapping_shl(s as u32);
631
0
        if (iq & 2047) == 0 {
632
0
            return f64::copysign(0.0, x);
633
0
        }
634
0
    }
635
636
0
    if ax <= 0x3fa2000000000000u64 {
637
        // |x| <= 0.03515625
638
        const PI: DoubleDouble = DoubleDouble::new(
639
            f64::from_bits(0x3ca1a62633145c07),
640
            f64::from_bits(0x400921fb54442d18),
641
        );
642
643
0
        if ax < 0x3c90000000000000 {
644
            // for x near zero, sinpi(x) = pi*x + O(x^3), thus worst cases are those
645
            // of the function pi*x, and if x is a worst case, then 2*x is another
646
            // one in the next binade. For this reason, worst cases are only included
647
            // for the binade [2^-1022, 2^-1021). For larger binades,
648
            // up to [2^-54,2^-53), worst cases should be deduced by multiplying
649
            // by some power of 2.
650
0
            if ax < 0x0350000000000000 {
651
0
                let t = x * f64::from_bits(0x4690000000000000);
652
0
                let z = backend.quick_mult_f64(PI, t);
653
0
                let r = z.to_f64();
654
0
                let rs = r * f64::from_bits(0x3950000000000000);
655
0
                let rt = rs * f64::from_bits(0x4690000000000000);
656
0
                return backend.dyad_fma(
657
0
                    (z.hi - rt) + z.lo,
658
0
                    f64::from_bits(0x3950000000000000),
659
0
                    rs,
660
                );
661
0
            }
662
0
            let z = backend.quick_mult_f64(PI, x);
663
0
            return z.to_f64();
664
0
        }
665
666
        /*
667
           Poly generated by Sollya:
668
           d = [0, 0.03515625];
669
           f_sin = sin(y*pi)/y;
670
           Q = fpminimax(f_sin, [|0, 2, 4, 6, 8, 10|], [|107, D...|], d, relative, floating);
671
672
           See ./notes/sinpi_zero.sollya
673
        */
674
675
0
        let x2 = x * x;
676
0
        let x3 = x2 * x;
677
0
        let x4 = x2 * x2;
678
679
0
        let eps = x * backend.fma(
680
0
            x2,
681
0
            f64::from_bits(0x3d00000000000000), // 2^-47
682
0
            f64::from_bits(0x3bd0000000000000), // 2^-66
683
0
        );
684
685
        const C: [u64; 4] = [
686
            0xc014abbce625be51,
687
            0x400466bc67754b46,
688
            0xbfe32d2cc12a51f4,
689
            0x3fb5060540058476,
690
        ];
691
692
        const C_PI: DoubleDouble =
693
            DoubleDouble::from_bit_pair((0x3ca1a67088eb1a46, 0x400921fb54442d18));
694
695
0
        let mut z = backend.quick_mult_f64(C_PI, x);
696
697
0
        let zl0 = backend.fma(x2, f64::from_bits(C[1]), f64::from_bits(C[0]));
698
0
        let zl1 = backend.fma(x2, f64::from_bits(C[3]), f64::from_bits(C[2]));
699
700
0
        z.lo = backend.fma(x3, backend.fma(x4, zl1, zl0), z.lo);
701
0
        let lb = z.hi + (z.lo - eps);
702
0
        let ub = z.hi + (z.lo + eps);
703
0
        if lb == ub {
704
0
            return lb;
705
0
        }
706
0
        return as_sinpi_zero(x, &backend);
707
0
    }
708
709
0
    let si = e.wrapping_sub(1011);
710
0
    if si >= 0 && (m0.wrapping_shl(si.wrapping_add(1) as u32)) == 0 {
711
        // x is integer or half-integer
712
0
        if (m0.wrapping_shl(si as u32)) == 0 {
713
0
            return f64::copysign(0.0, x); // x is integer
714
0
        }
715
0
        let t = (m0.wrapping_shl((si - 1) as u32)) >> 63;
716
        // t = 0 if |x| = 1/2 mod 2, t = 1 if |x| = 3/2 mod 2
717
0
        return if t == 0 {
718
0
            f64::copysign(1.0, x)
719
        } else {
720
0
            -f64::copysign(1.0, x)
721
        };
722
0
    }
723
724
0
    let (y, k) = backend.arg_reduce_pi_64(x);
725
726
    // cos(k * pi/64) = sin(k * pi/64 + pi/2) = sin((k + 32) * pi/64).
727
0
    let sin_k = DoubleDouble::from_bit_pair(SINPI_K_PI_OVER_64[((k as u64) & 127) as usize]);
728
0
    let cos_k = DoubleDouble::from_bit_pair(
729
0
        SINPI_K_PI_OVER_64[((k as u64).wrapping_add(32) & 127) as usize],
730
    );
731
732
0
    let r_sincos = sincospi_eval(y, &backend);
733
734
0
    let sin_k_cos_y = backend.quick_mult(sin_k, r_sincos.v_cos);
735
0
    let cos_k_sin_y = backend.quick_mult(cos_k, r_sincos.v_sin);
736
737
    // sin_k_cos_y is always >> cos_k_sin_y
738
0
    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
739
0
    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
740
741
0
    let ub = rr.hi + (rr.lo + r_sincos.err); // (rr.lo + ERR);
742
0
    let lb = rr.hi + (rr.lo - r_sincos.err); // (rr.lo - ERR);
743
744
0
    if ub == lb {
745
0
        return rr.to_f64();
746
0
    }
747
0
    sinpi_dd(y, sin_k, cos_k, &backend)
748
0
}
Unexecuted instantiation: pxfm::sincospi::sinpi_gen_impl::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sinpi_gen_impl::<pxfm::sincospi::GenSinCosPiBackend>
749
750
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
751
#[target_feature(enable = "avx", enable = "fma")]
752
0
unsafe fn sinpi_fma_impl(x: f64) -> f64 {
753
0
    sinpi_gen_impl(x, FmaSinCosPiBackend {})
754
0
}
755
756
/// Computes sin(PI*x)
757
///
758
/// Max ULP 0.5
759
0
pub fn f_sinpi(x: f64) -> f64 {
760
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
761
    {
762
        sinpi_gen_impl(x, GenSinCosPiBackend {})
763
    }
764
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
765
    {
766
        use std::sync::OnceLock;
767
        static EXECUTOR: OnceLock<unsafe fn(f64) -> f64> = OnceLock::new();
768
0
        let q = EXECUTOR.get_or_init(|| {
769
0
            if std::arch::is_x86_feature_detected!("avx")
770
0
                && std::arch::is_x86_feature_detected!("fma")
771
            {
772
0
                sinpi_fma_impl
773
            } else {
774
0
                fn def_sinpi(x: f64) -> f64 {
775
0
                    sinpi_gen_impl(x, GenSinCosPiBackend {})
776
0
                }
777
0
                def_sinpi
778
            }
779
0
        });
780
0
        unsafe { q(x) }
781
    }
782
0
}
783
784
#[inline(always)]
785
0
fn cospi_gen_impl<B: SinCosPiBackend>(x: f64, backend: B) -> f64 {
786
0
    let ix = x.to_bits();
787
0
    let ax = ix & 0x7fff_ffff_ffff_ffff;
788
0
    if ax == 0 {
789
0
        return 1.0;
790
0
    }
791
0
    let e: i32 = (ax >> 52) as i32;
792
    // e is the unbiased exponent, we have 2^(e-1023) <= |x| < 2^(e-1022)
793
0
    let m: i64 = ((ix & 0x000fffffffffffff) | (1u64 << 52)) as i64;
794
0
    let mut s = 1063i32.wrapping_sub(e); // 2^(40-s) <= |x| < 2^(41-s)
795
0
    if s < 0 {
796
        // |x| >= 2^41
797
0
        if e == 0x7ff {
798
            // NaN or Inf
799
0
            if ix.wrapping_shl(12) == 0 {
800
0
                return f64::NAN;
801
0
            }
802
0
            return x + x; // NaN
803
0
        }
804
0
        s = -s - 1; // now 2^(41+s) <= |x| < 2^(42+s)
805
0
        if s > 11 {
806
0
            return 1.0;
807
0
        } // |x| >= 2^53
808
0
        let iq: u64 = (m as u64).wrapping_shl(s as u32).wrapping_add(1024);
809
0
        if (iq & 2047) == 0 {
810
0
            return 0.0;
811
0
        }
812
0
    }
813
0
    if ax <= 0x3f30000000000000u64 {
814
        // |x| <= 2^-12, |x| <= 0.000244140625
815
0
        if ax <= 0x3e2ccf6429be6621u64 {
816
0
            return 1.0 - f64::from_bits(0x3c80000000000000);
817
0
        }
818
0
        let x2 = x * x;
819
0
        let x4 = x2 * x2;
820
0
        let eps = x2 * f64::from_bits(0x3cfa000000000000);
821
822
        /*
823
            Generated by Sollya:
824
            d = [0, 0.000244140625];
825
            f_cos = cos(y*pi);
826
            Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|107, 107, D...|], d, relative, floating);
827
828
            See ./notes/cospi.sollya
829
        */
830
831
        const C: [u64; 4] = [
832
            0xc013bd3cc9be45de,
833
            0x40103c1f081b5ac4,
834
            0xbff55d3c7ff79b60,
835
            0x3fd24c7b6f7d0690,
836
        ];
837
838
0
        let p0 = backend.fma(x2, f64::from_bits(C[3]), f64::from_bits(C[2]));
839
0
        let p1 = backend.fma(x2, f64::from_bits(C[1]), f64::from_bits(C[0]));
840
841
0
        let p = x2 * backend.fma(x4, p0, p1);
842
0
        let lb = (p - eps) + 1.;
843
0
        let ub = (p + eps) + 1.;
844
0
        if lb == ub {
845
0
            return lb;
846
0
        }
847
0
        return as_cospi_zero(x, &backend);
848
0
    }
849
850
0
    let si: i32 = e.wrapping_sub(1011);
851
0
    if si >= 0 && ((m as u64).wrapping_shl(si as u32) ^ 0x8000000000000000u64) == 0 {
852
0
        return 0.0;
853
0
    }
854
855
0
    let (y, k) = backend.arg_reduce_pi_64(x);
856
857
    // cos(k * pi/64) = sin(k * pi/64 + pi/2) = sin((k + 32) * pi/64).
858
0
    let msin_k = DoubleDouble::from_bit_pair(
859
0
        SINPI_K_PI_OVER_64[((k as u64).wrapping_add(64) & 127) as usize],
860
    );
861
0
    let cos_k = DoubleDouble::from_bit_pair(
862
0
        SINPI_K_PI_OVER_64[((k as u64).wrapping_add(32) & 127) as usize],
863
    );
864
865
0
    let r_sincos = sincospi_eval(y, &backend);
866
867
0
    let cos_k_cos_y = backend.quick_mult(r_sincos.v_cos, cos_k);
868
0
    let cos_k_msin_y = backend.quick_mult(r_sincos.v_sin, msin_k);
869
870
    // cos_k_cos_y is always >> cos_k_msin_y
871
0
    let mut rr = DoubleDouble::from_exact_add(cos_k_cos_y.hi, cos_k_msin_y.hi);
872
0
    rr.lo += cos_k_cos_y.lo + cos_k_msin_y.lo;
873
874
0
    let ub = rr.hi + (rr.lo + r_sincos.err); // (rr.lo + ERR);
875
0
    let lb = rr.hi + (rr.lo - r_sincos.err); // (rr.lo - ERR);
876
877
0
    if ub == lb {
878
0
        return rr.to_f64();
879
0
    }
880
0
    sinpi_dd(y, cos_k, msin_k, &backend)
881
0
}
Unexecuted instantiation: pxfm::sincospi::cospi_gen_impl::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::cospi_gen_impl::<pxfm::sincospi::GenSinCosPiBackend>
882
883
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
884
#[target_feature(enable = "avx", enable = "fma")]
885
0
unsafe fn cospi_fma_impl(x: f64) -> f64 {
886
0
    cospi_gen_impl(x, FmaSinCosPiBackend {})
887
0
}
888
889
/// Computes cos(PI*x)
890
///
891
/// Max found ULP 0.5
892
0
pub fn f_cospi(x: f64) -> f64 {
893
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
894
    {
895
        cospi_gen_impl(x, GenSinCosPiBackend {})
896
    }
897
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
898
    {
899
        use std::sync::OnceLock;
900
        static EXECUTOR: OnceLock<unsafe fn(f64) -> f64> = OnceLock::new();
901
0
        let q = EXECUTOR.get_or_init(|| {
902
0
            if std::arch::is_x86_feature_detected!("avx")
903
0
                && std::arch::is_x86_feature_detected!("fma")
904
            {
905
0
                cospi_fma_impl
906
            } else {
907
0
                fn def_cospi(x: f64) -> f64 {
908
0
                    cospi_gen_impl(x, GenSinCosPiBackend {})
909
0
                }
910
0
                def_cospi
911
            }
912
0
        });
913
0
        unsafe { q(x) }
914
    }
915
0
}
916
917
#[inline(always)]
918
0
fn sincospi_gen_impl<B: SinCosPiBackend>(x: f64, backend: B) -> (f64, f64) {
919
0
    let ix = x.to_bits();
920
0
    let ax = ix & 0x7fff_ffff_ffff_ffff;
921
0
    if ax == 0 {
922
0
        return (x, 1.0);
923
0
    }
924
0
    let e: i32 = (ax >> 52) as i32;
925
    // e is the unbiased exponent, we have 2^(e-1023) <= |x| < 2^(e-1022)
926
0
    let m0 = (ix & 0x000fffffffffffff) | (1u64 << 52);
927
0
    let m: i64 = ((ix & 0x000fffffffffffff) | (1u64 << 52)) as i64;
928
0
    let mut s = 1063i32.wrapping_sub(e); // 2^(40-s) <= |x| < 2^(41-s)
929
0
    if s < 0 {
930
        // |x| >= 2^41
931
0
        if e == 0x7ff {
932
            // NaN or Inf
933
0
            if ix.wrapping_shl(12) == 0 {
934
0
                return (f64::NAN, f64::NAN);
935
0
            }
936
0
            return (x + x, x + x); // NaN
937
0
        }
938
0
        s = -s - 1;
939
0
        if s > 10 {
940
            static CF: [f64; 2] = [1., -1.];
941
0
            let is_odd = backend.odd_integer(f64::from_bits(ax));
942
0
            let cos_x = CF[is_odd as usize];
943
0
            return (f64::copysign(0.0, x), cos_x);
944
0
        } // |x| >= 2^53
945
0
        let iq: u64 = (m as u64).wrapping_shl(s as u32);
946
947
        // sinpi = 0 when multiple of 2048
948
0
        let sin_zero = (iq & 2047) == 0;
949
950
        // cospi = 0 when offset-by-half multiple of 2048
951
0
        let cos_zero = ((m as u64).wrapping_shl(s as u32).wrapping_add(1024) & 2047) == 0;
952
953
0
        if sin_zero && cos_zero {
954
0
            // both zero (only possible if NaN or something degenerate)
955
0
        } else if sin_zero {
956
            static CF: [f64; 2] = [1., -1.];
957
0
            let is_odd = backend.odd_integer(f64::from_bits(ax));
958
0
            let cos_x = CF[is_odd as usize];
959
0
            return (0.0, cos_x); // sin = 0, cos = ±1
960
0
        } else if cos_zero {
961
            // x = k / 2 * PI
962
0
            let si = e.wrapping_sub(1011);
963
0
            let t = (m0.wrapping_shl((si - 1) as u32)) >> 63;
964
            // making sin decision based on quadrant
965
0
            return if t == 0 {
966
0
                (f64::copysign(1.0, x), 0.0)
967
            } else {
968
0
                (-f64::copysign(1.0, x), 0.0)
969
            }; // sin = ±1, cos = 0
970
0
        }
971
0
    }
972
973
0
    if ax <= 0x3f30000000000000u64 {
974
        // |x| <= 2^-12, |x| <= 0.000244140625
975
0
        if ax <= 0x3c90000000000000u64 {
976
            const PI: DoubleDouble = DoubleDouble::new(
977
                f64::from_bits(0x3ca1a62633145c07),
978
                f64::from_bits(0x400921fb54442d18),
979
            );
980
0
            let sin_x = if ax < 0x0350000000000000 {
981
0
                let t = x * f64::from_bits(0x4690000000000000);
982
0
                let z = backend.quick_mult_f64(PI, t);
983
0
                let r = z.to_f64();
984
0
                let rs = r * f64::from_bits(0x3950000000000000);
985
0
                let rt = rs * f64::from_bits(0x4690000000000000);
986
0
                backend.dyad_fma((z.hi - rt) + z.lo, f64::from_bits(0x3950000000000000), rs)
987
            } else {
988
0
                let z = backend.quick_mult_f64(PI, x);
989
0
                z.to_f64()
990
            };
991
0
            return (sin_x, 1.0 - f64::from_bits(0x3c80000000000000));
992
0
        }
993
0
        let x2 = x * x;
994
0
        let x4 = x2 * x2;
995
0
        let cos_eps = x2 * f64::from_bits(0x3cfa000000000000);
996
997
        /*
998
            Generated by Sollya:
999
            d = [0, 0.000244140625];
1000
            f_cos = cos(y*pi);
1001
            Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|107, 107, D...|], d, relative, floating);
1002
1003
            See ./notes/cospi.sollya
1004
        */
1005
1006
        const COS_C: [u64; 4] = [
1007
            0xc013bd3cc9be45de,
1008
            0x40103c1f081b5ac4,
1009
            0xbff55d3c7ff79b60,
1010
            0x3fd24c7b6f7d0690,
1011
        ];
1012
1013
0
        let p0 = backend.fma(x2, f64::from_bits(COS_C[3]), f64::from_bits(COS_C[2]));
1014
0
        let p1 = backend.fma(x2, f64::from_bits(COS_C[1]), f64::from_bits(COS_C[0]));
1015
1016
0
        let p = x2 * backend.fma(x4, p0, p1);
1017
0
        let cos_lb = (p - cos_eps) + 1.;
1018
0
        let cos_ub = (p + cos_eps) + 1.;
1019
0
        let cos_x = if cos_lb == cos_ub {
1020
0
            cos_lb
1021
        } else {
1022
0
            as_cospi_zero(x, &backend)
1023
        };
1024
1025
        /*
1026
            Poly generated by Sollya:
1027
            d = [0, 0.03515625];
1028
            f_sin = sin(y*pi)/y;
1029
            Q = fpminimax(f_sin, [|0, 2, 4, 6, 8, 10|], [|107, D...|], d, relative, floating);
1030
1031
            See ./notes/sinpi_zero.sollya
1032
        */
1033
1034
        const SIN_C: [u64; 4] = [
1035
            0xc014abbce625be51,
1036
            0x400466bc67754b46,
1037
            0xbfe32d2cc12a51f4,
1038
            0x3fb5060540058476,
1039
        ];
1040
1041
        const C_PI: DoubleDouble =
1042
            DoubleDouble::from_bit_pair((0x3ca1a67088eb1a46, 0x400921fb54442d18));
1043
1044
0
        let mut z = backend.quick_mult_f64(C_PI, x);
1045
1046
0
        let x3 = x2 * x;
1047
1048
0
        let zl0 = backend.fma(x2, f64::from_bits(SIN_C[1]), f64::from_bits(SIN_C[0]));
1049
0
        let zl1 = backend.fma(x2, f64::from_bits(SIN_C[3]), f64::from_bits(SIN_C[2]));
1050
1051
0
        let sin_eps = x * backend.fma(
1052
0
            x2,
1053
0
            f64::from_bits(0x3d00000000000000), // 2^-47
1054
0
            f64::from_bits(0x3bd0000000000000), // 2^-66
1055
0
        );
1056
1057
0
        z.lo = backend.fma(x3, backend.fma(x4, zl1, zl0), z.lo);
1058
0
        let sin_lb = z.hi + (z.lo - sin_eps);
1059
0
        let sin_ub = z.hi + (z.lo + sin_eps);
1060
0
        let sin_x = if sin_lb == sin_ub {
1061
0
            sin_lb
1062
        } else {
1063
0
            as_sinpi_zero(x, &backend)
1064
        };
1065
0
        return (sin_x, cos_x);
1066
0
    }
1067
1068
0
    let si = e.wrapping_sub(1011);
1069
0
    if si >= 0 && (m0.wrapping_shl(si.wrapping_add(1) as u32)) == 0 {
1070
        // x is integer or half-integer
1071
0
        if (m0.wrapping_shl(si as u32)) == 0 {
1072
            static CF: [f64; 2] = [1., -1.];
1073
0
            let is_odd = backend.odd_integer(f64::from_bits(ax));
1074
0
            let cos_x = CF[is_odd as usize];
1075
0
            return (f64::copysign(0.0, x), cos_x); // x is integer
1076
0
        }
1077
        // x is half-integer
1078
0
        let t = (m0.wrapping_shl((si - 1) as u32)) >> 63;
1079
        // t = 0 if |x| = 1/2 mod 2, t = 1 if |x| = 3/2 mod 2
1080
0
        return if t == 0 {
1081
0
            (f64::copysign(1.0, x), 0.0)
1082
        } else {
1083
0
            (-f64::copysign(1.0, x), 0.0)
1084
        };
1085
0
    }
1086
1087
0
    let (y, k) = backend.arg_reduce_pi_64(x);
1088
1089
    // cos(k * pi/64) = sin(k * pi/64 + pi/2) = sin((k + 32) * pi/64).
1090
0
    let sin_k = DoubleDouble::from_bit_pair(SINPI_K_PI_OVER_64[((k as u64) & 127) as usize]);
1091
0
    let cos_k = DoubleDouble::from_bit_pair(
1092
0
        SINPI_K_PI_OVER_64[((k as u64).wrapping_add(32) & 127) as usize],
1093
    );
1094
0
    let msin_k = -sin_k;
1095
1096
0
    let r_sincos = sincospi_eval(y, &backend);
1097
1098
0
    let sin_k_cos_y = backend.quick_mult(sin_k, r_sincos.v_cos);
1099
0
    let cos_k_sin_y = backend.quick_mult(cos_k, r_sincos.v_sin);
1100
1101
0
    let cos_k_cos_y = backend.quick_mult(r_sincos.v_cos, cos_k);
1102
0
    let msin_k_sin_y = backend.quick_mult(r_sincos.v_sin, msin_k);
1103
1104
    // sin_k_cos_y is always >> cos_k_sin_y
1105
0
    let mut rr_sin = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
1106
0
    rr_sin.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
1107
1108
0
    let sin_ub = rr_sin.hi + (rr_sin.lo + r_sincos.err); // (rr.lo + ERR);
1109
0
    let sin_lb = rr_sin.hi + (rr_sin.lo - r_sincos.err); // (rr.lo - ERR);
1110
1111
0
    let mut rr_cos = DoubleDouble::from_exact_add(cos_k_cos_y.hi, msin_k_sin_y.hi);
1112
0
    rr_cos.lo += cos_k_cos_y.lo + msin_k_sin_y.lo;
1113
1114
0
    let cos_ub = rr_cos.hi + (rr_cos.lo + r_sincos.err); // (rr.lo + ERR);
1115
0
    let cos_lb = rr_cos.hi + (rr_cos.lo - r_sincos.err); // (rr.lo - ERR);
1116
1117
0
    if sin_ub == sin_lb && cos_lb == cos_ub {
1118
0
        return (rr_sin.to_f64(), rr_cos.to_f64());
1119
0
    }
1120
1121
0
    sincospi_dd(y, sin_k, cos_k, cos_k, msin_k, &backend)
1122
0
}
Unexecuted instantiation: pxfm::sincospi::sincospi_gen_impl::<pxfm::sincospi::FmaSinCosPiBackend>
Unexecuted instantiation: pxfm::sincospi::sincospi_gen_impl::<pxfm::sincospi::GenSinCosPiBackend>
1123
1124
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1125
#[target_feature(enable = "avx", enable = "fma")]
1126
0
unsafe fn sincospi_fma_impl(x: f64) -> (f64, f64) {
1127
0
    sincospi_gen_impl(x, FmaSinCosPiBackend {})
1128
0
}
1129
1130
/// Computes sin(PI*x) and cos(PI*x)
1131
///
1132
/// Max found ULP 0.5
1133
0
pub fn f_sincospi(x: f64) -> (f64, f64) {
1134
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
1135
    {
1136
        sincospi_gen_impl(x, GenSinCosPiBackend {})
1137
    }
1138
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1139
    {
1140
        use std::sync::OnceLock;
1141
        static EXECUTOR: OnceLock<unsafe fn(f64) -> (f64, f64)> = OnceLock::new();
1142
0
        let q = EXECUTOR.get_or_init(|| {
1143
0
            if std::arch::is_x86_feature_detected!("avx")
1144
0
                && std::arch::is_x86_feature_detected!("fma")
1145
            {
1146
0
                sincospi_fma_impl
1147
            } else {
1148
0
                fn def_sincospi(x: f64) -> (f64, f64) {
1149
0
                    sincospi_gen_impl(x, GenSinCosPiBackend {})
1150
0
                }
1151
0
                def_sincospi
1152
            }
1153
0
        });
1154
0
        unsafe { q(x) }
1155
    }
1156
0
}
1157
1158
#[cfg(test)]
1159
mod tests {
1160
    use super::*;
1161
1162
    #[test]
1163
    fn test_sinpi() {
1164
        assert_eq!(f_sinpi(262143.50006870925), -0.9999999767029883);
1165
        assert_eq!(f_sinpi(7124076477593855.), 0.);
1166
        assert_eq!(f_sinpi(-11235582092889474000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.), -0.);
1167
        assert_eq!(f_sinpi(-2.7430620343968443e303), -0.0);
1168
        assert_eq!(f_sinpi(0.00003195557007273919), 0.00010039138401316004);
1169
        assert_eq!(f_sinpi(-0.038357843137253766), -0.12021328061499763);
1170
        assert_eq!(f_sinpi(1.0156097449358867), -0.04901980680173724);
1171
        assert_eq!(f_sinpi(74.8593852519989), 0.42752597787896457);
1172
        assert_eq!(f_sinpi(0.500091552734375), 0.9999999586369661);
1173
        assert_eq!(f_sinpi(0.5307886532952182), 0.9953257438106751);
1174
        assert_eq!(f_sinpi(3.1415926535897936), -0.43030121700009316);
1175
        assert_eq!(f_sinpi(-0.5305172747685276), -0.9954077178320563);
1176
        assert_eq!(f_sinpi(-0.03723630312089732), -0.1167146713267927);
1177
        assert_eq!(
1178
            f_sinpi(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022946074000077123),
1179
            0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007208721750737005
1180
        );
1181
        assert_eq!(
1182
            f_sinpi(0.000000000000000000000000000000000000007413093439574428),
1183
            2.3288919890141717e-38
1184
        );
1185
        assert_eq!(f_sinpi(0.0031909299901270445), 0.0100244343161398578);
1186
        assert_eq!(f_sinpi(0.11909245901270445), 0.36547215190661003);
1187
        assert_eq!(f_sinpi(0.99909245901270445), 0.0028511202357662186);
1188
        assert!(f_sinpi(f64::INFINITY).is_nan());
1189
        assert!(f_sinpi(f64::NEG_INFINITY).is_nan());
1190
        assert!(f_sinpi(f64::NAN).is_nan());
1191
    }
1192
1193
    #[test]
1194
    fn test_sincospi() {
1195
        let v0 = f_sincospi(1.0156097449358867);
1196
        assert_eq!(v0.0, f_sinpi(1.0156097449358867));
1197
        assert_eq!(v0.1, f_cospi(1.0156097449358867));
1198
1199
        let v1 = f_sincospi(4503599627370496.);
1200
        assert_eq!(v1.0, f_sinpi(4503599627370496.));
1201
        assert_eq!(v1.1, f_cospi(4503599627370496.));
1202
1203
        let v1 = f_sincospi(-108.);
1204
        assert_eq!(v1.0, f_sinpi(-108.));
1205
        assert_eq!(v1.1, f_cospi(-108.));
1206
1207
        let v1 = f_sincospi(3.);
1208
        assert_eq!(v1.0, f_sinpi(3.));
1209
        assert_eq!(v1.1, f_cospi(3.));
1210
1211
        let v1 = f_sincospi(13.5);
1212
        assert_eq!(v1.0, f_sinpi(13.5));
1213
        assert_eq!(v1.1, f_cospi(13.5));
1214
1215
        let v1 = f_sincospi(7124076477593855.);
1216
        assert_eq!(v1.0, f_sinpi(7124076477593855.));
1217
        assert_eq!(v1.1, f_cospi(7124076477593855.));
1218
1219
        let v1 = f_sincospi(2533419148247186.5);
1220
        assert_eq!(v1.0, f_sinpi(2533419148247186.5));
1221
        assert_eq!(v1.1, f_cospi(2533419148247186.5));
1222
1223
        let v1 = f_sincospi(2.2250653705240375E-308);
1224
        assert_eq!(v1.0, f_sinpi(2.2250653705240375E-308));
1225
        assert_eq!(v1.1, f_cospi(2.2250653705240375E-308));
1226
1227
        let v1 = f_sincospi(2533420818956351.);
1228
        assert_eq!(v1.0, f_sinpi(2533420818956351.));
1229
        assert_eq!(v1.1, f_cospi(2533420818956351.));
1230
1231
        let v1 = f_sincospi(2533822406803233.5);
1232
        assert_eq!(v1.0, f_sinpi(2533822406803233.5));
1233
        assert_eq!(v1.1, f_cospi(2533822406803233.5));
1234
1235
        let v1 = f_sincospi(-3040685725640478.5);
1236
        assert_eq!(v1.0, f_sinpi(-3040685725640478.5));
1237
        assert_eq!(v1.1, f_cospi(-3040685725640478.5));
1238
1239
        let v1 = f_sincospi(2533419148247186.5);
1240
        assert_eq!(v1.0, f_sinpi(2533419148247186.5));
1241
        assert_eq!(v1.1, f_cospi(2533419148247186.5));
1242
1243
        let v1 = f_sincospi(2533420819267583.5);
1244
        assert_eq!(v1.0, f_sinpi(2533420819267583.5));
1245
        assert_eq!(v1.1, f_cospi(2533420819267583.5));
1246
1247
        let v1 = f_sincospi(6979704728846336.);
1248
        assert_eq!(v1.0, f_sinpi(6979704728846336.));
1249
        assert_eq!(v1.1, f_cospi(6979704728846336.));
1250
1251
        let v1 = f_sincospi(7124076477593855.);
1252
        assert_eq!(v1.0, f_sinpi(7124076477593855.));
1253
        assert_eq!(v1.1, f_cospi(7124076477593855.));
1254
1255
        let v1 = f_sincospi(-0.00000000002728839192371484);
1256
        assert_eq!(v1.0, f_sinpi(-0.00000000002728839192371484));
1257
        assert_eq!(v1.1, f_cospi(-0.00000000002728839192371484));
1258
1259
        let v1 = f_sincospi(0.00002465398569495569);
1260
        assert_eq!(v1.0, f_sinpi(0.00002465398569495569));
1261
        assert_eq!(v1.1, f_cospi(0.00002465398569495569));
1262
    }
1263
1264
    #[test]
1265
    fn test_cospi() {
1266
        assert_eq!(0.9999497540959953, f_cospi(0.0031909299901270445));
1267
        assert_eq!(0.9308216542079669, f_cospi(0.11909299901270445));
1268
        assert_eq!(-0.1536194873288318, f_cospi(0.54909299901270445));
1269
        assert!(f_cospi(f64::INFINITY).is_nan());
1270
        assert!(f_cospi(f64::NEG_INFINITY).is_nan());
1271
        assert!(f_cospi(f64::NAN).is_nan());
1272
    }
1273
}