Debian Codegraph @

debian/missing-sources/simba/src/simd/portable_simd_impl.rs

raw
1 #![allow(missing_docs)]
2 #![allow(non_camel_case_types)] // For the simd type aliases.
3
4 //! Traits for SIMD values.
5
6 use crate::scalar::{ComplexField, Field, SubsetOf, SupersetOf};
7 use crate::simd::{
8 PrimitiveSimdValue, SimdBool, SimdComplexField, SimdPartialOrd, SimdRealField, SimdSigned,
9 SimdValue,
10 };
11 use approx::AbsDiffEq;
12 #[cfg(feature = "decimal")]
13 use decimal::d128;
14 use num::{FromPrimitive, Num, One, Zero};
15 use std::{
16 fmt,
17 ops::{
18 Add, AddAssign, BitAnd, BitOr, BitXor, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem,
19 RemAssign, Sub, SubAssign,
20 },
21 simd::{
22 self as portable_simd, cmp::SimdOrd, cmp::SimdPartialEq,
23 cmp::SimdPartialOrd as PortableSimdPartialOrd, num::SimdFloat, num::SimdInt, num::SimdUint,
24 StdFloat,
25 },
26 };
27
28 // This is a hack to allow use to reuse `_0` as integers or as identifier,
29 // depending on whether or not `ident_to_value` has been called in scope.
30 // This helps writing macros that define both `::new` and `From([T; lanes()])`.
31 macro_rules! ident_to_value (
32 () => {
33 const _0: usize = 0; const _1: usize = 1; const _2: usize = 2; const _3: usize = 3; const _4: usize = 4; const _5: usize = 5; const _6: usize = 6; const _7: usize = 7;
34 const _8: usize = 8; const _9: usize = 9; const _10: usize = 10; const _11: usize = 11; const _12: usize = 12; const _13: usize = 13; const _14: usize = 14; const _15: usize = 15;
35 const _16: usize = 16; const _17: usize = 17; const _18: usize = 18; const _19: usize = 19; const _20: usize = 20; const _21: usize = 21; const _22: usize = 22; const _23: usize = 23;
36 const _24: usize = 24; const _25: usize = 25; const _26: usize = 26; const _27: usize = 27; const _28: usize = 28; const _29: usize = 29; const _30: usize = 30; const _31: usize = 31;
37 const _32: usize = 32; const _33: usize = 33; const _34: usize = 34; const _35: usize = 35; const _36: usize = 36; const _37: usize = 37; const _38: usize = 38; const _39: usize = 39;
38 const _40: usize = 40; const _41: usize = 41; const _42: usize = 42; const _43: usize = 43; const _44: usize = 44; const _45: usize = 45; const _46: usize = 46; const _47: usize = 47;
39 const _48: usize = 48; const _49: usize = 49; const _50: usize = 50; const _51: usize = 51; const _52: usize = 52; const _53: usize = 53; const _54: usize = 54; const _55: usize = 55;
40 const _56: usize = 56; const _57: usize = 57; const _58: usize = 58; const _59: usize = 59; const _60: usize = 60; const _61: usize = 61; const _62: usize = 62; const _63: usize = 63;
41 }
42 );
43
44 /// An Simd structure that implements all the relevant traits from `num` an `simba`.
45 ///
46 /// This is needed to overcome the orphan rules.
47 #[repr(transparent)]
48 #[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
49 pub struct Simd<N>(pub N);
50
51 macro_rules! impl_bool_simd (
52 ($($t: ty, $lanes: literal, $($i: ident),*;)*) => {$(
53 impl fmt::Display for Simd<$t> {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 if Self::LANES == 1 {
56 return self.extract(0).fmt(f);
57 }
58
59 write!(f, "({}", self.extract(0))?;
60
61 for i in 1..Self::LANES {
62 write!(f, ", {}", self.extract(i))?;
63 }
64
65 write!(f, ")")
66 }
67 }
68
69 impl Simd<$t> {
70 #[inline]
71 pub fn new($($i: bool),*) -> Self {
72 Simd(<$t>::from_array([$($i),*]))
73 }
74 }
75
76 impl PrimitiveSimdValue for Simd<$t> {}
77
78 impl SimdValue for Simd<$t> {
79 const LANES: usize = $lanes;
80 type Element = bool;
81 type SimdBool = Simd<$t>;
82
83 #[inline(always)]
84 fn splat(val: Self::Element) -> Self {
85 Simd(<$t>::splat(val))
86 }
87
88 #[inline(always)]
89 fn extract(&self, i: usize) -> Self::Element {
90 self.0.test(i)
91 }
92
93 #[inline(always)]
94 unsafe fn extract_unchecked(&self, i: usize) -> Self::Element {
95 self.0.test_unchecked(i)
96 }
97
98 #[inline(always)]
99 fn replace(&mut self, i: usize, val: Self::Element) {
100 self.0.set(i, val)
101 }
102
103 #[inline(always)]
104 unsafe fn replace_unchecked(&mut self, i: usize, val: Self::Element) {
105 self.0.set_unchecked(i, val)
106 }
107
108 #[inline(always)]
109 fn select(self, cond: Self::SimdBool, other: Self) -> Self {
110 let x = cond.0.select(self.0.to_int(), other.0.to_int());
111 Self(<$t>::from_int(x))
112 }
113 }
114
115 impl From<[bool; $lanes]> for Simd<$t> {
116 #[inline(always)]
117 fn from(vals: [bool; $lanes]) -> Self {
118 ident_to_value!();
119 Simd(<$t>::from_array(vals))
120 }
121 }
122
123 impl Not for Simd<$t> {
124 type Output = Self;
125
126 #[inline(always)]
127 fn not(self) -> Self {
128 Self(!self.0)
129 }
130 }
131
132 impl BitAnd<Simd<$t>> for Simd<$t> {
133 type Output = Self;
134 #[inline(always)]
135 fn bitand(self, rhs: Self) -> Self {
136 Simd(self.0.bitand(rhs.0))
137 }
138 }
139
140 impl BitOr<Simd<$t>> for Simd<$t> {
141 type Output = Self;
142 #[inline(always)]
143 fn bitor(self, rhs: Self) -> Self {
144 Simd(self.0.bitor(rhs.0))
145 }
146 }
147
148 impl BitXor<Simd<$t>> for Simd<$t> {
149 type Output = Self;
150 #[inline(always)]
151 fn bitxor(self, rhs: Self) -> Self {
152 Simd(self.0.bitxor(rhs.0))
153 }
154 }
155
156 impl SimdBool for Simd<$t> {
157 #[inline(always)]
158 fn bitmask(self) -> u64 {
159 self.0.to_bitmask() as u64
160 }
161
162 #[inline(always)]
163 fn and(self) -> bool {
164 self.0.to_int().reduce_and() != 0
165 }
166
167 #[inline(always)]
168 fn or(self) -> bool {
169 self.0.to_int().reduce_or() != 0
170 }
171
172 #[inline(always)]
173 fn xor(self) -> bool {
174 self.0.to_int().reduce_xor() != 0
175 }
176
177 #[inline(always)]
178 fn all(self) -> bool {
179 self.0.all()
180 }
181
182 #[inline(always)]
183 fn any(self) -> bool {
184 self.0.any()
185 }
186
187 #[inline(always)]
188 fn none(self) -> bool {
189 !self.0.any()
190 }
191
192 #[inline(always)]
193 fn if_else<Res: SimdValue<SimdBool = Self>>(
194 self,
195 if_value: impl FnOnce() -> Res,
196 else_value: impl FnOnce() -> Res,
197 ) -> Res {
198 let a = if_value();
199 let b = else_value();
200 a.select(self, b)
201 }
202
203 #[inline(always)]
204 fn if_else2<Res: SimdValue<SimdBool = Self>>(
205 self,
206 if_value: impl FnOnce() -> Res,
207 else_if: (impl FnOnce() -> Self, impl FnOnce() -> Res),
208 else_value: impl FnOnce() -> Res,
209 ) -> Res {
210 let a = if_value();
211 let b = else_if.1();
212 let c = else_value();
213
214 let cond_a = self;
215 let cond_b = else_if.0();
216
217 a.select(cond_a, b.select(cond_b, c))
218 }
219
220 #[inline(always)]
221 fn if_else3<Res: SimdValue<SimdBool = Self>>(
222 self,
223 if_value: impl FnOnce() -> Res,
224 else_if: (impl FnOnce() -> Self, impl FnOnce() -> Res),
225 else_else_if: (impl FnOnce() -> Self, impl FnOnce() -> Res),
226 else_value: impl FnOnce() -> Res,
227 ) -> Res {
228 let a = if_value();
229 let b = else_if.1();
230 let c = else_else_if.1();
231 let d = else_value();
232
233 let cond_a = self;
234 let cond_b = else_if.0();
235 let cond_c = else_else_if.0();
236
237 a.select(cond_a, b.select(cond_b, c.select(cond_c, d)))
238 }
239 }
240 )*}
241 );
242
243 macro_rules! impl_scalar_subset_of_simd (
244 ($($t: ty),*) => {$(
245 impl<N2> SubsetOf<Simd<N2>> for $t
246 where Simd<N2>: SimdValue + Copy,
247 <Simd<N2> as SimdValue>::Element: SupersetOf<$t> + PartialEq, {
248 #[inline(always)]
249 fn to_superset(&self) -> Simd<N2> {
250 Simd::<N2>::splat(<Simd<N2> as SimdValue>::Element::from_subset(self))
251 }
252
253 #[inline(always)]
254 fn from_superset_unchecked(element: &Simd<N2>) -> $t {
255 element.extract(0).to_subset_unchecked()
256 }
257
258 #[inline(always)]
259 fn is_in_subset(c: &Simd<N2>) -> bool {
260 let elt0 = c.extract(0);
261 elt0.is_in_subset() &&
262 (1..Simd::<N2>::LANES).all(|i| c.extract(i) == elt0)
263 }
264 }
265 )*}
266 );
267
268 impl_scalar_subset_of_simd!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);
269 #[cfg(feature = "decimal")]
270 impl_scalar_subset_of_simd!(d128);
271
272 macro_rules! impl_simd_value (
273 ($($t: ty, $elt: ty, $bool: ty, $($i: ident),*;)*) => ($(
274 impl fmt::Display for Simd<$t> {
275 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
276 if Self::LANES == 1 {
277 return self.extract(0).fmt(f);
278 }
279
280 write!(f, "({}", self.extract(0))?;
281
282 for i in 1..Self::LANES {
283 write!(f, ", {}", self.extract(i))?;
284 }
285
286 write!(f, ")")
287 }
288 }
289
290 impl Simd<$t> {
291 pub const ZERO: Self = Simd(<$t>::from_array([0 as $elt; <$t>::LEN]));
292 pub const ONE: Self = Simd(<$t>::from_array([1 as $elt; <$t>::LEN]));
293
294 #[inline]
295 pub fn new($($i: $elt),*) -> Self {
296 Simd(<$t>::from_array([$($i),*]))
297 }
298 }
299
300 impl PrimitiveSimdValue for Simd<$t> {}
301
302 impl SimdValue for Simd<$t> {
303 const LANES: usize = <$t>::LEN;
304 type Element = $elt;
305 type SimdBool = $bool;
306
307 #[inline(always)]
308 fn splat(val: Self::Element) -> Self {
309 Simd(<$t>::splat(val))
310 }
311
312 #[inline(always)]
313 fn extract(&self, i: usize) -> Self::Element {
314 self.0[i]
315 }
316
317 #[inline(always)]
318 unsafe fn extract_unchecked(&self, i: usize) -> Self::Element {
319 self.0[i]
320 }
321
322 #[inline(always)]
323 fn replace(&mut self, i: usize, val: Self::Element) {
324 self.0[i] = val;
325 }
326
327 #[inline(always)]
328 unsafe fn replace_unchecked(&mut self, i: usize, val: Self::Element) {
329 self.0[i] = val;
330 }
331
332 #[inline(always)]
333 fn select(self, cond: Self::SimdBool, other: Self) -> Self {
334 Self(cond.0.select(self.0, other.0))
335 }
336 }
337 )*)
338 );
339
340 macro_rules! impl_uint_simd (
341 ($($t: ty, $elt: ty, $bool: ty, $($i: ident),*;)*) => ($(
342 impl_simd_value!($t, $elt, $bool $(, $i)*;);
343
344 impl Simd<$t> {
345 /// Instantiates a new vector with the values of the `slice`.
346 ///
347 /// # Panics
348 ///
349 /// If `slice.len() < Self::LANES`.
350 #[inline]
351 pub fn from_slice_unaligned(slice: &[$elt]) -> Self {
352 Simd(<$t>::from_slice(slice))
353 }
354 }
355
356 impl From<[$elt; <$t>::LEN]> for Simd<$t> {
357 #[inline(always)]
358 fn from(vals: [$elt; <$t>::LEN]) -> Self {
359 Simd(<$t>::from(vals))
360 }
361 }
362
363 impl From<Simd<$t>> for [$elt; <$t>::LEN] {
364 #[inline(always)]
365 fn from(val: Simd<$t>) -> [$elt; <$t>::LEN] {
366 val.0.to_array()
367 }
368 }
369
370 impl SubsetOf<Simd<$t>> for Simd<$t> {
371 #[inline(always)]
372 fn to_superset(&self) -> Self {
373 *self
374 }
375
376 #[inline(always)]
377 fn from_superset(element: &Self) -> Option<Self> {
378 Some(*element)
379 }
380
381 #[inline(always)]
382 fn from_superset_unchecked(element: &Self) -> Self {
383 *element
384 }
385
386 #[inline(always)]
387 fn is_in_subset(_: &Self) -> bool {
388 true
389 }
390 }
391
392 impl Num for Simd<$t> {
393 type FromStrRadixErr = <$elt as Num>::FromStrRadixErr;
394
395 #[inline(always)]
396 fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
397 <$elt>::from_str_radix(str, radix).map(Self::splat)
398 }
399 }
400
401 impl FromPrimitive for Simd<$t> {
402 #[inline(always)]
403 fn from_i64(n: i64) -> Option<Self> {
404 <$elt>::from_i64(n).map(Self::splat)
405 }
406
407 #[inline(always)]
408 fn from_u64(n: u64) -> Option<Self> {
409 <$elt>::from_u64(n).map(Self::splat)
410 }
411
412 #[inline(always)]
413 fn from_isize(n: isize) -> Option<Self> {
414 <$elt>::from_isize(n).map(Self::splat)
415 }
416
417 #[inline(always)]
418 fn from_i8(n: i8) -> Option<Self> {
419 <$elt>::from_i8(n).map(Self::splat)
420 }
421
422 #[inline(always)]
423 fn from_i16(n: i16) -> Option<Self> {
424 <$elt>::from_i16(n).map(Self::splat)
425 }
426
427 #[inline(always)]
428 fn from_i32(n: i32) -> Option<Self> {
429 <$elt>::from_i32(n).map(Self::splat)
430 }
431
432 #[inline(always)]
433 fn from_usize(n: usize) -> Option<Self> {
434 <$elt>::from_usize(n).map(Self::splat)
435 }
436
437 #[inline(always)]
438 fn from_u8(n: u8) -> Option<Self> {
439 <$elt>::from_u8(n).map(Self::splat)
440 }
441
442 #[inline(always)]
443 fn from_u16(n: u16) -> Option<Self> {
444 <$elt>::from_u16(n).map(Self::splat)
445 }
446
447 #[inline(always)]
448 fn from_u32(n: u32) -> Option<Self> {
449 <$elt>::from_u32(n).map(Self::splat)
450 }
451
452 #[inline(always)]
453 fn from_f32(n: f32) -> Option<Self> {
454 <$elt>::from_f32(n).map(Self::splat)
455 }
456
457 #[inline(always)]
458 fn from_f64(n: f64) -> Option<Self> {
459 <$elt>::from_f64(n).map(Self::splat)
460 }
461 }
462
463
464 impl Zero for Simd<$t> {
465 #[inline(always)]
466 fn zero() -> Self {
467 Simd(<$t>::splat(<$elt>::zero()))
468 }
469
470 #[inline(always)]
471 fn is_zero(&self) -> bool {
472 *self == Self::zero()
473 }
474 }
475
476 impl One for Simd<$t> {
477 #[inline(always)]
478 fn one() -> Self {
479 Simd(<$t>::splat(<$elt>::one()))
480 }
481 }
482
483 impl Add<Simd<$t>> for Simd<$t> {
484 type Output = Self;
485
486 #[inline(always)]
487 fn add(self, rhs: Self) -> Self {
488 Self(self.0 + rhs.0)
489 }
490 }
491
492 impl Sub<Simd<$t>> for Simd<$t> {
493 type Output = Self;
494
495 #[inline(always)]
496 fn sub(self, rhs: Self) -> Self {
497 Self(self.0 - rhs.0)
498 }
499 }
500
501 impl Mul<Simd<$t>> for Simd<$t> {
502 type Output = Self;
503
504 #[inline(always)]
505 fn mul(self, rhs: Self) -> Self {
506 Self(self.0 * rhs.0)
507 }
508 }
509
510 impl Div<Simd<$t>> for Simd<$t> {
511 type Output = Self;
512
513 #[inline(always)]
514 fn div(self, rhs: Self) -> Self {
515 Self(self.0 / rhs.0)
516 }
517 }
518
519 impl Rem<Simd<$t>> for Simd<$t> {
520 type Output = Self;
521
522 #[inline(always)]
523 fn rem(self, rhs: Self) -> Self {
524 Self(self.0 % rhs.0)
525 }
526 }
527
528 impl AddAssign<Simd<$t>> for Simd<$t> {
529 #[inline(always)]
530 fn add_assign(&mut self, rhs: Self) {
531 self.0 += rhs.0
532 }
533 }
534
535 impl SubAssign<Simd<$t>> for Simd<$t> {
536 #[inline(always)]
537 fn sub_assign(&mut self, rhs: Self) {
538 self.0 -= rhs.0
539 }
540 }
541
542 impl DivAssign<Simd<$t>> for Simd<$t> {
543 #[inline(always)]
544 fn div_assign(&mut self, rhs: Self) {
545 self.0 /= rhs.0
546 }
547 }
548
549 impl MulAssign<Simd<$t>> for Simd<$t> {
550 #[inline(always)]
551 fn mul_assign(&mut self, rhs: Self) {
552 self.0 *= rhs.0
553 }
554 }
555
556 impl RemAssign<Simd<$t>> for Simd<$t> {
557 #[inline(always)]
558 fn rem_assign(&mut self, rhs: Self) {
559 self.0 %= rhs.0
560 }
561 }
562
563 impl SimdPartialOrd for Simd<$t> {
564 #[inline(always)]
565 fn simd_gt(self, other: Self) -> Self::SimdBool {
566 Simd(self.0.simd_gt(other.0))
567 }
568
569 #[inline(always)]
570 fn simd_lt(self, other: Self) -> Self::SimdBool {
571 Simd(self.0.simd_lt(other.0))
572 }
573
574 #[inline(always)]
575 fn simd_ge(self, other: Self) -> Self::SimdBool {
576 Simd(self.0.simd_ge(other.0))
577 }
578
579 #[inline(always)]
580 fn simd_le(self, other: Self) -> Self::SimdBool {
581 Simd(self.0.simd_le(other.0))
582 }
583
584 #[inline(always)]
585 fn simd_eq(self, other: Self) -> Self::SimdBool {
586 Simd(self.0.simd_eq(other.0))
587 }
588
589 #[inline(always)]
590 fn simd_ne(self, other: Self) -> Self::SimdBool {
591 Simd(self.0.simd_ne(other.0))
592 }
593
594 #[inline(always)]
595 fn simd_max(self, other: Self) -> Self {
596 Simd(self.0.simd_max(other.0))
597 }
598 #[inline(always)]
599 fn simd_min(self, other: Self) -> Self {
600 Simd(self.0.simd_min(other.0))
601 }
602
603 #[inline(always)]
604 fn simd_clamp(self, min: Self, max: Self) -> Self {
605 self.simd_max(min).simd_min(max)
606 }
607
608 #[inline(always)]
609 fn simd_horizontal_min(self) -> Self::Element {
610 self.0.reduce_min()
611 }
612
613 #[inline(always)]
614 fn simd_horizontal_max(self) -> Self::Element {
615 self.0.reduce_max()
616 }
617 }
618
619 // impl MeetSemilattice for Simd<$t> {
620 // #[inline(always)]
621 // fn meet(&self, other: &Self) -> Self {
622 // Simd(self.0.min(other.0))
623 // }
624 // }
625 //
626 // impl JoinSemilattice for Simd<$t> {
627 // #[inline(always)]
628 // fn join(&self, other: &Self) -> Self {
629 // Simd(self.0.max(other.0))
630 // }
631 // }
632 )*)
633 );
634
635 macro_rules! impl_int_simd (
636 ($($t: ty, $elt: ty, $bool: ty, $($i: ident),*;)*) => ($(
637 impl_uint_simd!($t, $elt, $bool $(, $i)*;);
638
639 impl Neg for Simd<$t> {
640 type Output = Self;
641
642 #[inline(always)]
643 fn neg(self) -> Self {
644 Self(-self.0)
645 }
646 }
647 )*)
648 );
649
650 macro_rules! impl_float_simd (
651 ($($t: ty, $elt: ident, $int: ty, $bool: ty, $($i: ident),*;)*) => ($(
652 impl_int_simd!($t, $elt, $bool $(, $i)*;);
653
654 // TODO: this should be part of impl_int_simd
655 // but those methods do not seem to be implemented
656 // by portable_simd for integers.
657 impl SimdSigned for Simd<$t> {
658 #[inline(always)]
659 fn simd_abs(&self) -> Self {
660 Simd(self.0.abs())
661 }
662
663 #[inline(always)]
664 fn simd_abs_sub(&self, other: &Self) -> Self {
665 Simd((self.0 - other.0).simd_max(Self::zero().0))
666 }
667
668 #[inline(always)]
669 fn simd_signum(&self) -> Self {
670 // NOTE: is there a more efficient way of doing this?
671 let zero = Self::zero().0;
672 let one = Self::one().0;
673 let gt = self.0.simd_gt(zero);
674 let lt = self.0.simd_lt(zero);
675 Simd(lt.select(-one, gt.select(one, zero)))
676 }
677
678 #[inline(always)]
679 fn is_simd_positive(&self) -> Self::SimdBool {
680 self.simd_gt(Self::zero())
681 }
682
683 #[inline(always)]
684 fn is_simd_negative(&self) -> Self::SimdBool {
685 self.simd_lt(Self::zero())
686 }
687 }
688
689 impl Field for Simd<$t> {}
690
691 impl SimdRealField for Simd<$t> {
692 #[inline(always)]
693 fn simd_atan2(self, other: Self) -> Self {
694 self.zip_map_lanes(other, |a, b| a.atan2(b))
695 }
696
697 #[inline(always)]
698 fn simd_copysign(self, sign: Self) -> Self {
699 Simd(self.0.copysign(sign.0))
700 }
701
702 #[inline(always)]
703 fn simd_default_epsilon() -> Self {
704 Self::splat(<$elt>::default_epsilon())
705 }
706
707 #[inline(always)]
708 fn simd_pi() -> Self {
709 use std::$elt::consts::*;
710 Self::splat(PI)
711 }
712
713 #[inline(always)]
714 fn simd_two_pi() -> Self {
715 use std::$elt::consts::*;
716 Self::splat(PI + PI)
717 }
718
719 #[inline(always)]
720 fn simd_frac_pi_2() -> Self {
721 use std::$elt::consts::*;
722 Self::splat(FRAC_PI_2)
723 }
724
725 #[inline(always)]
726 fn simd_frac_pi_3() -> Self {
727 use std::$elt::consts::*;
728 Self::splat(FRAC_PI_3)
729 }
730
731 #[inline(always)]
732 fn simd_frac_pi_4() -> Self {
733 use std::$elt::consts::*;
734 Self::splat(FRAC_PI_4)
735 }
736
737 #[inline(always)]
738 fn simd_frac_pi_6() -> Self {
739 use std::$elt::consts::*;
740 Self::splat(FRAC_PI_6)
741 }
742
743 #[inline(always)]
744 fn simd_frac_pi_8() -> Self {
745 use std::$elt::consts::*;
746 Self::splat(FRAC_PI_8)
747 }
748
749 #[inline(always)]
750 fn simd_frac_1_pi() -> Self {
751 use std::$elt::consts::*;
752 Self::splat(FRAC_1_PI)
753 }
754
755 #[inline(always)]
756 fn simd_frac_2_pi() -> Self {
757 use std::$elt::consts::*;
758 Self::splat(FRAC_2_PI)
759 }
760
761 #[inline(always)]
762 fn simd_frac_2_sqrt_pi() -> Self {
763 use std::$elt::consts::*;
764 Self::splat(FRAC_2_SQRT_PI)
765 }
766
767 #[inline(always)]
768 fn simd_e() -> Self {
769 use std::$elt::consts::*;
770 Self::splat(E)
771 }
772
773 #[inline(always)]
774 fn simd_log2_e() -> Self {
775 use std::$elt::consts::*;
776 Self::splat(LOG2_E)
777 }
778
779 #[inline(always)]
780 fn simd_log10_e() -> Self {
781 use std::$elt::consts::*;
782 Self::splat(LOG10_E)
783 }
784
785 #[inline(always)]
786 fn simd_ln_2() -> Self {
787 use std::$elt::consts::*;
788 Self::splat(LN_2)
789 }
790
791 #[inline(always)]
792 fn simd_ln_10() -> Self {
793 use std::$elt::consts::*;
794 Self::splat(LN_10)
795 }
796 }
797
798 impl SimdComplexField for Simd<$t> {
799 type SimdRealField = Self;
800
801 #[inline(always)]
802 fn simd_horizontal_sum(self) -> Self::Element {
803 self.0.reduce_sum()
804 }
805
806 #[inline(always)]
807 fn simd_horizontal_product(self) -> Self::Element {
808 self.0.reduce_product()
809 }
810
811 #[inline(always)]
812 fn from_simd_real(re: Self::SimdRealField) -> Self {
813 re
814 }
815
816 #[inline(always)]
817 fn simd_real(self) -> Self::SimdRealField {
818 self
819 }
820
821 #[inline(always)]
822 fn simd_imaginary(self) -> Self::SimdRealField {
823 Self::zero()
824 }
825
826 #[inline(always)]
827 fn simd_norm1(self) -> Self::SimdRealField {
828 Simd(self.0.abs())
829 }
830
831 #[inline(always)]
832 fn simd_modulus(self) -> Self::SimdRealField {
833 Simd(self.0.abs())
834 }
835
836 #[inline(always)]
837 fn simd_modulus_squared(self) -> Self::SimdRealField {
838 self * self
839 }
840
841 #[inline(always)]
842 fn simd_argument(self) -> Self::SimdRealField {
843 self.map_lanes(|e| e.argument())
844 }
845
846 #[inline(always)]
847 fn simd_to_exp(self) -> (Self::SimdRealField, Self) {
848 let ge = self.0.simd_ge(Self::one().0);
849 let exp = ge.select(Self::one().0, -Self::one().0);
850 (Simd(self.0 * exp), Simd(exp))
851 }
852
853 #[inline(always)]
854 fn simd_recip(self) -> Self {
855 Self(self.0.recip())
856 }
857
858 #[inline(always)]
859 fn simd_conjugate(self) -> Self {
860 self
861 }
862
863 #[inline(always)]
864 fn simd_scale(self, factor: Self::SimdRealField) -> Self {
865 Simd(self.0 * factor.0)
866 }
867
868 #[inline(always)]
869 fn simd_unscale(self, factor: Self::SimdRealField) -> Self {
870 Simd(self.0 / factor.0)
871 }
872
873 #[inline(always)]
874 fn simd_floor(self) -> Self {
875 Self(self.0.floor())
876 }
877
878 #[inline(always)]
879 fn simd_ceil(self) -> Self {
880 Self(self.0.ceil())
881 }
882
883 #[inline(always)]
884 fn simd_round(self) -> Self {
885 Self(self.0.round())
886 }
887
888 #[inline(always)]
889 fn simd_trunc(self) -> Self {
890 Self(self.0.trunc())
891 }
892
893 #[inline(always)]
894 fn simd_fract(self) -> Self {
895 Self(self.0.fract())
896 }
897
898 #[inline(always)]
899 fn simd_abs(self) -> Self {
900 Self(self.0.abs())
901 }
902
903 #[inline(always)]
904 fn simd_signum(self) -> Self {
905 Self(self.0.signum())
906 }
907
908 #[inline(always)]
909 fn simd_mul_add(self, a: Self, b: Self) -> Self {
910 Simd(self.0.mul_add(a.0, b.0))
911 }
912
913 #[inline(always)]
914 fn simd_powi(self, n: i32) -> Self {
915 self.map_lanes(|e| e.powi(n))
916 }
917
918 #[inline(always)]
919 fn simd_powf(self, n: Self) -> Self {
920 self.zip_map_lanes(n, |e, p| e.powf(p))
921 }
922
923 #[inline(always)]
924 fn simd_powc(self, n: Self) -> Self {
925 self.simd_powf(n)
926 }
927
928 #[inline(always)]
929 fn simd_sqrt(self) -> Self {
930 Simd(self.0.sqrt())
931 }
932
933 #[inline(always)]
934 fn simd_exp(self) -> Self {
935 Self(self.0.exp())
936 }
937
938 #[inline(always)]
939 fn simd_exp2(self) -> Self {
940 Self(self.0.exp2())
941 }
942
943 #[inline(always)]
944 fn simd_exp_m1(self) -> Self {
945 self.map_lanes(|e| e.exp_m1())
946 }
947
948 #[inline(always)]
949 fn simd_ln_1p(self) -> Self {
950 self.map_lanes(|e| e.ln_1p())
951 }
952
953 #[inline(always)]
954 fn simd_ln(self) -> Self {
955 Self(self.0.ln())
956 }
957
958 #[inline(always)]
959 fn simd_log(self, base: Self) -> Self {
960 self.zip_map_lanes(base, |e, b| e.log(b))
961 }
962
963 #[inline(always)]
964 fn simd_log2(self) -> Self {
965 Self(self.0.log2())
966 }
967
968 #[inline(always)]
969 fn simd_log10(self) -> Self {
970 Self(self.0.log10())
971 }
972
973 #[inline(always)]
974 fn simd_cbrt(self) -> Self {
975 self.map_lanes(|e| e.cbrt())
976 }
977
978 #[inline(always)]
979 fn simd_hypot(self, other: Self) -> Self::SimdRealField {
980 self.zip_map_lanes(other, |e, o| e.hypot(o))
981 }
982
983 #[inline(always)]
984 fn simd_sin(self) -> Self {
985 Self(self.0.sin())
986 }
987
988 #[inline(always)]
989 fn simd_cos(self) -> Self {
990 Self(self.0.cos())
991 }
992
993 #[inline(always)]
994 fn simd_tan(self) -> Self {
995 self.map_lanes(|e| e.tan())
996 }
997
998 #[inline(always)]
999 fn simd_asin(self) -> Self {
1000 self.map_lanes(|e| e.asin())
1001 }
1002
1003 #[inline(always)]
1004 fn simd_acos(self) -> Self {
1005 self.map_lanes(|e| e.acos())
1006 }
1007
1008 #[inline(always)]
1009 fn simd_atan(self) -> Self {
1010 self.map_lanes(|e| e.atan())
1011 }
1012
1013 #[inline(always)]
1014 fn simd_sin_cos(self) -> (Self, Self) {
1015 (self.simd_sin(), self.simd_cos())
1016 }
1017
1018 // #[inline(always]
1019 // fn simd_exp_m1(self) -> Self {
1020 // $libm::exp_m1(self)
1021 // }
1022 //
1023 // #[inline(always]
1024 // fn simd_ln_1p(self) -> Self {
1025 // $libm::ln_1p(self)
1026 // }
1027 //
1028 #[inline(always)]
1029 fn simd_sinh(self) -> Self {
1030 self.map_lanes(|e| e.sinh())
1031 }
1032
1033 #[inline(always)]
1034 fn simd_cosh(self) -> Self {
1035 self.map_lanes(|e| e.cosh())
1036 }
1037
1038 #[inline(always)]
1039 fn simd_tanh(self) -> Self {
1040 self.map_lanes(|e| e.tanh())
1041 }
1042
1043 #[inline(always)]
1044 fn simd_asinh(self) -> Self {
1045 self.map_lanes(|e| e.asinh())
1046 }
1047
1048 #[inline(always)]
1049 fn simd_acosh(self) -> Self {
1050 self.map_lanes(|e| e.acosh())
1051 }
1052
1053 #[inline(always)]
1054 fn simd_atanh(self) -> Self {
1055 self.map_lanes(|e| e.atanh())
1056 }
1057 }
1058
1059 // NOTE: most of the impls in there are copy-paste from the implementation of
1060 // ComplexField for num_complex::Complex. Unfortunately, we can't reuse the implementations
1061 // so easily.
1062 impl SimdComplexField for num_complex::Complex<Simd<$t>> {
1063 type SimdRealField = Simd<$t>;
1064
1065 #[inline(always)]
1066 fn simd_horizontal_sum(self) -> Self::Element {
1067 num_complex::Complex::new(self.re.simd_horizontal_sum(), self.im.simd_horizontal_sum())
1068 }
1069
1070 #[inline(always)]
1071 fn simd_horizontal_product(self) -> Self::Element {
1072 let mut prod = self.extract(0);
1073 for ii in 1..Self::LANES {
1074 prod *= self.extract(ii)
1075 }
1076 prod
1077 }
1078
1079 #[inline]
1080 fn from_simd_real(re: Self::SimdRealField) -> Self {
1081 Self::new(re, Self::SimdRealField::zero())
1082 }
1083
1084 #[inline]
1085 fn simd_real(self) -> Self::SimdRealField {
1086 self.re
1087 }
1088
1089 #[inline]
1090 fn simd_imaginary(self) -> Self::SimdRealField {
1091 self.im
1092 }
1093
1094 #[inline]
1095 fn simd_argument(self) -> Self::SimdRealField {
1096 self.im.simd_atan2(self.re)
1097 }
1098
1099 #[inline]
1100 fn simd_modulus(self) -> Self::SimdRealField {
1101 self.re.simd_hypot(self.im)
1102 }
1103
1104 #[inline]
1105 fn simd_modulus_squared(self) -> Self::SimdRealField {
1106 self.re * self.re + self.im * self.im
1107 }
1108
1109 #[inline]
1110 fn simd_norm1(self) -> Self::SimdRealField {
1111 self.re.simd_abs() + self.im.simd_abs()
1112 }
1113
1114 #[inline]
1115 fn simd_recip(self) -> Self {
1116 Self::one() / self
1117 }
1118
1119 #[inline]
1120 fn simd_conjugate(self) -> Self {
1121 self.conj()
1122 }
1123
1124 #[inline]
1125 fn simd_scale(self, factor: Self::SimdRealField) -> Self {
1126 self * factor
1127 }
1128
1129 #[inline]
1130 fn simd_unscale(self, factor: Self::SimdRealField) -> Self {
1131 self / factor
1132 }
1133
1134 #[inline]
1135 fn simd_floor(self) -> Self {
1136 Self::new(self.re.simd_floor(), self.im.simd_floor())
1137 }
1138
1139 #[inline]
1140 fn simd_ceil(self) -> Self {
1141 Self::new(self.re.simd_ceil(), self.im.simd_ceil())
1142 }
1143
1144 #[inline]
1145 fn simd_round(self) -> Self {
1146 Self::new(self.re.simd_round(), self.im.simd_round())
1147 }
1148
1149 #[inline]
1150 fn simd_trunc(self) -> Self {
1151 Self::new(self.re.simd_trunc(), self.im.simd_trunc())
1152 }
1153
1154 #[inline]
1155 fn simd_fract(self) -> Self {
1156 Self::new(self.re.simd_fract(), self.im.simd_fract())
1157 }
1158
1159 #[inline]
1160 fn simd_mul_add(self, a: Self, b: Self) -> Self {
1161 self * a + b
1162 }
1163
1164 #[inline]
1165 fn simd_abs(self) -> Self::SimdRealField {
1166 self.simd_modulus()
1167 }
1168
1169 #[inline]
1170 fn simd_exp2(self) -> Self {
1171 let _2 = Simd::<$t>::one() + Simd::<$t>::one();
1172 num_complex::Complex::new(_2, Simd::<$t>::zero()).simd_powc(self)
1173 }
1174
1175 #[inline]
1176 fn simd_exp_m1(self) -> Self {
1177 self.simd_exp() - Self::one()
1178 }
1179
1180 #[inline]
1181 fn simd_ln_1p(self) -> Self {
1182 (Self::one() + self).simd_ln()
1183 }
1184
1185 #[inline]
1186 fn simd_log2(self) -> Self {
1187 let _2 = Simd::<$t>::one() + Simd::<$t>::one();
1188 self.simd_log(_2)
1189 }
1190
1191 #[inline]
1192 fn simd_log10(self) -> Self {
1193 let _10 = Simd::<$t>::from_subset(&10.0f64);
1194 self.simd_log(_10)
1195 }
1196
1197 #[inline]
1198 fn simd_cbrt(self) -> Self {
1199 let one_third = Simd::<$t>::from_subset(&(1.0 / 3.0));
1200 self.simd_powf(one_third)
1201 }
1202
1203 #[inline]
1204 fn simd_powi(self, n: i32) -> Self {
1205 // TODO: is there a more accurate solution?
1206 let n = Simd::<$t>::from_subset(&(n as f64));
1207 self.simd_powf(n)
1208 }
1209
1210 /*
1211 *
1212 *
1213 * Unfortunately we are forced to copy-paste all
1214 * those impls from https://github.com/rust-num/num-complex/blob/master/src/lib.rs
1215 * to avoid requiring `std`.
1216 *
1217 *
1218 */
1219 /// Computes `e^(self)`, where `e` is the base of the natural logarithm.
1220 #[inline]
1221 fn simd_exp(self) -> Self {
1222 // formula: e^(a + bi) = e^a (cos(b) + i*sin(b))
1223 // = from_polar(e^a, b)
1224 simd_complex_from_polar(self.re.simd_exp(), self.im)
1225 }
1226
1227 /// Computes the principal value of natural logarithm of `self`.
1228 ///
1229 /// This function has one branch cut:
1230 ///
1231 /// * `(-∞, 0]`, continuous from above.
1232 ///
1233 /// The branch satisfies `-π ≤ arg(ln(z)) ≤ π`.
1234 #[inline]
1235 fn simd_ln(self) -> Self {
1236 // formula: ln(z) = ln|z| + i*arg(z)
1237 let (r, theta) = self.simd_to_polar();
1238 Self::new(r.simd_ln(), theta)
1239 }
1240
1241 /// Computes the principal value of the square root of `self`.
1242 ///
1243 /// This function has one branch cut:
1244 ///
1245 /// * `(-∞, 0)`, continuous from above.
1246 ///
1247 /// The branch satisfies `-π/2 ≤ arg(sqrt(z)) ≤ π/2`.
1248 #[inline]
1249 fn simd_sqrt(self) -> Self {
1250 // formula: sqrt(r e^(it)) = sqrt(r) e^(it/2)
1251 let two = Simd::<$t>::one() + Simd::<$t>::one();
1252 let (r, theta) = self.simd_to_polar();
1253 simd_complex_from_polar(r.simd_sqrt(), theta / two)
1254 }
1255
1256 #[inline]
1257 fn simd_hypot(self, b: Self) -> Self::SimdRealField {
1258 (self.simd_modulus_squared() + b.simd_modulus_squared()).simd_sqrt()
1259 }
1260
1261 /// Raises `self` to a floating point power.
1262 #[inline]
1263 fn simd_powf(self, exp: Self::SimdRealField) -> Self {
1264 // formula: x^y = (ρ e^(i θ))^y = ρ^y e^(i θ y)
1265 // = from_polar(ρ^y, θ y)
1266 let (r, theta) = self.simd_to_polar();
1267 simd_complex_from_polar(r.simd_powf(exp), theta * exp)
1268 }
1269
1270 /// Returns the logarithm of `self` with respect to an arbitrary base.
1271 #[inline]
1272 fn simd_log(self, base: Simd<$t>) -> Self {
1273 // formula: log_y(x) = log_y(ρ e^(i θ))
1274 // = log_y(ρ) + log_y(e^(i θ)) = log_y(ρ) + ln(e^(i θ)) / ln(y)
1275 // = log_y(ρ) + i θ / ln(y)
1276 let (r, theta) = self.simd_to_polar();
1277 Self::new(r.simd_log(base), theta / base.simd_ln())
1278 }
1279
1280 /// Raises `self` to a complex power.
1281 #[inline]
1282 fn simd_powc(self, exp: Self) -> Self {
1283 // formula: x^y = (a + i b)^(c + i d)
1284 // = (ρ e^(i θ))^c (ρ e^(i θ))^(i d)
1285 // where ρ=|x| and θ=arg(x)
1286 // = ρ^c e^(−d θ) e^(i c θ) ρ^(i d)
1287 // = p^c e^(−d θ) (cos(c θ)
1288 // + i sin(c θ)) (cos(d ln(ρ)) + i sin(d ln(ρ)))
1289 // = p^c e^(−d θ) (
1290 // cos(c θ) cos(d ln(ρ)) − sin(c θ) sin(d ln(ρ))
1291 // + i(cos(c θ) sin(d ln(ρ)) + sin(c θ) cos(d ln(ρ))))
1292 // = p^c e^(−d θ) (cos(c θ + d ln(ρ)) + i sin(c θ + d ln(ρ)))
1293 // = from_polar(p^c e^(−d θ), c θ + d ln(ρ))
1294 let (r, theta) = self.simd_to_polar();
1295 simd_complex_from_polar(
1296 r.simd_powf(exp.re) * (-exp.im * theta).simd_exp(),
1297 exp.re * theta + exp.im * r.simd_ln(),
1298 )
1299 }
1300
1301 /*
1302 /// Raises a floating point number to the complex power `self`.
1303 #[inline]
1304 fn simd_expf(&self, base: T) -> Self {
1305 // formula: x^(a+bi) = x^a x^bi = x^a e^(b ln(x) i)
1306 // = from_polar(x^a, b ln(x))
1307 Self::from_polar(&base.powf(self.re), &(self.im * base.ln()))
1308 }
1309 */
1310
1311 /// Computes the sine of `self`.
1312 #[inline]
1313 fn simd_sin(self) -> Self {
1314 // formula: sin(a + bi) = sin(a)cosh(b) + i*cos(a)sinh(b)
1315 Self::new(
1316 self.re.simd_sin() * self.im.simd_cosh(),
1317 self.re.simd_cos() * self.im.simd_sinh(),
1318 )
1319 }
1320
1321 /// Computes the cosine of `self`.
1322 #[inline]
1323 fn simd_cos(self) -> Self {
1324 // formula: cos(a + bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
1325 Self::new(
1326 self.re.simd_cos() * self.im.simd_cosh(),
1327 -self.re.simd_sin() * self.im.simd_sinh(),
1328 )
1329 }
1330
1331 #[inline]
1332 fn simd_sin_cos(self) -> (Self, Self) {
1333 let (rsin, rcos) = self.re.simd_sin_cos();
1334 let (isinh, icosh) = self.im.simd_sinh_cosh();
1335 let sin = Self::new(rsin * icosh, rcos * isinh);
1336 let cos = Self::new(rcos * icosh, -rsin * isinh);
1337
1338 (sin, cos)
1339 }
1340
1341 /// Computes the tangent of `self`.
1342 #[inline]
1343 fn simd_tan(self) -> Self {
1344 // formula: tan(a + bi) = (sin(2a) + i*sinh(2b))/(cos(2a) + cosh(2b))
1345 let (two_re, two_im) = (self.re + self.re, self.im + self.im);
1346 Self::new(two_re.simd_sin(), two_im.simd_sinh()).unscale(two_re.simd_cos() + two_im.simd_cosh())
1347 }
1348
1349 /// Computes the principal value of the inverse sine of `self`.
1350 ///
1351 /// This function has two branch cuts:
1352 ///
1353 /// * `(-∞, -1)`, continuous from above.
1354 /// * `(1, ∞)`, continuous from below.
1355 ///
1356 /// The branch satisfies `-π/2 ≤ Re(asin(z)) ≤ π/2`.
1357 #[inline]
1358 fn simd_asin(self) -> Self {
1359 // formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz)
1360 let i = Self::i();
1361 -i * ((Self::one() - self * self).simd_sqrt() + i * self).simd_ln()
1362 }
1363
1364 /// Computes the principal value of the inverse cosine of `self`.
1365 ///
1366 /// This function has two branch cuts:
1367 ///
1368 /// * `(-∞, -1)`, continuous from above.
1369 /// * `(1, ∞)`, continuous from below.
1370 ///
1371 /// The branch satisfies `0 ≤ Re(acos(z)) ≤ π`.
1372 #[inline]
1373 fn simd_acos(self) -> Self {
1374 // formula: arccos(z) = -i ln(i sqrt(1-z^2) + z)
1375 let i = Self::i();
1376 -i * (i * (Self::one() - self * self).simd_sqrt() + self).simd_ln()
1377 }
1378
1379 /// Computes the principal value of the inverse tangent of `self`.
1380 ///
1381 /// This function has two branch cuts:
1382 ///
1383 /// * `(-∞i, -i]`, continuous from the left.
1384 /// * `[i, ∞i)`, continuous from the right.
1385 ///
1386 /// The branch satisfies `-π/2 ≤ Re(atan(z)) ≤ π/2`.
1387 #[inline]
1388 fn simd_atan(self) -> Self {
1389 // formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i)
1390 let i = Self::i();
1391 let one = Self::one();
1392 let two = one + one;
1393
1394 if self == i {
1395 return Self::new(Simd::<$t>::zero(), Simd::<$t>::one() / Simd::<$t>::zero());
1396 } else if self == -i {
1397 return Self::new(Simd::<$t>::zero(), -Simd::<$t>::one() / Simd::<$t>::zero());
1398 }
1399
1400 ((one + i * self).simd_ln() - (one - i * self).simd_ln()) / (two * i)
1401 }
1402
1403 /// Computes the hyperbolic sine of `self`.
1404 #[inline]
1405 fn simd_sinh(self) -> Self {
1406 // formula: sinh(a + bi) = sinh(a)cos(b) + i*cosh(a)sin(b)
1407 Self::new(
1408 self.re.simd_sinh() * self.im.simd_cos(),
1409 self.re.simd_cosh() * self.im.simd_sin(),
1410 )
1411 }
1412
1413 /// Computes the hyperbolic cosine of `self`.
1414 #[inline]
1415 fn simd_cosh(self) -> Self {
1416 // formula: cosh(a + bi) = cosh(a)cos(b) + i*sinh(a)sin(b)
1417 Self::new(
1418 self.re.simd_cosh() * self.im.simd_cos(),
1419 self.re.simd_sinh() * self.im.simd_sin(),
1420 )
1421 }
1422
1423 #[inline]
1424 fn simd_sinh_cosh(self) -> (Self, Self) {
1425 let (rsinh, rcosh) = self.re.simd_sinh_cosh();
1426 let (isin, icos) = self.im.simd_sin_cos();
1427 let sin = Self::new(rsinh * icos, rcosh * isin);
1428 let cos = Self::new(rcosh * icos, rsinh * isin);
1429
1430 (sin, cos)
1431 }
1432
1433 /// Computes the hyperbolic tangent of `self`.
1434 #[inline]
1435 fn simd_tanh(self) -> Self {
1436 // formula: tanh(a + bi) = (sinh(2a) + i*sin(2b))/(cosh(2a) + cos(2b))
1437 let (two_re, two_im) = (self.re + self.re, self.im + self.im);
1438 Self::new(two_re.simd_sinh(), two_im.simd_sin()).unscale(two_re.simd_cosh() + two_im.simd_cos())
1439 }
1440
1441 /// Computes the principal value of inverse hyperbolic sine of `self`.
1442 ///
1443 /// This function has two branch cuts:
1444 ///
1445 /// * `(-∞i, -i)`, continuous from the left.
1446 /// * `(i, ∞i)`, continuous from the right.
1447 ///
1448 /// The branch satisfies `-π/2 ≤ Im(asinh(z)) ≤ π/2`.
1449 #[inline]
1450 fn simd_asinh(self) -> Self {
1451 // formula: arcsinh(z) = ln(z + sqrt(1+z^2))
1452 let one = Self::one();
1453 (self + (one + self * self).simd_sqrt()).simd_ln()
1454 }
1455
1456 /// Computes the principal value of inverse hyperbolic cosine of `self`.
1457 ///
1458 /// This function has one branch cut:
1459 ///
1460 /// * `(-∞, 1)`, continuous from above.
1461 ///
1462 /// The branch satisfies `-π ≤ Im(acosh(z)) ≤ π` and `0 ≤ Re(acosh(z)) < ∞`.
1463 #[inline]
1464 fn simd_acosh(self) -> Self {
1465 // formula: arccosh(z) = 2 ln(sqrt((z+1)/2) + sqrt((z-1)/2))
1466 let one = Self::one();
1467 let two = one + one;
1468 two * (((self + one) / two).simd_sqrt() + ((self - one) / two).simd_sqrt()).simd_ln()
1469 }
1470
1471 /// Computes the principal value of inverse hyperbolic tangent of `self`.
1472 ///
1473 /// This function has two branch cuts:
1474 ///
1475 /// * `(-∞, -1]`, continuous from above.
1476 /// * `[1, ∞)`, continuous from below.
1477 ///
1478 /// The branch satisfies `-π/2 ≤ Im(atanh(z)) ≤ π/2`.
1479 #[inline]
1480 fn simd_atanh(self) -> Self {
1481 // formula: arctanh(z) = (ln(1+z) - ln(1-z))/2
1482 let one = Self::one();
1483 let two = one + one;
1484 if self == one {
1485 return Self::new(Simd::<$t>::one() / Simd::<$t>::zero(), Simd::<$t>::zero());
1486 } else if self == -one {
1487 return Self::new(-Simd::<$t>::one() / Simd::<$t>::zero(), Simd::<$t>::zero());
1488 }
1489 ((one + self).simd_ln() - (one - self).simd_ln()) / two
1490 }
1491 }
1492 )*)
1493 );
1494
1495 #[inline]
1496 fn simd_complex_from_polar<N: SimdRealField>(r: N, theta: N) -> num_complex::Complex<N> {
1497 num_complex::Complex::new(r.clone() * theta.clone().simd_cos(), r * theta.simd_sin())
1498 }
1499
1500 impl_float_simd!(
1501 portable_simd::f32x2, f32, portable_simd::i32x2, mask32x2, _0, _1;
1502 portable_simd::f32x4, f32, portable_simd::i32x4, mask32x4, _0, _1, _2, _3;
1503 portable_simd::f32x8, f32, portable_simd::i32x8, mask32x8, _0, _1, _2, _3, _4, _5, _6, _7;
1504 portable_simd::f32x16, f32, portable_simd::i32x16, mask32x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1505 portable_simd::f64x2, f64, portable_simd::i64x2, mask64x2, _0, _1;
1506 portable_simd::f64x4, f64, portable_simd::i64x4, mask64x4, _0, _1, _2, _3;
1507 portable_simd::f64x8, f64, portable_simd::i64x8, mask64x8, _0, _1, _2, _3, _4, _5, _6, _7;
1508 );
1509
1510 impl_int_simd!(
1511 portable_simd::i16x2, i16, mask16x2, _0, _1;
1512 portable_simd::i16x4, i16, mask16x4, _0, _1, _2, _3;
1513 portable_simd::i16x8, i16, mask16x8, _0, _1, _2, _3, _4, _5, _6, _7;
1514 portable_simd::i16x16, i16, mask16x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1515 portable_simd::i16x32, i16, mask16x32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1516 portable_simd::i32x2, i32, mask32x2, _0, _1;
1517 portable_simd::i32x4, i32, mask32x4, _0, _1, _2, _3;
1518 portable_simd::i32x8, i32, mask32x8, _0, _1, _2, _3, _4, _5, _6, _7;
1519 portable_simd::i32x16, i32, mask32x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1520 portable_simd::i64x2, i64, mask64x2, _0, _1;
1521 portable_simd::i64x4, i64, mask64x4, _0, _1, _2, _3;
1522 portable_simd::i64x8, i64, mask64x8, _0, _1, _2, _3, _4, _5, _6, _7;
1523 portable_simd::i8x2, i8, mask8x2, _0, _1;
1524 portable_simd::i8x4, i8, mask8x4, _0, _1, _2, _3;
1525 portable_simd::i8x8, i8, mask8x8, _0, _1, _2, _3, _4, _5, _6, _7;
1526 portable_simd::i8x16, i8, mask8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1527 portable_simd::i8x32, i8, mask8x32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1528 portable_simd::i8x64, i8, mask8x64, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63;
1529 portable_simd::isizex2, isize, masksizex2, _0, _1;
1530 portable_simd::isizex4, isize, masksizex4, _0, _1, _2, _3;
1531 portable_simd::isizex8, isize, masksizex8, _0, _1, _2, _3, _4, _5, _6, _7;
1532 );
1533
1534 impl_uint_simd!(
1535 portable_simd::u16x2, u16, mask16x2, _0, _1;
1536 portable_simd::u16x4, u16, mask16x4, _0, _1, _2, _3;
1537 portable_simd::u16x8, u16, mask16x8, _0, _1, _2, _3, _4, _5, _6, _7;
1538 portable_simd::u16x16, u16, mask16x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1539 portable_simd::u16x32, u16, mask16x32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1540 portable_simd::u32x2, u32, mask32x2, _0, _1;
1541 portable_simd::u32x4, u32, mask32x4, _0, _1, _2, _3;
1542 portable_simd::u32x8, u32, mask32x8, _0, _1, _2, _3, _4, _5, _6, _7;
1543 portable_simd::u32x16, u32, mask32x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1544 portable_simd::u64x2, u64, mask64x2, _0, _1;
1545 portable_simd::u64x4, u64, mask64x4, _0, _1, _2, _3;
1546 portable_simd::u64x8, u64, mask64x8, _0, _1, _2, _3, _4, _5, _6, _7;
1547 portable_simd::u8x2, u8, mask8x2, _0, _1;
1548 portable_simd::u8x4, u8, mask8x4, _0, _1, _2, _3;
1549 portable_simd::u8x8, u8, mask8x8, _0, _1, _2, _3, _4, _5, _6, _7;
1550 portable_simd::u8x16, u8, mask8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1551 portable_simd::u8x32, u8, mask8x32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1552 portable_simd::u8x64, u8, mask8x64, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63;
1553 portable_simd::usizex2, usize, masksizex2, _0, _1;
1554 portable_simd::usizex4, usize, masksizex4, _0, _1, _2, _3;
1555 portable_simd::usizex8, usize, masksizex8, _0, _1, _2, _3, _4, _5, _6, _7;
1556 );
1557
1558 impl_bool_simd!(
1559 portable_simd::mask16x2, 2, _0, _1;
1560 portable_simd::mask16x4, 4, _0, _1, _2, _3;
1561 portable_simd::mask16x8, 8, _0, _1, _2, _3, _4, _5, _6, _7;
1562 portable_simd::mask16x16, 16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1563 portable_simd::mask16x32, 32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1564 portable_simd::mask32x2, 2, _0, _1;
1565 portable_simd::mask32x4, 4, _0, _1, _2, _3;
1566 portable_simd::mask32x8, 8, _0, _1, _2, _3, _4, _5, _6, _7;
1567 portable_simd::mask32x16, 16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1568 portable_simd::mask64x2, 2, _0, _1;
1569 portable_simd::mask64x4, 4, _0, _1, _2, _3;
1570 portable_simd::mask64x8, 8, _0, _1, _2, _3, _4, _5, _6, _7;
1571 portable_simd::mask8x2, 2, _0, _1;
1572 portable_simd::mask8x4, 4, _0, _1, _2, _3;
1573 portable_simd::mask8x8, 8, _0, _1, _2, _3, _4, _5, _6, _7;
1574 portable_simd::mask8x16, 16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
1575 portable_simd::mask8x32, 32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1576 portable_simd::mask8x64, 64, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63;
1577 portable_simd::masksizex2, 2, _0, _1;
1578 portable_simd::masksizex4, 4, _0, _1, _2, _3;
1579 portable_simd::masksizex8, 8, _0, _1, _2, _3, _4, _5, _6, _7;
1580 );
1581
1582 //
1583 // NOTE: the following does not work because of the orphan rules.
1584 //
1585 //macro_rules! impl_simd_complex_from(
1586 // ($($t: ty, $elt: ty $(, $i: expr)*;)*) => ($(
1587 // impl From<[num_complex::Complex<$elt>; <$t>::LANES]> for num_complex::Complex<Simd<$t>> {
1588 // #[inline(always)]
1589 // fn from(vals: [num_complex::Complex<$elt>; <$t>::LANES]) -> Self {
1590 // num_complex::Complex {
1591 // re: <$t>::from([$(vals[$i].re),*]),
1592 // im: <$t>::from([$(vals[$i].im),*]),
1593 // }
1594 // }
1595 // }
1596 // )*)
1597 //);
1598 //
1599 //impl_simd_complex_from!(
1600 // portable_simd::f32x2, f32, 0, 1;
1601 // portable_simd::f32x4, f32, 0, 1, 2, 3;
1602 // portable_simd::f32x8, f32, 0, 1, 2, 3, 4, 5, 6, 7;
1603 // portable_simd::f32x16, f32, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15;
1604 //);
1605
1606 //////////////////////////////////////////
1607 // Aliases //
1608 //////////////////////////////////////////
1609
1610 pub type f32x2 = Simd<portable_simd::f32x2>;
1611 pub type f32x4 = Simd<portable_simd::f32x4>;
1612 pub type f32x8 = Simd<portable_simd::f32x8>;
1613 pub type f32x16 = Simd<portable_simd::f32x16>;
1614 pub type f64x2 = Simd<portable_simd::f64x2>;
1615 pub type f64x4 = Simd<portable_simd::f64x4>;
1616 pub type f64x8 = Simd<portable_simd::f64x8>;
1617 pub type i16x2 = Simd<portable_simd::i16x2>;
1618 pub type i16x4 = Simd<portable_simd::i16x4>;
1619 pub type i16x8 = Simd<portable_simd::i16x8>;
1620 pub type i16x16 = Simd<portable_simd::i16x16>;
1621 pub type i16x32 = Simd<portable_simd::i16x32>;
1622 pub type i32x2 = Simd<portable_simd::i32x2>;
1623 pub type i32x4 = Simd<portable_simd::i32x4>;
1624 pub type i32x8 = Simd<portable_simd::i32x8>;
1625 pub type i32x16 = Simd<portable_simd::i32x16>;
1626 pub type i64x2 = Simd<portable_simd::i64x2>;
1627 pub type i64x4 = Simd<portable_simd::i64x4>;
1628 pub type i64x8 = Simd<portable_simd::i64x8>;
1629 pub type i8x2 = Simd<portable_simd::i8x2>;
1630 pub type i8x4 = Simd<portable_simd::i8x4>;
1631 pub type i8x8 = Simd<portable_simd::i8x8>;
1632 pub type i8x16 = Simd<portable_simd::i8x16>;
1633 pub type i8x32 = Simd<portable_simd::i8x32>;
1634 pub type i8x64 = Simd<portable_simd::i8x64>;
1635 pub type isizex2 = Simd<portable_simd::isizex2>;
1636 pub type isizex4 = Simd<portable_simd::isizex4>;
1637 pub type isizex8 = Simd<portable_simd::isizex8>;
1638 pub type u16x2 = Simd<portable_simd::u16x2>;
1639 pub type u16x4 = Simd<portable_simd::u16x4>;
1640 pub type u16x8 = Simd<portable_simd::u16x8>;
1641 pub type u16x16 = Simd<portable_simd::u16x16>;
1642 pub type u16x32 = Simd<portable_simd::u16x32>;
1643 pub type u32x2 = Simd<portable_simd::u32x2>;
1644 pub type u32x4 = Simd<portable_simd::u32x4>;
1645 pub type u32x8 = Simd<portable_simd::u32x8>;
1646 pub type u32x16 = Simd<portable_simd::u32x16>;
1647 pub type u64x2 = Simd<portable_simd::u64x2>;
1648 pub type u64x4 = Simd<portable_simd::u64x4>;
1649 pub type u64x8 = Simd<portable_simd::u64x8>;
1650 pub type u8x2 = Simd<portable_simd::u8x2>;
1651 pub type u8x4 = Simd<portable_simd::u8x4>;
1652 pub type u8x8 = Simd<portable_simd::u8x8>;
1653 pub type u8x16 = Simd<portable_simd::u8x16>;
1654 pub type u8x32 = Simd<portable_simd::u8x32>;
1655 pub type u8x64 = Simd<portable_simd::u8x64>;
1656 pub type usizex2 = Simd<portable_simd::usizex2>;
1657 pub type usizex4 = Simd<portable_simd::usizex4>;
1658 pub type usizex8 = Simd<portable_simd::usizex8>;
1659
1660 pub type mask8x1 = Simd<portable_simd::mask8x1>;
1661 pub type mask8x2 = Simd<portable_simd::mask8x2>;
1662 pub type mask8x4 = Simd<portable_simd::mask8x4>;
1663 pub type mask8x8 = Simd<portable_simd::mask8x8>;
1664 pub type mask8x16 = Simd<portable_simd::mask8x16>;
1665 pub type mask8x32 = Simd<portable_simd::mask8x32>;
1666 pub type mask8x64 = Simd<portable_simd::mask8x64>;
1667 pub type mask16x1 = Simd<portable_simd::mask16x1>;
1668 pub type mask16x2 = Simd<portable_simd::mask16x2>;
1669 pub type mask16x4 = Simd<portable_simd::mask16x4>;
1670 pub type mask16x8 = Simd<portable_simd::mask16x8>;
1671 pub type mask16x16 = Simd<portable_simd::mask16x16>;
1672 pub type mask16x32 = Simd<portable_simd::mask16x32>;
1673 pub type mask32x1 = Simd<portable_simd::mask32x1>;
1674 pub type mask32x2 = Simd<portable_simd::mask32x2>;
1675 pub type mask32x4 = Simd<portable_simd::mask32x4>;
1676 pub type mask32x8 = Simd<portable_simd::mask32x8>;
1677 pub type mask32x16 = Simd<portable_simd::mask32x16>;
1678 pub type mask64x1 = Simd<portable_simd::mask64x1>;
1679 pub type mask64x2 = Simd<portable_simd::mask64x2>;
1680 pub type mask64x4 = Simd<portable_simd::mask64x4>;
1681 pub type mask64x8 = Simd<portable_simd::mask64x8>;
1682 pub type masksizex1 = Simd<portable_simd::masksizex1>;
1683 pub type masksizex2 = Simd<portable_simd::masksizex2>;
1684 pub type masksizex4 = Simd<portable_simd::masksizex4>;
1685 pub type masksizex8 = Simd<portable_simd::masksizex8>;