Skip to main content

elem_simd_math/
common.rs

1//! Type aliases for the `f64` kernel family, and the translation table they follow.
2//!
3//! `sleef-rs` expresses these as extension traits on `Simd<f64, N>`/`Simd<f32, N>`. Extension
4//! traits cannot be implemented for `S::f64s` here (the projection is not a local type, and two
5//! such impls conflict), so they are free functions parameterised by the SIMD token. The
6//! translation table used throughout this crate is:
7//!
8//! | `sleef-rs` | `elem-simd-math` |
9//! |---|---|
10//! | `F64x<N>` | `F64s<S>` |
11//! | `x.mla(y, z)` | `x.mul_add_precise(y, z)` |
12//! | `F64x::splat(v)` | `F64s::<S>::splat(simd, v)` |
13//! | `F64x::poly8(…)` | `poly8(simd, …)` |
14//! | `m.select_splat(l, r)` | `select_splat(m, l, r)` |
15//! | `m.select(l, r)` | `m.select(l, r)` |
16//! | `x.to_bits()` / `F64x::from_bits(b)` | `to_bits(x)` / `from_bits(b)` |
17
18use fearless_simd::Simd;
19
20/// Native-width `f64` vector for SIMD level `S`.
21pub type F64s<S> = <S as Simd>::f64s;
22/// Native-width `u64` vector for SIMD level `S`.
23pub type U64s<S> = <S as Simd>::u64s;
24/// Native-width `i64` vector for SIMD level `S`.
25pub type I64s<S> = <S as Simd>::i64s;
26/// Native-width mask for `f64` lanes.
27pub type M64s<S> = <S as Simd>::mask64s;
28
29/// Native-width `f32` vector for SIMD level `S`.
30pub type F32s<S> = <S as Simd>::f32s;
31/// Native-width `u32` vector for SIMD level `S`.
32pub type U32s<S> = <S as Simd>::u32s;
33/// Native-width `i32` vector for SIMD level `S`.
34pub type I32s<S> = <S as Simd>::i32s;
35/// Native-width mask for `f32` lanes.
36pub type M32s<S> = <S as Simd>::mask32s;
37
38use fearless_simd::prelude::*;
39
40/// `x * y + z`, fused wherever the CPU supports it.
41///
42/// `sleef-rs` selects a formulation at compile time with `cfg(target_feature = "fma")`. This crate
43/// always takes the FMA formulation, which upstream also asserts the documented bounds for, and
44/// evaluates it with a fused operation on every level that has one. Shared by the `f64` and `f32`
45/// kernel families, hence generic over the vector type.
46#[inline(always)]
47pub fn mla<S: Simd, V: SimdFloat<S>>(_simd: S, x: V, y: V, z: V) -> V {
48    x.mul_add_precise(y, z)
49}
50
51/// `x * y - z`, fused wherever the CPU supports it. See [`mla`].
52#[inline(always)]
53pub fn mul_sub<S: Simd, V: SimdFloat<S>>(_simd: S, x: V, y: V, z: V) -> V {
54    x.mul_sub_precise(y, z)
55}
56
57/// `z - x * y`, fused wherever the CPU supports it. See [`mla`].
58#[inline(always)]
59pub fn neg_mul_add<S: Simd, V: SimdFloat<S>>(_simd: S, x: V, y: V, z: V) -> V {
60    (-x).mul_add_precise(y, z)
61}
62
63/// Estrin polynomial evaluation with scalar coefficients, degree 2.
64///
65/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
66/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
67#[allow(clippy::too_many_arguments)]
68#[inline(always)]
69pub fn poly2<S: Simd, V: SimdFloat<S> + Copy>(
70    simd: S,
71    x: V,
72    x2: V,
73    x4: V,
74    c1: V::Element,
75    c0: V::Element,
76) -> V {
77    let _ = (x2, x4);
78    mla(simd, x, V::splat(simd, c1), V::splat(simd, c0))
79}
80
81/// Estrin polynomial evaluation with scalar coefficients, degree 3.
82///
83/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
84/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
85#[allow(clippy::too_many_arguments)]
86#[inline(always)]
87pub fn poly3<S: Simd, V: SimdFloat<S> + Copy>(
88    simd: S,
89    x: V,
90    x2: V,
91    x4: V,
92    c2: V::Element,
93    c1: V::Element,
94    c0: V::Element,
95) -> V {
96    mla(simd, x2, V::splat(simd, c2), poly2(simd, x, x2, x4, c1, c0))
97}
98
99/// Estrin polynomial evaluation with scalar coefficients, degree 4.
100///
101/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
102/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
103#[allow(clippy::too_many_arguments)]
104#[inline(always)]
105pub fn poly4<S: Simd, V: SimdFloat<S> + Copy>(
106    simd: S,
107    x: V,
108    x2: V,
109    x4: V,
110    c3: V::Element,
111    c2: V::Element,
112    c1: V::Element,
113    c0: V::Element,
114) -> V {
115    let _ = x4;
116    mla(
117        simd,
118        x2,
119        mla(simd, x, V::splat(simd, c3), V::splat(simd, c2)),
120        mla(simd, x, V::splat(simd, c1), V::splat(simd, c0)),
121    )
122}
123
124/// Estrin polynomial evaluation with scalar coefficients, degree 5.
125///
126/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
127/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
128#[allow(clippy::too_many_arguments)]
129#[inline(always)]
130pub fn poly5<S: Simd, V: SimdFloat<S> + Copy>(
131    simd: S,
132    x: V,
133    x2: V,
134    x4: V,
135    c4: V::Element,
136    c3: V::Element,
137    c2: V::Element,
138    c1: V::Element,
139    c0: V::Element,
140) -> V {
141    mla(
142        simd,
143        x4,
144        V::splat(simd, c4),
145        poly4(simd, x, x2, x4, c3, c2, c1, c0),
146    )
147}
148
149/// Estrin polynomial evaluation with scalar coefficients, degree 6.
150///
151/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
152/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
153#[allow(clippy::too_many_arguments)]
154#[inline(always)]
155pub fn poly6<S: Simd, V: SimdFloat<S> + Copy>(
156    simd: S,
157    x: V,
158    x2: V,
159    x4: V,
160    c5: V::Element,
161    c4: V::Element,
162    c3: V::Element,
163    c2: V::Element,
164    c1: V::Element,
165    c0: V::Element,
166) -> V {
167    mla(
168        simd,
169        x4,
170        poly2(simd, x, x2, x4, c5, c4),
171        poly4(simd, x, x2, x4, c3, c2, c1, c0),
172    )
173}
174
175/// Estrin polynomial evaluation with scalar coefficients, degree 7.
176///
177/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
178/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
179#[allow(clippy::too_many_arguments)]
180#[inline(always)]
181pub fn poly7<S: Simd, V: SimdFloat<S> + Copy>(
182    simd: S,
183    x: V,
184    x2: V,
185    x4: V,
186    c6: V::Element,
187    c5: V::Element,
188    c4: V::Element,
189    c3: V::Element,
190    c2: V::Element,
191    c1: V::Element,
192    c0: V::Element,
193) -> V {
194    mla(
195        simd,
196        x4,
197        poly3(simd, x, x2, x4, c6, c5, c4),
198        poly4(simd, x, x2, x4, c3, c2, c1, c0),
199    )
200}
201
202/// Estrin polynomial evaluation with scalar coefficients, degree 8.
203///
204/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
205/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
206#[allow(clippy::too_many_arguments)]
207#[inline(always)]
208pub fn poly8<S: Simd, V: SimdFloat<S> + Copy>(
209    simd: S,
210    x: V,
211    x2: V,
212    x4: V,
213    c7: V::Element,
214    c6: V::Element,
215    c5: V::Element,
216    c4: V::Element,
217    c3: V::Element,
218    c2: V::Element,
219    c1: V::Element,
220    c0: V::Element,
221) -> V {
222    mla(
223        simd,
224        x4,
225        poly4(simd, x, x2, x4, c7, c6, c5, c4),
226        poly4(simd, x, x2, x4, c3, c2, c1, c0),
227    )
228}
229
230/// Estrin polynomial evaluation with scalar coefficients, degree 9.
231///
232/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
233/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
234#[allow(clippy::too_many_arguments)]
235#[inline(always)]
236pub fn poly9<S: Simd, V: SimdFloat<S> + Copy>(
237    simd: S,
238    x: V,
239    x2: V,
240    x4: V,
241    x8: V,
242    c8: V::Element,
243    c7: V::Element,
244    c6: V::Element,
245    c5: V::Element,
246    c4: V::Element,
247    c3: V::Element,
248    c2: V::Element,
249    c1: V::Element,
250    c0: V::Element,
251) -> V {
252    mla(
253        simd,
254        x8,
255        V::splat(simd, c8),
256        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
257    )
258}
259
260/// Estrin polynomial evaluation with scalar coefficients, degree 10.
261///
262/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
263/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
264#[allow(clippy::too_many_arguments)]
265#[inline(always)]
266pub fn poly10<S: Simd, V: SimdFloat<S> + Copy>(
267    simd: S,
268    x: V,
269    x2: V,
270    x4: V,
271    x8: V,
272    c9: V::Element,
273    c8: V::Element,
274    c7: V::Element,
275    c6: V::Element,
276    c5: V::Element,
277    c4: V::Element,
278    c3: V::Element,
279    c2: V::Element,
280    c1: V::Element,
281    c0: V::Element,
282) -> V {
283    mla(
284        simd,
285        x8,
286        poly2(simd, x, x2, x4, c9, c8),
287        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
288    )
289}
290
291/// Estrin polynomial evaluation with scalar coefficients, degree 11.
292///
293/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
294/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
295#[allow(clippy::too_many_arguments)]
296#[inline(always)]
297pub fn poly11<S: Simd, V: SimdFloat<S> + Copy>(
298    simd: S,
299    x: V,
300    x2: V,
301    x4: V,
302    x8: V,
303    ca: V::Element,
304    c9: V::Element,
305    c8: V::Element,
306    c7: V::Element,
307    c6: V::Element,
308    c5: V::Element,
309    c4: V::Element,
310    c3: V::Element,
311    c2: V::Element,
312    c1: V::Element,
313    c0: V::Element,
314) -> V {
315    mla(
316        simd,
317        x8,
318        poly3(simd, x, x2, x4, ca, c9, c8),
319        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
320    )
321}
322
323/// Estrin polynomial evaluation with scalar coefficients, degree 12.
324///
325/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
326/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
327#[allow(clippy::too_many_arguments)]
328#[inline(always)]
329pub fn poly12<S: Simd, V: SimdFloat<S> + Copy>(
330    simd: S,
331    x: V,
332    x2: V,
333    x4: V,
334    x8: V,
335    cb: V::Element,
336    ca: V::Element,
337    c9: V::Element,
338    c8: V::Element,
339    c7: V::Element,
340    c6: V::Element,
341    c5: V::Element,
342    c4: V::Element,
343    c3: V::Element,
344    c2: V::Element,
345    c1: V::Element,
346    c0: V::Element,
347) -> V {
348    mla(
349        simd,
350        x8,
351        poly4(simd, x, x2, x4, cb, ca, c9, c8),
352        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
353    )
354}
355
356/// Estrin polynomial evaluation with scalar coefficients, degree 13.
357///
358/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
359/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
360#[allow(clippy::too_many_arguments)]
361#[inline(always)]
362pub fn poly13<S: Simd, V: SimdFloat<S> + Copy>(
363    simd: S,
364    x: V,
365    x2: V,
366    x4: V,
367    x8: V,
368    cc: V::Element,
369    cb: V::Element,
370    ca: V::Element,
371    c9: V::Element,
372    c8: V::Element,
373    c7: V::Element,
374    c6: V::Element,
375    c5: V::Element,
376    c4: V::Element,
377    c3: V::Element,
378    c2: V::Element,
379    c1: V::Element,
380    c0: V::Element,
381) -> V {
382    mla(
383        simd,
384        x8,
385        poly5(simd, x, x2, x4, cc, cb, ca, c9, c8),
386        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
387    )
388}
389
390/// Estrin polynomial evaluation with scalar coefficients, degree 14.
391///
392/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
393/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
394#[allow(clippy::too_many_arguments)]
395#[inline(always)]
396pub fn poly14<S: Simd, V: SimdFloat<S> + Copy>(
397    simd: S,
398    x: V,
399    x2: V,
400    x4: V,
401    x8: V,
402    cd: V::Element,
403    cc: V::Element,
404    cb: V::Element,
405    ca: V::Element,
406    c9: V::Element,
407    c8: V::Element,
408    c7: V::Element,
409    c6: V::Element,
410    c5: V::Element,
411    c4: V::Element,
412    c3: V::Element,
413    c2: V::Element,
414    c1: V::Element,
415    c0: V::Element,
416) -> V {
417    mla(
418        simd,
419        x8,
420        poly6(simd, x, x2, x4, cd, cc, cb, ca, c9, c8),
421        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
422    )
423}
424
425/// Estrin polynomial evaluation with scalar coefficients, degree 15.
426///
427/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
428/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
429#[allow(clippy::too_many_arguments)]
430#[inline(always)]
431pub fn poly15<S: Simd, V: SimdFloat<S> + Copy>(
432    simd: S,
433    x: V,
434    x2: V,
435    x4: V,
436    x8: V,
437    ce: V::Element,
438    cd: V::Element,
439    cc: V::Element,
440    cb: V::Element,
441    ca: V::Element,
442    c9: V::Element,
443    c8: V::Element,
444    c7: V::Element,
445    c6: V::Element,
446    c5: V::Element,
447    c4: V::Element,
448    c3: V::Element,
449    c2: V::Element,
450    c1: V::Element,
451    c0: V::Element,
452) -> V {
453    mla(
454        simd,
455        x8,
456        poly7(simd, x, x2, x4, ce, cd, cc, cb, ca, c9, c8),
457        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
458    )
459}
460
461/// Estrin polynomial evaluation with scalar coefficients, degree 16.
462///
463/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
464/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
465#[allow(clippy::too_many_arguments)]
466#[inline(always)]
467pub fn poly16<S: Simd, V: SimdFloat<S> + Copy>(
468    simd: S,
469    x: V,
470    x2: V,
471    x4: V,
472    x8: V,
473    cf: V::Element,
474    ce: V::Element,
475    cd: V::Element,
476    cc: V::Element,
477    cb: V::Element,
478    ca: V::Element,
479    c9: V::Element,
480    c8: V::Element,
481    c7: V::Element,
482    c6: V::Element,
483    c5: V::Element,
484    c4: V::Element,
485    c3: V::Element,
486    c2: V::Element,
487    c1: V::Element,
488    c0: V::Element,
489) -> V {
490    mla(
491        simd,
492        x8,
493        poly8(simd, x, x2, x4, cf, ce, cd, cc, cb, ca, c9, c8),
494        poly8(simd, x, x2, x4, c7, c6, c5, c4, c3, c2, c1, c0),
495    )
496}
497
498/// Estrin polynomial evaluation with scalar coefficients, degree 17.
499///
500/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
501/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
502#[allow(clippy::too_many_arguments)]
503#[inline(always)]
504pub fn poly17<S: Simd, V: SimdFloat<S> + Copy>(
505    simd: S,
506    x: V,
507    x2: V,
508    x4: V,
509    x8: V,
510    x16: V,
511    d0: V::Element,
512    cf: V::Element,
513    ce: V::Element,
514    cd: V::Element,
515    cc: V::Element,
516    cb: V::Element,
517    ca: V::Element,
518    c9: V::Element,
519    c8: V::Element,
520    c7: V::Element,
521    c6: V::Element,
522    c5: V::Element,
523    c4: V::Element,
524    c3: V::Element,
525    c2: V::Element,
526    c1: V::Element,
527    c0: V::Element,
528) -> V {
529    mla(
530        simd,
531        x16,
532        V::splat(simd, d0),
533        poly16(
534            simd, x, x2, x4, x8, cf, ce, cd, cc, cb, ca, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0,
535        ),
536    )
537}
538
539/// Estrin polynomial evaluation with scalar coefficients, degree 18.
540///
541/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
542/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
543#[allow(clippy::too_many_arguments)]
544#[inline(always)]
545pub fn poly18<S: Simd, V: SimdFloat<S> + Copy>(
546    simd: S,
547    x: V,
548    x2: V,
549    x4: V,
550    x8: V,
551    x16: V,
552    d1: V::Element,
553    d0: V::Element,
554    cf: V::Element,
555    ce: V::Element,
556    cd: V::Element,
557    cc: V::Element,
558    cb: V::Element,
559    ca: V::Element,
560    c9: V::Element,
561    c8: V::Element,
562    c7: V::Element,
563    c6: V::Element,
564    c5: V::Element,
565    c4: V::Element,
566    c3: V::Element,
567    c2: V::Element,
568    c1: V::Element,
569    c0: V::Element,
570) -> V {
571    mla(
572        simd,
573        x16,
574        poly2(simd, x, x2, x4, d1, d0),
575        poly16(
576            simd, x, x2, x4, x8, cf, ce, cd, cc, cb, ca, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0,
577        ),
578    )
579}
580
581/// Estrin polynomial evaluation with scalar coefficients, degree 19.
582///
583/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
584/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
585#[allow(clippy::too_many_arguments)]
586#[inline(always)]
587pub fn poly19<S: Simd, V: SimdFloat<S> + Copy>(
588    simd: S,
589    x: V,
590    x2: V,
591    x4: V,
592    x8: V,
593    x16: V,
594    d2: V::Element,
595    d1: V::Element,
596    d0: V::Element,
597    cf: V::Element,
598    ce: V::Element,
599    cd: V::Element,
600    cc: V::Element,
601    cb: V::Element,
602    ca: V::Element,
603    c9: V::Element,
604    c8: V::Element,
605    c7: V::Element,
606    c6: V::Element,
607    c5: V::Element,
608    c4: V::Element,
609    c3: V::Element,
610    c2: V::Element,
611    c1: V::Element,
612    c0: V::Element,
613) -> V {
614    mla(
615        simd,
616        x16,
617        poly3(simd, x, x2, x4, d2, d1, d0),
618        poly16(
619            simd, x, x2, x4, x8, cf, ce, cd, cc, cb, ca, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0,
620        ),
621    )
622}
623
624/// Estrin polynomial evaluation with scalar coefficients, degree 20.
625///
626/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
627/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
628#[allow(clippy::too_many_arguments)]
629#[inline(always)]
630pub fn poly20<S: Simd, V: SimdFloat<S> + Copy>(
631    simd: S,
632    x: V,
633    x2: V,
634    x4: V,
635    x8: V,
636    x16: V,
637    d3: V::Element,
638    d2: V::Element,
639    d1: V::Element,
640    d0: V::Element,
641    cf: V::Element,
642    ce: V::Element,
643    cd: V::Element,
644    cc: V::Element,
645    cb: V::Element,
646    ca: V::Element,
647    c9: V::Element,
648    c8: V::Element,
649    c7: V::Element,
650    c6: V::Element,
651    c5: V::Element,
652    c4: V::Element,
653    c3: V::Element,
654    c2: V::Element,
655    c1: V::Element,
656    c0: V::Element,
657) -> V {
658    mla(
659        simd,
660        x16,
661        poly4(simd, x, x2, x4, d3, d2, d1, d0),
662        poly16(
663            simd, x, x2, x4, x8, cf, ce, cd, cc, cb, ca, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0,
664        ),
665    )
666}
667
668/// Estrin polynomial evaluation with scalar coefficients, degree 21.
669///
670/// Every `polyN` takes the same `(simd, x, x2, x4 [, x8] [, x16], coefficients...)`
671/// shape so the nesting below stays uniform; the higher powers are unused below degree 5.
672#[allow(clippy::too_many_arguments)]
673#[inline(always)]
674pub fn poly21<S: Simd, V: SimdFloat<S> + Copy>(
675    simd: S,
676    x: V,
677    x2: V,
678    x4: V,
679    x8: V,
680    x16: V,
681    d4: V::Element,
682    d3: V::Element,
683    d2: V::Element,
684    d1: V::Element,
685    d0: V::Element,
686    cf: V::Element,
687    ce: V::Element,
688    cd: V::Element,
689    cc: V::Element,
690    cb: V::Element,
691    ca: V::Element,
692    c9: V::Element,
693    c8: V::Element,
694    c7: V::Element,
695    c6: V::Element,
696    c5: V::Element,
697    c4: V::Element,
698    c3: V::Element,
699    c2: V::Element,
700    c1: V::Element,
701    c0: V::Element,
702) -> V {
703    mla(
704        simd,
705        x16,
706        poly5(simd, x, x2, x4, d4, d3, d2, d1, d0),
707        poly16(
708            simd, x, x2, x4, x8, cf, ce, cd, cc, cb, ca, c9, c8, c7, c6, c5, c4, c3, c2, c1, c0,
709        ),
710    )
711}