Skip to main content

wide/
f64x4_.rs

1use super::*;
2
3pick! {
4  if #[cfg(target_feature="avx")] {
5    /// A SIMD vector with four elements of type [`f64`].
6    ///
7    /// See the [crate level documentation] for more information about SIMD
8    /// vectors.
9    ///
10    /// [crate level documentation]: crate
11    #[derive(Default, Clone, Copy, PartialEq)]
12    #[repr(C, align(32))]
13    pub struct f64x4 { pub(crate) avx: m256d }
14  } else {
15    /// A SIMD vector with four elements of type [`f64`].
16    ///
17    /// See the [crate level documentation] for more information about SIMD
18    /// vectors.
19    ///
20    /// [crate level documentation]: crate
21    #[derive(Default, Clone, Copy, PartialEq)]
22    #[repr(C, align(32))]
23    pub struct f64x4 { pub(crate) a: f64x2, pub(crate) b: f64x2 }
24  }
25}
26
27macro_rules! const_f64_as_f64x4 {
28  ($i:ident, $f:expr) => {
29    #[allow(non_upper_case_globals)]
30    pub const $i: f64x4 = f64x4::new([$f; 4]);
31  };
32}
33
34impl_simd_float! {
35  unsafe {
36    T = f64,
37    N = 4,
38    Simd = f64x4,
39    IntSimd = i64x4,
40    UintT = u64,
41    UintSimd = u64x4,
42    optional_type_x86_inner { X86Inner = __m256d },
43    optional_type_arm_inner {},
44    optional_type_wasm_inner {},
45  }
46  old_powf_simd_fn_name = pow_f64x4,
47
48  #[inline]
49  fn neg(self) -> Self::Output {
50    pick! {
51      if #[cfg(target_feature="avx")] {
52        Self { avx: bitxor_m256d(self.avx, Self::splat(-0.0).avx) }
53      } else {
54        Self {
55          a : self.a.neg(),
56          b : self.b.neg(),
57        }
58      }
59    }
60  }
61
62  #[inline]
63  fn not(self) -> Self {
64    pick! {
65      if #[cfg(target_feature="avx")] {
66        Self { avx: self.avx.not()  }
67      } else {
68        Self {
69          a : self.a.not(),
70          b : self.b.not(),
71        }
72      }
73    }
74  }
75
76  #[inline]
77  fn add(self, rhs: Self) -> Self::Output {
78    pick! {
79      if #[cfg(target_feature="avx")] {
80        Self { avx: add_m256d(self.avx, rhs.avx) }
81      } else {
82        Self {
83          a : self.a.add(rhs.a),
84          b : self.b.add(rhs.b),
85        }
86      }
87    }
88  }
89
90  #[inline]
91  fn sub(self, rhs: Self) -> Self::Output {
92    pick! {
93      if #[cfg(target_feature="avx")] {
94        Self { avx: sub_m256d(self.avx, rhs.avx) }
95      } else {
96        Self {
97          a : self.a.sub(rhs.a),
98          b : self.b.sub(rhs.b),
99        }
100      }
101    }
102  }
103
104  #[inline]
105  fn mul(self, rhs: Self) -> Self::Output {
106    pick! {
107      if #[cfg(target_feature="avx")] {
108        Self { avx: mul_m256d(self.avx, rhs.avx) }
109      } else {
110        Self {
111          a : self.a.mul(rhs.a),
112          b : self.b.mul(rhs.b),
113        }
114      }
115    }
116  }
117
118  #[inline]
119  fn div(self, rhs: Self) -> Self::Output {
120    pick! {
121      if #[cfg(target_feature="avx")] {
122        Self { avx: div_m256d(self.avx, rhs.avx) }
123      } else {
124        Self {
125          a : self.a.div(rhs.a),
126          b : self.b.div(rhs.b),
127        }
128      }
129    }
130  }
131
132  #[inline]
133  fn rem(self, rhs: Self) -> Self::Output {
134    Self::new([
135      self.to_array()[0] % rhs.to_array()[0],
136      self.to_array()[1] % rhs.to_array()[1],
137      self.to_array()[2] % rhs.to_array()[2],
138      self.to_array()[3] % rhs.to_array()[3],
139    ])
140  }
141
142  #[inline]
143  fn bitand(self, rhs: Self) -> Self::Output {
144    pick! {
145      if #[cfg(target_feature="avx")] {
146        Self { avx: bitand_m256d(self.avx, rhs.avx) }
147      } else {
148        Self {
149          a : self.a.bitand(rhs.a),
150          b : self.b.bitand(rhs.b),
151        }
152      }
153    }
154  }
155
156  #[inline]
157  fn bitor(self, rhs: Self) -> Self::Output {
158    pick! {
159      if #[cfg(target_feature="avx")] {
160        Self { avx: bitor_m256d(self.avx, rhs.avx) }
161      } else {
162        Self {
163          a : self.a.bitor(rhs.a),
164          b : self.b.bitor(rhs.b),
165        }
166      }
167    }
168  }
169
170  #[inline]
171  fn bitxor(self, rhs: Self) -> Self::Output {
172    pick! {
173      if #[cfg(target_feature="avx")] {
174        Self { avx: bitxor_m256d(self.avx, rhs.avx) }
175      } else {
176        Self {
177          a : self.a.bitxor(rhs.a),
178          b : self.b.bitxor(rhs.b),
179        }
180      }
181    }
182  }
183
184  #[inline]
185  fn simd_eq(self, rhs: Self) -> Self::Output {
186    pick! {
187      if #[cfg(target_feature="avx")]{
188        Self { avx: cmp_op_mask_m256d::<{cmp_op!(EqualOrdered)}>(self.avx, rhs.avx) }
189      } else {
190        Self {
191          a : self.a.simd_eq(rhs.a),
192          b : self.b.simd_eq(rhs.b),
193        }
194      }
195    }
196  }
197
198  #[inline]
199  fn simd_ne(self, rhs: Self) -> Self::Output {
200    pick! {
201      if #[cfg(target_feature="avx")]{
202        Self { avx: cmp_op_mask_m256d::<{cmp_op!(NotEqualUnordered)}>(self.avx, rhs.avx) }
203      } else {
204        Self {
205          a : self.a.simd_ne(rhs.a),
206          b : self.b.simd_ne(rhs.b),
207        }
208      }
209    }
210  }
211
212  #[inline]
213  fn simd_lt(self, rhs: Self) -> Self::Output {
214    pick! {
215      if #[cfg(target_feature="avx")]{
216        Self { avx: cmp_op_mask_m256d::<{cmp_op!(LessThanOrdered)}>(self.avx, rhs.avx) }
217      } else {
218        Self {
219          a : self.a.simd_lt(rhs.a),
220          b : self.b.simd_lt(rhs.b),
221        }
222      }
223    }
224  }
225
226  #[inline]
227  fn simd_gt(self, rhs: Self) -> Self::Output {
228    pick! {
229      if #[cfg(target_feature="avx")]{
230        Self { avx: cmp_op_mask_m256d::<{cmp_op!( GreaterThanOrdered)}>(self.avx, rhs.avx) }
231      } else {
232        Self {
233          a : self.a.simd_gt(rhs.a),
234          b : self.b.simd_gt(rhs.b),
235        }
236      }
237    }
238  }
239
240  #[inline]
241  fn simd_le(self, rhs: Self) -> Self::Output {
242    pick! {
243      if #[cfg(target_feature="avx")]{
244        Self { avx: cmp_op_mask_m256d::<{cmp_op!(LessEqualOrdered)}>(self.avx, rhs.avx) }
245      } else {
246        Self {
247          a : self.a.simd_le(rhs.a),
248          b : self.b.simd_le(rhs.b),
249        }
250      }
251    }
252  }
253
254  #[inline]
255  fn simd_ge(self, rhs: Self) -> Self::Output {
256    pick! {
257      if #[cfg(target_feature="avx")]{
258        Self { avx: cmp_op_mask_m256d::<{cmp_op!(GreaterEqualOrdered)}>(self.avx, rhs.avx) }
259      } else {
260        Self {
261          a : self.a.simd_ge(rhs.a),
262          b : self.b.simd_ge(rhs.b),
263        }
264      }
265    }
266  }
267
268  #[inline]
269  pub fn reduce_add(self) -> f64 {
270    pick! {
271      if #[cfg(target_feature="avx")] {
272        // From https://stackoverflow.com/questions/49941645/get-sum-of-values-stored-in-m256d-with-sse-avx
273        let lo = cast_to_m128d_from_m256d(self.avx);
274        let hi = extract_m128d_from_m256d::<1>(self.avx);
275        let lo = add_m128d(lo,hi);
276        let hi64 = unpack_high_m128d(lo,lo);
277        let sum = add_m128d_s(lo,hi64);
278        get_f64_from_m128d_s(sum)
279      } else {
280        self.a.reduce_add() + self.b.reduce_add()
281      }
282    }
283  }
284
285  #[inline]
286  pub fn reduce_mul(self) -> f64 {
287    pick! {
288      if #[cfg(target_feature="avx")] {
289        // From https://stackoverflow.com/questions/49941645/get-sum-of-values-stored-in-m256d-with-sse-avx
290        let lo = cast_to_m128d_from_m256d(self.avx);
291        let hi = extract_m128d_from_m256d::<1>(self.avx);
292        let lo = mul_m128d(lo,hi);
293        let hi64 = unpack_high_m128d(lo,lo);
294        let product = mul_m128d_s(lo,hi64);
295        get_f64_from_m128d_s(product)
296      } else {
297        self.a.reduce_mul() * self.b.reduce_mul()
298      }
299    }
300  }
301
302  #[inline]
303  pub fn bitselect(self, if_one: Self, if_zero: Self) -> Self {
304    pick! {
305      if #[cfg(target_feature="avx")] {
306        Self {
307          avx: bitor_m256d(
308            bitand_m256d(if_one.avx, self.avx),
309            bitandnot_m256d(self.avx, if_zero.avx),
310          ),
311        }
312      } else {
313        Self {
314          a: self.a.bitselect(if_one.a, if_zero.a),
315          b: self.b.bitselect(if_one.b, if_zero.b),
316        }
317      }
318    }
319  }
320
321  #[inline]
322  fn select(self, if_true: Self, if_false: Self) -> Self {
323    pick! {
324      if #[cfg(target_feature="avx")] {
325        Self { avx: blend_varying_m256d(if_false.avx, if_true.avx, self.avx) }
326      } else {
327        Self {
328          a : self.a.select(if_true.a, if_false.a),
329          b : self.b.select(if_true.b, if_false.b),
330        }
331      }
332    }
333  }
334
335  #[inline]
336  pub fn to_bitmask(self) -> u32 {
337    pick! {
338      if #[cfg(target_feature="avx")] {
339        move_mask_m256d(self.avx) as u32
340      } else {
341        (self.b.to_bitmask() << 2) | self.a.to_bitmask()
342      }
343    }
344  }
345
346  #[inline]
347  pub fn any(self) -> bool {
348    pick! {
349      if #[cfg(target_feature="avx")] {
350        move_mask_m256d(self.avx) != 0
351      } else {
352        self.a.any() || self.b.any()
353      }
354    }
355  }
356
357  #[inline]
358  pub fn all(self) -> bool {
359    pick! {
360      if #[cfg(target_feature="avx")] {
361        move_mask_m256d(self.avx) == 0b1111
362      } else {
363        self.a.all() && self.b.all()
364      }
365    }
366  }
367
368  ///
369  /// Currently this function is only accelerated on `avx`.
370  #[inline]
371  pub fn transpose(data: [f64x4; 4]) -> [f64x4; 4] {
372    pick! {
373      if #[cfg(target_feature="avx")] {
374        // Can this be optimized?
375        let a = data[0].unpack_lo(data[2]);
376        let b = data[1].unpack_lo(data[3]);
377        let c = data[0].unpack_hi(data[2]);
378        let d = data[1].unpack_hi(data[3]);
379        [
380          a.unpack_lo(b),
381          a.unpack_hi(b),
382          c.unpack_lo(d),
383          c.unpack_hi(d),
384        ]
385      } else {
386        #[inline(always)]
387        fn transpose_column(data: &[f64x4; 4], index: usize) -> f64x4 {
388          f64x4::new([
389            data[0].as_array()[index],
390            data[1].as_array()[index],
391            data[2].as_array()[index],
392            data[3].as_array()[index],
393          ])
394        }
395
396        [
397          transpose_column(&data, 0),
398          transpose_column(&data, 1),
399          transpose_column(&data, 2),
400          transpose_column(&data, 3),
401        ]
402      }
403    }
404  }
405
406  #[inline]
407  pub fn is_nan(self) -> Self {
408    pick! {
409      if #[cfg(target_feature="avx")] {
410        Self { avx: cmp_op_mask_m256d::<{cmp_op!(Unordered)}>(self.avx, self.avx ) }
411      } else {
412        Self {
413          a : self.a.is_nan(),
414          b : self.b.is_nan(),
415        }
416      }
417    }
418  }
419
420  #[inline]
421  pub fn is_inf(self) -> Self {
422    let shifted_inf = u64x4::from(0xFFE0000000000000);
423    let u: u64x4 = cast(self);
424    let shift_u = u << 1_u64;
425    let out = (shift_u).simd_eq(shifted_inf);
426    cast(out)
427  }
428
429  #[inline]
430  pub fn is_finite(self) -> Self {
431    let shifted_exp_mask = u64x4::from(0xFFE0000000000000);
432    let u: u64x4 = cast(self);
433    let shift_u = u << 1_u64;
434    let out = !(shift_u & shifted_exp_mask).simd_eq(shifted_exp_mask);
435    cast(out)
436  }
437
438  #[inline]
439  pub fn is_sign_positive(self) -> Self {
440    const SIGN_MASK: u64x4 = u64x4::splat((-0.0_f64).to_bits());
441
442    let bits = cast::<f64x4, u64x4>(self);
443    let sign = bits & SIGN_MASK;
444    let result = sign.simd_eq(u64x4::ZERO);
445    cast::<u64x4, f64x4>(result)
446  }
447
448  #[inline]
449  pub fn is_sign_negative(self) -> Self {
450    const SIGN_MASK: u64x4 = u64x4::splat((-0.0_f64).to_bits());
451
452    let bits = cast::<f64x4, u64x4>(self);
453    let sign = bits & SIGN_MASK;
454    let result = sign.simd_eq(SIGN_MASK);
455    cast::<u64x4, f64x4>(result)
456  }
457
458  #[inline]
459  pub fn recip(self) -> Self {
460    // There does not seem to be a `recip` intrinsic for any architecture. The
461    // closest is `_mm256_rcp14_pd` which has relative error.
462    Self::ONE / self
463  }
464
465  #[inline]
466  pub fn recip_sqrt(self) -> Self {
467    // There does not seem to be a `recip_sqrt` intrinsic for any architecture.
468    // The closest is `_mm256_rsqrt14_pd` which has relative error.
469    Self::ONE / self.sqrt()
470  }
471
472  #[inline]
473  pub fn max(self, rhs: Self) -> Self {
474    pick! {
475      if #[cfg(target_feature="avx")] {
476        // max_m256d seems to do rhs < self ? self : rhs. So if there's any NaN
477        // involved, it chooses rhs, so we need to specifically check rhs for
478        // NaN.
479        rhs.is_nan().select(self, Self { avx: max_m256d(self.avx, rhs.avx) })
480      } else {
481        Self {
482          a : self.a.max(rhs.a),
483          b : self.b.max(rhs.b),
484        }
485      }
486    }
487  }
488
489  #[inline]
490  pub fn fast_max(self, rhs: Self) -> Self {
491    pick! {
492      if #[cfg(target_feature="avx")] {
493        Self { avx: max_m256d(self.avx, rhs.avx) }
494      } else {
495        Self {
496          a : self.a.fast_max(rhs.a),
497          b : self.b.fast_max(rhs.b),
498        }
499      }
500    }
501  }
502
503  #[inline]
504  pub fn min(self, rhs: Self) -> Self {
505    pick! {
506      if #[cfg(target_feature="avx")] {
507        // min_m256d seems to do rhs < self ? self : rhs. So if there's any NaN
508        // involved, it chooses rhs, so we need to specifically check rhs for
509        // NaN.
510        rhs.is_nan().select(self, Self { avx: min_m256d(self.avx, rhs.avx) })
511      } else {
512        Self {
513          a : self.a.min(rhs.a),
514          b : self.b.min(rhs.b),
515        }
516      }
517    }
518  }
519
520  #[inline]
521  pub fn fast_min(self, rhs: Self) -> Self {
522    pick! {
523      if #[cfg(target_feature="avx")] {
524        Self { avx: min_m256d(self.avx, rhs.avx) }
525      } else {
526        Self {
527          a : self.a.fast_min(rhs.a),
528          b : self.b.fast_min(rhs.b),
529        }
530      }
531    }
532  }
533
534  #[inline]
535  pub fn clamp(self, min: Self, max: Self) -> Self {
536    pick! {
537      if #[cfg(target_feature="avx")] {
538        // This works since all bits set is NaN.
539        self.fast_clamp(min, max) | min.is_nan() | max.is_nan()
540      } else {
541        // Some targets have better implementations than the above one.
542        Self {
543          a: self.a.clamp(min.a, max.a),
544          b: self.b.clamp(min.b, max.b),
545        }
546      }
547    }
548  }
549
550  #[inline]
551  pub fn fast_clamp(self, min: Self, max: Self) -> Self {
552    pick! {
553      if #[cfg(target_feature="avx")] {
554        // For both `min_m256d` and `max_m256d` if any input is NaN, `rhs` gets
555        // chosen. For `self` to be chosen, `self` must be the second argument.
556        Self { avx: max_m256d(min.avx, min_m256d(max.avx, self.avx)) }
557      } else {
558        Self {
559          a: self.a.fast_clamp(min.a, max.a),
560          b: self.b.fast_clamp(min.b, max.b),
561        }
562      }
563    }
564  }
565
566  #[inline]
567  pub fn abs(self) -> Self {
568    pick! {
569      if #[cfg(target_feature="avx")] {
570        let non_sign_bits = f64x4::from(f64::from_bits(i64::MAX as u64));
571        self & non_sign_bits
572      } else {
573        Self {
574          a : self.a.abs(),
575          b : self.b.abs(),
576        }
577      }
578    }
579  }
580
581  #[inline]
582  pub fn floor(self) -> Self {
583    pick! {
584      if #[cfg(target_feature="avx")] {
585        Self { avx: floor_m256d(self.avx) }
586      } else {
587        Self {
588          a : self.a.floor(),
589          b : self.b.floor(),
590        }
591      }
592    }
593  }
594
595  #[inline]
596  pub fn ceil(self) -> Self {
597    pick! {
598      if #[cfg(target_feature="avx")] {
599        Self { avx: ceil_m256d(self.avx) }
600      } else {
601        Self {
602          a : self.a.ceil(),
603          b : self.b.ceil(),
604        }
605      }
606    }
607  }
608
609  #[inline]
610  pub fn round(self) -> Self {
611    pick! {
612      if #[cfg(target_feature="avx2")] {
613        const_f64_as_f64x4!(HALF_NEXT_DOWN, 0.5_f64.next_down());
614        const_f64_as_f64x4!(BOUNDS_LIMIT, 4503599627370496.0);
615
616        let self_abs = self.abs();
617
618        let adjusted_self = self_abs + Self::HALF;
619        let result_abs = Self { avx: round_m256d::<{round_op!(Zero)}>(adjusted_self.avx) };
620        // The addition breaks for `0.5.next_down()` which incorrectly rounds to
621        // `1.0`. This resets the result back to `0.0`.
622        let result_abs = result_abs & self_abs.simd_ne(HALF_NEXT_DOWN);
623
624        // Large value, infinity and NaN need special handling.
625        let bounds_mask: Self = cast(cmp_gt_mask_i64_m256i(cast(BOUNDS_LIMIT), cast(self_abs)));
626
627        // `abs` keeps the original sign.
628        bounds_mask.abs().bitselect(result_abs, self)
629      } else {
630        let [a, b] = cast::<f64x4, [f64x2; 2]>(self);
631        cast([a.round(), b.round()])
632      }
633    }
634  }
635
636  #[inline]
637  pub fn round_int(self) -> i64x4 {
638    pick! {
639      if #[cfg(all(target_feature="avx512dq", target_feature="avx512vl"))] {
640        #[cfg(target_arch = "x86")]
641        use core::arch::x86::_mm256_cvtpd_epi64;
642        #[cfg(target_arch = "x86_64")]
643        use core::arch::x86_64::_mm256_cvtpd_epi64;
644
645        // Based on: https://github.com/v8/v8/blob/210987a552a2bf2a854b0baa9588a5959ff3979d/src/codegen/shared-ia32-x64/macro-assembler-shared-ia32-x64.h#L489-L504
646        let non_nan_mask = self.simd_eq(self);
647        let non_nan = self & non_nan_mask;
648        let flip_to_max: i64x4 = cast(self.simd_ge(Self::splat(9223372036854775808.0)));
649
650        // TODO(safe_arch): Add `_mm256_cvtpd_epi64`.
651        let cast: i64x4 = cast(m256i(unsafe { _mm256_cvtpd_epi64(non_nan.avx.0) }));
652        flip_to_max ^ cast
653      } else {
654        let [a, b]: [f64x2; 2] = cast(self);
655        cast([a.round_int(), b.round_int()])
656      }
657    }
658  }
659
660  #[inline]
661  pub fn fast_round_int(self) -> i64x4 {
662    pick! {
663      if #[cfg(all(target_feature="avx512dq", target_feature="avx512vl"))] {
664        #[cfg(target_arch = "x86")]
665        use core::arch::x86::_mm256_cvtpd_epi64;
666        #[cfg(target_arch = "x86_64")]
667        use core::arch::x86_64::_mm256_cvtpd_epi64;
668
669        // TODO(safe_arch): Add `_mm256_cvtpd_epi64`.
670        cast(m256i(unsafe { _mm256_cvtpd_epi64(self.avx.0) }))
671      } else {
672        let [a, b]: [f64x2; 2] = cast(self);
673        cast([a.fast_round_int(), b.fast_round_int()])
674      }
675    }
676  }
677
678  #[inline]
679  pub fn round_ties_even(self) -> Self {
680    pick! {
681      if #[cfg(target_feature="avx")] {
682        Self { avx: round_m256d::<{round_op!(Nearest)}>(self.avx) }
683      } else {
684        Self {
685          a : self.a.round_ties_even(),
686          b : self.b.round_ties_even(),
687        }
688      }
689    }
690  }
691
692  #[inline]
693  pub fn trunc(self) -> Self {
694    pick! {
695      if #[cfg(target_feature="avx")] {
696        Self { avx: round_m256d::<{round_op!(Zero)}>(self.avx) }
697      } else {
698        Self {
699          a : self.a.trunc(),
700          b : self.b.trunc(),
701        }
702      }
703    }
704  }
705
706  #[inline]
707  pub fn trunc_int(self) -> i64x4 {
708    pick! {
709      if #[cfg(all(target_feature="avx512dq", target_feature="avx512vl"))] {
710        #[cfg(target_arch = "x86")]
711        use core::arch::x86::_mm256_cvttpd_epi64;
712        #[cfg(target_arch = "x86_64")]
713        use core::arch::x86_64::_mm256_cvttpd_epi64;
714
715        // Based on: https://github.com/v8/v8/blob/210987a552a2bf2a854b0baa9588a5959ff3979d/src/codegen/shared-ia32-x64/macro-assembler-shared-ia32-x64.h#L489-L504
716        let non_nan_mask = self.simd_eq(self);
717        let non_nan = self & non_nan_mask;
718        let flip_to_max: i64x4 = cast(self.simd_ge(Self::splat(9223372036854775808.0)));
719
720        // TODO(safe_arch): Add `_mm256_cvttpd_epi64`.
721        let cast: i64x4 = cast(m256i(unsafe { _mm256_cvttpd_epi64(non_nan.avx.0) }));
722        flip_to_max ^ cast
723      } else {
724        let [a, b]: [f64x2; 2] = cast(self);
725        cast([a.trunc_int(), b.trunc_int()])
726      }
727    }
728  }
729
730  #[inline]
731  pub fn fast_trunc_int(self) -> i64x4 {
732    pick! {
733      if #[cfg(all(target_feature="avx512dq", target_feature="avx512vl"))] {
734        #[cfg(target_arch = "x86")]
735        use core::arch::x86::_mm256_cvttpd_epi64;
736        #[cfg(target_arch = "x86_64")]
737        use core::arch::x86_64::_mm256_cvttpd_epi64;
738
739        // TODO(safe_arch): Add `_mm256_cvttpd_epi64`.
740        cast(m256i(unsafe { _mm256_cvttpd_epi64(self.avx.0) }))
741      } else {
742        let [a, b]: [f64x2; 2] = cast(self);
743        cast([a.fast_trunc_int(), b.fast_trunc_int()])
744      }
745    }
746  }
747
748  ///
749  /// # Platform-specific behavior (may change in the future)
750  ///
751  /// - On `x86`/`x86_64` with AVX+FMA: Uses 256-bit `vfmadd` (single rounding,
752  ///   best accuracy)
753  /// - On `x86`/`x86_64` with AVX only: Uses `(self * m) + a` (two roundings)
754  /// - Other platforms: Delegates to [`f64x2`] (may use NEON FMA or fallback)
755  #[inline]
756  pub fn mul_add(self, a: Self, b: Self) -> Self {
757    pick! {
758      if #[cfg(all(target_feature="avx",target_feature="fma"))] {
759        Self { avx: fused_mul_add_m256d(self.avx, a.avx, b.avx) }
760      } else if #[cfg(target_feature="avx")] {
761        // still want to use 256 bit ops
762        (self * a) + b
763      } else {
764        Self {
765          a : self.a.mul_add(a.a, b.a),
766          b : self.b.mul_add(a.b, b.b),
767        }
768      }
769    }
770  }
771
772  ///
773  /// # Platform-specific behavior (may change in the future)
774  ///
775  /// - On `x86`/`x86_64` with AVX+FMA: Uses 256-bit `vfmsub` (single rounding,
776  ///   best accuracy)
777  /// - On `x86`/`x86_64` with AVX only: Uses `(self * m) - s` (two roundings)
778  /// - Other platforms: Delegates to [`f64x2`] (may use NEON FMA or fallback)
779  #[inline]
780  pub fn mul_sub(self, a: Self, b: Self) -> Self {
781    pick! {
782      if #[cfg(all(target_feature="avx",target_feature="fma"))] {
783        Self { avx: fused_mul_sub_m256d(self.avx, a.avx, b.avx) }
784      } else if #[cfg(target_feature="avx")] {
785        // still want to use 256 bit ops
786        (self * a) - b
787      } else {
788        Self {
789          a : self.a.mul_sub(a.a, b.a),
790          b : self.b.mul_sub(a.b, b.b),
791        }
792      }
793    }
794  }
795
796  ///
797  /// # Platform-specific behavior (may change in the future)
798  ///
799  /// - On `x86`/`x86_64` with AVX+FMA: Uses 256-bit `vfnmadd` (single rounding,
800  ///   best accuracy)
801  /// - On `x86`/`x86_64` with AVX only: Uses `a - (self * m)` (two roundings)
802  /// - Other platforms: Delegates to [`f64x2`] (may use NEON FMA or fallback)
803  #[inline]
804  pub fn mul_neg_add(self, a: Self, b: Self) -> Self {
805    pick! {
806      if #[cfg(all(target_feature="avx",target_feature="fma"))] {
807        Self { avx: fused_mul_neg_add_m256d(self.avx, a.avx, b.avx) }
808      } else if #[cfg(target_feature="avx")] {
809        // still want to use 256 bit ops
810        b - (self * a)
811      } else {
812        Self {
813          a : self.a.mul_neg_add(a.a, b.a),
814          b : self.b.mul_neg_add(a.b, b.b),
815        }
816      }
817    }
818  }
819
820  ///
821  /// # Platform-specific behavior (may change in the future)
822  ///
823  /// - On `x86`/`x86_64` with AVX+FMA: Uses 256-bit `vfnmsub` (single rounding,
824  ///   best accuracy)
825  /// - On `x86`/`x86_64` with AVX only: Uses `-(self * m) - s` (two roundings)
826  /// - Other platforms: Delegates to [`f64x2`] (may use NEON FMA or fallback)
827  #[inline]
828  pub fn mul_neg_sub(self, a: Self, b: Self) -> Self {
829    pick! {
830       if #[cfg(all(target_feature="avx",target_feature="fma"))] {
831         Self { avx: fused_mul_neg_sub_m256d(self.avx, a.avx, b.avx) }
832        } else if #[cfg(target_feature="avx")] {
833          // still want to use 256 bit ops
834          -(self * a) - b
835        } else {
836         Self {
837           a : self.a.mul_neg_sub(a.a, b.a),
838           b : self.b.mul_neg_sub(a.b, b.b),
839         }
840       }
841    }
842  }
843
844  #[inline]
845  pub fn powf_simd(self, n: Self) -> Self {
846    const_f64_as_f64x4!(ln2d_hi, 0.693145751953125);
847    const_f64_as_f64x4!(ln2d_lo, 1.42860682030941723212E-6);
848    const_f64_as_f64x4!(P0log, 2.0039553499201281259648E1);
849    const_f64_as_f64x4!(P1log, 5.7112963590585538103336E1);
850    const_f64_as_f64x4!(P2log, 6.0949667980987787057556E1);
851    const_f64_as_f64x4!(P3log, 2.9911919328553073277375E1);
852    const_f64_as_f64x4!(P4log, 6.5787325942061044846969E0);
853    const_f64_as_f64x4!(P5log, 4.9854102823193375972212E-1);
854    const_f64_as_f64x4!(P6log, 4.5270000862445199635215E-5);
855    const_f64_as_f64x4!(Q0log, 6.0118660497603843919306E1);
856    const_f64_as_f64x4!(Q1log, 2.1642788614495947685003E2);
857    const_f64_as_f64x4!(Q2log, 3.0909872225312059774938E2);
858    const_f64_as_f64x4!(Q3log, 2.2176239823732856465394E2);
859    const_f64_as_f64x4!(Q4log, 8.3047565967967209469434E1);
860    const_f64_as_f64x4!(Q5log, 1.5062909083469192043167E1);
861
862    // Taylor expansion constants
863    const_f64_as_f64x4!(p2, 1.0 / 2.0); // coefficients for Taylor expansion of exp
864    const_f64_as_f64x4!(p3, 1.0 / 6.0);
865    const_f64_as_f64x4!(p4, 1.0 / 24.0);
866    const_f64_as_f64x4!(p5, 1.0 / 120.0);
867    const_f64_as_f64x4!(p6, 1.0 / 720.0);
868    const_f64_as_f64x4!(p7, 1.0 / 5040.0);
869    const_f64_as_f64x4!(p8, 1.0 / 40320.0);
870    const_f64_as_f64x4!(p9, 1.0 / 362880.0);
871    const_f64_as_f64x4!(p10, 1.0 / 3628800.0);
872    const_f64_as_f64x4!(p11, 1.0 / 39916800.0);
873    const_f64_as_f64x4!(p12, 1.0 / 479001600.0);
874    const_f64_as_f64x4!(p13, 1.0 / 6227020800.0);
875
876    let x1 = self.abs();
877    let x = x1.fraction_2();
878    let mask = x.simd_gt(f64x4::SQRT_2 * f64x4::HALF);
879    let x = (!mask).select(x + x, x);
880    let x = x - f64x4::ONE;
881    let x2 = x * x;
882    let px = polynomial_6!(x, P0log, P1log, P2log, P3log, P4log, P5log, P6log);
883    let px = px * x * x2;
884    let qx = polynomial_6n!(x, Q0log, Q1log, Q2log, Q3log, Q4log, Q5log);
885    let lg1 = px / qx;
886
887    let ef = x1.exponent();
888    let ef = mask.select(ef + f64x4::ONE, ef);
889    let e1 = (ef * n).round_ties_even();
890    let yr = ef.mul_sub(n, e1);
891
892    let lg = f64x4::HALF.mul_neg_add(x2, x) + lg1;
893    let x2err = (f64x4::HALF * x).mul_sub(x, f64x4::HALF * x2);
894    let lg_err = f64x4::HALF.mul_add(x2, lg - x) - lg1;
895
896    let e2 = (lg * n * f64x4::LOG2_E).round_ties_even();
897    let v = lg.mul_sub(n, e2 * ln2d_hi);
898    let v = e2.mul_neg_add(ln2d_lo, v);
899    let v = v - (lg_err + x2err).mul_sub(n, yr * f64x4::LN_2);
900
901    let x = v;
902    let e3 = (x * f64x4::LOG2_E).round_ties_even();
903    let x = e3.mul_neg_add(f64x4::LN_2, x);
904    let z =
905      polynomial_13!(x, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13)
906        + f64x4::ONE;
907    let ee = e1 + e2 + e3;
908    let ei = cast::<_, i64x4>(ee.round_int());
909    let ej = cast::<_, i64x4>(ei + (cast::<_, i64x4>(z) >> 52));
910
911    let overflow = cast::<_, f64x4>(!ej.simd_lt(i64x4::splat(0x07FF)))
912      | ee.simd_gt(f64x4::splat(3000.0));
913    let underflow = cast::<_, f64x4>(!ej.simd_gt(i64x4::splat(0x000)))
914      | ee.simd_lt(f64x4::splat(-3000.0));
915
916    // Add exponent by integer addition
917    let z = cast::<_, f64x4>(cast::<_, i64x4>(z) + (ei << 52));
918
919    // Check for overflow/underflow
920    let z = if (overflow | underflow).any() {
921      let z = underflow.select(f64x4::ZERO, z);
922      overflow.select(Self::infinity(), z)
923    } else {
924      z
925    };
926
927    // Check for self == 0
928    let x_zero = self.is_zero_or_subnormal();
929    let z = x_zero.select(
930      n.simd_lt(f64x4::ZERO).select(
931        Self::infinity(),
932        n.simd_eq(f64x4::ZERO).select(f64x4::ONE, f64x4::ZERO),
933      ),
934      z,
935    );
936
937    let x_sign = self.is_sign_negative();
938
939    let z = if x_sign.any() {
940      // Y into an integer
941      let yi = n.simd_eq(n.round_ties_even());
942      // Is y odd? If yes flip the sign of the result.
943      let y_odd = cast::<i64x4, f64x4>(n.round_int() << 63);
944
945      let z1 = yi
946        .select(z | y_odd, self.simd_eq(Self::ZERO).select(z, Self::nan_pow()));
947      x_sign.select(z1, z)
948    } else {
949      z
950    };
951
952    let x_finite = self.is_finite();
953    let y_finite = n.is_finite();
954    let e_finite = ee.is_finite();
955
956    if (x_finite & y_finite & (e_finite | x_zero)).all() {
957      return z;
958    }
959
960    (self.is_nan() | n.is_nan()).select(self + n, z)
961  }
962
963  #[inline]
964  pub fn sqrt(self) -> Self {
965    pick! {
966      if #[cfg(target_feature="avx")] {
967        Self { avx: sqrt_m256d(self.avx) }
968      } else {
969        Self {
970          a : self.a.sqrt(),
971          b : self.b.sqrt(),
972        }
973      }
974    }
975  }
976
977  /// Calculate the exponent of a packed `f64x4`
978  #[inline]
979  pub fn exp(self) -> Self {
980    const_f64_as_f64x4!(P2, 1.0 / 2.0);
981    const_f64_as_f64x4!(P3, 1.0 / 6.0);
982    const_f64_as_f64x4!(P4, 1.0 / 24.0);
983    const_f64_as_f64x4!(P5, 1.0 / 120.0);
984    const_f64_as_f64x4!(P6, 1.0 / 720.0);
985    const_f64_as_f64x4!(P7, 1.0 / 5040.0);
986    const_f64_as_f64x4!(P8, 1.0 / 40320.0);
987    const_f64_as_f64x4!(P9, 1.0 / 362880.0);
988    const_f64_as_f64x4!(P10, 1.0 / 3628800.0);
989    const_f64_as_f64x4!(P11, 1.0 / 39916800.0);
990    const_f64_as_f64x4!(P12, 1.0 / 479001600.0);
991    const_f64_as_f64x4!(P13, 1.0 / 6227020800.0);
992    // LN2D_HI/LO: double-double decomposition of ln(2) for exp range reduction,
993    // following fdlibm's approach (Sun Microsystems, https://www.netlib.org/fdlibm/ e_exp.c).
994    // Values chosen so LN2D_HI + LN2D_LO = ln(2) to full f64 precision.
995    const_f64_as_f64x4!(LN2D_HI, 0.693145751953125);
996    const_f64_as_f64x4!(LN2D_LO, 1.42860682030941723212E-6);
997    let max_x = f64x4::from(709.783);
998    let min_x = f64x4::from(-744.79);
999    let finite = self.is_finite();
1000    // x < min_x: e^x underflows to 0 -- skip the entire pipeline
1001    let neg_underflow = self.simd_lt(min_x) & finite;
1002    if neg_underflow.all() {
1003      return Self::ZERO;
1004    }
1005    let max_r = f64x4::from(1023.0);
1006    let r = (self * Self::LOG2_E).round_ties_even();
1007    let big = r.simd_gt(max_r);
1008    let r_safe = big.select(max_r, r);
1009    let excess = r - max_r;
1010    let excess = big.select(excess, Self::ZERO);
1011    let scale = Self::vm_pow2n(excess);
1012    let x = r.mul_neg_add(LN2D_HI, self);
1013    let x = r.mul_neg_add(LN2D_LO, x);
1014    let z =
1015      polynomial_13!(x, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13);
1016    let n2 = Self::vm_pow2n(r_safe);
1017    let z = (z + Self::ONE) * scale * n2;
1018    let nan_mask = self.is_nan();
1019    let mut result = nan_mask.select(Self::nan_pow(), z);
1020    let pos_overflow = self.simd_gt(max_x) & finite;
1021    result = pos_overflow.select(Self::infinity(), result);
1022    result = neg_underflow.select(Self::ZERO, result);
1023    let pos_inf = !finite & !self.is_sign_negative() & !nan_mask;
1024    result = pos_inf.select(Self::infinity(), result);
1025    let neg_inf = !finite & self.is_sign_negative() & !nan_mask;
1026    result = neg_inf.select(Self::ZERO, result);
1027    result
1028  }
1029
1030  #[inline]
1031  pub fn exp2(self) -> Self {
1032    const_f64_as_f64x4!(P2, 1.0 / 2.0);
1033    const_f64_as_f64x4!(P3, 1.0 / 6.0);
1034    const_f64_as_f64x4!(P4, 1.0 / 24.0);
1035    const_f64_as_f64x4!(P5, 1.0 / 120.0);
1036    const_f64_as_f64x4!(P6, 1.0 / 720.0);
1037    const_f64_as_f64x4!(P7, 1.0 / 5040.0);
1038    const_f64_as_f64x4!(P8, 1.0 / 40320.0);
1039    const_f64_as_f64x4!(P9, 1.0 / 362880.0);
1040    const_f64_as_f64x4!(P10, 1.0 / 3628800.0);
1041
1042    // max_x = log2(f64::MAX) ≈ 1023.9999999999999
1043    // min_x = log2(f64::MIN_POSITIVE) - 52 ≈ -1022 - 52 = -1074
1044    let max_x = f64x4::from(1023.9999999999999);
1045    let min_x = f64x4::from(-1074.5);
1046    let finite = self.is_finite();
1047    let neg_underflow = self.simd_lt(min_x) & finite;
1048    if neg_underflow.all() {
1049      return Self::ZERO;
1050    }
1051
1052    let round = self.round_ties_even();
1053    let max_r = f64x4::from(1023.0);
1054    let big = round.simd_gt(max_r);
1055    let r_safe = big.select(max_r, round);
1056    let excess = round - max_r;
1057    let excess = big.select(excess, Self::ZERO);
1058    let scale = Self::vm_pow2n(excess);
1059
1060    let fract = (self - round) * Self::LN_2;
1061    let fract_partial_exp2 =
1062      polynomial_8!(fract, P2, P3, P4, P5, P6, P7, P8, P9, P10);
1063    let fract2 = fract * fract;
1064    let fract_exp2 = fract_partial_exp2.mul_add(fract2, fract) + Self::ONE;
1065
1066    let n2 = Self::vm_pow2n(r_safe);
1067    let result = fract_exp2 * scale * n2;
1068
1069    let nan_mask = self.is_nan();
1070    let mut result = nan_mask.select(Self::nan_pow(), result);
1071    let pos_overflow = self.simd_gt(max_x) & finite;
1072    result = pos_overflow.select(Self::infinity(), result);
1073    result = neg_underflow.select(Self::ZERO, result);
1074    let pos_inf = !finite & !self.is_sign_negative() & !nan_mask;
1075    result = pos_inf.select(Self::infinity(), result);
1076    let neg_inf = !finite & self.is_sign_negative() & !nan_mask;
1077    result = neg_inf.select(Self::ZERO, result);
1078    result
1079  }
1080
1081  #[inline]
1082  pub fn ln(self) -> Self {
1083    const_f64_as_f64x4!(HALF, 0.5);
1084    const_f64_as_f64x4!(P0, 7.70838733755885391666E0);
1085    const_f64_as_f64x4!(P1, 1.79368678507819816313E1);
1086    const_f64_as_f64x4!(P2, 1.44989225341610930846E1);
1087    const_f64_as_f64x4!(P3, 4.70579119878881725854E0);
1088    const_f64_as_f64x4!(P4, 4.97494994976747001425E-1);
1089    const_f64_as_f64x4!(P5, 1.01875663804580931796E-4);
1090
1091    const_f64_as_f64x4!(Q0, 2.31251620126765340583E1);
1092    const_f64_as_f64x4!(Q1, 7.11544750618563894466E1);
1093    const_f64_as_f64x4!(Q2, 8.29875266912776603211E1);
1094    const_f64_as_f64x4!(Q3, 4.52279145837532221105E1);
1095    const_f64_as_f64x4!(Q4, 1.12873587189167450590E1);
1096    // LN2F_HI/LO from fdlibm (Freely Distributable LIBM)
1097    // Sun Microsystems, Inc. https://www.netlib.org/fdlibm/
1098    // e_log.c: bit-exact double-double decomposition of ln(2) for f64.
1099    // Replaced the original f32-literals (0.693359375, -2.12194440e-4)
1100    // which had ~10 significant digits, causing ~630 ULP error in f64 ln.
1101    const_f64_as_f64x4!(LN2F_HI, f64::from_bits(0x3FE62E42FEE00000));
1102    const_f64_as_f64x4!(LN2F_LO, f64::from_bits(0x3DEA39EF35793C76));
1103    const_f64_as_f64x4!(VM_SQRT2, 1.414213562373095048801);
1104    const_f64_as_f64x4!(VM_SMALLEST_NORMAL, 2.2250738585072014E-308);
1105
1106    let x1 = self;
1107    let x = Self::fraction_2(x1);
1108    let e = Self::exponent(x1);
1109    let mask = x.simd_gt(VM_SQRT2 * HALF);
1110    let x = (!mask).select(x + x, x);
1111    let fe = mask.select(e + Self::ONE, e);
1112    let x = x - Self::ONE;
1113    let px = polynomial_5!(x, P0, P1, P2, P3, P4, P5);
1114    let x2 = x * x;
1115    let px = x2 * x * px;
1116    let qx = polynomial_5n!(x, Q0, Q1, Q2, Q3, Q4);
1117    let res = px / qx;
1118    let res = fe.mul_add(LN2F_LO, res);
1119    let res = res + x2.mul_neg_add(HALF, x);
1120    let res = fe.mul_add(LN2F_HI, res);
1121    let overflow = !self.is_finite();
1122    let underflow = x1.simd_lt(VM_SMALLEST_NORMAL);
1123    let mask = overflow | underflow;
1124    if !mask.any() {
1125      res
1126    } else {
1127      let is_zero = self.is_zero_or_subnormal();
1128      let res = underflow.select(Self::nan_log(), res);
1129      // Note: is_zero_or_subnormal() lumps subnormals (exponent==0) with zero.
1130      // Both get -Inf here. True subnormal inputs (~5e-324..2.225e-308) should
1131      // produce a finite negative result, but are vanishingly rare in
1132      // practice.
1133      let res = is_zero.select(-Self::infinity(), res);
1134      let res = overflow.select(self, res);
1135      // This must come *after* overflow.blend to overwrite ln(-∞) = -∞ to NaN
1136      let res = (!self.is_finite() & self.is_sign_negative())
1137        .select(Self::nan_log(), res);
1138      res
1139    }
1140  }
1141
1142  #[inline]
1143  pub fn cbrt(self) -> Self {
1144    let a = self.abs();
1145    let zero = a.simd_eq(Self::ZERO);
1146    if zero.all() {
1147      return self; // preserves -0.0
1148    }
1149    let inf = a.is_inf();
1150    let nan = self.is_nan();
1151
1152    const SUBN_SCALE: f64 = 1.8014398509481984e16;
1153    const SUBN_CBRT: f64 = 262144.0;
1154    let tiny = a.simd_lt(Self::from(f64::MIN_POSITIVE));
1155    let a = tiny.select(a * Self::from(SUBN_SCALE), a);
1156
1157    let e = Self::exponent(a) + Self::ONE;
1158    let d = Self::fraction_2(a);
1159
1160    // C0..C5 from SLEEF's minimax polynomial for 1/cbrt(d) on [0.5, 1.0)
1161    // Naoki Shibata et al., "SLEEF: A Portable Vectorized Library of C99
1162    // Mathematical Functions", https://sleef.org / https://github.com/shibatch/sleef
1163    // Licensed under the Boost Software License 1.0.
1164    const_f64_as_f64x4!(C0, 2.2307275302496609725722);
1165    const_f64_as_f64x4!(C1, -3.85841935510444988821632);
1166    const_f64_as_f64x4!(C2, 6.03990368989458747961407);
1167    const_f64_as_f64x4!(C3, -5.73353060922947843636166);
1168    const_f64_as_f64x4!(C4, 2.96155103020039511818595);
1169    const_f64_as_f64x4!(C5, -0.640245898480692909870982);
1170    let mut x = polynomial_5!(d, C0, C1, C2, C3, C4, C5);
1171
1172    // Newton for 1/cbrt: x = x - (d * x^4 - x) / 3.
1173    let x2 = x * x;
1174    let x4 = x2 * x2;
1175    x = x - d.mul_add(x4, -x) * Self::from(1.0 / 3.0);
1176
1177    // cbrt(d) = d * x^2, then polish
1178    let mut y = (d * x) * x;
1179    let yx = y * x;
1180    let t = Self::from(2.0 / 3.0);
1181    y = y - t * y * (yx - Self::ONE);
1182
1183    // Scale by 2^(e/3) = 2^k * 2^(r/3)
1184    let three = Self::from(3.0);
1185    let two = Self::from(2.0);
1186    let neg = e.simd_lt(Self::ZERO);
1187    let e_adj = neg.select(e - two, e);
1188    let k = (e_adj / three).trunc();
1189    let r = e - three * k;
1190    const_f64_as_f64x4!(CBRT2, 1.2599210498948732);
1191    const_f64_as_f64x4!(CBRT4, 1.5874010519681994);
1192    y = r.simd_eq(Self::ONE).select(y * CBRT2, y);
1193    y = r.simd_eq(two).select(y * CBRT4, y);
1194    y *= Self::vm_pow2n(k);
1195    y = tiny.select(y / Self::from(SUBN_CBRT), y);
1196
1197    let result = y.flip_signs(self);
1198    let result = nan.select(self, result);
1199    let result = zero.select(self, result);
1200    let result = inf.select(self, result);
1201    result
1202  }
1203
1204  #[inline]
1205  pub fn asin(self) -> Self {
1206    // Based on the Agner Fog "vector class library":
1207    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1208    const_f64_as_f64x4!(R4asin, 2.967721961301243206100E-3);
1209    const_f64_as_f64x4!(R3asin, -5.634242780008963776856E-1);
1210    const_f64_as_f64x4!(R2asin, 6.968710824104713396794E0);
1211    const_f64_as_f64x4!(R1asin, -2.556901049652824852289E1);
1212    const_f64_as_f64x4!(R0asin, 2.853665548261061424989E1);
1213
1214    const_f64_as_f64x4!(S3asin, -2.194779531642920639778E1);
1215    const_f64_as_f64x4!(S2asin, 1.470656354026814941758E2);
1216    const_f64_as_f64x4!(S1asin, -3.838770957603691357202E2);
1217    const_f64_as_f64x4!(S0asin, 3.424398657913078477438E2);
1218
1219    const_f64_as_f64x4!(P5asin, 4.253011369004428248960E-3);
1220    const_f64_as_f64x4!(P4asin, -6.019598008014123785661E-1);
1221    const_f64_as_f64x4!(P3asin, 5.444622390564711410273E0);
1222    const_f64_as_f64x4!(P2asin, -1.626247967210700244449E1);
1223    const_f64_as_f64x4!(P1asin, 1.956261983317594739197E1);
1224    const_f64_as_f64x4!(P0asin, -8.198089802484824371615E0);
1225
1226    const_f64_as_f64x4!(Q4asin, -1.474091372988853791896E1);
1227    const_f64_as_f64x4!(Q3asin, 7.049610280856842141659E1);
1228    const_f64_as_f64x4!(Q2asin, -1.471791292232726029859E2);
1229    const_f64_as_f64x4!(Q1asin, 1.395105614657485689735E2);
1230    const_f64_as_f64x4!(Q0asin, -4.918853881490881290097E1);
1231
1232    let xa = self.abs();
1233
1234    let big = xa.simd_ge(f64x4::splat(0.625));
1235
1236    let x1 = big.select(f64x4::splat(1.0) - xa, xa * xa);
1237
1238    let x2 = x1 * x1;
1239    let x3 = x2 * x1;
1240    let x4 = x2 * x2;
1241    let x5 = x4 * x1;
1242
1243    let do_big = big.any();
1244    let do_small = !big.all();
1245
1246    let mut rx = f64x4::default();
1247    let mut sx = f64x4::default();
1248    let mut px = f64x4::default();
1249    let mut qx = f64x4::default();
1250
1251    if do_big {
1252      rx = x3.mul_add(R3asin, x2 * R2asin)
1253        + x4.mul_add(R4asin, x1.mul_add(R1asin, R0asin));
1254      sx =
1255        x3.mul_add(S3asin, x4) + x2.mul_add(S2asin, x1.mul_add(S1asin, S0asin));
1256    }
1257    if do_small {
1258      px = x3.mul_add(P3asin, P0asin)
1259        + x4.mul_add(P4asin, x1 * P1asin)
1260        + x5.mul_add(P5asin, x2 * P2asin);
1261      qx = x4.mul_add(Q4asin, x5)
1262        + x3.mul_add(Q3asin, x1 * Q1asin)
1263        + x2.mul_add(Q2asin, Q0asin);
1264    };
1265
1266    let vx = big.select(rx, px);
1267    let wx = big.select(sx, qx);
1268
1269    let y1 = vx / wx * x1;
1270
1271    let mut z1 = f64x4::default();
1272    let mut z2 = f64x4::default();
1273    if do_big {
1274      let xb = (x1 + x1).sqrt();
1275      z1 = xb.mul_add(y1, xb);
1276    }
1277
1278    if do_small {
1279      z2 = xa.mul_add(y1, xa);
1280    }
1281
1282    // asin
1283    let z3 = f64x4::FRAC_PI_2 - z1;
1284    let asin = big.select(z3, z2);
1285    let asin = asin.flip_signs(self);
1286
1287    asin
1288  }
1289
1290  #[inline]
1291  pub fn acos(self) -> Self {
1292    // Based on the Agner Fog "vector class library":
1293    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1294    const_f64_as_f64x4!(R4asin, 2.967721961301243206100E-3);
1295    const_f64_as_f64x4!(R3asin, -5.634242780008963776856E-1);
1296    const_f64_as_f64x4!(R2asin, 6.968710824104713396794E0);
1297    const_f64_as_f64x4!(R1asin, -2.556901049652824852289E1);
1298    const_f64_as_f64x4!(R0asin, 2.853665548261061424989E1);
1299
1300    const_f64_as_f64x4!(S3asin, -2.194779531642920639778E1);
1301    const_f64_as_f64x4!(S2asin, 1.470656354026814941758E2);
1302    const_f64_as_f64x4!(S1asin, -3.838770957603691357202E2);
1303    const_f64_as_f64x4!(S0asin, 3.424398657913078477438E2);
1304
1305    const_f64_as_f64x4!(P5asin, 4.253011369004428248960E-3);
1306    const_f64_as_f64x4!(P4asin, -6.019598008014123785661E-1);
1307    const_f64_as_f64x4!(P3asin, 5.444622390564711410273E0);
1308    const_f64_as_f64x4!(P2asin, -1.626247967210700244449E1);
1309    const_f64_as_f64x4!(P1asin, 1.956261983317594739197E1);
1310    const_f64_as_f64x4!(P0asin, -8.198089802484824371615E0);
1311
1312    const_f64_as_f64x4!(Q4asin, -1.474091372988853791896E1);
1313    const_f64_as_f64x4!(Q3asin, 7.049610280856842141659E1);
1314    const_f64_as_f64x4!(Q2asin, -1.471791292232726029859E2);
1315    const_f64_as_f64x4!(Q1asin, 1.395105614657485689735E2);
1316    const_f64_as_f64x4!(Q0asin, -4.918853881490881290097E1);
1317
1318    let xa = self.abs();
1319
1320    let big = xa.simd_ge(f64x4::splat(0.625));
1321
1322    let x1 = big.select(f64x4::splat(1.0) - xa, xa * xa);
1323
1324    let x2 = x1 * x1;
1325    let x3 = x2 * x1;
1326    let x4 = x2 * x2;
1327    let x5 = x4 * x1;
1328
1329    let do_big = big.any();
1330    let do_small = !big.all();
1331
1332    let mut rx = f64x4::default();
1333    let mut sx = f64x4::default();
1334    let mut px = f64x4::default();
1335    let mut qx = f64x4::default();
1336
1337    if do_big {
1338      rx = x3.mul_add(R3asin, x2 * R2asin)
1339        + x4.mul_add(R4asin, x1.mul_add(R1asin, R0asin));
1340      sx =
1341        x3.mul_add(S3asin, x4) + x2.mul_add(S2asin, x1.mul_add(S1asin, S0asin));
1342    }
1343    if do_small {
1344      px = x3.mul_add(P3asin, P0asin)
1345        + x4.mul_add(P4asin, x1 * P1asin)
1346        + x5.mul_add(P5asin, x2 * P2asin);
1347      qx = x4.mul_add(Q4asin, x5)
1348        + x3.mul_add(Q3asin, x1 * Q1asin)
1349        + x2.mul_add(Q2asin, Q0asin);
1350    };
1351
1352    let vx = big.select(rx, px);
1353    let wx = big.select(sx, qx);
1354
1355    let y1 = vx / wx * x1;
1356
1357    let mut z1 = f64x4::default();
1358    let mut z2 = f64x4::default();
1359    if do_big {
1360      let xb = (x1 + x1).sqrt();
1361      z1 = xb.mul_add(y1, xb);
1362    }
1363
1364    if do_small {
1365      z2 = xa.mul_add(y1, xa);
1366    }
1367
1368    // acos
1369    let z3 = self.simd_lt(f64x4::ZERO).select(f64x4::PI - z1, z1);
1370    let z4 = f64x4::FRAC_PI_2 - z2.flip_signs(self);
1371    let acos = big.select(z3, z4);
1372
1373    acos
1374  }
1375
1376  #[inline]
1377  pub fn atan(self) -> Self {
1378    // Based on the Agner Fog "vector class library":
1379    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1380    const_f64_as_f64x4!(MORE_BITS, 6.123233995736765886130E-17);
1381    const_f64_as_f64x4!(MORE_BITS_O2, 6.123233995736765886130E-17 * 0.5);
1382    const_f64_as_f64x4!(T3PO8, core::f64::consts::SQRT_2 + 1.0);
1383
1384    const_f64_as_f64x4!(P4atan, -8.750608600031904122785E-1);
1385    const_f64_as_f64x4!(P3atan, -1.615753718733365076637E1);
1386    const_f64_as_f64x4!(P2atan, -7.500855792314704667340E1);
1387    const_f64_as_f64x4!(P1atan, -1.228866684490136173410E2);
1388    const_f64_as_f64x4!(P0atan, -6.485021904942025371773E1);
1389
1390    const_f64_as_f64x4!(Q4atan, 2.485846490142306297962E1);
1391    const_f64_as_f64x4!(Q3atan, 1.650270098316988542046E2);
1392    const_f64_as_f64x4!(Q2atan, 4.328810604912902668951E2);
1393    const_f64_as_f64x4!(Q1atan, 4.853903996359136964868E2);
1394    const_f64_as_f64x4!(Q0atan, 1.945506571482613964425E2);
1395
1396    let t = self.abs();
1397
1398    // small:  t < 0.66
1399    // medium: t <= t <= 2.4142 (1+sqrt(2))
1400    // big:    t > 2.4142
1401    let notbig = t.simd_le(T3PO8);
1402    let notsmal = t.simd_ge(Self::splat(0.66));
1403
1404    let mut s = notbig.select(Self::FRAC_PI_4, Self::FRAC_PI_2);
1405    s = notsmal & s;
1406    let mut fac = notbig.select(MORE_BITS_O2, MORE_BITS);
1407    fac = notsmal & fac;
1408
1409    // small:  z = t / 1.0;
1410    // medium: z = (t-1.0) / (t+1.0);
1411    // big:    z = -1.0 / t;
1412    let mut a = notbig & t;
1413    a = notsmal.select(a - Self::ONE, a);
1414    let mut b = notbig & Self::ONE;
1415    b = notsmal.select(b + t, b);
1416    let z = a / b;
1417
1418    let zz = z * z;
1419
1420    let px = polynomial_4!(zz, P0atan, P1atan, P2atan, P3atan, P4atan);
1421    let qx = polynomial_5n!(zz, Q0atan, Q1atan, Q2atan, Q3atan, Q4atan);
1422
1423    let mut re = (px / qx).mul_add(z * zz, z);
1424    re += s + fac;
1425
1426    // get sign bit
1427    re = (self.is_sign_negative()).select(-re, re);
1428
1429    re
1430  }
1431
1432  #[inline]
1433  pub fn atan2(self, x: Self) -> Self {
1434    // Based on the Agner Fog "vector class library":
1435    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1436    const_f64_as_f64x4!(MORE_BITS, 6.123233995736765886130E-17);
1437    const_f64_as_f64x4!(MORE_BITS_O2, 6.123233995736765886130E-17 * 0.5);
1438    const_f64_as_f64x4!(T3PO8, core::f64::consts::SQRT_2 + 1.0);
1439
1440    const_f64_as_f64x4!(P4atan, -8.750608600031904122785E-1);
1441    const_f64_as_f64x4!(P3atan, -1.615753718733365076637E1);
1442    const_f64_as_f64x4!(P2atan, -7.500855792314704667340E1);
1443    const_f64_as_f64x4!(P1atan, -1.228866684490136173410E2);
1444    const_f64_as_f64x4!(P0atan, -6.485021904942025371773E1);
1445
1446    const_f64_as_f64x4!(Q4atan, 2.485846490142306297962E1);
1447    const_f64_as_f64x4!(Q3atan, 1.650270098316988542046E2);
1448    const_f64_as_f64x4!(Q2atan, 4.328810604912902668951E2);
1449    const_f64_as_f64x4!(Q1atan, 4.853903996359136964868E2);
1450    const_f64_as_f64x4!(Q0atan, 1.945506571482613964425E2);
1451
1452    let y = self;
1453
1454    // move in first octant
1455    let x1 = x.abs();
1456    let y1 = y.abs();
1457    let swapxy = y1.simd_gt(x1);
1458    // swap x and y if y1 > x1
1459    let mut x2 = swapxy.select(y1, x1);
1460    let mut y2 = swapxy.select(x1, y1);
1461
1462    // check for special case: x and y are both +/- INF
1463    let both_infinite = x.is_inf() & y.is_inf();
1464    if both_infinite.any() {
1465      let minus_one = -Self::ONE;
1466      x2 = both_infinite.select(x2 & minus_one, x2);
1467      y2 = both_infinite.select(y2 & minus_one, y2);
1468    }
1469
1470    // x = y = 0 gives NAN here
1471    let t = y2 / x2;
1472
1473    // small:  t < 0.66
1474    // medium: t <= t <= 2.4142 (1+sqrt(2))
1475    // big:    t > 2.4142
1476    let notbig = t.simd_le(T3PO8);
1477    let notsmal = t.simd_ge(Self::splat(0.66));
1478
1479    let mut s = notbig.select(Self::FRAC_PI_4, Self::FRAC_PI_2);
1480    s = notsmal & s;
1481    let mut fac = notbig.select(MORE_BITS_O2, MORE_BITS);
1482    fac = notsmal & fac;
1483
1484    // small:  z = t / 1.0;
1485    // medium: z = (t-1.0) / (t+1.0);
1486    // big:    z = -1.0 / t;
1487    let mut a = notbig & t;
1488    a = notsmal.select(a - Self::ONE, a);
1489    let mut b = notbig & Self::ONE;
1490    b = notsmal.select(b + t, b);
1491    let z = a / b;
1492
1493    let zz = z * z;
1494
1495    let px = polynomial_4!(zz, P0atan, P1atan, P2atan, P3atan, P4atan);
1496    let qx = polynomial_5n!(zz, Q0atan, Q1atan, Q2atan, Q3atan, Q4atan);
1497
1498    let mut re = (px / qx).mul_add(z * zz, z);
1499    re += s + fac;
1500
1501    // move back in place
1502    re = swapxy.select(Self::FRAC_PI_2 - re, re);
1503    re = ((x | y).simd_eq(Self::ZERO)).select(Self::ZERO, re);
1504    re = (x.is_sign_negative()).select(Self::PI - re, re);
1505
1506    // get sign bit
1507    re = (y.is_sign_negative()).select(-re, re);
1508
1509    re
1510  }
1511
1512  #[inline]
1513  pub fn sin_cos(self) -> (Self, Self) {
1514    // Based on the Agner Fog "vector class library":
1515    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1516
1517    const_f64_as_f64x4!(P0sin, -1.66666666666666307295E-1);
1518    const_f64_as_f64x4!(P1sin, 8.33333333332211858878E-3);
1519    const_f64_as_f64x4!(P2sin, -1.98412698295895385996E-4);
1520    const_f64_as_f64x4!(P3sin, 2.75573136213857245213E-6);
1521    const_f64_as_f64x4!(P4sin, -2.50507477628578072866E-8);
1522    const_f64_as_f64x4!(P5sin, 1.58962301576546568060E-10);
1523
1524    const_f64_as_f64x4!(P0cos, 4.16666666666665929218E-2);
1525    const_f64_as_f64x4!(P1cos, -1.38888888888730564116E-3);
1526    const_f64_as_f64x4!(P2cos, 2.48015872888517045348E-5);
1527    const_f64_as_f64x4!(P3cos, -2.75573141792967388112E-7);
1528    const_f64_as_f64x4!(P4cos, 2.08757008419747316778E-9);
1529    const_f64_as_f64x4!(P5cos, -1.13585365213876817300E-11);
1530
1531    const_f64_as_f64x4!(DP1, 7.853981554508209228515625E-1 * 2.);
1532    const_f64_as_f64x4!(DP2, 7.94662735614792836714E-9 * 2.);
1533    const_f64_as_f64x4!(DP3, 3.06161699786838294307E-17 * 2.);
1534
1535    const_f64_as_f64x4!(TWO_OVER_PI, 2.0 / core::f64::consts::PI);
1536
1537    let xa = self.abs();
1538
1539    let y = (xa * TWO_OVER_PI).round_ties_even();
1540    let q = y.round_int();
1541
1542    let x = y.mul_neg_add(DP3, y.mul_neg_add(DP2, y.mul_neg_add(DP1, xa)));
1543
1544    let x2 = x * x;
1545    let mut s = polynomial_5!(x2, P0sin, P1sin, P2sin, P3sin, P4sin, P5sin);
1546    let mut c = polynomial_5!(x2, P0cos, P1cos, P2cos, P3cos, P4cos, P5cos);
1547    s = (x * x2).mul_add(s, x);
1548    c =
1549      (x2 * x2).mul_add(c, x2.mul_neg_add(f64x4::from(0.5), f64x4::from(1.0)));
1550
1551    let swap = !((q & i64x4::from(1)).simd_eq(i64x4::from(0)));
1552
1553    let mut overflow: f64x4 = cast(q.simd_gt(i64x4::from(0x80000000000000)));
1554    overflow &= xa.is_finite();
1555    s = overflow.select(f64x4::from(0.0), s);
1556    c = overflow.select(f64x4::from(1.0), c);
1557
1558    // calc sin
1559    let mut sin1 = cast::<_, f64x4>(swap).select(c, s);
1560    let sign_sin: i64x4 = (q << 62) ^ cast::<_, i64x4>(self);
1561    sin1 = sin1.flip_signs(cast(sign_sin));
1562
1563    // calc cos
1564    let mut cos1 = cast::<_, f64x4>(swap).select(s, c);
1565    let sign_cos: i64x4 = ((q + i64x4::from(1)) & i64x4::from(2)) << 62;
1566    cos1 ^= cast::<_, f64x4>(sign_cos);
1567
1568    // IEEE 754: sin/cos(±∞) = NaN, sin/cos(NaN) = NaN
1569    let finite = self.is_finite();
1570    let nan = Self::splat(f64::NAN);
1571    let sin_final = finite.select(sin1, nan);
1572    let cos_final = finite.select(cos1, nan);
1573
1574    (sin_final, cos_final)
1575  }
1576
1577  #[inline]
1578  pub fn asin_acos(self) -> (Self, Self) {
1579    // Based on the Agner Fog "vector class library":
1580    // https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
1581    const_f64_as_f64x4!(R4asin, 2.967721961301243206100E-3);
1582    const_f64_as_f64x4!(R3asin, -5.634242780008963776856E-1);
1583    const_f64_as_f64x4!(R2asin, 6.968710824104713396794E0);
1584    const_f64_as_f64x4!(R1asin, -2.556901049652824852289E1);
1585    const_f64_as_f64x4!(R0asin, 2.853665548261061424989E1);
1586
1587    const_f64_as_f64x4!(S3asin, -2.194779531642920639778E1);
1588    const_f64_as_f64x4!(S2asin, 1.470656354026814941758E2);
1589    const_f64_as_f64x4!(S1asin, -3.838770957603691357202E2);
1590    const_f64_as_f64x4!(S0asin, 3.424398657913078477438E2);
1591
1592    const_f64_as_f64x4!(P5asin, 4.253011369004428248960E-3);
1593    const_f64_as_f64x4!(P4asin, -6.019598008014123785661E-1);
1594    const_f64_as_f64x4!(P3asin, 5.444622390564711410273E0);
1595    const_f64_as_f64x4!(P2asin, -1.626247967210700244449E1);
1596    const_f64_as_f64x4!(P1asin, 1.956261983317594739197E1);
1597    const_f64_as_f64x4!(P0asin, -8.198089802484824371615E0);
1598
1599    const_f64_as_f64x4!(Q4asin, -1.474091372988853791896E1);
1600    const_f64_as_f64x4!(Q3asin, 7.049610280856842141659E1);
1601    const_f64_as_f64x4!(Q2asin, -1.471791292232726029859E2);
1602    const_f64_as_f64x4!(Q1asin, 1.395105614657485689735E2);
1603    const_f64_as_f64x4!(Q0asin, -4.918853881490881290097E1);
1604
1605    let xa = self.abs();
1606
1607    let big = xa.simd_ge(f64x4::splat(0.625));
1608
1609    let x1 = big.select(f64x4::splat(1.0) - xa, xa * xa);
1610
1611    let x2 = x1 * x1;
1612    let x3 = x2 * x1;
1613    let x4 = x2 * x2;
1614    let x5 = x4 * x1;
1615
1616    let do_big = big.any();
1617    let do_small = !big.all();
1618
1619    let mut rx = f64x4::default();
1620    let mut sx = f64x4::default();
1621    let mut px = f64x4::default();
1622    let mut qx = f64x4::default();
1623
1624    if do_big {
1625      rx = x3.mul_add(R3asin, x2 * R2asin)
1626        + x4.mul_add(R4asin, x1.mul_add(R1asin, R0asin));
1627      sx =
1628        x3.mul_add(S3asin, x4) + x2.mul_add(S2asin, x1.mul_add(S1asin, S0asin));
1629    }
1630
1631    if do_small {
1632      px = x3.mul_add(P3asin, P0asin)
1633        + x4.mul_add(P4asin, x1 * P1asin)
1634        + x5.mul_add(P5asin, x2 * P2asin);
1635      qx = x4.mul_add(Q4asin, x5)
1636        + x3.mul_add(Q3asin, x1 * Q1asin)
1637        + x2.mul_add(Q2asin, Q0asin);
1638    };
1639
1640    let vx = big.select(rx, px);
1641    let wx = big.select(sx, qx);
1642
1643    let y1 = vx / wx * x1;
1644
1645    let mut z1 = f64x4::default();
1646    let mut z2 = f64x4::default();
1647    if do_big {
1648      let xb = (x1 + x1).sqrt();
1649      z1 = xb.mul_add(y1, xb);
1650    }
1651
1652    if do_small {
1653      z2 = xa.mul_add(y1, xa);
1654    }
1655
1656    // asin
1657    let z3 = f64x4::FRAC_PI_2 - z1;
1658    let asin = big.select(z3, z2);
1659    let asin = asin.flip_signs(self);
1660
1661    // acos
1662    let z3 = self.simd_lt(f64x4::ZERO).select(f64x4::PI - z1, z1);
1663    let z4 = f64x4::FRAC_PI_2 - z2.flip_signs(self);
1664    let acos = big.select(z3, z4);
1665
1666    (asin, acos)
1667  }
1668
1669  #[inline]
1670  pub fn exp_m1(self) -> Self {
1671    const_f64_as_f64x4!(P2, 1.0 / 2.0);
1672    const_f64_as_f64x4!(P3, 1.0 / 6.0);
1673    const_f64_as_f64x4!(P4, 1.0 / 24.0);
1674    const_f64_as_f64x4!(P5, 1.0 / 120.0);
1675    const_f64_as_f64x4!(P6, 1.0 / 720.0);
1676    const_f64_as_f64x4!(P7, 1.0 / 5040.0);
1677    const_f64_as_f64x4!(P8, 1.0 / 40320.0);
1678    const_f64_as_f64x4!(P9, 1.0 / 362880.0);
1679    const_f64_as_f64x4!(P10, 1.0 / 3628800.0);
1680    const_f64_as_f64x4!(P11, 1.0 / 39916800.0);
1681    const_f64_as_f64x4!(P12, 1.0 / 479001600.0);
1682    const_f64_as_f64x4!(P13, 1.0 / 6227020800.0);
1683    // LN2D_HI/LO: double-double decomposition of ln(2) for exp range reduction,
1684    // following fdlibm's approach (Sun Microsystems, https://www.netlib.org/fdlibm/ e_exp.c).
1685    const_f64_as_f64x4!(LN2D_HI, 0.693145751953125);
1686    const_f64_as_f64x4!(LN2D_LO, 1.42860682030941723212E-6);
1687    // x < -37.429: e^x < 2⁻⁵⁴, exp_m1(x) = -1.0 exactly (mantissa exhaustion)
1688    // IEEE simd_lt returns false for NaN, so NaN lanes can't reach here.
1689    // -inf is < -37.429, and exp_m1(-inf) = -1.0, also correct.
1690    if self.simd_lt(f64x4::from(-37.429)).all() {
1691      return f64x4::from(-1.0);
1692    }
1693    // max_x = ln(f64::MAX) ≈ 709.7827129, max_r = 1023 (IEEE max normal
1694    // exponent) min_x = -1074.5 ln(2) ≈ -744.79: min r for vm_pow2n to
1695    // construct subnormal
1696    let max_x = f64x4::from(709.783);
1697    let min_x = f64x4::from(-744.79);
1698    let max_r = f64x4::from(1023.0);
1699    let r = (self * Self::LOG2_E).round_ties_even();
1700    let big = r.simd_gt(max_r);
1701    let r_safe = big.select(max_r, r);
1702    let excess = r - max_r;
1703    let excess = big.select(excess, Self::ZERO);
1704    let scale = Self::vm_pow2n(excess);
1705    let x = r.mul_neg_add(LN2D_HI, self);
1706    let x = r.mul_neg_add(LN2D_LO, x);
1707    let z =
1708      polynomial_13!(x, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13);
1709    let n2 = Self::vm_pow2n(r_safe);
1710    let exp_val = (z + Self::ONE) * scale * n2;
1711    // When r == 0, z is already e^x - 1 from the Taylor poly.
1712    // Computing (z+1) - 1 would lose low bits for small x (catastrophic
1713    // cancellation at z ~ 0), so keep z directly.
1714    let r_is_zero = r.simd_eq(Self::ZERO);
1715    let z = r_is_zero.select(z, exp_val - Self::ONE);
1716    let nan_mask = self.is_nan();
1717    let finite = self.is_finite();
1718    let mut result = nan_mask.select(Self::nan_pow(), z);
1719    let pos_overflow = self.simd_gt(max_x) & finite;
1720    result = pos_overflow.select(Self::infinity(), result);
1721    let neg_underflow = self.simd_lt(min_x) & finite;
1722    result = neg_underflow.select(-Self::ONE, result);
1723    let pos_inf = !finite & !self.is_sign_negative() & !nan_mask;
1724    result = pos_inf.select(Self::infinity(), result);
1725    let neg_inf = !finite & self.is_sign_negative() & !nan_mask;
1726    result = neg_inf.select(-Self::ONE, result);
1727    let is_zero = self.simd_eq(Self::ZERO);
1728    result = is_zero.select(self, result);
1729    result
1730  }
1731
1732  #[inline]
1733  pub fn ln_1p(self) -> Self {
1734    // Based on the identity ln(1+x) = x·ln(1+x)/((1+x)-1), i.e. x·ln(u)/(u-1)
1735    // where u = 1+x. From MUSL libc (Rich Felker et al., https://musl.libc.org) src/math/log1p.c
1736    // and fdlibm (Sun Microsystems, https://www.netlib.org/fdlibm/) s_log1p.c.
1737    // When 1+x rounds to 1 exactly (subnormal x), return x directly.
1738    // When 1+x overflows (+inf), return ln(u) without correction.
1739    // Mathematically exact: compensates for the rounding loss in 1+x without
1740    // needing a series threshold.
1741    let u = self + Self::ONE;
1742    let eq = u.simd_eq(Self::ONE);
1743    let ln_u = Self::ln(u);
1744    let correction = self * (ln_u / (u - Self::ONE));
1745    let result = eq.select(self, correction);
1746    let over = u.is_inf();
1747    over.select(ln_u, result)
1748  }
1749
1750  #[inline]
1751  pub fn sinh(self) -> Self {
1752    const_f64_as_f64x4!(P0, 1.0);
1753    const_f64_as_f64x4!(P1, 1.0 / 6.0);
1754    const_f64_as_f64x4!(P2, 1.0 / 120.0);
1755    const_f64_as_f64x4!(P3, 1.0 / 5040.0);
1756    const_f64_as_f64x4!(P4, 1.0 / 362880.0);
1757    const_f64_as_f64x4!(P5, 1.0 / 39916800.0);
1758    const_f64_as_f64x4!(P6, 1.0 / 6227020800.0);
1759    let a = self.abs();
1760    // |x| < 0.5: Taylor poly; last truncation term < 1 ULP at x=0.5 for both types
1761    let small = a.simd_lt(f64x4::from(0.5));
1762    let t = a * a;
1763    let poly = a * polynomial_6!(t, P0, P1, P2, P3, P4, P5, P6);
1764    let exp_based = {
1765      let e = a.exp();
1766      (e - Self::ONE / e) * Self::HALF
1767    };
1768    let result = small.select(poly, exp_based);
1769    result.flip_signs(self)
1770  }
1771
1772  #[inline]
1773  pub fn cosh(self) -> Self {
1774    const_f64_as_f64x4!(P0, 1.0);
1775    const_f64_as_f64x4!(P1, 1.0 / 2.0);
1776    const_f64_as_f64x4!(P2, 1.0 / 24.0);
1777    const_f64_as_f64x4!(P3, 1.0 / 720.0);
1778    const_f64_as_f64x4!(P4, 1.0 / 40320.0);
1779    const_f64_as_f64x4!(P5, 1.0 / 3628800.0);
1780    const_f64_as_f64x4!(P6, 1.0 / 479001600.0);
1781    const_f64_as_f64x4!(P7, 1.0 / 87178291200.0);
1782    let a = self.abs();
1783    // |x| < 0.5: Taylor poly; last truncation term < 1 ULP at x=0.5 for both types
1784    let small = a.simd_lt(f64x4::from(0.5));
1785    let t = a * a;
1786    let poly = polynomial_7!(t, P0, P1, P2, P3, P4, P5, P6, P7);
1787    let exp_based = {
1788      let e = a.exp();
1789      (e + Self::ONE / e) * Self::HALF
1790    };
1791    small.select(poly, exp_based)
1792  }
1793
1794  #[inline]
1795  pub fn tanh(self) -> Self {
1796    // |x| < 5e-8: tanh(x) ≈ x, error x³/3 < 16·ULP(x)
1797    // bound: x² < 48·2⁻⁵² → x < 1.03e-7; 5e-8 has 2× margin
1798    // |x| > 19.062: tanh(x) = ±1 to f64 precision (e⁻²ˣ < 2⁻⁵⁴)
1799    let a = self.abs();
1800    let large = a.simd_gt(f64x4::from(19.062));
1801    if large.all() {
1802      return Self::ONE.flip_signs(self);
1803    }
1804    let small = a.simd_lt(f64x4::from(5e-8));
1805    let exp_based = {
1806      let t = (Self::from(-2.0) * a).exp_m1();
1807      let pos = -t / (t + Self::from(2.0));
1808      pos.flip_signs(self)
1809    };
1810    let result = small.select(self, exp_based);
1811    large.select(Self::ONE.flip_signs(self), result)
1812  }
1813}
1814
1815/// The following functionality exists only for [`f64x4`], or only for
1816/// particular types inconsistently.
1817impl f64x4 {
1818  #[inline]
1819  fn vm_pow2n(self) -> Self {
1820    const_f64_as_f64x4!(pow2_52, 4503599627370496.0);
1821    const_f64_as_f64x4!(bias, 1023.0);
1822    let a = self + (bias + pow2_52);
1823    let c = cast::<_, i64x4>(a) << 52;
1824    let std_result = cast::<_, f64x4>(c);
1825
1826    let min_exp = f64x4::from(-1022.0);
1827    let is_sub = self.simd_lt(min_exp);
1828    if is_sub.any() {
1829      let valid = self.simd_ge(f64x4::from(-1074.0));
1830      let shift_f = self + f64x4::from(1074.0);
1831      let mut shift_i = shift_f.trunc_int();
1832      shift_i = cast::<_, i64x4>(valid).select(shift_i, i64x4::ZERO);
1833      let mantissa = i64x4::ONE << shift_i;
1834      let sub_result = cast::<_, f64x4>(mantissa);
1835      let sub_result = valid.select(sub_result, f64x4::ZERO);
1836      is_sub.select(sub_result, std_result)
1837    } else {
1838      std_result
1839    }
1840  }
1841
1842  #[inline]
1843  fn exponent(self) -> f64x4 {
1844    const_f64_as_f64x4!(pow2_52, 4503599627370496.0);
1845    const_f64_as_f64x4!(bias, 1023.0);
1846    let a = cast::<_, u64x4>(self);
1847    let b = a >> 52;
1848    let c = b | cast::<_, u64x4>(pow2_52);
1849    let d = cast::<_, f64x4>(c);
1850    let e = d - (pow2_52 + bias);
1851    e
1852  }
1853
1854  #[inline]
1855  fn fraction_2(self) -> Self {
1856    let t1 = cast::<_, u64x4>(self);
1857    let t2 = cast::<_, u64x4>(
1858      (t1 & u64x4::from(0x000FFFFFFFFFFFFF)) | u64x4::from(0x3FE0000000000000),
1859    );
1860    cast::<_, f64x4>(t2)
1861  }
1862  #[inline]
1863  fn is_zero_or_subnormal(self) -> Self {
1864    let t = cast::<_, i64x4>(self);
1865    let t = t & i64x4::splat(0x7FF0000000000000);
1866    let mask = t.simd_eq(i64x4::splat(0));
1867    cast::<_, f64x4>(mask)
1868  }
1869  #[inline]
1870  fn infinity() -> Self {
1871    cast::<_, f64x4>(i64x4::splat(0x7FF0000000000000))
1872  }
1873  #[inline]
1874  fn nan_log() -> Self {
1875    cast::<_, f64x4>(i64x4::splat(0x7FF8000000000000 | 0x101 << 29))
1876  }
1877  #[inline]
1878  fn nan_pow() -> Self {
1879    cast::<_, f64x4>(i64x4::splat(0x7FF8000000000000 | 0x101 << 29))
1880  }
1881
1882  // Sometimes used for `transpose`.
1883  #[must_use]
1884  #[inline]
1885  #[allow(dead_code)]
1886  pub(crate) fn unpack_lo(self, b: Self) -> Self {
1887    pick! {
1888      if #[cfg(target_feature="avx")] {
1889        // `unpack_lo_m256d` cannot be used because it actually performs a
1890        // different operation.
1891        let [aa, _]: [f64x2; 2] = cast(self);
1892        let [ba, _]: [f64x2; 2] = cast(b);
1893        cast([aa.unpack_lo(ba), aa.unpack_hi(ba)])
1894      } else {
1895        Self { a: self.a.unpack_lo(b.a), b: self.a.unpack_hi(b.a) }
1896      }
1897    }
1898  }
1899
1900  // Sometimes used for `transpose`.
1901  #[must_use]
1902  #[inline]
1903  #[allow(dead_code)]
1904  pub(crate) fn unpack_hi(self, b: Self) -> Self {
1905    pick! {
1906      if #[cfg(target_feature="avx")] {
1907        // `unpack_hi_m256d` cannot be used because it actually performs a
1908        // different operation.
1909        let [_, ab]: [f64x2; 2] = cast(self);
1910        let [_, bb]: [f64x2; 2] = cast(b);
1911        cast([ab.unpack_lo(bb), ab.unpack_hi(bb)])
1912      } else {
1913        Self { a: self.b.unpack_lo(b.b), b: self.b.unpack_hi(b.b) }
1914      }
1915    }
1916  }
1917
1918  /// Converts each element from [`i32`] to [`f64`].
1919  #[inline]
1920  pub fn from_i32x4(v: i32x4) -> Self {
1921    pick! {
1922      if #[cfg(target_feature="avx")] {
1923        Self { avx: convert_to_m256d_from_i32_m128i(v.sse) }
1924      } else {
1925        Self::new([
1926          v.as_array()[0] as f64,
1927          v.as_array()[1] as f64,
1928          v.as_array()[2] as f64,
1929          v.as_array()[3] as f64,
1930        ])
1931      }
1932    }
1933  }
1934}
1935
1936impl From<i32x4> for f64x4 {
1937  #[inline]
1938  fn from(v: i32x4) -> Self {
1939    Self::from_i32x4(v)
1940  }
1941}