1#![cfg_attr(not(feature = "std"), no_std)]
2#![allow(non_camel_case_types)]
3use bytemuck::Pod;
4use core::fmt::Debug;
5use num_complex::Complex;
6use pulp::Simd;
7use qd::Quad;
8pub mod imply {
10 pub trait Imply<T: ?Sized>: sealed::ImplyInner<T, Is = T> {}
11 impl<T: ?Sized, U: ?Sized> Imply<T> for U {}
12 mod sealed {
13 pub trait ImplyInner<T: ?Sized> {
14 type Is: ?Sized;
15 }
16 impl<T: ?Sized, U: ?Sized> ImplyInner<T> for U {
17 type Is = T;
18 }
19 }
20}
21use core::ops::*;
22use imply::Imply;
23use math_utils::*;
24pub mod math_utils {
25 use crate::{ByRef, ComplexField, RealField, abs_impl};
26 #[inline(always)]
27 #[must_use]
28 pub fn eps<T: RealField>() -> T {
29 T::Real::epsilon_impl()
30 }
31 #[inline(always)]
32 #[must_use]
33 pub fn nbits<T: ComplexField>() -> usize {
34 T::Real::nbits_impl()
35 }
36 #[inline(always)]
37 #[must_use]
38 pub fn min_positive<T: RealField>() -> T {
39 T::min_positive_impl()
40 }
41 #[inline(always)]
42 #[must_use]
43 pub fn max_positive<T: RealField>() -> T {
44 T::max_positive_impl()
45 }
46 #[inline(always)]
47 #[must_use]
48 pub fn sqrt_min_positive<T: RealField>() -> T {
49 T::sqrt_min_positive_impl()
50 }
51 #[inline(always)]
52 #[must_use]
53 pub fn sqrt_max_positive<T: RealField>() -> T {
54 T::sqrt_max_positive_impl()
55 }
56 #[inline(always)]
57 #[must_use]
58 pub fn zero<T: ComplexField>() -> T {
59 T::zero_impl()
60 }
61 #[inline(always)]
62 #[must_use]
63 pub fn one<T: ComplexField>() -> T {
64 T::one_impl()
65 }
66 #[inline(always)]
67 #[must_use]
68 pub fn nan<T: ComplexField>() -> T {
69 T::nan_impl()
70 }
71 #[inline(always)]
72 #[must_use]
73 pub fn infinity<T: ComplexField>() -> T {
74 T::infinity_impl()
75 }
76 #[inline(always)]
77 #[must_use]
78 pub fn as_real<T: ComplexField>(value: &T) -> T {
79 T::from_real_impl(&T::real_part_impl((value).by_ref()))
80 }
81 #[inline(always)]
82 #[must_use]
83 pub fn real<T: ComplexField>(value: &T) -> T::Real {
84 T::real_part_impl((value).by_ref())
85 }
86 #[inline(always)]
87 #[must_use]
88 pub fn imag<T: ComplexField>(value: &T) -> T::Real {
89 T::imag_part_impl((value).by_ref())
90 }
91 #[inline(always)]
92 #[track_caller]
93 #[must_use]
94 pub fn neg<T: NegByRef>(value: &T) -> T::Output {
95 value.neg_by_ref()
96 }
97 #[inline(always)]
98 #[must_use]
99 pub fn copy<T: ComplexField>(value: &T) -> T {
100 T::copy_impl((value).by_ref())
101 }
102 #[inline(always)]
103 #[must_use]
104 pub fn conj<T: ComplexField>(value: &T) -> T {
105 T::conj_impl((value).by_ref())
106 }
107 #[inline(always)]
108 #[track_caller]
109 #[must_use]
110 pub fn add<T: AddByRef<U>, U>(lhs: &T, rhs: &U) -> T::Output {
111 lhs.add_by_ref(rhs)
112 }
113 #[inline(always)]
114 #[track_caller]
115 #[must_use]
116 pub fn sub<T: SubByRef<U>, U>(lhs: &T, rhs: &U) -> T::Output {
117 lhs.sub_by_ref(rhs)
118 }
119 #[inline(always)]
120 #[track_caller]
121 #[must_use]
122 pub fn mul<T: MulByRef<U>, U>(lhs: &T, rhs: &U) -> T::Output {
123 lhs.mul_by_ref(rhs)
124 }
125 #[inline(always)]
126 #[track_caller]
127 #[must_use]
128 pub fn div<T: DivByRef<U>, U>(lhs: &T, rhs: &U) -> T::Output {
129 lhs.div_by_ref(rhs)
130 }
131 #[inline(always)]
132 #[must_use]
133 pub fn mul_real<T: ComplexField>(lhs: &T, rhs: &T::Real) -> T {
134 T::mul_real_impl((lhs).by_ref(), (rhs).by_ref())
135 }
136 #[inline(always)]
137 #[must_use]
138 pub fn mul_pow2<T: ComplexField>(lhs: &T, rhs: &T::Real) -> T {
139 T::mul_real_impl((lhs).by_ref(), (rhs).by_ref())
140 }
141 #[inline(always)]
142 #[must_use]
143 pub fn abs1<T: ComplexField>(value: &T) -> T::Real {
144 T::abs1_impl((value).by_ref())
145 }
146 #[inline(always)]
147 #[must_use]
148 pub fn absmax<T: ComplexField>(value: &T) -> T::Real {
149 if const { T::IS_REAL } {
150 T::abs1_impl(value)
151 } else {
152 &T::Real::abs1_impl(&real(value))
153 + &T::Real::abs1_impl(&imag(value))
154 }
155 }
156 #[inline(always)]
157 #[must_use]
158 pub fn abs<T: ComplexField>(value: &T) -> T::Real {
159 T::abs_impl((value).by_ref())
160 }
161 #[inline(always)]
162 #[must_use]
163 pub fn hypot<T: RealField>(lhs: &T, rhs: &T) -> T {
164 abs_impl::<T::Real>(lhs.clone(), rhs.clone())
165 }
166 #[inline(always)]
167 #[must_use]
168 pub fn abs2<T: ComplexField>(value: &T) -> T::Real {
169 T::abs2_impl((value).by_ref())
170 }
171 #[inline(always)]
172 #[must_use]
173 pub fn max<T: RealField>(lhs: &T, rhs: &T) -> T {
174 if lhs > rhs { copy(lhs) } else { copy(rhs) }
175 }
176 #[inline(always)]
177 #[must_use]
178 pub fn min<T: RealField>(lhs: &T, rhs: &T) -> T {
179 if lhs < rhs { copy(lhs) } else { copy(rhs) }
180 }
181 #[inline(always)]
182 #[must_use]
183 pub fn is_nan<T: ComplexField>(value: &T) -> bool {
184 T::is_nan_impl((value).by_ref())
185 }
186 #[inline(always)]
187 #[must_use]
188 pub fn is_finite<T: ComplexField>(value: &T) -> bool {
189 T::is_finite_impl((value).by_ref())
190 }
191 #[inline(always)]
192 #[must_use]
193 pub fn sqrt<T: ComplexField>(value: &T) -> T {
194 T::sqrt_impl((value).by_ref())
195 }
196 #[inline(always)]
197 #[must_use]
198 pub fn recip<T: ComplexField>(value: &T) -> T {
199 T::recip_impl((value).by_ref())
200 }
201 #[inline(always)]
202 #[must_use]
203 pub fn from_real<T: ComplexField>(value: &T::Real) -> T {
204 T::from_real_impl((value).by_ref())
205 }
206 #[inline(always)]
207 #[must_use]
208 pub fn from_f64<T: ComplexField>(value: f64) -> T {
209 T::from_f64_impl(value)
210 }
211 pub use crate::{AddByRef, DivByRef, MulByRef, NegByRef, SubByRef};
212}
213macro_rules! impl_op {
214 ({$(
215 impl$(<$T:ident: $bound:ident>)? $op:ident for $ty:ty {
216 fn $op_fn:ident($self:ident, $rhs:ident: Self) -> Self::Output $body:block
217 }
218 )*}) => {$(
219 impl$(<$T: $bound>)? $op for $ty {
220 type Output = $ty;
221 #[inline(always)]
222 fn $op_fn($self, $rhs: $ty) -> Self::Output $body
223 }
224 impl$(<$T: $bound>)? $op<&$ty> for $ty {
225 type Output = $ty;
226 #[inline(always)]
227 fn $op_fn($self, $rhs: &$ty) -> Self::Output $body
228 }
229 impl$(<$T: $bound>)? $op<$ty> for &$ty {
230 type Output = $ty;
231 #[inline(always)]
232 fn $op_fn($self, $rhs: $ty) -> Self::Output $body
233 }
234 impl$(<$T: $bound>)? $op<&$ty> for &$ty {
235 type Output = $ty;
236 #[inline(always)]
237 fn $op_fn($self, $rhs: &$ty) -> Self::Output $body
238 }
239 )*};
240}
241macro_rules! impl_assign_op {
242 ({$(
243 impl$(<$T:ident: $bound:ident>)? $op:ident for $ty:ty {
244 fn $op_fn:ident(&mut $self:ident, $rhs:ident: Self) $body:block
245 }
246 )*}) => {$(
247 impl$(<$T: $bound>)? $op for $ty {
248 #[inline(always)]
249 fn $op_fn(&mut $self, $rhs: Self) $body
250 }
251 impl$(<$T: $bound>)? $op<&$ty> for $ty {
252 #[inline(always)]
253 fn $op_fn(&mut $self, $rhs: &$ty) $body
254 }
255 )*};
256}
257pub trait AddByRef<Rhs = Self> {
258 type Output;
259 fn add_by_ref(&self, rhs: &Rhs) -> Self::Output;
260}
261pub trait SubByRef<Rhs = Self> {
262 type Output;
263 fn sub_by_ref(&self, rhs: &Rhs) -> Self::Output;
264}
265pub trait NegByRef {
266 type Output;
267 fn neg_by_ref(&self) -> Self::Output;
268}
269pub trait MulByRef<Rhs = Self> {
270 type Output;
271 fn mul_by_ref(&self, rhs: &Rhs) -> Self::Output;
272}
273pub trait DivByRef<Rhs = Self> {
274 type Output;
275 fn div_by_ref(&self, rhs: &Rhs) -> Self::Output;
276}
277impl<Rhs, Lhs, Output> AddByRef<Rhs> for Lhs
278where
279 for<'a> &'a Lhs: core::ops::Add<&'a Rhs, Output = Output>,
280{
281 type Output = Output;
282
283 #[inline]
284 #[track_caller]
285 fn add_by_ref(&self, rhs: &Rhs) -> Self::Output {
286 self + rhs
287 }
288}
289impl<Rhs, Lhs, Output> SubByRef<Rhs> for Lhs
290where
291 for<'a> &'a Lhs: core::ops::Sub<&'a Rhs, Output = Output>,
292{
293 type Output = Output;
294
295 #[inline]
296 #[track_caller]
297 fn sub_by_ref(&self, rhs: &Rhs) -> Self::Output {
298 self - rhs
299 }
300}
301impl<Rhs, Lhs, Output> MulByRef<Rhs> for Lhs
302where
303 for<'a> &'a Lhs: core::ops::Mul<&'a Rhs, Output = Output>,
304{
305 type Output = Output;
306
307 #[inline]
308 #[track_caller]
309 fn mul_by_ref(&self, rhs: &Rhs) -> Self::Output {
310 self * rhs
311 }
312}
313impl<Rhs, Lhs, Output> DivByRef<Rhs> for Lhs
314where
315 for<'a> &'a Lhs: core::ops::Div<&'a Rhs, Output = Output>,
316{
317 type Output = Output;
318
319 #[inline]
320 #[track_caller]
321 fn div_by_ref(&self, rhs: &Rhs) -> Self::Output {
322 self / rhs
323 }
324}
325impl<T, Output> NegByRef for T
326where
327 for<'a> &'a T: core::ops::Neg<Output = Output>,
328{
329 type Output = Output;
330
331 #[inline]
332 #[track_caller]
333 fn neg_by_ref(&self) -> Self::Output {
334 -self
335 }
336}
337fn abs_impl<T: RealField>(re: T, im: T) -> T {
338 use ext::*;
339 let (re, im) = &(re, im);
340 let small = &sqrt_min_positive();
341 let big = &sqrt_max_positive();
342 let one = &one();
343 let re_abs = &re.abs();
344 let im_abs = &im.abs();
345 let (scale, unscale) = if re_abs > big || im_abs > big {
351 (small * eps::<T>(), big / eps::<T>())
352 } else if re_abs > one || im_abs > one {
353 (one.copy(), one.copy())
354 } else if re_abs < small && im_abs < small {
355 (big / eps::<T>(), small * eps::<T>())
356 } else {
357 (big.copy(), small.copy())
358 };
359 ((re * &scale).abs2() + (im * &scale).abs2()).sqrt() * unscale
360}
361fn recip_impl<T: RealField>(re: T, im: T) -> (T, T) {
362 use ext::*;
363 let (re, im) = &(re, im);
364 if is_nan(re) || is_nan(im) {
365 return (nan(), nan());
366 }
367 if *re == zero() && *im == zero() {
368 return (infinity(), infinity());
369 }
370 if !is_finite(re) || !is_finite(im) {
371 return (zero(), zero());
372 }
373 let small = &sqrt_min_positive();
374 let big = &sqrt_max_positive();
375 let one = &one();
376 let re_abs = &abs(re);
377 let im_abs = &abs(im);
378 if re_abs > big || im_abs > big {
379 let re = re * small;
380 let im = im * small;
381 let inv = &(re.abs2() + im.abs2()).recip();
382 (((re * inv) * small), ((-im * inv) * small))
383 } else if re_abs > one || im_abs > one {
384 let inv = &(re.abs2() + im.abs2()).recip();
385 ((re * inv), (-im * inv))
386 } else {
387 let re = re * big;
388 let im = im * big;
389 if &abs(&re) < small && &abs(&im) < small {
390 let re = re * big;
393 let im = im * big;
394 let inv = &(re.abs2() + im.abs2()).recip();
395 (((re * inv) * big) * big, ((-im * inv) * big) * big)
396 } else {
397 let inv = &(re.abs2() + im.abs2()).recip();
398 (((re * inv) * big), ((-im * inv) * big))
399 }
400 }
401}
402fn sqrt_impl<T: RealField>(re: T, im: T) -> (T, T) {
403 use ext::*;
404 let im_negative = im < zero();
405 let half = &from_f64(0.5);
406 let abs = &abs_impl(re.clone(), im);
407 let zero = &zero::<T>();
408 let re = &re;
409 let mut sum = re + abs;
410 if &sum < zero {
411 sum = zero.copy();
412 }
413 let out_re = sum.mul_pow2(half).sqrt();
414 let mut out_im = (abs - re).mul_pow2(half).fmax(zero).sqrt();
415 if im_negative {
416 out_im = -out_im;
417 }
418 (out_re, out_im)
419}
420pub trait ByRef<T> {
421 fn by_ref(&self) -> &T;
422}
423impl<T> ByRef<T> for T {
424 #[inline]
425 fn by_ref(&self) -> &T {
426 self
427 }
428}
429impl<T> ByRef<T> for &T {
430 #[inline]
431 fn by_ref(&self) -> &T {
432 self
433 }
434}
435#[repr(transparent)]
436pub struct SimdCtx<T: ComplexField, S: Simd>(pub T::SimdCtx<S>);
437impl<T: ComplexField, S: Simd> Copy for SimdCtx<T, S> {}
438impl<T: ComplexField, S: Simd> Clone for SimdCtx<T, S> {
439 #[inline]
440 fn clone(&self) -> Self {
441 *self
442 }
443}
444#[derive(Copy, Clone, Debug)]
445#[repr(transparent)]
446pub struct RealReg<T>(pub T);
447impl<T: ComplexField, S: Simd> SimdCtx<T, S> {
448 #[inline(always)]
449 pub fn new(ctx: &T::SimdCtx<S>) -> &Self {
450 unsafe { &*(ctx as *const T::SimdCtx<S> as *const Self) }
451 }
452
453 #[inline(always)]
454 pub fn zero(&self) -> T::SimdVec<S> {
455 unsafe { core::mem::zeroed() }
456 }
457
458 #[inline(always)]
459 pub fn splat(&self, value: impl ByRef<T>) -> T::SimdVec<S> {
460 unsafe {
461 core::mem::transmute_copy(&T::simd_splat(&self.0, value.by_ref()))
462 }
463 }
464
465 #[inline(always)]
466 pub fn splat_real(
467 &self,
468 value: impl ByRef<T::Real>,
469 ) -> RealReg<T::SimdVec<S>> {
470 RealReg(unsafe {
471 core::mem::transmute_copy(&T::simd_splat_real(
472 &self.0,
473 value.by_ref(),
474 ))
475 })
476 }
477
478 #[inline(always)]
479 pub fn add(&self, lhs: T::SimdVec<S>, rhs: T::SimdVec<S>) -> T::SimdVec<S> {
480 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
481 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
482 unsafe { core::mem::transmute_copy(&T::simd_add(&self.0, lhs, rhs)) }
483 }
484
485 #[inline(always)]
486 pub fn sub(&self, lhs: T::SimdVec<S>, rhs: T::SimdVec<S>) -> T::SimdVec<S> {
487 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
488 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
489 unsafe { core::mem::transmute_copy(&T::simd_sub(&self.0, lhs, rhs)) }
490 }
491
492 #[inline(always)]
493 pub fn neg(&self, value: T::SimdVec<S>) -> T::SimdVec<S> {
494 let value = unsafe { core::mem::transmute_copy(&value) };
495 unsafe { core::mem::transmute_copy(&T::simd_neg(&self.0, value)) }
496 }
497
498 #[inline(always)]
499 pub fn conj(&self, value: T::SimdVec<S>) -> T::SimdVec<S> {
500 let value = unsafe { core::mem::transmute_copy(&value) };
501 unsafe { core::mem::transmute_copy(&T::simd_conj(&self.0, value)) }
502 }
503
504 #[inline(always)]
505 pub fn abs1(&self, value: T::SimdVec<S>) -> RealReg<T::SimdVec<S>> {
506 let value = unsafe { core::mem::transmute_copy(&value) };
507 RealReg(unsafe {
508 core::mem::transmute_copy(&T::simd_abs1(&self.0, value))
509 })
510 }
511
512 #[inline(always)]
513 pub fn abs_max(&self, value: T::SimdVec<S>) -> RealReg<T::SimdVec<S>> {
514 let value = unsafe { core::mem::transmute_copy(&value) };
515 RealReg(unsafe {
516 core::mem::transmute_copy(&T::simd_abs_max(&self.0, value))
517 })
518 }
519
520 #[inline(always)]
521 pub fn mul_real(
522 &self,
523 lhs: T::SimdVec<S>,
524 rhs: RealReg<T::SimdVec<S>>,
525 ) -> T::SimdVec<S> {
526 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
527 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
528 unsafe {
529 core::mem::transmute_copy(&T::simd_mul_real(&self.0, lhs, rhs))
530 }
531 }
532
533 #[inline(always)]
534 pub fn mul_pow2(
535 &self,
536 lhs: T::SimdVec<S>,
537 rhs: RealReg<T::SimdVec<S>>,
538 ) -> T::SimdVec<S> {
539 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
540 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
541 unsafe {
542 core::mem::transmute_copy(&T::simd_mul_pow2(&self.0, lhs, rhs))
543 }
544 }
545
546 #[inline(always)]
547 pub fn mul(&self, lhs: T::SimdVec<S>, rhs: T::SimdVec<S>) -> T::SimdVec<S> {
548 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
549 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
550 unsafe { core::mem::transmute_copy(&T::simd_mul(&self.0, lhs, rhs)) }
551 }
552
553 #[inline(always)]
554 pub fn conj_mul(
555 &self,
556 lhs: T::SimdVec<S>,
557 rhs: T::SimdVec<S>,
558 ) -> T::SimdVec<S> {
559 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
560 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
561 unsafe {
562 core::mem::transmute_copy(&T::simd_conj_mul(&self.0, lhs, rhs))
563 }
564 }
565
566 #[inline(always)]
567 pub fn mul_add(
568 &self,
569 lhs: T::SimdVec<S>,
570 rhs: T::SimdVec<S>,
571 acc: T::SimdVec<S>,
572 ) -> T::SimdVec<S> {
573 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
574 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
575 let acc = unsafe { core::mem::transmute_copy(&acc) };
576 unsafe {
577 core::mem::transmute_copy(&T::simd_mul_add(&self.0, lhs, rhs, acc))
578 }
579 }
580
581 #[inline(always)]
582 pub fn conj_mul_add(
583 &self,
584 lhs: T::SimdVec<S>,
585 rhs: T::SimdVec<S>,
586 acc: T::SimdVec<S>,
587 ) -> T::SimdVec<S> {
588 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
589 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
590 let acc = unsafe { core::mem::transmute_copy(&acc) };
591 unsafe {
592 core::mem::transmute_copy(&T::simd_conj_mul_add(
593 &self.0, lhs, rhs, acc,
594 ))
595 }
596 }
597
598 #[inline(always)]
599 pub fn maybe_conj_mul_add<const CONJ: bool>(
600 &self,
601 lhs: T::SimdVec<S>,
602 rhs: T::SimdVec<S>,
603 acc: T::SimdVec<S>,
604 ) -> T::SimdVec<S> {
605 if const { CONJ } {
606 self.conj_mul_add(lhs, rhs, acc)
607 } else {
608 self.mul_add(lhs, rhs, acc)
609 }
610 }
611
612 #[inline(always)]
613 pub fn abs2(&self, value: T::SimdVec<S>) -> RealReg<T::SimdVec<S>> {
614 let value = unsafe { core::mem::transmute_copy(&value) };
615 RealReg(unsafe {
616 core::mem::transmute_copy(&T::simd_abs2(&self.0, value))
617 })
618 }
619
620 #[inline(always)]
621 pub fn abs2_add(
622 &self,
623 value: T::SimdVec<S>,
624 acc: RealReg<T::SimdVec<S>>,
625 ) -> RealReg<T::SimdVec<S>> {
626 let value = unsafe { core::mem::transmute_copy(&value) };
627 let acc = unsafe { core::mem::transmute_copy(&acc) };
628 RealReg(unsafe {
629 core::mem::transmute_copy(&T::simd_abs2_add(&self.0, value, acc))
630 })
631 }
632
633 #[inline(always)]
634 pub fn reduce_sum(&self, value: T::SimdVec<S>) -> T {
635 let value = unsafe { core::mem::transmute_copy(&value) };
636 unsafe {
637 core::mem::transmute_copy(&T::simd_reduce_sum(&self.0, value))
638 }
639 }
640
641 #[inline(always)]
642 pub fn reduce_max(&self, value: RealReg<T::SimdVec<S>>) -> T {
643 let value = unsafe { core::mem::transmute_copy(&value) };
644 unsafe {
645 core::mem::transmute_copy(&T::simd_reduce_max(&self.0, value))
646 }
647 }
648
649 #[inline(always)]
650 pub fn reduce_sum_real(&self, value: RealReg<T::SimdVec<S>>) -> Real<T> {
651 let value = T::simd_reduce_sum(&self.0, value.0);
652 if const { T::SIMD_ABS_SPLIT_REAL_IMAG && !S::IS_SCALAR } {
653 let r = real(&value);
654 let i = imag(&value);
655 &r + &i
656 } else {
657 real(&value)
658 }
659 }
660
661 #[inline(always)]
662 pub fn reduce_max_real(&self, value: RealReg<T::SimdVec<S>>) -> Real<T> {
663 use crate::ext::*;
664 let value = T::simd_reduce_max(&self.0, value.0);
665 if const { T::SIMD_ABS_SPLIT_REAL_IMAG && !S::IS_SCALAR } {
666 value.real().fmax(value.imag())
667 } else {
668 value.real()
669 }
670 }
671
672 #[inline(always)]
673 pub fn max(
674 &self,
675 lhs: RealReg<T::SimdVec<S>>,
676 rhs: RealReg<T::SimdVec<S>>,
677 ) -> RealReg<T::SimdVec<S>> {
678 let cmp = self.gt(lhs, rhs);
679 RealReg(self.select(cmp, lhs.0, rhs.0))
680 }
681
682 #[inline(always)]
683 pub fn eq(&self, lhs: T::SimdVec<S>, rhs: T::SimdVec<S>) -> T::SimdMask<S> {
684 T::simd_equal(&self.0, lhs, rhs)
685 }
686
687 #[inline(always)]
688 pub fn lt(
689 &self,
690 lhs: RealReg<T::SimdVec<S>>,
691 rhs: RealReg<T::SimdVec<S>>,
692 ) -> T::SimdMask<S> {
693 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
694 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
695 unsafe {
696 core::mem::transmute_copy(&T::simd_less_than(&self.0, lhs, rhs))
697 }
698 }
699
700 #[inline(always)]
701 pub fn gt(
702 &self,
703 lhs: RealReg<T::SimdVec<S>>,
704 rhs: RealReg<T::SimdVec<S>>,
705 ) -> T::SimdMask<S> {
706 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
707 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
708 unsafe {
709 core::mem::transmute_copy(&T::simd_greater_than(&self.0, lhs, rhs))
710 }
711 }
712
713 #[inline(always)]
714 pub fn le(
715 &self,
716 lhs: RealReg<T::SimdVec<S>>,
717 rhs: RealReg<T::SimdVec<S>>,
718 ) -> T::SimdMask<S> {
719 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
720 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
721 unsafe {
722 core::mem::transmute_copy(&T::simd_less_than_or_equal(
723 &self.0, lhs, rhs,
724 ))
725 }
726 }
727
728 #[inline(always)]
729 pub fn ge(
730 &self,
731 lhs: RealReg<T::SimdVec<S>>,
732 rhs: RealReg<T::SimdVec<S>>,
733 ) -> T::SimdMask<S> {
734 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
735 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
736 unsafe {
737 core::mem::transmute_copy(&T::simd_greater_than_or_equal(
738 &self.0, lhs, rhs,
739 ))
740 }
741 }
742
743 #[inline(always)]
744 pub fn select(
745 &self,
746 mask: T::SimdMask<S>,
747 lhs: T::SimdVec<S>,
748 rhs: T::SimdVec<S>,
749 ) -> T::SimdVec<S> {
750 let lhs = unsafe { core::mem::transmute_copy(&lhs) };
751 let rhs = unsafe { core::mem::transmute_copy(&rhs) };
752 unsafe {
753 core::mem::transmute_copy(&T::simd_select(&self.0, mask, lhs, rhs))
754 }
755 }
756
757 #[inline(always)]
758 pub fn iselect(
759 &self,
760 mask: T::SimdMask<S>,
761 lhs: T::SimdIndex<S>,
762 rhs: T::SimdIndex<S>,
763 ) -> T::SimdIndex<S> {
764 unsafe {
765 core::mem::transmute_copy(&T::simd_index_select(
766 &self.0, mask, lhs, rhs,
767 ))
768 }
769 }
770
771 #[inline(always)]
772 pub fn isplat(&self, value: T::Index) -> T::SimdIndex<S> {
773 unsafe {
774 core::mem::transmute_copy(&T::simd_index_splat(&self.0, value))
775 }
776 }
777
778 #[inline(always)]
779 pub fn izero(&self) -> T::SimdIndex<S> {
780 unsafe { core::mem::zeroed() }
781 }
782
783 #[inline(always)]
784 pub fn iadd(
785 &self,
786 lhs: T::SimdIndex<S>,
787 rhs: T::SimdIndex<S>,
788 ) -> T::SimdIndex<S> {
789 unsafe {
790 core::mem::transmute_copy(&T::simd_index_add(&self.0, lhs, rhs))
791 }
792 }
793
794 #[inline(always)]
795 pub fn or_mask(
796 &self,
797 lhs: T::SimdMask<S>,
798 rhs: T::SimdMask<S>,
799 ) -> T::SimdMask<S> {
800 T::simd_or_mask(&self.0, lhs, rhs)
801 }
802
803 #[inline(always)]
804 pub fn and_mask(
805 &self,
806 lhs: T::SimdMask<S>,
807 rhs: T::SimdMask<S>,
808 ) -> T::SimdMask<S> {
809 T::simd_and_mask(&self.0, lhs, rhs)
810 }
811
812 #[inline(always)]
813 pub fn not_mask(&self, mask: T::SimdMask<S>) -> T::SimdMask<S> {
814 T::simd_not_mask(&self.0, mask)
815 }
816
817 #[inline(always)]
818 pub fn first_true_mask(&self, value: T::SimdMask<S>) -> usize {
819 T::simd_first_true_mask(&self.0, value)
820 }
821
822 #[inline(always)]
827 pub unsafe fn mask_load(
828 &self,
829 mask: T::SimdMemMask<S>,
830 ptr: *const T::SimdVec<S>,
831 ) -> T::SimdVec<S> {
832 unsafe { T::simd_mask_load(&self.0, mask, ptr) }
833 }
834
835 #[inline(always)]
840 pub unsafe fn mask_store(
841 &self,
842 mask: T::SimdMemMask<S>,
843 ptr: *mut T::SimdVec<S>,
844 value: T::SimdVec<S>,
845 ) {
846 let value = unsafe { core::mem::transmute_copy(&value) };
847 unsafe { T::simd_mask_store(&self.0, mask, ptr, value) }
848 }
849
850 #[inline(always)]
851 pub fn load(&self, ptr: &T::SimdVec<S>) -> T::SimdVec<S> {
852 unsafe { core::mem::transmute_copy(&T::simd_load(&self.0, ptr)) }
853 }
854
855 #[inline(always)]
856 pub fn store(&self, ptr: &mut T::SimdVec<S>, value: T::SimdVec<S>) {
857 let value = unsafe { core::mem::transmute_copy(&value) };
858 unsafe {
859 core::mem::transmute_copy(&T::simd_store(&self.0, ptr, value))
860 }
861 }
862}
863pub unsafe trait Conjugate:
872 Send + Sync + core::fmt::Debug + Imply<Self::Canonical, Is: RefOps>
873{
874 const IS_CANONICAL: bool;
875 type Conj: Conjugate<Conj = Self, Canonical = Self::Canonical>;
876 type Canonical: Conjugate<Canonical = Self::Canonical> + ComplexField;
877}
878pub type Real<T> = <<T as Conjugate>::Canonical as ComplexField>::Real;
879#[derive(Copy, Clone, Debug, PartialEq, Eq)]
880pub struct ComplexConj<T> {
881 pub re: T,
882 pub im_neg: T,
883}
884#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
885pub enum SimdCapabilities {
886 None,
887 Copy,
888 Simd,
889}
890impl SimdCapabilities {
891 #[inline]
892 pub const fn is_copy(self) -> bool {
893 matches!(self, Self::Copy | Self::Simd)
894 }
895
896 #[inline]
897 pub const fn is_simd(self) -> bool {
898 matches!(self, Self::Simd)
899 }
900}
901mod seal {
902 pub trait Seal {}
903 impl Seal for u8 {}
904 impl Seal for u16 {}
905 impl Seal for u32 {}
906 impl Seal for u64 {}
907 impl Seal for u128 {}
908 impl Seal for usize {}
909 impl Seal for i32 {}
910 impl Seal for i64 {}
911 impl Seal for isize {}
912}
913pub trait Seal: seal::Seal {}
914impl<T: seal::Seal> Seal for T {}
915pub trait SignedIndex:
919 Seal
920 + core::fmt::Debug
921 + core::ops::Neg<Output = Self>
922 + core::ops::Add<Output = Self>
923 + core::ops::Sub<Output = Self>
924 + core::ops::AddAssign
925 + core::ops::SubAssign
926 + bytemuck::Pod
927 + Eq
928 + Ord
929 + Send
930 + Sync
931{
932 const MAX: Self;
934 #[must_use]
936 fn truncate(value: usize) -> Self;
937 #[must_use]
939 fn zx(self) -> usize;
940 #[must_use]
942 fn sx(self) -> usize;
943 fn sum_nonnegative(slice: &[Self]) -> Option<Self> {
945 let mut acc = Self::zeroed();
946 for &i in slice {
947 if Self::MAX - i < acc {
948 return None;
949 }
950 acc += i;
951 }
952 Some(acc)
953 }
954}
955impl SignedIndex for i32 {
956 const MAX: Self = Self::MAX;
957
958 #[inline(always)]
959 fn truncate(value: usize) -> Self {
960 value as isize as Self
961 }
962
963 #[inline(always)]
964 fn zx(self) -> usize {
965 self as u32 as usize
966 }
967
968 #[inline(always)]
969 fn sx(self) -> usize {
970 self as isize as usize
971 }
972}
973impl SignedIndex for i64 {
974 const MAX: Self = Self::MAX;
975
976 #[inline(always)]
977 fn truncate(value: usize) -> Self {
978 value as isize as Self
979 }
980
981 #[inline(always)]
982 fn zx(self) -> usize {
983 self as u64 as usize
984 }
985
986 #[inline(always)]
987 fn sx(self) -> usize {
988 self as isize as usize
989 }
990}
991impl SignedIndex for isize {
992 const MAX: Self = Self::MAX;
993
994 #[inline(always)]
995 fn truncate(value: usize) -> Self {
996 value as isize
997 }
998
999 #[inline(always)]
1000 fn zx(self) -> usize {
1001 self as usize
1002 }
1003
1004 #[inline(always)]
1005 fn sx(self) -> usize {
1006 self as usize
1007 }
1008}
1009pub trait IndexCore:
1010 Seal
1011 + core::fmt::Debug
1012 + core::ops::Not<Output = Self>
1013 + core::ops::BitAnd<Output = Self>
1014 + core::ops::BitOr<Output = Self>
1015 + core::ops::BitXor<Output = Self>
1016 + core::ops::Add<Output = Self>
1017 + core::ops::Sub<Output = Self>
1018 + core::ops::AddAssign
1019 + core::ops::SubAssign
1020 + bytemuck::Pod
1021 + Eq
1022 + Ord
1023 + Send
1024 + Sync
1025 + Ord
1026{
1027 const MAX: Self;
1028 #[must_use]
1030 fn truncate(value: usize) -> Self;
1031 #[must_use]
1033 fn zx(self) -> usize;
1034}
1035pub trait Index: IndexCore {
1036 type FixedWidth: Index;
1038 type Signed: SignedIndex;
1040 const BITS: u32 = core::mem::size_of::<Self>() as u32 * 8;
1041 #[inline(always)]
1043 fn canonicalize(slice: &[Self]) -> &[Self::FixedWidth] {
1044 bytemuck::cast_slice(slice)
1045 }
1046 #[inline(always)]
1048 fn canonicalize_mut(slice: &mut [Self]) -> &mut [Self::FixedWidth] {
1049 bytemuck::cast_slice_mut(slice)
1050 }
1051 #[inline(always)]
1053 fn from_signed(value: Self::Signed) -> Self {
1054 bytemuck::cast(value)
1055 }
1056 #[inline(always)]
1058 fn to_signed(self) -> Self::Signed {
1059 bytemuck::cast(self)
1060 }
1061 #[inline]
1063 fn sum_nonnegative(slice: &[Self]) -> Option<Self> {
1064 Self::Signed::sum_nonnegative(bytemuck::cast_slice(slice))
1065 .map(Self::from_signed)
1066 }
1067}
1068impl IndexCore for u8 {
1069 const MAX: Self = Self::MAX;
1070
1071 #[inline(always)]
1072 fn truncate(value: usize) -> Self {
1073 value as _
1074 }
1075
1076 #[inline(always)]
1077 fn zx(self) -> usize {
1078 self as _
1079 }
1080}
1081impl IndexCore for u16 {
1082 const MAX: Self = Self::MAX;
1083
1084 #[inline(always)]
1085 fn truncate(value: usize) -> Self {
1086 value as _
1087 }
1088
1089 #[inline(always)]
1090 fn zx(self) -> usize {
1091 self as _
1092 }
1093}
1094impl IndexCore for u32 {
1095 const MAX: Self = Self::MAX;
1096
1097 #[inline(always)]
1098 fn truncate(value: usize) -> Self {
1099 value as _
1100 }
1101
1102 #[inline(always)]
1103 fn zx(self) -> usize {
1104 self as _
1105 }
1106}
1107impl IndexCore for u64 {
1108 const MAX: Self = Self::MAX;
1109
1110 #[inline(always)]
1111 fn truncate(value: usize) -> Self {
1112 value as _
1113 }
1114
1115 #[inline(always)]
1116 fn zx(self) -> usize {
1117 self as _
1118 }
1119}
1120impl IndexCore for u128 {
1121 const MAX: Self = Self::MAX;
1122
1123 #[inline(always)]
1124 fn truncate(value: usize) -> Self {
1125 value as _
1126 }
1127
1128 #[inline(always)]
1129 fn zx(self) -> usize {
1130 self as _
1131 }
1132}
1133impl IndexCore for usize {
1134 const MAX: Self = Self::MAX;
1135
1136 #[inline(always)]
1137 fn truncate(value: usize) -> Self {
1138 value
1139 }
1140
1141 #[inline(always)]
1142 fn zx(self) -> usize {
1143 self
1144 }
1145}
1146impl Index for u32 {
1147 type FixedWidth = u32;
1148 type Signed = i32;
1149}
1150impl Index for u64 {
1151 type FixedWidth = u64;
1152 type Signed = i64;
1153}
1154impl Index for usize {
1155 #[cfg(target_pointer_width = "32")]
1156 type FixedWidth = u32;
1157 #[cfg(target_pointer_width = "64")]
1158 type FixedWidth = u64;
1159 type Signed = isize;
1160}
1161unsafe impl<T: RealField> Conjugate for T {
1162 type Canonical = T;
1163 type Conj = T;
1164
1165 const IS_CANONICAL: bool = true;
1166}
1167unsafe impl<T: RealField> Conjugate for Complex<T> {
1168 type Canonical = Complex<T>;
1169 type Conj = ComplexConj<T>;
1170
1171 const IS_CANONICAL: bool = true;
1172}
1173unsafe impl<T: RealField> Conjugate for ComplexConj<T> {
1174 type Canonical = Complex<T>;
1175 type Conj = Complex<T>;
1176
1177 const IS_CANONICAL: bool = false;
1178}
1179pub trait SimdArch: Copy + Default + Send + Sync {
1180 fn dispatch<R>(self, f: impl pulp::WithSimd<Output = R>) -> R;
1181}
1182impl SimdArch for pulp::Arch {
1183 #[inline]
1184 fn dispatch<R>(self, f: impl pulp::WithSimd<Output = R>) -> R {
1185 self.dispatch(f)
1186 }
1187}
1188impl SimdArch for pulp::Scalar {
1189 #[inline]
1190 fn dispatch<R>(self, f: impl pulp::WithSimd<Output = R>) -> R {
1191 f.with_simd(self)
1192 }
1193}
1194pub trait RefOps:
1195 Sized
1196 + core::ops::Neg<Output = Self>
1197 + core::ops::Add<Output = Self>
1198 + core::ops::Sub<Output = Self>
1199 + core::ops::Mul<Output = Self>
1200 + core::ops::Div<Output = Self>
1201 + core::ops::AddAssign
1202 + core::ops::SubAssign
1203 + core::ops::MulAssign
1204 + core::ops::DivAssign
1205 + for<'a> core::ops::Add<&'a Self, Output = Self>
1206 + for<'a> core::ops::Sub<&'a Self, Output = Self>
1207 + for<'a> core::ops::Mul<&'a Self, Output = Self>
1208 + for<'a> core::ops::Div<&'a Self, Output = Self>
1209 + for<'a> core::ops::AddAssign<&'a Self>
1210 + for<'a> core::ops::SubAssign<&'a Self>
1211 + for<'a> core::ops::MulAssign<&'a Self>
1212 + for<'a> core::ops::DivAssign<&'a Self>
1213 + for<'a> Imply<
1214 &'a Self,
1215 Is: core::ops::Neg<Output = Self>
1216 + core::ops::Add<Output = Self>
1217 + core::ops::Sub<Output = Self>
1218 + core::ops::Mul<Output = Self>
1219 + core::ops::Div<Output = Self>
1220 + core::ops::Add<Self, Output = Self>
1221 + core::ops::Sub<Self, Output = Self>
1222 + core::ops::Mul<Self, Output = Self>
1223 + core::ops::Div<Self, Output = Self>,
1224 >
1225{
1226}
1227impl<T> RefOps for T
1228where
1229 T: core::ops::Neg<Output = Self>
1230 + core::ops::Add<Output = Self>
1231 + core::ops::Sub<Output = Self>
1232 + core::ops::Mul<Output = Self>
1233 + core::ops::Div<Output = Self>
1234 + core::ops::AddAssign
1235 + core::ops::SubAssign
1236 + core::ops::MulAssign
1237 + core::ops::DivAssign
1238 + for<'a> core::ops::Add<&'a T, Output = Self>
1239 + for<'a> core::ops::Sub<&'a T, Output = Self>
1240 + for<'a> core::ops::Mul<&'a T, Output = Self>
1241 + for<'a> core::ops::Div<&'a T, Output = Self>
1242 + for<'a> core::ops::AddAssign<&'a T>
1243 + for<'a> core::ops::SubAssign<&'a T>
1244 + for<'a> core::ops::MulAssign<&'a T>
1245 + for<'a> core::ops::DivAssign<&'a T>,
1246 for<'a> &'a T: core::ops::Neg<Output = T>
1247 + core::ops::Add<Output = T>
1248 + core::ops::Sub<Output = T>
1249 + core::ops::Mul<Output = T>
1250 + core::ops::Div<Output = T>
1251 + core::ops::Add<T, Output = T>
1252 + core::ops::Sub<T, Output = T>
1253 + core::ops::Mul<T, Output = T>
1254 + core::ops::Div<T, Output = T>,
1255{
1256}
1257pub trait ComplexField:
1258 Debug
1259 + Clone
1260 + Conjugate<Canonical = Self>
1261 + PartialEq
1262 + RefOps
1263 + Imply<Self::Real, Is: RefOps>
1264{
1265 const IS_REAL: bool;
1266 const SIMD_ABS_SPLIT_REAL_IMAG: bool = false;
1267 type Arch: SimdArch;
1268 type Unit: ComplexField;
1269 type SimdCtx<S: Simd>: Copy;
1270 type Index: IndexCore;
1271 type Real: RealField;
1272 #[doc(hidden)]
1273 const IS_NATIVE_F32: bool = false;
1274 #[doc(hidden)]
1275 const IS_NATIVE_C32: bool = false;
1276 #[doc(hidden)]
1277 const IS_NATIVE_F64: bool = false;
1278 #[doc(hidden)]
1279 const IS_NATIVE_C64: bool = false;
1280 const SIMD_CAPABILITIES: SimdCapabilities;
1281 type SimdMask<S: Simd>: Copy + Debug;
1282 type SimdMemMask<S: Simd>: Copy + Debug;
1283 type SimdVec<S: Simd>: Pod + Debug;
1284 type SimdIndex<S: Simd>: Pod + Debug;
1285 fn zero_impl() -> Self;
1286 fn one_impl() -> Self;
1287 fn nan_impl() -> Self;
1288 fn infinity_impl() -> Self;
1289 fn from_real_impl(real: &Self::Real) -> Self;
1290 fn from_f64_impl(real: f64) -> Self;
1291 fn real_part_impl(value: &Self) -> Self::Real;
1292 fn imag_part_impl(value: &Self) -> Self::Real;
1293 fn copy_impl(value: &Self) -> Self;
1294 fn conj_impl(value: &Self) -> Self;
1295 fn recip_impl(value: &Self) -> Self;
1296 fn sqrt_impl(value: &Self) -> Self;
1297 fn abs_impl(value: &Self) -> Self::Real;
1298 fn abs1_impl(value: &Self) -> Self::Real;
1299 fn abs2_impl(value: &Self) -> Self::Real;
1300 fn mul_real_impl(lhs: &Self, rhs: &Self::Real) -> Self;
1301 fn mul_pow2_impl(lhs: &Self, rhs: &Self::Real) -> Self;
1302 fn is_finite_impl(value: &Self) -> bool;
1303 #[allow(clippy::eq_op)]
1305 fn is_nan_impl(value: &Self) -> bool {
1306 value != value
1307 }
1308 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S>;
1309 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S;
1310 fn simd_mask_between<S: Simd>(
1311 ctx: &Self::SimdCtx<S>,
1312 start: Self::Index,
1313 end: Self::Index,
1314 ) -> Self::SimdMask<S>;
1315 fn simd_mem_mask_between<S: Simd>(
1316 ctx: &Self::SimdCtx<S>,
1317 start: Self::Index,
1318 end: Self::Index,
1319 ) -> Self::SimdMemMask<S>;
1320 unsafe fn simd_mask_load_raw<S: Simd>(
1325 ctx: &Self::SimdCtx<S>,
1326 mask: Self::SimdMemMask<S>,
1327 ptr: *const Self::SimdVec<S>,
1328 ) -> Self::SimdVec<S>;
1329 unsafe fn simd_mask_store_raw<S: Simd>(
1334 ctx: &Self::SimdCtx<S>,
1335 mask: Self::SimdMemMask<S>,
1336 ptr: *mut Self::SimdVec<S>,
1337 values: Self::SimdVec<S>,
1338 );
1339 fn simd_splat<S: Simd>(
1340 ctx: &Self::SimdCtx<S>,
1341 value: &Self,
1342 ) -> Self::SimdVec<S>;
1343 fn simd_splat_real<S: Simd>(
1344 ctx: &Self::SimdCtx<S>,
1345 value: &Self::Real,
1346 ) -> Self::SimdVec<S>;
1347 fn simd_add<S: Simd>(
1348 ctx: &Self::SimdCtx<S>,
1349 lhs: Self::SimdVec<S>,
1350 rhs: Self::SimdVec<S>,
1351 ) -> Self::SimdVec<S>;
1352 fn simd_sub<S: Simd>(
1353 ctx: &Self::SimdCtx<S>,
1354 lhs: Self::SimdVec<S>,
1355 rhs: Self::SimdVec<S>,
1356 ) -> Self::SimdVec<S>;
1357 fn simd_neg<S: Simd>(
1358 ctx: &Self::SimdCtx<S>,
1359 value: Self::SimdVec<S>,
1360 ) -> Self::SimdVec<S>;
1361 fn simd_conj<S: Simd>(
1362 ctx: &Self::SimdCtx<S>,
1363 value: Self::SimdVec<S>,
1364 ) -> Self::SimdVec<S>;
1365 fn simd_abs1<S: Simd>(
1366 ctx: &Self::SimdCtx<S>,
1367 value: Self::SimdVec<S>,
1368 ) -> Self::SimdVec<S>;
1369 fn simd_abs_max<S: Simd>(
1370 ctx: &Self::SimdCtx<S>,
1371 value: Self::SimdVec<S>,
1372 ) -> Self::SimdVec<S>;
1373 fn simd_mul_real<S: Simd>(
1374 ctx: &Self::SimdCtx<S>,
1375 lhs: Self::SimdVec<S>,
1376 real_rhs: Self::SimdVec<S>,
1377 ) -> Self::SimdVec<S>;
1378 fn simd_mul_pow2<S: Simd>(
1379 ctx: &Self::SimdCtx<S>,
1380 lhs: Self::SimdVec<S>,
1381 real_rhs: Self::SimdVec<S>,
1382 ) -> Self::SimdVec<S>;
1383 fn simd_mul<S: Simd>(
1384 ctx: &Self::SimdCtx<S>,
1385 lhs: Self::SimdVec<S>,
1386 rhs: Self::SimdVec<S>,
1387 ) -> Self::SimdVec<S>;
1388 fn simd_conj_mul<S: Simd>(
1389 ctx: &Self::SimdCtx<S>,
1390 lhs: Self::SimdVec<S>,
1391 rhs: Self::SimdVec<S>,
1392 ) -> Self::SimdVec<S>;
1393 fn simd_mul_add<S: Simd>(
1394 ctx: &Self::SimdCtx<S>,
1395 lhs: Self::SimdVec<S>,
1396 rhs: Self::SimdVec<S>,
1397 acc: Self::SimdVec<S>,
1398 ) -> Self::SimdVec<S>;
1399 fn simd_conj_mul_add<S: Simd>(
1400 ctx: &Self::SimdCtx<S>,
1401 lhs: Self::SimdVec<S>,
1402 rhs: Self::SimdVec<S>,
1403 acc: Self::SimdVec<S>,
1404 ) -> Self::SimdVec<S>;
1405 fn simd_abs2<S: Simd>(
1406 ctx: &Self::SimdCtx<S>,
1407 value: Self::SimdVec<S>,
1408 ) -> Self::SimdVec<S>;
1409 fn simd_abs2_add<S: Simd>(
1410 ctx: &Self::SimdCtx<S>,
1411 value: Self::SimdVec<S>,
1412 acc: Self::SimdVec<S>,
1413 ) -> Self::SimdVec<S>;
1414 fn simd_reduce_sum<S: Simd>(
1415 ctx: &Self::SimdCtx<S>,
1416 value: Self::SimdVec<S>,
1417 ) -> Self;
1418 fn simd_reduce_max<S: Simd>(
1419 ctx: &Self::SimdCtx<S>,
1420 value: Self::SimdVec<S>,
1421 ) -> Self;
1422 fn simd_equal<S: Simd>(
1423 ctx: &Self::SimdCtx<S>,
1424 real_lhs: Self::SimdVec<S>,
1425 real_rhs: Self::SimdVec<S>,
1426 ) -> Self::SimdMask<S>;
1427 fn simd_less_than<S: Simd>(
1428 ctx: &Self::SimdCtx<S>,
1429 real_lhs: Self::SimdVec<S>,
1430 real_rhs: Self::SimdVec<S>,
1431 ) -> Self::SimdMask<S>;
1432 fn simd_less_than_or_equal<S: Simd>(
1433 ctx: &Self::SimdCtx<S>,
1434 real_lhs: Self::SimdVec<S>,
1435 real_rhs: Self::SimdVec<S>,
1436 ) -> Self::SimdMask<S>;
1437 fn simd_greater_than<S: Simd>(
1438 ctx: &Self::SimdCtx<S>,
1439 real_lhs: Self::SimdVec<S>,
1440 real_rhs: Self::SimdVec<S>,
1441 ) -> Self::SimdMask<S>;
1442 fn simd_greater_than_or_equal<S: Simd>(
1443 ctx: &Self::SimdCtx<S>,
1444 real_lhs: Self::SimdVec<S>,
1445 real_rhs: Self::SimdVec<S>,
1446 ) -> Self::SimdMask<S>;
1447 fn simd_select<S: Simd>(
1448 ctx: &Self::SimdCtx<S>,
1449 mask: Self::SimdMask<S>,
1450 lhs: Self::SimdVec<S>,
1451 rhs: Self::SimdVec<S>,
1452 ) -> Self::SimdVec<S>;
1453 fn simd_index_select<S: Simd>(
1454 ctx: &Self::SimdCtx<S>,
1455 mask: Self::SimdMask<S>,
1456 lhs: Self::SimdIndex<S>,
1457 rhs: Self::SimdIndex<S>,
1458 ) -> Self::SimdIndex<S>;
1459 fn simd_index_splat<S: Simd>(
1460 ctx: &Self::SimdCtx<S>,
1461 value: Self::Index,
1462 ) -> Self::SimdIndex<S>;
1463 fn simd_index_add<S: Simd>(
1464 ctx: &Self::SimdCtx<S>,
1465 lhs: Self::SimdIndex<S>,
1466 rhs: Self::SimdIndex<S>,
1467 ) -> Self::SimdIndex<S>;
1468 fn simd_index_less_than<S: Simd>(
1469 ctx: &Self::SimdCtx<S>,
1470 lhs: Self::SimdIndex<S>,
1471 rhs: Self::SimdIndex<S>,
1472 ) -> Self::SimdMask<S>;
1473 #[inline(always)]
1474 fn simd_index_greater_than<S: Simd>(
1475 ctx: &Self::SimdCtx<S>,
1476 lhs: Self::SimdIndex<S>,
1477 rhs: Self::SimdIndex<S>,
1478 ) -> Self::SimdMask<S> {
1479 Self::simd_index_less_than(ctx, rhs, lhs)
1480 }
1481 #[inline(always)]
1482 fn simd_index_less_than_or_equal<S: Simd>(
1483 ctx: &Self::SimdCtx<S>,
1484 lhs: Self::SimdIndex<S>,
1485 rhs: Self::SimdIndex<S>,
1486 ) -> Self::SimdMask<S> {
1487 Self::simd_not_mask(ctx, Self::simd_index_less_than(ctx, rhs, lhs))
1488 }
1489 #[inline(always)]
1490 fn simd_index_greater_than_or_equal<S: Simd>(
1491 ctx: &Self::SimdCtx<S>,
1492 lhs: Self::SimdIndex<S>,
1493 rhs: Self::SimdIndex<S>,
1494 ) -> Self::SimdMask<S> {
1495 Self::simd_not_mask(ctx, Self::simd_index_greater_than(ctx, rhs, lhs))
1496 }
1497 fn simd_and_mask<S: Simd>(
1498 ctx: &Self::SimdCtx<S>,
1499 lhs: Self::SimdMask<S>,
1500 rhs: Self::SimdMask<S>,
1501 ) -> Self::SimdMask<S>;
1502 fn simd_or_mask<S: Simd>(
1503 ctx: &Self::SimdCtx<S>,
1504 lhs: Self::SimdMask<S>,
1505 rhs: Self::SimdMask<S>,
1506 ) -> Self::SimdMask<S>;
1507 fn simd_not_mask<S: Simd>(
1508 ctx: &Self::SimdCtx<S>,
1509 mask: Self::SimdMask<S>,
1510 ) -> Self::SimdMask<S>;
1511 fn simd_first_true_mask<S: Simd>(
1512 ctx: &Self::SimdCtx<S>,
1513 value: Self::SimdMask<S>,
1514 ) -> usize;
1515 #[inline(always)]
1516 fn simd_load<S: Simd>(
1517 ctx: &Self::SimdCtx<S>,
1518 ptr: &Self::SimdVec<S>,
1519 ) -> Self::SimdVec<S> {
1520 let simd = Self::ctx_from_simd(ctx);
1521 if const { Self::Unit::IS_NATIVE_F32 } {
1522 simd.deinterleave_shfl_f32s(*ptr)
1523 } else if const { Self::Unit::IS_NATIVE_F64 } {
1524 simd.deinterleave_shfl_f64s(*ptr)
1525 } else {
1526 panic!();
1527 }
1528 }
1529 #[inline(always)]
1530 fn simd_store<S: Simd>(
1531 ctx: &Self::SimdCtx<S>,
1532 ptr: &mut Self::SimdVec<S>,
1533 value: Self::SimdVec<S>,
1534 ) {
1535 let simd = Self::ctx_from_simd(ctx);
1536 if const { Self::Unit::IS_NATIVE_F32 } {
1537 *ptr = simd.interleave_shfl_f32s(value)
1538 } else if const { Self::Unit::IS_NATIVE_F64 } {
1539 *ptr = simd.interleave_shfl_f64s(value)
1540 } else {
1541 panic!();
1542 }
1543 }
1544 #[inline(always)]
1549 unsafe fn simd_mask_load<S: Simd>(
1550 ctx: &Self::SimdCtx<S>,
1551 mask: Self::SimdMemMask<S>,
1552 ptr: *const Self::SimdVec<S>,
1553 ) -> Self::SimdVec<S> {
1554 let simd = Self::ctx_from_simd(ctx);
1555 let value = Self::simd_mask_load_raw(ctx, mask, ptr);
1556 if const { Self::Unit::IS_NATIVE_F32 } {
1557 simd.deinterleave_shfl_f32s(value)
1558 } else if const { Self::Unit::IS_NATIVE_F64 } {
1559 simd.deinterleave_shfl_f64s(value)
1560 } else {
1561 panic!();
1562 }
1563 }
1564 #[inline(always)]
1569 unsafe fn simd_mask_store<S: Simd>(
1570 ctx: &Self::SimdCtx<S>,
1571 mask: Self::SimdMemMask<S>,
1572 ptr: *mut Self::SimdVec<S>,
1573 value: Self::SimdVec<S>,
1574 ) {
1575 let simd = Self::ctx_from_simd(ctx);
1576 if const { Self::Unit::IS_NATIVE_F32 } {
1577 Self::simd_mask_store_raw(
1578 ctx,
1579 mask,
1580 ptr,
1581 simd.interleave_shfl_f32s(value),
1582 )
1583 } else if const { Self::Unit::IS_NATIVE_F64 } {
1584 Self::simd_mask_store_raw(
1585 ctx,
1586 mask,
1587 ptr,
1588 simd.interleave_shfl_f64s(value),
1589 )
1590 } else {
1591 panic!();
1592 }
1593 }
1594 #[inline(always)]
1595 fn simd_iota<S: Simd>(ctx: &Self::SimdCtx<S>) -> Self::SimdIndex<S> {
1596 let simd = Self::ctx_from_simd(ctx);
1597 #[repr(transparent)]
1598 struct Interleave<T>(T);
1599 unsafe impl<T> pulp::Interleave for Interleave<T> {}
1600 unsafe {
1601 if const { Self::Unit::IS_NATIVE_F32 } {
1602 core::mem::transmute_copy::<_, Self::SimdIndex<S>>(
1603 &simd.deinterleave_shfl_f32s(Interleave(
1604 core::mem::transmute_copy::<_, Self::SimdVec<S>>(
1605 &pulp::iota_32::<Interleave<Self>, 64>(),
1606 ),
1607 )),
1608 )
1609 } else if const { Self::Unit::IS_NATIVE_F64 } {
1610 core::mem::transmute_copy::<_, Self::SimdIndex<S>>(
1611 &simd.deinterleave_shfl_f64s(core::mem::transmute_copy::<
1612 _,
1613 Self::SimdVec<S>,
1614 >(&pulp::iota_64::<
1615 Interleave<Self>,
1616 64,
1617 >())),
1618 )
1619 } else {
1620 panic!();
1621 }
1622 }
1623 }
1624}
1625pub trait RealField:
1626 ComplexField<Real = Self, Conj = Self>
1627 + PartialOrd
1628 + num_traits::NumAssign
1629 + num_traits::Num
1630 + RefOps
1631{
1632 fn epsilon_impl() -> Self;
1633 fn nbits_impl() -> usize;
1634 fn min_positive_impl() -> Self;
1635 fn max_positive_impl() -> Self;
1636 fn sqrt_min_positive_impl() -> Self;
1637 fn sqrt_max_positive_impl() -> Self;
1638}
1639impl ComplexField for f32 {
1640 type Arch = pulp::Arch;
1641 type Index = u32;
1642 type Real = Self;
1643 type SimdCtx<S: Simd> = S;
1644 type SimdIndex<S: Simd> = S::u32s;
1645 type SimdMask<S: Simd> = S::m32s;
1646 type SimdMemMask<S: Simd> = pulp::MemMask<S::m32s>;
1647 type SimdVec<S: Simd> = S::f32s;
1648 type Unit = Self;
1649
1650 const IS_NATIVE_F32: bool = true;
1651 const IS_REAL: bool = true;
1652 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Simd;
1653
1654 #[inline(always)]
1655 fn zero_impl() -> Self {
1656 0.0
1657 }
1658
1659 #[inline(always)]
1660 fn one_impl() -> Self {
1661 1.0
1662 }
1663
1664 #[inline(always)]
1665 fn nan_impl() -> Self {
1666 Self::NAN
1667 }
1668
1669 #[inline(always)]
1670 fn infinity_impl() -> Self {
1671 Self::INFINITY
1672 }
1673
1674 #[inline(always)]
1675 fn from_real_impl(value: &Self) -> Self {
1676 *value
1677 }
1678
1679 #[inline(always)]
1680 fn from_f64_impl(value: f64) -> Self {
1681 value as _
1682 }
1683
1684 #[inline(always)]
1685 fn real_part_impl(value: &Self) -> Self {
1686 *value
1687 }
1688
1689 #[inline(always)]
1690 fn imag_part_impl(_: &Self) -> Self {
1691 0.0
1692 }
1693
1694 #[inline(always)]
1695 fn copy_impl(value: &Self) -> Self {
1696 *value
1697 }
1698
1699 #[inline(always)]
1700 fn conj_impl(value: &Self) -> Self {
1701 *value
1702 }
1703
1704 #[inline(always)]
1705 fn recip_impl(value: &Self) -> Self {
1706 1.0 / *value
1707 }
1708
1709 #[inline(always)]
1710 fn sqrt_impl(value: &Self) -> Self {
1711 #[cfg(feature = "std")]
1712 {
1713 Self::sqrt(*value)
1714 }
1715 #[cfg(not(feature = "std"))]
1716 {
1717 libm::sqrtf(*value)
1718 }
1719 }
1720
1721 #[inline(always)]
1722 fn abs_impl(value: &Self) -> Self {
1723 (*value).abs()
1724 }
1725
1726 #[inline(always)]
1727 fn abs1_impl(value: &Self) -> Self {
1728 (*value).abs()
1729 }
1730
1731 #[inline(always)]
1732 fn abs2_impl(value: &Self) -> Self {
1733 (*value) * (*value)
1734 }
1735
1736 #[inline(always)]
1737 fn mul_real_impl(lhs: &Self, rhs: &Self) -> Self {
1738 (*lhs) * (*rhs)
1739 }
1740
1741 #[inline(always)]
1742 fn mul_pow2_impl(lhs: &Self, rhs: &Self) -> Self {
1743 (*lhs) * (*rhs)
1744 }
1745
1746 #[inline(always)]
1747 fn is_finite_impl(value: &Self) -> bool {
1748 (*value).is_finite()
1749 }
1750
1751 #[inline(always)]
1752 fn is_nan_impl(value: &Self) -> bool {
1753 (*value).is_nan()
1754 }
1755
1756 #[inline(always)]
1757 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
1758 simd
1759 }
1760
1761 #[inline(always)]
1762 fn simd_splat<S: Simd>(
1763 ctx: &Self::SimdCtx<S>,
1764 value: &Self,
1765 ) -> Self::SimdVec<S> {
1766 ctx.splat_f32s(*value)
1767 }
1768
1769 #[inline(always)]
1770 fn simd_splat_real<S: Simd>(
1771 ctx: &Self::SimdCtx<S>,
1772 value: &Self::Real,
1773 ) -> Self::SimdVec<S> {
1774 ctx.splat_f32s(*value)
1775 }
1776
1777 #[inline(always)]
1778 fn simd_add<S: Simd>(
1779 ctx: &Self::SimdCtx<S>,
1780 lhs: Self::SimdVec<S>,
1781 rhs: Self::SimdVec<S>,
1782 ) -> Self::SimdVec<S> {
1783 ctx.add_f32s(lhs, rhs)
1784 }
1785
1786 #[inline(always)]
1787 fn simd_sub<S: Simd>(
1788 ctx: &Self::SimdCtx<S>,
1789 lhs: Self::SimdVec<S>,
1790 rhs: Self::SimdVec<S>,
1791 ) -> Self::SimdVec<S> {
1792 ctx.sub_f32s(lhs, rhs)
1793 }
1794
1795 #[inline(always)]
1796 fn simd_neg<S: Simd>(
1797 ctx: &Self::SimdCtx<S>,
1798 value: Self::SimdVec<S>,
1799 ) -> Self::SimdVec<S> {
1800 ctx.neg_f32s(value)
1801 }
1802
1803 #[inline(always)]
1804 fn simd_conj<S: Simd>(
1805 _: &Self::SimdCtx<S>,
1806 value: Self::SimdVec<S>,
1807 ) -> Self::SimdVec<S> {
1808 value
1809 }
1810
1811 #[inline(always)]
1812 fn simd_abs1<S: Simd>(
1813 ctx: &Self::SimdCtx<S>,
1814 value: Self::SimdVec<S>,
1815 ) -> Self::SimdVec<S> {
1816 ctx.abs_f32s(value)
1817 }
1818
1819 #[inline(always)]
1820 fn simd_mul<S: Simd>(
1821 ctx: &Self::SimdCtx<S>,
1822 lhs: Self::SimdVec<S>,
1823 rhs: Self::SimdVec<S>,
1824 ) -> Self::SimdVec<S> {
1825 ctx.mul_f32s(lhs, rhs)
1826 }
1827
1828 #[inline(always)]
1829 fn simd_mul_real<S: Simd>(
1830 ctx: &Self::SimdCtx<S>,
1831 lhs: Self::SimdVec<S>,
1832 real_rhs: Self::SimdVec<S>,
1833 ) -> Self::SimdVec<S> {
1834 ctx.mul_f32s(lhs, real_rhs)
1835 }
1836
1837 #[inline(always)]
1838 fn simd_mul_pow2<S: Simd>(
1839 ctx: &Self::SimdCtx<S>,
1840 lhs: Self::SimdVec<S>,
1841 real_rhs: Self::SimdVec<S>,
1842 ) -> Self::SimdVec<S> {
1843 ctx.mul_f32s(lhs, real_rhs)
1844 }
1845
1846 #[inline(always)]
1847 fn simd_conj_mul<S: Simd>(
1848 ctx: &Self::SimdCtx<S>,
1849 lhs: Self::SimdVec<S>,
1850 rhs: Self::SimdVec<S>,
1851 ) -> Self::SimdVec<S> {
1852 ctx.mul_f32s(lhs, rhs)
1853 }
1854
1855 #[inline(always)]
1856 fn simd_mul_add<S: Simd>(
1857 ctx: &Self::SimdCtx<S>,
1858 lhs: Self::SimdVec<S>,
1859 rhs: Self::SimdVec<S>,
1860 acc: Self::SimdVec<S>,
1861 ) -> Self::SimdVec<S> {
1862 ctx.mul_add_e_f32s(lhs, rhs, acc)
1863 }
1864
1865 #[inline(always)]
1866 fn simd_conj_mul_add<S: Simd>(
1867 ctx: &Self::SimdCtx<S>,
1868 lhs: Self::SimdVec<S>,
1869 rhs: Self::SimdVec<S>,
1870 acc: Self::SimdVec<S>,
1871 ) -> Self::SimdVec<S> {
1872 ctx.mul_add_e_f32s(lhs, rhs, acc)
1873 }
1874
1875 #[inline(always)]
1876 fn simd_abs2<S: Simd>(
1877 ctx: &Self::SimdCtx<S>,
1878 value: Self::SimdVec<S>,
1879 ) -> Self::SimdVec<S> {
1880 ctx.mul_f32s(value, value)
1881 }
1882
1883 #[inline(always)]
1884 fn simd_abs2_add<S: Simd>(
1885 ctx: &Self::SimdCtx<S>,
1886 value: Self::SimdVec<S>,
1887 acc: Self::SimdVec<S>,
1888 ) -> Self::SimdVec<S> {
1889 ctx.mul_add_e_f32s(value, value, acc)
1890 }
1891
1892 #[inline(always)]
1893 fn simd_reduce_sum<S: Simd>(
1894 ctx: &Self::SimdCtx<S>,
1895 value: Self::SimdVec<S>,
1896 ) -> Self {
1897 ctx.reduce_sum_f32s(value)
1898 }
1899
1900 #[inline(always)]
1901 fn simd_reduce_max<S: Simd>(
1902 ctx: &Self::SimdCtx<S>,
1903 value: Self::SimdVec<S>,
1904 ) -> Self {
1905 ctx.reduce_max_f32s(value)
1906 }
1907
1908 #[inline(always)]
1909 fn simd_equal<S: Simd>(
1910 ctx: &Self::SimdCtx<S>,
1911 real_lhs: Self::SimdVec<S>,
1912 real_rhs: Self::SimdVec<S>,
1913 ) -> Self::SimdMask<S> {
1914 ctx.equal_f32s(real_lhs, real_rhs)
1915 }
1916
1917 #[inline(always)]
1918 fn simd_less_than<S: Simd>(
1919 ctx: &Self::SimdCtx<S>,
1920 real_lhs: Self::SimdVec<S>,
1921 real_rhs: Self::SimdVec<S>,
1922 ) -> Self::SimdMask<S> {
1923 ctx.less_than_f32s(real_lhs, real_rhs)
1924 }
1925
1926 #[inline(always)]
1927 fn simd_greater_than<S: Simd>(
1928 ctx: &Self::SimdCtx<S>,
1929 real_lhs: Self::SimdVec<S>,
1930 real_rhs: Self::SimdVec<S>,
1931 ) -> Self::SimdMask<S> {
1932 ctx.greater_than_f32s(real_lhs, real_rhs)
1933 }
1934
1935 #[inline(always)]
1936 fn simd_less_than_or_equal<S: Simd>(
1937 ctx: &Self::SimdCtx<S>,
1938 real_lhs: Self::SimdVec<S>,
1939 real_rhs: Self::SimdVec<S>,
1940 ) -> Self::SimdMask<S> {
1941 ctx.less_than_or_equal_f32s(real_lhs, real_rhs)
1942 }
1943
1944 #[inline(always)]
1945 fn simd_greater_than_or_equal<S: Simd>(
1946 ctx: &Self::SimdCtx<S>,
1947 real_lhs: Self::SimdVec<S>,
1948 real_rhs: Self::SimdVec<S>,
1949 ) -> Self::SimdMask<S> {
1950 ctx.greater_than_or_equal_f32s(real_lhs, real_rhs)
1951 }
1952
1953 #[inline(always)]
1954 fn simd_select<S: Simd>(
1955 ctx: &Self::SimdCtx<S>,
1956 mask: Self::SimdMask<S>,
1957 lhs: Self::SimdVec<S>,
1958 rhs: Self::SimdVec<S>,
1959 ) -> Self::SimdVec<S> {
1960 ctx.select_f32s(mask, lhs, rhs)
1961 }
1962
1963 #[inline(always)]
1964 fn simd_index_select<S: Simd>(
1965 ctx: &Self::SimdCtx<S>,
1966 mask: Self::SimdMask<S>,
1967 lhs: Self::SimdIndex<S>,
1968 rhs: Self::SimdIndex<S>,
1969 ) -> Self::SimdIndex<S> {
1970 ctx.select_u32s(mask, lhs, rhs)
1971 }
1972
1973 #[inline(always)]
1974 fn simd_index_splat<S: Simd>(
1975 ctx: &Self::SimdCtx<S>,
1976 value: Self::Index,
1977 ) -> Self::SimdIndex<S> {
1978 ctx.splat_u32s(value as _)
1979 }
1980
1981 #[inline(always)]
1982 fn simd_index_add<S: Simd>(
1983 ctx: &Self::SimdCtx<S>,
1984 lhs: Self::SimdIndex<S>,
1985 rhs: Self::SimdIndex<S>,
1986 ) -> Self::SimdIndex<S> {
1987 ctx.add_u32s(lhs, rhs)
1988 }
1989
1990 #[inline(always)]
1991 fn simd_index_less_than<S: Simd>(
1992 ctx: &Self::SimdCtx<S>,
1993 lhs: Self::SimdIndex<S>,
1994 rhs: Self::SimdIndex<S>,
1995 ) -> Self::SimdMask<S> {
1996 ctx.less_than_u32s(lhs, rhs)
1997 }
1998
1999 #[inline(always)]
2000 fn simd_abs_max<S: Simd>(
2001 ctx: &Self::SimdCtx<S>,
2002 value: Self::SimdVec<S>,
2003 ) -> Self::SimdVec<S> {
2004 ctx.abs_f32s(value)
2005 }
2006
2007 #[inline(always)]
2008 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
2009 *ctx
2010 }
2011
2012 #[inline(always)]
2013 fn simd_and_mask<S: Simd>(
2014 simd: &Self::SimdCtx<S>,
2015 lhs: Self::SimdMask<S>,
2016 rhs: Self::SimdMask<S>,
2017 ) -> Self::SimdMask<S> {
2018 simd.and_m32s(lhs, rhs)
2019 }
2020
2021 #[inline(always)]
2022 fn simd_or_mask<S: Simd>(
2023 simd: &Self::SimdCtx<S>,
2024 lhs: Self::SimdMask<S>,
2025 rhs: Self::SimdMask<S>,
2026 ) -> Self::SimdMask<S> {
2027 simd.or_m32s(lhs, rhs)
2028 }
2029
2030 #[inline(always)]
2031 fn simd_not_mask<S: Simd>(
2032 ctx: &Self::SimdCtx<S>,
2033 mask: Self::SimdMask<S>,
2034 ) -> Self::SimdMask<S> {
2035 ctx.not_m32s(mask)
2036 }
2037
2038 #[inline(always)]
2039 fn simd_first_true_mask<S: Simd>(
2040 ctx: &Self::SimdCtx<S>,
2041 value: Self::SimdMask<S>,
2042 ) -> usize {
2043 ctx.first_true_m32s(value)
2044 }
2045
2046 #[inline(always)]
2047 fn simd_mem_mask_between<S: Simd>(
2048 ctx: &Self::SimdCtx<S>,
2049 start: Self::Index,
2050 end: Self::Index,
2051 ) -> Self::SimdMemMask<S> {
2052 ctx.mask_between_m32s(start as _, end as _)
2053 }
2054
2055 #[inline(always)]
2056 fn simd_mask_between<S: Simd>(
2057 ctx: &Self::SimdCtx<S>,
2058 start: Self::Index,
2059 end: Self::Index,
2060 ) -> Self::SimdMask<S> {
2061 ctx.mask_between_m32s(start as _, end as _).mask()
2062 }
2063
2064 #[inline(always)]
2065 unsafe fn simd_mask_load_raw<S: Simd>(
2066 ctx: &Self::SimdCtx<S>,
2067 mask: Self::SimdMemMask<S>,
2068 ptr: *const Self::SimdVec<S>,
2069 ) -> Self::SimdVec<S> {
2070 ctx.mask_load_ptr_f32s(mask, ptr as _)
2071 }
2072
2073 #[inline(always)]
2074 unsafe fn simd_mask_store_raw<S: Simd>(
2075 ctx: &Self::SimdCtx<S>,
2076 mask: Self::SimdMemMask<S>,
2077 ptr: *mut Self::SimdVec<S>,
2078 values: Self::SimdVec<S>,
2079 ) {
2080 ctx.mask_store_ptr_f32s(mask, ptr as _, values);
2081 }
2082}
2083impl RealField for f32 {
2084 #[inline(always)]
2085 fn epsilon_impl() -> Self {
2086 Self::EPSILON
2087 }
2088
2089 #[inline(always)]
2090 fn min_positive_impl() -> Self {
2091 Self::MIN_POSITIVE
2092 }
2093
2094 #[inline(always)]
2095 fn max_positive_impl() -> Self {
2096 Self::MIN_POSITIVE.recip()
2097 }
2098
2099 #[inline(always)]
2100 fn sqrt_min_positive_impl() -> Self {
2101 Self::sqrt_impl(&Self::MIN_POSITIVE)
2102 }
2103
2104 #[inline(always)]
2105 fn sqrt_max_positive_impl() -> Self {
2106 Self::sqrt_impl(&Self::MIN_POSITIVE.recip())
2107 }
2108
2109 #[inline(always)]
2110 fn nbits_impl() -> usize {
2111 Self::MANTISSA_DIGITS as usize
2112 }
2113}
2114impl ComplexField for f64 {
2115 type Arch = pulp::Arch;
2116 type Index = u64;
2117 type Real = Self;
2118 type SimdCtx<S: Simd> = S;
2119 type SimdIndex<S: Simd> = S::u64s;
2120 type SimdMask<S: Simd> = S::m64s;
2121 type SimdMemMask<S: Simd> = pulp::MemMask<S::m64s>;
2122 type SimdVec<S: Simd> = S::f64s;
2123 type Unit = Self;
2124
2125 const IS_NATIVE_F64: bool = true;
2126 const IS_REAL: bool = true;
2127 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Simd;
2128
2129 #[inline(always)]
2130 fn zero_impl() -> Self {
2131 0.0
2132 }
2133
2134 #[inline(always)]
2135 fn one_impl() -> Self {
2136 1.0
2137 }
2138
2139 #[inline(always)]
2140 fn nan_impl() -> Self {
2141 Self::NAN
2142 }
2143
2144 #[inline(always)]
2145 fn infinity_impl() -> Self {
2146 Self::INFINITY
2147 }
2148
2149 #[inline(always)]
2150 fn from_real_impl(value: &Self) -> Self {
2151 *value
2152 }
2153
2154 #[inline(always)]
2155 fn from_f64_impl(value: f64) -> Self {
2156 value as _
2157 }
2158
2159 #[inline(always)]
2160 fn real_part_impl(value: &Self) -> Self {
2161 *value
2162 }
2163
2164 #[inline(always)]
2165 fn imag_part_impl(_: &Self) -> Self {
2166 0.0
2167 }
2168
2169 #[inline(always)]
2170 fn copy_impl(value: &Self) -> Self {
2171 *value
2172 }
2173
2174 #[inline(always)]
2175 fn conj_impl(value: &Self) -> Self {
2176 *value
2177 }
2178
2179 #[inline(always)]
2180 fn recip_impl(value: &Self) -> Self {
2181 1.0 / *value
2182 }
2183
2184 #[inline(always)]
2185 fn sqrt_impl(value: &Self) -> Self {
2186 #[cfg(feature = "std")]
2187 {
2188 Self::sqrt(*value)
2189 }
2190 #[cfg(not(feature = "std"))]
2191 {
2192 libm::sqrt(*value)
2193 }
2194 }
2195
2196 #[inline(always)]
2197 fn abs_impl(value: &Self) -> Self {
2198 (*value).abs()
2199 }
2200
2201 #[inline(always)]
2202 fn abs1_impl(value: &Self) -> Self {
2203 (*value).abs()
2204 }
2205
2206 #[inline(always)]
2207 fn abs2_impl(value: &Self) -> Self {
2208 (*value) * (*value)
2209 }
2210
2211 #[inline(always)]
2212 fn mul_real_impl(lhs: &Self, rhs: &Self) -> Self {
2213 (*lhs) * (*rhs)
2214 }
2215
2216 #[inline(always)]
2217 fn mul_pow2_impl(lhs: &Self, rhs: &Self) -> Self {
2218 (*lhs) * (*rhs)
2219 }
2220
2221 #[inline(always)]
2222 fn is_nan_impl(value: &Self) -> bool {
2223 (*value).is_nan()
2224 }
2225
2226 #[inline(always)]
2227 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
2228 simd
2229 }
2230
2231 #[inline(always)]
2232 fn simd_splat<S: Simd>(
2233 ctx: &Self::SimdCtx<S>,
2234 value: &Self,
2235 ) -> Self::SimdVec<S> {
2236 ctx.splat_f64s(*value)
2237 }
2238
2239 #[inline(always)]
2240 fn simd_splat_real<S: Simd>(
2241 ctx: &Self::SimdCtx<S>,
2242 value: &Self::Real,
2243 ) -> Self::SimdVec<S> {
2244 ctx.splat_f64s(*value)
2245 }
2246
2247 #[inline(always)]
2248 fn simd_add<S: Simd>(
2249 ctx: &Self::SimdCtx<S>,
2250 lhs: Self::SimdVec<S>,
2251 rhs: Self::SimdVec<S>,
2252 ) -> Self::SimdVec<S> {
2253 ctx.add_f64s(lhs, rhs)
2254 }
2255
2256 #[inline(always)]
2257 fn simd_sub<S: Simd>(
2258 ctx: &Self::SimdCtx<S>,
2259 lhs: Self::SimdVec<S>,
2260 rhs: Self::SimdVec<S>,
2261 ) -> Self::SimdVec<S> {
2262 ctx.sub_f64s(lhs, rhs)
2263 }
2264
2265 #[inline(always)]
2266 fn simd_neg<S: Simd>(
2267 ctx: &Self::SimdCtx<S>,
2268 value: Self::SimdVec<S>,
2269 ) -> Self::SimdVec<S> {
2270 ctx.neg_f64s(value)
2271 }
2272
2273 #[inline(always)]
2274 fn simd_conj<S: Simd>(
2275 _: &Self::SimdCtx<S>,
2276 value: Self::SimdVec<S>,
2277 ) -> Self::SimdVec<S> {
2278 value
2279 }
2280
2281 #[inline(always)]
2282 fn simd_abs1<S: Simd>(
2283 ctx: &Self::SimdCtx<S>,
2284 value: Self::SimdVec<S>,
2285 ) -> Self::SimdVec<S> {
2286 ctx.abs_f64s(value)
2287 }
2288
2289 #[inline(always)]
2290 fn simd_mul<S: Simd>(
2291 ctx: &Self::SimdCtx<S>,
2292 lhs: Self::SimdVec<S>,
2293 rhs: Self::SimdVec<S>,
2294 ) -> Self::SimdVec<S> {
2295 ctx.mul_f64s(lhs, rhs)
2296 }
2297
2298 #[inline(always)]
2299 fn simd_mul_real<S: Simd>(
2300 ctx: &Self::SimdCtx<S>,
2301 lhs: Self::SimdVec<S>,
2302 real_rhs: Self::SimdVec<S>,
2303 ) -> Self::SimdVec<S> {
2304 ctx.mul_f64s(lhs, real_rhs)
2305 }
2306
2307 #[inline(always)]
2308 fn simd_mul_pow2<S: Simd>(
2309 ctx: &Self::SimdCtx<S>,
2310 lhs: Self::SimdVec<S>,
2311 real_rhs: Self::SimdVec<S>,
2312 ) -> Self::SimdVec<S> {
2313 ctx.mul_f64s(lhs, real_rhs)
2314 }
2315
2316 #[inline(always)]
2317 fn simd_conj_mul<S: Simd>(
2318 ctx: &Self::SimdCtx<S>,
2319 lhs: Self::SimdVec<S>,
2320 rhs: Self::SimdVec<S>,
2321 ) -> Self::SimdVec<S> {
2322 ctx.mul_f64s(lhs, rhs)
2323 }
2324
2325 #[inline(always)]
2326 fn simd_mul_add<S: Simd>(
2327 ctx: &Self::SimdCtx<S>,
2328 lhs: Self::SimdVec<S>,
2329 rhs: Self::SimdVec<S>,
2330 acc: Self::SimdVec<S>,
2331 ) -> Self::SimdVec<S> {
2332 ctx.mul_add_e_f64s(lhs, rhs, acc)
2333 }
2334
2335 #[inline(always)]
2336 fn simd_conj_mul_add<S: Simd>(
2337 ctx: &Self::SimdCtx<S>,
2338 lhs: Self::SimdVec<S>,
2339 rhs: Self::SimdVec<S>,
2340 acc: Self::SimdVec<S>,
2341 ) -> Self::SimdVec<S> {
2342 ctx.mul_add_e_f64s(lhs, rhs, acc)
2343 }
2344
2345 #[inline(always)]
2346 fn simd_abs2<S: Simd>(
2347 ctx: &Self::SimdCtx<S>,
2348 value: Self::SimdVec<S>,
2349 ) -> Self::SimdVec<S> {
2350 ctx.mul_f64s(value, value)
2351 }
2352
2353 #[inline(always)]
2354 fn simd_abs2_add<S: Simd>(
2355 ctx: &Self::SimdCtx<S>,
2356 value: Self::SimdVec<S>,
2357 acc: Self::SimdVec<S>,
2358 ) -> Self::SimdVec<S> {
2359 ctx.mul_add_e_f64s(value, value, acc)
2360 }
2361
2362 #[inline(always)]
2363 fn simd_reduce_sum<S: Simd>(
2364 ctx: &Self::SimdCtx<S>,
2365 value: Self::SimdVec<S>,
2366 ) -> Self {
2367 ctx.reduce_sum_f64s(value)
2368 }
2369
2370 #[inline(always)]
2371 fn simd_reduce_max<S: Simd>(
2372 ctx: &Self::SimdCtx<S>,
2373 value: Self::SimdVec<S>,
2374 ) -> Self {
2375 ctx.reduce_max_f64s(value)
2376 }
2377
2378 #[inline(always)]
2379 fn simd_equal<S: Simd>(
2380 ctx: &Self::SimdCtx<S>,
2381 real_lhs: Self::SimdVec<S>,
2382 real_rhs: Self::SimdVec<S>,
2383 ) -> Self::SimdMask<S> {
2384 ctx.equal_f64s(real_lhs, real_rhs)
2385 }
2386
2387 #[inline(always)]
2388 fn simd_less_than<S: Simd>(
2389 ctx: &Self::SimdCtx<S>,
2390 real_lhs: Self::SimdVec<S>,
2391 real_rhs: Self::SimdVec<S>,
2392 ) -> Self::SimdMask<S> {
2393 ctx.less_than_f64s(real_lhs, real_rhs)
2394 }
2395
2396 #[inline(always)]
2397 fn simd_greater_than<S: Simd>(
2398 ctx: &Self::SimdCtx<S>,
2399 real_lhs: Self::SimdVec<S>,
2400 real_rhs: Self::SimdVec<S>,
2401 ) -> Self::SimdMask<S> {
2402 ctx.greater_than_f64s(real_lhs, real_rhs)
2403 }
2404
2405 #[inline(always)]
2406 fn simd_less_than_or_equal<S: Simd>(
2407 ctx: &Self::SimdCtx<S>,
2408 real_lhs: Self::SimdVec<S>,
2409 real_rhs: Self::SimdVec<S>,
2410 ) -> Self::SimdMask<S> {
2411 ctx.less_than_or_equal_f64s(real_lhs, real_rhs)
2412 }
2413
2414 #[inline(always)]
2415 fn simd_greater_than_or_equal<S: Simd>(
2416 ctx: &Self::SimdCtx<S>,
2417 real_lhs: Self::SimdVec<S>,
2418 real_rhs: Self::SimdVec<S>,
2419 ) -> Self::SimdMask<S> {
2420 ctx.greater_than_or_equal_f64s(real_lhs, real_rhs)
2421 }
2422
2423 #[inline(always)]
2424 fn simd_select<S: Simd>(
2425 ctx: &Self::SimdCtx<S>,
2426 mask: Self::SimdMask<S>,
2427 lhs: Self::SimdVec<S>,
2428 rhs: Self::SimdVec<S>,
2429 ) -> Self::SimdVec<S> {
2430 ctx.select_f64s(mask, lhs, rhs)
2431 }
2432
2433 #[inline(always)]
2434 fn simd_index_select<S: Simd>(
2435 ctx: &Self::SimdCtx<S>,
2436 mask: Self::SimdMask<S>,
2437 lhs: Self::SimdIndex<S>,
2438 rhs: Self::SimdIndex<S>,
2439 ) -> Self::SimdIndex<S> {
2440 ctx.select_u64s(mask, lhs, rhs)
2441 }
2442
2443 #[inline(always)]
2444 fn simd_index_splat<S: Simd>(
2445 ctx: &Self::SimdCtx<S>,
2446 value: Self::Index,
2447 ) -> Self::SimdIndex<S> {
2448 ctx.splat_u64s(value as _)
2449 }
2450
2451 #[inline(always)]
2452 fn simd_index_add<S: Simd>(
2453 ctx: &Self::SimdCtx<S>,
2454 lhs: Self::SimdIndex<S>,
2455 rhs: Self::SimdIndex<S>,
2456 ) -> Self::SimdIndex<S> {
2457 ctx.add_u64s(lhs, rhs)
2458 }
2459
2460 #[inline(always)]
2461 fn simd_index_less_than<S: Simd>(
2462 ctx: &Self::SimdCtx<S>,
2463 lhs: Self::SimdIndex<S>,
2464 rhs: Self::SimdIndex<S>,
2465 ) -> Self::SimdMask<S> {
2466 ctx.less_than_u64s(lhs, rhs)
2467 }
2468
2469 #[inline(always)]
2470 fn simd_abs_max<S: Simd>(
2471 ctx: &Self::SimdCtx<S>,
2472 value: Self::SimdVec<S>,
2473 ) -> Self::SimdVec<S> {
2474 ctx.abs_f64s(value)
2475 }
2476
2477 #[inline(always)]
2478 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
2479 *ctx
2480 }
2481
2482 #[inline(always)]
2483 fn simd_and_mask<S: Simd>(
2484 simd: &Self::SimdCtx<S>,
2485 lhs: Self::SimdMask<S>,
2486 rhs: Self::SimdMask<S>,
2487 ) -> Self::SimdMask<S> {
2488 simd.and_m64s(lhs, rhs)
2489 }
2490
2491 #[inline(always)]
2492 fn simd_or_mask<S: Simd>(
2493 simd: &Self::SimdCtx<S>,
2494 lhs: Self::SimdMask<S>,
2495 rhs: Self::SimdMask<S>,
2496 ) -> Self::SimdMask<S> {
2497 simd.or_m64s(lhs, rhs)
2498 }
2499
2500 #[inline(always)]
2501 fn simd_not_mask<S: Simd>(
2502 ctx: &Self::SimdCtx<S>,
2503 mask: Self::SimdMask<S>,
2504 ) -> Self::SimdMask<S> {
2505 ctx.not_m64s(mask)
2506 }
2507
2508 #[inline(always)]
2509 fn simd_first_true_mask<S: Simd>(
2510 ctx: &Self::SimdCtx<S>,
2511 value: Self::SimdMask<S>,
2512 ) -> usize {
2513 ctx.first_true_m64s(value)
2514 }
2515
2516 #[inline(always)]
2517 fn is_finite_impl(value: &Self) -> bool {
2518 (*value).is_finite()
2519 }
2520
2521 #[inline(always)]
2522 fn simd_mem_mask_between<S: Simd>(
2523 ctx: &Self::SimdCtx<S>,
2524 start: Self::Index,
2525 end: Self::Index,
2526 ) -> Self::SimdMemMask<S> {
2527 ctx.mask_between_m64s(start as _, end as _)
2528 }
2529
2530 #[inline(always)]
2531 fn simd_mask_between<S: Simd>(
2532 ctx: &Self::SimdCtx<S>,
2533 start: Self::Index,
2534 end: Self::Index,
2535 ) -> Self::SimdMask<S> {
2536 ctx.mask_between_m64s(start as _, end as _).mask()
2537 }
2538
2539 #[inline(always)]
2540 unsafe fn simd_mask_load_raw<S: Simd>(
2541 ctx: &Self::SimdCtx<S>,
2542 mask: Self::SimdMemMask<S>,
2543 ptr: *const Self::SimdVec<S>,
2544 ) -> Self::SimdVec<S> {
2545 ctx.mask_load_ptr_f64s(mask, ptr as _)
2546 }
2547
2548 #[inline(always)]
2549 unsafe fn simd_mask_store_raw<S: Simd>(
2550 ctx: &Self::SimdCtx<S>,
2551 mask: Self::SimdMemMask<S>,
2552 ptr: *mut Self::SimdVec<S>,
2553 values: Self::SimdVec<S>,
2554 ) {
2555 ctx.mask_store_ptr_f64s(mask, ptr as _, values);
2556 }
2557}
2558impl RealField for f64 {
2559 #[inline(always)]
2560 fn epsilon_impl() -> Self {
2561 Self::EPSILON
2562 }
2563
2564 #[inline(always)]
2565 fn min_positive_impl() -> Self {
2566 Self::MIN_POSITIVE
2567 }
2568
2569 #[inline(always)]
2570 fn max_positive_impl() -> Self {
2571 Self::MIN_POSITIVE.recip()
2572 }
2573
2574 #[inline(always)]
2575 fn sqrt_min_positive_impl() -> Self {
2576 Self::sqrt_impl(&Self::MIN_POSITIVE)
2577 }
2578
2579 #[inline(always)]
2580 fn sqrt_max_positive_impl() -> Self {
2581 Self::sqrt_impl(&Self::MIN_POSITIVE.recip())
2582 }
2583
2584 #[inline(always)]
2585 fn nbits_impl() -> usize {
2586 Self::MANTISSA_DIGITS as usize
2587 }
2588}
2589impl<T: RealField<Unit: ComplexField>> ComplexField for Complex<T> {
2590 type Arch = T::Arch;
2591 type Index = T::Index;
2592 type Real = T;
2593 type SimdCtx<S: Simd> = T::SimdCtx<S>;
2594 type SimdIndex<S: Simd> = T::SimdIndex<S>;
2595 type SimdMask<S: Simd> = T::SimdMask<S>;
2596 type SimdMemMask<S: Simd> = Complex<T::SimdMemMask<S>>;
2597 type SimdVec<S: Simd> = Complex<T::SimdVec<S>>;
2598 type Unit = T::Unit;
2599
2600 const IS_NATIVE_C32: bool = T::IS_NATIVE_F32;
2601 const IS_NATIVE_C64: bool = T::IS_NATIVE_F64;
2602 const IS_REAL: bool = false;
2603 const SIMD_CAPABILITIES: SimdCapabilities = T::SIMD_CAPABILITIES;
2604
2605 #[inline]
2606 fn zero_impl() -> Self {
2607 Complex {
2608 re: T::zero_impl(),
2609 im: T::zero_impl(),
2610 }
2611 }
2612
2613 #[inline]
2614 fn one_impl() -> Self {
2615 Complex {
2616 re: T::one_impl(),
2617 im: T::zero_impl(),
2618 }
2619 }
2620
2621 #[inline]
2622 fn nan_impl() -> Self {
2623 Complex {
2624 re: T::nan_impl(),
2625 im: T::nan_impl(),
2626 }
2627 }
2628
2629 #[inline]
2630 fn infinity_impl() -> Self {
2631 Complex {
2632 re: T::infinity_impl(),
2633 im: T::infinity_impl(),
2634 }
2635 }
2636
2637 #[inline]
2638 fn from_real_impl(real: &Self::Real) -> Self {
2639 Complex {
2640 re: real.clone(),
2641 im: T::zero_impl(),
2642 }
2643 }
2644
2645 #[inline]
2646 fn from_f64_impl(real: f64) -> Self {
2647 Complex {
2648 re: T::from_f64_impl(real),
2649 im: T::zero_impl(),
2650 }
2651 }
2652
2653 #[inline]
2654 fn real_part_impl(value: &Self) -> Self::Real {
2655 value.re.clone()
2656 }
2657
2658 #[inline]
2659 fn imag_part_impl(value: &Self) -> Self::Real {
2660 value.im.clone()
2661 }
2662
2663 #[inline]
2664 fn copy_impl(value: &Self) -> Self {
2665 value.clone()
2666 }
2667
2668 #[inline]
2669 fn conj_impl(value: &Self) -> Self {
2670 Self {
2671 re: value.re.clone(),
2672 im: value.im.neg_by_ref(),
2673 }
2674 }
2675
2676 #[inline]
2677 fn recip_impl(value: &Self) -> Self {
2678 let (re, im) = recip_impl(value.re.clone(), value.im.clone());
2679 Complex { re, im }
2680 }
2681
2682 #[inline]
2683 fn sqrt_impl(value: &Self) -> Self {
2684 let (re, im) = sqrt_impl(value.re.clone(), value.im.clone());
2685 Complex { re, im }
2686 }
2687
2688 #[inline]
2689 fn abs_impl(value: &Self) -> Self::Real {
2690 abs_impl(value.re.clone(), value.im.clone())
2691 }
2692
2693 #[inline]
2694 fn abs1_impl(value: &Self) -> Self::Real {
2695 use crate::ext::*;
2696 value.re.abs1() + value.im.abs1()
2697 }
2698
2699 #[inline]
2700 fn abs2_impl(value: &Self) -> Self::Real {
2701 use crate::ext::*;
2702 value.re.abs2() + value.im.abs2()
2703 }
2704
2705 #[inline]
2706 fn mul_real_impl(lhs: &Self, rhs: &Self::Real) -> Self {
2707 Complex {
2708 re: &lhs.re * rhs,
2709 im: &lhs.im * rhs,
2710 }
2711 }
2712
2713 #[inline]
2714 fn mul_pow2_impl(lhs: &Self, rhs: &Self::Real) -> Self {
2715 use crate::ext::*;
2716 Complex {
2717 re: lhs.re.mul_pow2(rhs),
2718 im: lhs.im.mul_pow2(rhs),
2719 }
2720 }
2721
2722 #[inline]
2723 fn is_finite_impl(value: &Self) -> bool {
2724 use crate::ext::*;
2725 value.re.is_finite() && value.im.is_finite()
2726 }
2727
2728 #[inline(always)]
2729 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
2730 T::simd_ctx(simd)
2731 }
2732
2733 #[inline(always)]
2734 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
2735 T::ctx_from_simd(ctx)
2736 }
2737
2738 #[inline(always)]
2739 fn simd_splat<S: Simd>(
2740 ctx: &Self::SimdCtx<S>,
2741 value: &Self,
2742 ) -> Self::SimdVec<S> {
2743 Complex {
2744 re: T::simd_splat(ctx, &value.re),
2745 im: T::simd_splat(ctx, &value.im),
2746 }
2747 }
2748
2749 #[inline(always)]
2750 fn simd_splat_real<S: Simd>(
2751 ctx: &Self::SimdCtx<S>,
2752 value: &Self::Real,
2753 ) -> Self::SimdVec<S> {
2754 Complex {
2755 re: T::simd_splat_real(ctx, value),
2756 im: T::simd_splat_real(ctx, value),
2757 }
2758 }
2759
2760 #[inline(always)]
2761 fn simd_add<S: Simd>(
2762 ctx: &Self::SimdCtx<S>,
2763 lhs: Self::SimdVec<S>,
2764 rhs: Self::SimdVec<S>,
2765 ) -> Self::SimdVec<S> {
2766 Complex {
2767 re: T::simd_add(ctx, lhs.re, rhs.re),
2768 im: T::simd_add(ctx, lhs.im, rhs.im),
2769 }
2770 }
2771
2772 #[inline(always)]
2773 fn simd_sub<S: Simd>(
2774 ctx: &Self::SimdCtx<S>,
2775 lhs: Self::SimdVec<S>,
2776 rhs: Self::SimdVec<S>,
2777 ) -> Self::SimdVec<S> {
2778 Complex {
2779 re: T::simd_sub(ctx, lhs.re, rhs.re),
2780 im: T::simd_sub(ctx, lhs.im, rhs.im),
2781 }
2782 }
2783
2784 #[inline(always)]
2785 fn simd_neg<S: Simd>(
2786 ctx: &Self::SimdCtx<S>,
2787 value: Self::SimdVec<S>,
2788 ) -> Self::SimdVec<S> {
2789 Complex {
2790 re: T::simd_neg(ctx, value.re),
2791 im: T::simd_neg(ctx, value.im),
2792 }
2793 }
2794
2795 #[inline(always)]
2796 fn simd_conj<S: Simd>(
2797 ctx: &Self::SimdCtx<S>,
2798 value: Self::SimdVec<S>,
2799 ) -> Self::SimdVec<S> {
2800 Complex {
2801 re: value.re,
2802 im: T::simd_neg(ctx, value.im),
2803 }
2804 }
2805
2806 #[inline(always)]
2807 fn simd_abs1<S: Simd>(
2808 ctx: &Self::SimdCtx<S>,
2809 value: Self::SimdVec<S>,
2810 ) -> Self::SimdVec<S> {
2811 let v = T::simd_add(
2812 ctx,
2813 T::simd_abs1(ctx, value.re),
2814 T::simd_abs1(ctx, value.im),
2815 );
2816 Complex { re: v, im: v }
2817 }
2818
2819 #[inline(always)]
2820 fn simd_abs_max<S: Simd>(
2821 ctx: &Self::SimdCtx<S>,
2822 value: Self::SimdVec<S>,
2823 ) -> Self::SimdVec<S> {
2824 let re = T::simd_abs_max(ctx, value.re);
2825 let im = T::simd_abs_max(ctx, value.im);
2826 let v = T::simd_select(ctx, T::simd_greater_than(ctx, re, im), re, im);
2827 let re_nan = T::simd_not_mask(ctx, T::simd_equal(ctx, re, re));
2832 let v = T::simd_select(ctx, re_nan, re, v);
2833 Complex { re: v, im: v }
2834 }
2835
2836 #[inline(always)]
2837 fn simd_mul_real<S: Simd>(
2838 ctx: &Self::SimdCtx<S>,
2839 lhs: Self::SimdVec<S>,
2840 real_rhs: Self::SimdVec<S>,
2841 ) -> Self::SimdVec<S> {
2842 Complex {
2843 re: T::simd_mul_real(ctx, lhs.re, real_rhs.re),
2844 im: T::simd_mul_real(ctx, lhs.im, real_rhs.re),
2845 }
2846 }
2847
2848 #[inline(always)]
2849 fn simd_mul_pow2<S: Simd>(
2850 ctx: &Self::SimdCtx<S>,
2851 lhs: Self::SimdVec<S>,
2852 real_rhs: Self::SimdVec<S>,
2853 ) -> Self::SimdVec<S> {
2854 Complex {
2855 re: T::simd_mul_pow2(ctx, lhs.re, real_rhs.re),
2856 im: T::simd_mul_pow2(ctx, lhs.im, real_rhs.re),
2857 }
2858 }
2859
2860 #[inline(always)]
2861 fn simd_mul<S: Simd>(
2862 ctx: &Self::SimdCtx<S>,
2863 lhs: Self::SimdVec<S>,
2864 rhs: Self::SimdVec<S>,
2865 ) -> Self::SimdVec<S> {
2866 Complex {
2867 re: T::simd_mul_add(
2868 ctx,
2869 lhs.re,
2870 rhs.re,
2871 T::simd_neg(ctx, T::simd_mul(ctx, lhs.im, rhs.im)),
2872 ),
2873 im: T::simd_mul_add(
2874 ctx,
2875 lhs.re,
2876 rhs.im,
2877 T::simd_mul(ctx, lhs.im, rhs.re),
2878 ),
2879 }
2880 }
2881
2882 #[inline(always)]
2883 fn simd_conj_mul<S: Simd>(
2884 ctx: &Self::SimdCtx<S>,
2885 lhs: Self::SimdVec<S>,
2886 rhs: Self::SimdVec<S>,
2887 ) -> Self::SimdVec<S> {
2888 Complex {
2889 re: T::simd_mul_add(
2890 ctx,
2891 lhs.re,
2892 rhs.re,
2893 T::simd_mul(ctx, lhs.im, rhs.im),
2894 ),
2895 im: T::simd_mul_add(
2896 ctx,
2897 lhs.re,
2898 rhs.im,
2899 T::simd_neg(ctx, T::simd_mul(ctx, lhs.im, rhs.re)),
2900 ),
2901 }
2902 }
2903
2904 #[inline(always)]
2905 fn simd_mul_add<S: Simd>(
2906 ctx: &Self::SimdCtx<S>,
2907 lhs: Self::SimdVec<S>,
2908 rhs: Self::SimdVec<S>,
2909 acc: Self::SimdVec<S>,
2910 ) -> Self::SimdVec<S> {
2911 Complex {
2912 re: T::simd_mul_add(
2913 ctx,
2914 T::simd_neg(ctx, lhs.im),
2915 rhs.im,
2916 T::simd_mul_add(ctx, lhs.re, rhs.re, acc.re),
2917 ),
2918 im: T::simd_mul_add(
2919 ctx,
2920 lhs.re,
2921 rhs.im,
2922 T::simd_mul_add(ctx, lhs.im, rhs.re, acc.im),
2923 ),
2924 }
2925 }
2926
2927 #[inline(always)]
2928 fn simd_conj_mul_add<S: Simd>(
2929 ctx: &Self::SimdCtx<S>,
2930 lhs: Self::SimdVec<S>,
2931 rhs: Self::SimdVec<S>,
2932 acc: Self::SimdVec<S>,
2933 ) -> Self::SimdVec<S> {
2934 Complex {
2935 re: T::simd_mul_add(
2936 ctx,
2937 lhs.im,
2938 rhs.im,
2939 T::simd_mul_add(ctx, lhs.re, rhs.re, acc.re),
2940 ),
2941 im: T::simd_mul_add(
2942 ctx,
2943 lhs.re,
2944 rhs.im,
2945 T::simd_mul_add(ctx, T::simd_neg(ctx, lhs.im), rhs.re, acc.im),
2946 ),
2947 }
2948 }
2949
2950 #[inline(always)]
2951 fn simd_abs2<S: Simd>(
2952 ctx: &Self::SimdCtx<S>,
2953 value: Self::SimdVec<S>,
2954 ) -> Self::SimdVec<S> {
2955 let v = T::simd_abs2_add(ctx, value.re, T::simd_abs2(ctx, value.im));
2956 Complex { re: v, im: v }
2957 }
2958
2959 #[inline(always)]
2960 fn simd_abs2_add<S: Simd>(
2961 ctx: &Self::SimdCtx<S>,
2962 value: Self::SimdVec<S>,
2963 acc: Self::SimdVec<S>,
2964 ) -> Self::SimdVec<S> {
2965 let v = T::simd_abs2_add(
2966 ctx,
2967 value.re,
2968 T::simd_abs2_add(ctx, value.im, acc.re),
2969 );
2970 Complex { re: v, im: v }
2971 }
2972
2973 #[inline(always)]
2974 fn simd_reduce_sum<S: Simd>(
2975 ctx: &Self::SimdCtx<S>,
2976 value: Self::SimdVec<S>,
2977 ) -> Self {
2978 Complex {
2979 re: T::simd_reduce_sum(ctx, value.re),
2980 im: T::simd_reduce_sum(ctx, value.im),
2981 }
2982 }
2983
2984 #[inline(always)]
2985 fn simd_reduce_max<S: Simd>(
2986 ctx: &Self::SimdCtx<S>,
2987 value: Self::SimdVec<S>,
2988 ) -> Self {
2989 Complex {
2990 re: T::simd_reduce_max(ctx, value.re),
2991 im: T::simd_reduce_max(ctx, value.im),
2992 }
2993 }
2994
2995 #[inline(always)]
2996 fn simd_equal<S: Simd>(
2997 ctx: &Self::SimdCtx<S>,
2998 real_lhs: Self::SimdVec<S>,
2999 real_rhs: Self::SimdVec<S>,
3000 ) -> Self::SimdMask<S> {
3001 T::simd_and_mask(
3002 ctx,
3003 T::simd_equal(ctx, real_lhs.re, real_rhs.re),
3004 T::simd_equal(ctx, real_lhs.im, real_rhs.im),
3005 )
3006 }
3007
3008 #[inline(always)]
3009 fn simd_less_than<S: Simd>(
3010 ctx: &Self::SimdCtx<S>,
3011 real_lhs: Self::SimdVec<S>,
3012 real_rhs: Self::SimdVec<S>,
3013 ) -> Self::SimdMask<S> {
3014 T::simd_less_than(ctx, real_lhs.re, real_rhs.re)
3015 }
3016
3017 #[inline(always)]
3018 fn simd_less_than_or_equal<S: Simd>(
3019 ctx: &Self::SimdCtx<S>,
3020 real_lhs: Self::SimdVec<S>,
3021 real_rhs: Self::SimdVec<S>,
3022 ) -> Self::SimdMask<S> {
3023 T::simd_less_than_or_equal(ctx, real_lhs.re, real_rhs.re)
3024 }
3025
3026 #[inline(always)]
3027 fn simd_greater_than<S: Simd>(
3028 ctx: &Self::SimdCtx<S>,
3029 real_lhs: Self::SimdVec<S>,
3030 real_rhs: Self::SimdVec<S>,
3031 ) -> Self::SimdMask<S> {
3032 T::simd_greater_than(ctx, real_lhs.re, real_rhs.re)
3033 }
3034
3035 #[inline(always)]
3036 fn simd_greater_than_or_equal<S: Simd>(
3037 ctx: &Self::SimdCtx<S>,
3038 real_lhs: Self::SimdVec<S>,
3039 real_rhs: Self::SimdVec<S>,
3040 ) -> Self::SimdMask<S> {
3041 T::simd_greater_than_or_equal(ctx, real_lhs.re, real_rhs.re)
3042 }
3043
3044 #[inline(always)]
3045 fn simd_select<S: Simd>(
3046 ctx: &Self::SimdCtx<S>,
3047 mask: Self::SimdMask<S>,
3048 lhs: Self::SimdVec<S>,
3049 rhs: Self::SimdVec<S>,
3050 ) -> Self::SimdVec<S> {
3051 Complex {
3052 re: T::simd_select(ctx, mask, lhs.re, rhs.re),
3053 im: T::simd_select(ctx, mask, lhs.im, rhs.im),
3054 }
3055 }
3056
3057 #[inline(always)]
3058 fn simd_index_select<S: Simd>(
3059 ctx: &Self::SimdCtx<S>,
3060 mask: Self::SimdMask<S>,
3061 lhs: Self::SimdIndex<S>,
3062 rhs: Self::SimdIndex<S>,
3063 ) -> Self::SimdIndex<S> {
3064 T::simd_index_select(ctx, mask, lhs, rhs)
3065 }
3066
3067 #[inline(always)]
3068 fn simd_index_splat<S: Simd>(
3069 ctx: &Self::SimdCtx<S>,
3070 value: Self::Index,
3071 ) -> Self::SimdIndex<S> {
3072 T::simd_index_splat(ctx, value)
3073 }
3074
3075 #[inline(always)]
3076 fn simd_index_add<S: Simd>(
3077 ctx: &Self::SimdCtx<S>,
3078 lhs: Self::SimdIndex<S>,
3079 rhs: Self::SimdIndex<S>,
3080 ) -> Self::SimdIndex<S> {
3081 T::simd_index_add(ctx, lhs, rhs)
3082 }
3083
3084 #[inline(always)]
3085 fn simd_index_less_than<S: Simd>(
3086 ctx: &Self::SimdCtx<S>,
3087 lhs: Self::SimdIndex<S>,
3088 rhs: Self::SimdIndex<S>,
3089 ) -> Self::SimdMask<S> {
3090 T::simd_index_less_than(ctx, lhs, rhs)
3091 }
3092
3093 #[inline(always)]
3094 fn simd_and_mask<S: Simd>(
3095 ctx: &Self::SimdCtx<S>,
3096 lhs: Self::SimdMask<S>,
3097 rhs: Self::SimdMask<S>,
3098 ) -> Self::SimdMask<S> {
3099 T::simd_and_mask(ctx, lhs, rhs)
3100 }
3101
3102 #[inline(always)]
3103 fn simd_or_mask<S: Simd>(
3104 ctx: &Self::SimdCtx<S>,
3105 lhs: Self::SimdMask<S>,
3106 rhs: Self::SimdMask<S>,
3107 ) -> Self::SimdMask<S> {
3108 T::simd_or_mask(ctx, lhs, rhs)
3109 }
3110
3111 #[inline(always)]
3112 fn simd_not_mask<S: Simd>(
3113 ctx: &Self::SimdCtx<S>,
3114 mask: Self::SimdMask<S>,
3115 ) -> Self::SimdMask<S> {
3116 T::simd_not_mask(ctx, mask)
3117 }
3118
3119 #[inline(always)]
3120 fn simd_first_true_mask<S: Simd>(
3121 ctx: &Self::SimdCtx<S>,
3122 value: Self::SimdMask<S>,
3123 ) -> usize {
3124 T::simd_first_true_mask(ctx, value)
3125 }
3126
3127 #[inline(always)]
3128 fn simd_mem_mask_between<S: Simd>(
3129 ctx: &Self::SimdCtx<S>,
3130 start: Self::Index,
3131 end: Self::Index,
3132 ) -> Self::SimdMemMask<S> {
3133 let n = core::mem::size_of::<Self::SimdVec<S>>()
3134 / core::mem::size_of::<Self>();
3135 let start = start.zx() * 2;
3136 let end = end.zx() * 2;
3137 let mut sa = start.min(n);
3138 let mut ea = end.min(n);
3139 let mut sb = start.max(n) - n;
3140 let mut eb = end.max(n) - n;
3141 if sa == ea {
3142 sa = 0;
3143 ea = 0;
3144 }
3145 if sb == eb {
3146 sb = 0;
3147 eb = 0;
3148 }
3149 let re = T::simd_mem_mask_between(
3150 ctx,
3151 T::Index::truncate(sa),
3152 T::Index::truncate(ea),
3153 );
3154 let im = T::simd_mem_mask_between(
3155 ctx,
3156 T::Index::truncate(sb),
3157 T::Index::truncate(eb),
3158 );
3159 Complex { re, im }
3160 }
3161
3162 #[inline(always)]
3163 fn simd_mask_between<S: Simd>(
3164 ctx: &Self::SimdCtx<S>,
3165 start: Self::Index,
3166 end: Self::Index,
3167 ) -> Self::SimdMask<S> {
3168 T::simd_mask_between(ctx, start, end)
3169 }
3170
3171 #[inline(always)]
3172 unsafe fn simd_mask_load_raw<S: Simd>(
3173 ctx: &Self::SimdCtx<S>,
3174 mask: Self::SimdMemMask<S>,
3175 ptr: *const Self::SimdVec<S>,
3176 ) -> Self::SimdVec<S> {
3177 Complex {
3178 re: T::simd_mask_load_raw(
3179 ctx,
3180 mask.re,
3181 core::ptr::addr_of!((*ptr).re),
3182 ),
3183 im: T::simd_mask_load_raw(
3184 ctx,
3185 mask.im,
3186 core::ptr::addr_of!((*ptr).im),
3187 ),
3188 }
3189 }
3190
3191 #[inline(always)]
3192 unsafe fn simd_mask_store_raw<S: Simd>(
3193 ctx: &Self::SimdCtx<S>,
3194 mask: Self::SimdMemMask<S>,
3195 ptr: *mut Self::SimdVec<S>,
3196 values: Self::SimdVec<S>,
3197 ) {
3198 T::simd_mask_store_raw(
3199 ctx,
3200 mask.re,
3201 core::ptr::addr_of_mut!((*ptr).re),
3202 values.re,
3203 );
3204 T::simd_mask_store_raw(
3205 ctx,
3206 mask.im,
3207 core::ptr::addr_of_mut!((*ptr).im),
3208 values.im,
3209 );
3210 }
3211}
3212#[repr(transparent)]
3213#[doc(hidden)]
3214#[derive(Copy, Clone, Debug, PartialEq)]
3215pub struct ComplexImpl<T>(Complex<T>);
3216#[repr(transparent)]
3217#[doc(hidden)]
3218#[derive(Copy, Clone, Debug, PartialEq)]
3219pub struct ComplexImplConj<T>(Complex<T>);
3220unsafe impl Conjugate for ComplexImpl<f32> {
3221 type Canonical = ComplexImpl<f32>;
3222 type Conj = ComplexImplConj<f32>;
3223
3224 const IS_CANONICAL: bool = true;
3225}
3226unsafe impl Conjugate for ComplexImplConj<f32> {
3227 type Canonical = ComplexImpl<f32>;
3228 type Conj = ComplexImpl<f32>;
3229
3230 const IS_CANONICAL: bool = false;
3231}
3232unsafe impl Conjugate for ComplexImpl<f64> {
3233 type Canonical = ComplexImpl<f64>;
3234 type Conj = ComplexImplConj<f64>;
3235
3236 const IS_CANONICAL: bool = true;
3237}
3238unsafe impl Conjugate for ComplexImplConj<f64> {
3239 type Canonical = ComplexImpl<f64>;
3240 type Conj = ComplexImpl<f64>;
3241
3242 const IS_CANONICAL: bool = false;
3243}
3244impl<T: RealField> core::ops::Neg for ComplexImpl<T> {
3245 type Output = ComplexImpl<T>;
3246
3247 #[inline]
3248 fn neg(self) -> Self::Output {
3249 use math_utils::*;
3250 ComplexImpl(neg(&self.0))
3251 }
3252}
3253impl<T: RealField> core::ops::Neg for &ComplexImpl<T> {
3254 type Output = ComplexImpl<T>;
3255
3256 #[inline]
3257 fn neg(self) -> Self::Output {
3258 use math_utils::*;
3259 ComplexImpl(neg(&self.0))
3260 }
3261}
3262impl_op!({
3263 impl<T: RealField> Add for ComplexImpl<T> {
3264 fn add(self, rhs: Self) -> Self::Output {
3265 use math_utils::*;
3266 ComplexImpl(add(&self.0, &rhs.0))
3267 }
3268 }
3269 impl<T: RealField> Sub for ComplexImpl<T> {
3270 fn sub(self, rhs: Self) -> Self::Output {
3271 use math_utils::*;
3272 ComplexImpl(sub(&self.0, &rhs.0))
3273 }
3274 }
3275 impl<T: RealField> Mul for ComplexImpl<T> {
3276 fn mul(self, rhs: Self) -> Self::Output {
3277 use math_utils::*;
3278 ComplexImpl(mul(&self.0, &rhs.0))
3279 }
3280 }
3281 impl<T: RealField> Div for ComplexImpl<T> {
3282 fn div(self, rhs: Self) -> Self::Output {
3283 use math_utils::*;
3284 ComplexImpl(div(&self.0, &rhs.0))
3285 }
3286 }
3287});
3288impl_assign_op!({
3289 impl<T: RealField> AddAssign for ComplexImpl<T> {
3290 fn add_assign(&mut self, rhs: Self) {
3291 use math_utils::*;
3292 *self = ComplexImpl(add(&self.0, &rhs.0));
3293 }
3294 }
3295 impl<T: RealField> SubAssign for ComplexImpl<T> {
3296 fn sub_assign(&mut self, rhs: Self) {
3297 use math_utils::*;
3298 *self = ComplexImpl(sub(&self.0, &rhs.0));
3299 }
3300 }
3301 impl<T: RealField> MulAssign for ComplexImpl<T> {
3302 fn mul_assign(&mut self, rhs: Self) {
3303 use math_utils::*;
3304 *self = ComplexImpl(mul(&self.0, &rhs.0));
3305 }
3306 }
3307 impl<T: RealField> DivAssign for ComplexImpl<T> {
3308 fn div_assign(&mut self, rhs: Self) {
3309 use math_utils::*;
3310 *self = ComplexImpl(div(&self.0, &rhs.0));
3311 }
3312 }
3313});
3314impl<T> From<Complex<T>> for ComplexImpl<T> {
3315 #[inline]
3316 fn from(value: Complex<T>) -> Self {
3317 Self(value)
3318 }
3319}
3320impl ComplexField for ComplexImpl<f32> {
3321 type Arch = pulp::Arch;
3322 type Index = u32;
3323 type Real = f32;
3324 type SimdCtx<S: Simd> = S;
3325 type SimdIndex<S: Simd> = S::u32s;
3326 type SimdMask<S: Simd> = S::m32s;
3327 type SimdMemMask<S: Simd> = pulp::MemMask<S::m32s>;
3328 type SimdVec<S: Simd> = S::c32s;
3329 type Unit = f32;
3330
3331 const IS_NATIVE_C32: bool = true;
3332 const IS_REAL: bool = false;
3333 const SIMD_ABS_SPLIT_REAL_IMAG: bool = true;
3334 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Simd;
3335
3336 #[inline]
3337 fn zero_impl() -> Self {
3338 Complex {
3339 re: f32::zero_impl(),
3340 im: f32::zero_impl(),
3341 }
3342 .into()
3343 }
3344
3345 #[inline]
3346 fn one_impl() -> Self {
3347 Complex {
3348 re: f32::one_impl(),
3349 im: f32::zero_impl(),
3350 }
3351 .into()
3352 }
3353
3354 #[inline]
3355 fn nan_impl() -> Self {
3356 Complex {
3357 re: f32::nan_impl(),
3358 im: f32::nan_impl(),
3359 }
3360 .into()
3361 }
3362
3363 #[inline]
3364 fn infinity_impl() -> Self {
3365 Complex {
3366 re: f32::infinity_impl(),
3367 im: f32::infinity_impl(),
3368 }
3369 .into()
3370 }
3371
3372 #[inline]
3373 fn from_real_impl(real: &Self::Real) -> Self {
3374 Complex {
3375 re: *real,
3376 im: f32::zero_impl(),
3377 }
3378 .into()
3379 }
3380
3381 #[inline]
3382 fn from_f64_impl(real: f64) -> Self {
3383 Complex {
3384 re: f32::from_f64_impl(real),
3385 im: f32::zero_impl(),
3386 }
3387 .into()
3388 }
3389
3390 #[inline]
3391 fn real_part_impl(value: &Self) -> Self::Real {
3392 value.0.re
3393 }
3394
3395 #[inline]
3396 fn imag_part_impl(value: &Self) -> Self::Real {
3397 value.0.im
3398 }
3399
3400 #[inline]
3401 fn copy_impl(value: &Self) -> Self {
3402 *value
3403 }
3404
3405 #[inline]
3406 fn conj_impl(value: &Self) -> Self {
3407 Complex {
3408 re: value.0.re,
3409 im: value.0.im.neg_by_ref(),
3410 }
3411 .into()
3412 }
3413
3414 #[inline]
3415 fn recip_impl(value: &Self) -> Self {
3416 let (re, im) = recip_impl(value.0.re, value.0.im);
3417 Complex { re, im }.into()
3418 }
3419
3420 #[inline]
3421 fn sqrt_impl(value: &Self) -> Self {
3422 let (re, im) = sqrt_impl(value.0.re, value.0.im);
3423 Complex { re, im }.into()
3424 }
3425
3426 #[inline]
3427 fn abs_impl(value: &Self) -> Self::Real {
3428 abs_impl(value.0.re, value.0.im)
3429 }
3430
3431 #[inline]
3432 fn abs1_impl(value: &Self) -> Self::Real {
3433 use crate::ext::*;
3434 value.0.re.abs1() + value.0.im.abs1()
3435 }
3436
3437 #[inline]
3438 fn abs2_impl(value: &Self) -> Self::Real {
3439 use crate::ext::*;
3440 value.0.re.abs2() + value.0.im.abs2()
3441 }
3442
3443 #[inline]
3444 fn mul_real_impl(lhs: &Self, rhs: &Self::Real) -> Self {
3445 Complex {
3446 re: lhs.0.re * rhs,
3447 im: lhs.0.im * rhs,
3448 }
3449 .into()
3450 }
3451
3452 #[inline]
3453 fn mul_pow2_impl(lhs: &Self, rhs: &Self::Real) -> Self {
3454 use crate::ext::*;
3455 Complex {
3456 re: lhs.0.re.mul_pow2(rhs),
3457 im: lhs.0.im.mul_pow2(rhs),
3458 }
3459 .into()
3460 }
3461
3462 #[inline]
3463 fn is_finite_impl(value: &Self) -> bool {
3464 value.0.re.is_finite() && value.0.im.is_finite()
3465 }
3466
3467 #[inline(always)]
3468 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
3469 f32::simd_ctx(simd)
3470 }
3471
3472 #[inline(always)]
3473 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
3474 f32::ctx_from_simd(ctx)
3475 }
3476
3477 #[inline(always)]
3478 fn simd_splat<S: Simd>(
3479 ctx: &Self::SimdCtx<S>,
3480 value: &Self,
3481 ) -> Self::SimdVec<S> {
3482 ctx.splat_c32s(value.0)
3483 }
3484
3485 #[inline(always)]
3486 fn simd_splat_real<S: Simd>(
3487 ctx: &Self::SimdCtx<S>,
3488 value: &Self::Real,
3489 ) -> Self::SimdVec<S> {
3490 ctx.splat_c32s(Complex {
3491 re: *value,
3492 im: *value,
3493 })
3494 }
3495
3496 #[inline(always)]
3497 fn simd_add<S: Simd>(
3498 ctx: &Self::SimdCtx<S>,
3499 lhs: Self::SimdVec<S>,
3500 rhs: Self::SimdVec<S>,
3501 ) -> Self::SimdVec<S> {
3502 ctx.add_c32s(lhs, rhs)
3503 }
3504
3505 #[inline(always)]
3506 fn simd_sub<S: Simd>(
3507 ctx: &Self::SimdCtx<S>,
3508 lhs: Self::SimdVec<S>,
3509 rhs: Self::SimdVec<S>,
3510 ) -> Self::SimdVec<S> {
3511 ctx.sub_c32s(lhs, rhs)
3512 }
3513
3514 #[inline(always)]
3515 fn simd_neg<S: Simd>(
3516 ctx: &Self::SimdCtx<S>,
3517 value: Self::SimdVec<S>,
3518 ) -> Self::SimdVec<S> {
3519 ctx.neg_c32s(value)
3520 }
3521
3522 #[inline(always)]
3523 fn simd_conj<S: Simd>(
3524 ctx: &Self::SimdCtx<S>,
3525 value: Self::SimdVec<S>,
3526 ) -> Self::SimdVec<S> {
3527 ctx.conj_c32s(value)
3528 }
3529
3530 #[inline(always)]
3531 fn simd_abs1<S: Simd>(
3532 ctx: &Self::SimdCtx<S>,
3533 value: Self::SimdVec<S>,
3534 ) -> Self::SimdVec<S> {
3535 if const {
3536 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3537 } {
3538 bytemuck::cast(ctx.abs_f32s(bytemuck::cast(value)))
3539 } else if const {
3540 core::mem::size_of::<S::c32s>()
3541 == core::mem::size_of::<Complex<f32>>()
3542 } {
3543 let value: Complex<f32> = bytemuck::cast(value);
3544 let v = value.re.abs() + value.im.abs();
3545 bytemuck::cast(Complex { re: v, im: v })
3546 } else {
3547 panic!();
3548 }
3549 }
3550
3551 #[inline(always)]
3552 fn simd_abs_max<S: Simd>(
3553 ctx: &Self::SimdCtx<S>,
3554 value: Self::SimdVec<S>,
3555 ) -> Self::SimdVec<S> {
3556 if const {
3557 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3558 } {
3559 bytemuck::cast(ctx.abs_f32s(bytemuck::cast(value)))
3560 } else if const {
3561 core::mem::size_of::<S::c32s>()
3562 == core::mem::size_of::<Complex<f32>>()
3563 } {
3564 let value: Complex<f32> = bytemuck::cast(value);
3565 let re = value.re.abs();
3566 let im = value.im.abs();
3567 let v = if re.is_nan() {
3570 re
3571 } else if im.is_nan() {
3572 im
3573 } else if re > im {
3574 re
3575 } else {
3576 im
3577 };
3578 bytemuck::cast(Complex { re: v, im: v })
3579 } else {
3580 panic!();
3581 }
3582 }
3583
3584 #[inline(always)]
3585 fn simd_mul_real<S: Simd>(
3586 ctx: &Self::SimdCtx<S>,
3587 lhs: Self::SimdVec<S>,
3588 real_rhs: Self::SimdVec<S>,
3589 ) -> Self::SimdVec<S> {
3590 if const {
3591 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3592 } {
3593 bytemuck::cast(
3594 ctx.mul_f32s(bytemuck::cast(lhs), bytemuck::cast(real_rhs)),
3595 )
3596 } else if const {
3597 core::mem::size_of::<S::c32s>()
3598 == core::mem::size_of::<Complex<f32>>()
3599 } {
3600 let mut lhs: Complex<f32> = bytemuck::cast(lhs);
3601 let rhs: Complex<f32> = bytemuck::cast(real_rhs);
3602 lhs *= rhs.re;
3603 bytemuck::cast(lhs)
3604 } else {
3605 panic!();
3606 }
3607 }
3608
3609 #[inline(always)]
3610 fn simd_mul_pow2<S: Simd>(
3611 ctx: &Self::SimdCtx<S>,
3612 lhs: Self::SimdVec<S>,
3613 real_rhs: Self::SimdVec<S>,
3614 ) -> Self::SimdVec<S> {
3615 Self::simd_mul_real(ctx, lhs, real_rhs)
3616 }
3617
3618 #[inline(always)]
3619 fn simd_mul<S: Simd>(
3620 ctx: &Self::SimdCtx<S>,
3621 lhs: Self::SimdVec<S>,
3622 rhs: Self::SimdVec<S>,
3623 ) -> Self::SimdVec<S> {
3624 ctx.mul_e_c32s(lhs, rhs)
3625 }
3626
3627 #[inline(always)]
3628 fn simd_conj_mul<S: Simd>(
3629 ctx: &Self::SimdCtx<S>,
3630 lhs: Self::SimdVec<S>,
3631 rhs: Self::SimdVec<S>,
3632 ) -> Self::SimdVec<S> {
3633 ctx.conj_mul_e_c32s(lhs, rhs)
3634 }
3635
3636 #[inline(always)]
3637 fn simd_mul_add<S: Simd>(
3638 ctx: &Self::SimdCtx<S>,
3639 lhs: Self::SimdVec<S>,
3640 rhs: Self::SimdVec<S>,
3641 acc: Self::SimdVec<S>,
3642 ) -> Self::SimdVec<S> {
3643 ctx.mul_add_e_c32s(lhs, rhs, acc)
3644 }
3645
3646 #[inline(always)]
3647 fn simd_conj_mul_add<S: Simd>(
3648 ctx: &Self::SimdCtx<S>,
3649 lhs: Self::SimdVec<S>,
3650 rhs: Self::SimdVec<S>,
3651 acc: Self::SimdVec<S>,
3652 ) -> Self::SimdVec<S> {
3653 ctx.conj_mul_add_e_c32s(lhs, rhs, acc)
3654 }
3655
3656 #[inline(always)]
3657 fn simd_abs2<S: Simd>(
3658 ctx: &Self::SimdCtx<S>,
3659 value: Self::SimdVec<S>,
3660 ) -> Self::SimdVec<S> {
3661 if const {
3662 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3663 } {
3664 bytemuck::cast(
3665 ctx.mul_f32s(bytemuck::cast(value), bytemuck::cast(value)),
3666 )
3667 } else if const {
3668 core::mem::size_of::<S::c32s>()
3669 == core::mem::size_of::<Complex<f32>>()
3670 } {
3671 let value: Complex<f32> = bytemuck::cast(value);
3672 let v = value.re * value.re + value.im * value.im;
3673 bytemuck::cast(Complex { re: v, im: v })
3674 } else {
3675 panic!();
3676 }
3677 }
3678
3679 #[inline(always)]
3680 fn simd_abs2_add<S: Simd>(
3681 ctx: &Self::SimdCtx<S>,
3682 value: Self::SimdVec<S>,
3683 acc: Self::SimdVec<S>,
3684 ) -> Self::SimdVec<S> {
3685 if const {
3686 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3687 } {
3688 bytemuck::cast(ctx.mul_add_f32s(
3689 bytemuck::cast(value),
3690 bytemuck::cast(value),
3691 bytemuck::cast(acc),
3692 ))
3693 } else if const {
3694 core::mem::size_of::<S::c32s>()
3695 == core::mem::size_of::<Complex<f32>>()
3696 } {
3697 let value: Complex<f32> = bytemuck::cast(value);
3698 let acc: Complex<f32> = bytemuck::cast(acc);
3699 let v = value.re * value.re + value.im * value.im + acc.re;
3700 bytemuck::cast(Complex { re: v, im: v })
3701 } else {
3702 panic!();
3703 }
3704 }
3705
3706 #[inline(always)]
3707 fn simd_reduce_sum<S: Simd>(
3708 ctx: &Self::SimdCtx<S>,
3709 value: Self::SimdVec<S>,
3710 ) -> Self {
3711 ctx.reduce_sum_c32s(value).into()
3712 }
3713
3714 #[inline(always)]
3715 fn simd_reduce_max<S: Simd>(
3716 ctx: &Self::SimdCtx<S>,
3717 value: Self::SimdVec<S>,
3718 ) -> Self {
3719 ctx.reduce_max_c32s(value).into()
3720 }
3721
3722 #[inline(always)]
3723 fn simd_equal<S: Simd>(
3724 _: &Self::SimdCtx<S>,
3725 _: Self::SimdVec<S>,
3726 _: Self::SimdVec<S>,
3727 ) -> Self::SimdMask<S> {
3728 panic!()
3729 }
3730
3731 #[inline(always)]
3732 fn simd_less_than<S: Simd>(
3733 ctx: &Self::SimdCtx<S>,
3734 real_lhs: Self::SimdVec<S>,
3735 real_rhs: Self::SimdVec<S>,
3736 ) -> Self::SimdMask<S> {
3737 if const {
3738 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3739 } {
3740 ctx.less_than_f32s(
3741 bytemuck::cast(real_lhs),
3742 bytemuck::cast(real_rhs),
3743 )
3744 } else if const {
3745 core::mem::size_of::<S::c32s>()
3746 == core::mem::size_of::<Complex<f32>>()
3747 } {
3748 assert!(
3749 const {
3750 core::mem::size_of::<S::m32s>()
3751 == core::mem::size_of::<bool>()
3752 }
3753 );
3754 let lhs: Complex<f32> = bytemuck::cast(real_lhs);
3755 let rhs: Complex<f32> = bytemuck::cast(real_rhs);
3756 unsafe { core::mem::transmute_copy(&(lhs.re < rhs.re)) }
3757 } else {
3758 panic!();
3759 }
3760 }
3761
3762 #[inline(always)]
3763 fn simd_less_than_or_equal<S: Simd>(
3764 ctx: &Self::SimdCtx<S>,
3765 real_lhs: Self::SimdVec<S>,
3766 real_rhs: Self::SimdVec<S>,
3767 ) -> Self::SimdMask<S> {
3768 if const {
3769 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3770 } {
3771 ctx.less_than_or_equal_f32s(
3772 bytemuck::cast(real_lhs),
3773 bytemuck::cast(real_rhs),
3774 )
3775 } else if const {
3776 core::mem::size_of::<S::c32s>()
3777 == core::mem::size_of::<Complex<f32>>()
3778 } {
3779 assert!(
3780 const {
3781 core::mem::size_of::<S::m32s>()
3782 == core::mem::size_of::<bool>()
3783 }
3784 );
3785 let lhs: Complex<f32> = bytemuck::cast(real_lhs);
3786 let rhs: Complex<f32> = bytemuck::cast(real_rhs);
3787 unsafe { core::mem::transmute_copy(&(lhs.re <= rhs.re)) }
3788 } else {
3789 panic!();
3790 }
3791 }
3792
3793 #[inline(always)]
3794 fn simd_greater_than<S: Simd>(
3795 ctx: &Self::SimdCtx<S>,
3796 real_lhs: Self::SimdVec<S>,
3797 real_rhs: Self::SimdVec<S>,
3798 ) -> Self::SimdMask<S> {
3799 if const {
3800 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3801 } {
3802 ctx.greater_than_f32s(
3803 bytemuck::cast(real_lhs),
3804 bytemuck::cast(real_rhs),
3805 )
3806 } else if const {
3807 core::mem::size_of::<S::c32s>()
3808 == core::mem::size_of::<Complex<f32>>()
3809 } {
3810 assert!(
3811 const {
3812 core::mem::size_of::<S::m32s>()
3813 == core::mem::size_of::<bool>()
3814 }
3815 );
3816 let lhs: Complex<f32> = bytemuck::cast(real_lhs);
3817 let rhs: Complex<f32> = bytemuck::cast(real_rhs);
3818 unsafe { core::mem::transmute_copy(&(lhs.re > rhs.re)) }
3819 } else {
3820 panic!();
3821 }
3822 }
3823
3824 #[inline(always)]
3825 fn simd_greater_than_or_equal<S: Simd>(
3826 ctx: &Self::SimdCtx<S>,
3827 real_lhs: Self::SimdVec<S>,
3828 real_rhs: Self::SimdVec<S>,
3829 ) -> Self::SimdMask<S> {
3830 if const {
3831 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3832 } {
3833 ctx.greater_than_or_equal_f32s(
3834 bytemuck::cast(real_lhs),
3835 bytemuck::cast(real_rhs),
3836 )
3837 } else if const {
3838 core::mem::size_of::<S::c32s>()
3839 == core::mem::size_of::<Complex<f32>>()
3840 } {
3841 assert!(
3842 const {
3843 core::mem::size_of::<S::m32s>()
3844 == core::mem::size_of::<bool>()
3845 }
3846 );
3847 let lhs: Complex<f32> = bytemuck::cast(real_lhs);
3848 let rhs: Complex<f32> = bytemuck::cast(real_rhs);
3849 unsafe { core::mem::transmute_copy(&(lhs.re >= rhs.re)) }
3850 } else {
3851 panic!();
3852 }
3853 }
3854
3855 #[inline(always)]
3856 fn simd_select<S: Simd>(
3857 ctx: &Self::SimdCtx<S>,
3858 mask: Self::SimdMask<S>,
3859 lhs: Self::SimdVec<S>,
3860 rhs: Self::SimdVec<S>,
3861 ) -> Self::SimdVec<S> {
3862 if const {
3863 core::mem::size_of::<S::c32s>() == core::mem::size_of::<S::f32s>()
3864 } {
3865 bytemuck::cast(ctx.select_f32s(
3866 mask,
3867 bytemuck::cast(lhs),
3868 bytemuck::cast(rhs),
3869 ))
3870 } else if const {
3871 core::mem::size_of::<S::c32s>()
3872 == core::mem::size_of::<Complex<f32>>()
3873 } {
3874 assert!(
3875 const {
3876 core::mem::size_of::<S::m32s>()
3877 == core::mem::size_of::<bool>()
3878 }
3879 );
3880 let mask: bool = unsafe { core::mem::transmute_copy(&mask) };
3881 let lhs: Complex<f32> = bytemuck::cast(lhs);
3882 let rhs: Complex<f32> = bytemuck::cast(rhs);
3883 bytemuck::cast(if mask { lhs } else { rhs })
3884 } else {
3885 panic!();
3886 }
3887 }
3888
3889 #[inline(always)]
3890 fn simd_index_select<S: Simd>(
3891 ctx: &Self::SimdCtx<S>,
3892 mask: Self::SimdMask<S>,
3893 lhs: Self::SimdIndex<S>,
3894 rhs: Self::SimdIndex<S>,
3895 ) -> Self::SimdIndex<S> {
3896 f32::simd_index_select(ctx, mask, lhs, rhs)
3897 }
3898
3899 #[inline(always)]
3900 fn simd_index_splat<S: Simd>(
3901 ctx: &Self::SimdCtx<S>,
3902 value: Self::Index,
3903 ) -> Self::SimdIndex<S> {
3904 f32::simd_index_splat(ctx, value)
3905 }
3906
3907 #[inline(always)]
3908 fn simd_index_add<S: Simd>(
3909 ctx: &Self::SimdCtx<S>,
3910 lhs: Self::SimdIndex<S>,
3911 rhs: Self::SimdIndex<S>,
3912 ) -> Self::SimdIndex<S> {
3913 f32::simd_index_add(ctx, lhs, rhs)
3914 }
3915
3916 #[inline(always)]
3917 fn simd_index_less_than<S: Simd>(
3918 ctx: &Self::SimdCtx<S>,
3919 lhs: Self::SimdIndex<S>,
3920 rhs: Self::SimdIndex<S>,
3921 ) -> Self::SimdMask<S> {
3922 f32::simd_index_less_than(ctx, lhs, rhs)
3923 }
3924
3925 #[inline(always)]
3926 fn simd_and_mask<S: Simd>(
3927 ctx: &Self::SimdCtx<S>,
3928 lhs: Self::SimdMask<S>,
3929 rhs: Self::SimdMask<S>,
3930 ) -> Self::SimdMask<S> {
3931 f32::simd_and_mask(ctx, lhs, rhs)
3932 }
3933
3934 #[inline(always)]
3935 fn simd_or_mask<S: Simd>(
3936 ctx: &Self::SimdCtx<S>,
3937 lhs: Self::SimdMask<S>,
3938 rhs: Self::SimdMask<S>,
3939 ) -> Self::SimdMask<S> {
3940 f32::simd_or_mask(ctx, lhs, rhs)
3941 }
3942
3943 #[inline(always)]
3944 fn simd_not_mask<S: Simd>(
3945 ctx: &Self::SimdCtx<S>,
3946 mask: Self::SimdMask<S>,
3947 ) -> Self::SimdMask<S> {
3948 f32::simd_not_mask(ctx, mask)
3949 }
3950
3951 #[inline(always)]
3952 fn simd_first_true_mask<S: Simd>(
3953 ctx: &Self::SimdCtx<S>,
3954 value: Self::SimdMask<S>,
3955 ) -> usize {
3956 f32::simd_first_true_mask(ctx, value)
3957 }
3958
3959 #[inline(always)]
3960 fn simd_mem_mask_between<S: Simd>(
3961 ctx: &Self::SimdCtx<S>,
3962 start: Self::Index,
3963 end: Self::Index,
3964 ) -> Self::SimdMemMask<S> {
3965 ctx.mask_between_m32s((2 * start) as _, (2 * end) as _)
3966 }
3967
3968 #[inline(always)]
3969 fn simd_mask_between<S: Simd>(
3970 ctx: &Self::SimdCtx<S>,
3971 start: Self::Index,
3972 end: Self::Index,
3973 ) -> Self::SimdMask<S> {
3974 ctx.mask_between_m32s((2 * start) as _, (2 * end) as _)
3975 .mask()
3976 }
3977
3978 #[inline(always)]
3979 unsafe fn simd_mask_load_raw<S: Simd>(
3980 ctx: &Self::SimdCtx<S>,
3981 mask: Self::SimdMemMask<S>,
3982 ptr: *const Self::SimdVec<S>,
3983 ) -> Self::SimdVec<S> {
3984 ctx.mask_load_ptr_c32s(mask, ptr as _)
3985 }
3986
3987 #[inline(always)]
3988 unsafe fn simd_mask_store_raw<S: Simd>(
3989 ctx: &Self::SimdCtx<S>,
3990 mask: Self::SimdMemMask<S>,
3991 ptr: *mut Self::SimdVec<S>,
3992 values: Self::SimdVec<S>,
3993 ) {
3994 ctx.mask_store_ptr_c32s(mask, ptr as _, values);
3995 }
3996}
3997impl ComplexField for ComplexImpl<f64> {
3998 type Arch = pulp::Arch;
3999 type Index = u64;
4000 type Real = f64;
4001 type SimdCtx<S: Simd> = S;
4002 type SimdIndex<S: Simd> = S::u64s;
4003 type SimdMask<S: Simd> = S::m64s;
4004 type SimdMemMask<S: Simd> = pulp::MemMask<S::m64s>;
4005 type SimdVec<S: Simd> = S::c64s;
4006 type Unit = f64;
4007
4008 const IS_NATIVE_C64: bool = true;
4009 const IS_REAL: bool = false;
4010 const SIMD_ABS_SPLIT_REAL_IMAG: bool = true;
4011 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Simd;
4012
4013 #[inline]
4014 fn zero_impl() -> Self {
4015 Complex {
4016 re: f64::zero_impl(),
4017 im: f64::zero_impl(),
4018 }
4019 .into()
4020 }
4021
4022 #[inline]
4023 fn one_impl() -> Self {
4024 Complex {
4025 re: f64::one_impl(),
4026 im: f64::zero_impl(),
4027 }
4028 .into()
4029 }
4030
4031 #[inline]
4032 fn nan_impl() -> Self {
4033 Complex {
4034 re: f64::nan_impl(),
4035 im: f64::nan_impl(),
4036 }
4037 .into()
4038 }
4039
4040 #[inline]
4041 fn infinity_impl() -> Self {
4042 Complex {
4043 re: f64::infinity_impl(),
4044 im: f64::infinity_impl(),
4045 }
4046 .into()
4047 }
4048
4049 #[inline]
4050 fn from_real_impl(real: &Self::Real) -> Self {
4051 Complex {
4052 re: *real,
4053 im: f64::zero_impl(),
4054 }
4055 .into()
4056 }
4057
4058 #[inline]
4059 fn from_f64_impl(real: f64) -> Self {
4060 Complex {
4061 re: f64::from_f64_impl(real),
4062 im: f64::zero_impl(),
4063 }
4064 .into()
4065 }
4066
4067 #[inline]
4068 fn real_part_impl(value: &Self) -> Self::Real {
4069 value.0.re
4070 }
4071
4072 #[inline]
4073 fn imag_part_impl(value: &Self) -> Self::Real {
4074 value.0.im
4075 }
4076
4077 #[inline]
4078 fn copy_impl(value: &Self) -> Self {
4079 *value
4080 }
4081
4082 #[inline]
4083 fn conj_impl(value: &Self) -> Self {
4084 Complex {
4085 re: value.0.re,
4086 im: value.0.im.neg_by_ref(),
4087 }
4088 .into()
4089 }
4090
4091 #[inline]
4092 fn recip_impl(value: &Self) -> Self {
4093 let (re, im) = recip_impl(value.0.re, value.0.im);
4094 Complex { re, im }.into()
4095 }
4096
4097 #[inline]
4098 fn sqrt_impl(value: &Self) -> Self {
4099 let (re, im) = sqrt_impl(value.0.re, value.0.im);
4100 Complex { re, im }.into()
4101 }
4102
4103 #[inline]
4104 fn abs_impl(value: &Self) -> Self::Real {
4105 abs_impl(value.0.re, value.0.im)
4106 }
4107
4108 #[inline]
4109 fn abs1_impl(value: &Self) -> Self::Real {
4110 use crate::ext::*;
4111 value.0.re.abs1() + value.0.im.abs1()
4112 }
4113
4114 #[inline]
4115 fn abs2_impl(value: &Self) -> Self::Real {
4116 use crate::ext::*;
4117 value.0.re.abs2() + value.0.im.abs2()
4118 }
4119
4120 #[inline]
4121 fn mul_real_impl(lhs: &Self, rhs: &Self::Real) -> Self {
4122 Complex {
4123 re: lhs.0.re * rhs,
4124 im: lhs.0.im * rhs,
4125 }
4126 .into()
4127 }
4128
4129 #[inline]
4130 fn mul_pow2_impl(lhs: &Self, rhs: &Self::Real) -> Self {
4131 use crate::ext::*;
4132 Complex {
4133 re: lhs.0.re.mul_pow2(rhs),
4134 im: lhs.0.im.mul_pow2(rhs),
4135 }
4136 .into()
4137 }
4138
4139 #[inline]
4140 fn is_finite_impl(value: &Self) -> bool {
4141 value.0.re.is_finite() && value.0.im.is_finite()
4142 }
4143
4144 #[inline(always)]
4145 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
4146 f64::simd_ctx(simd)
4147 }
4148
4149 #[inline(always)]
4150 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
4151 f64::ctx_from_simd(ctx)
4152 }
4153
4154 #[inline(always)]
4155 fn simd_splat<S: Simd>(
4156 ctx: &Self::SimdCtx<S>,
4157 value: &Self,
4158 ) -> Self::SimdVec<S> {
4159 ctx.splat_c64s(value.0)
4160 }
4161
4162 #[inline(always)]
4163 fn simd_splat_real<S: Simd>(
4164 ctx: &Self::SimdCtx<S>,
4165 value: &Self::Real,
4166 ) -> Self::SimdVec<S> {
4167 ctx.splat_c64s(Complex {
4168 re: *value,
4169 im: *value,
4170 })
4171 }
4172
4173 #[inline(always)]
4174 fn simd_add<S: Simd>(
4175 ctx: &Self::SimdCtx<S>,
4176 lhs: Self::SimdVec<S>,
4177 rhs: Self::SimdVec<S>,
4178 ) -> Self::SimdVec<S> {
4179 ctx.add_c64s(lhs, rhs)
4180 }
4181
4182 #[inline(always)]
4183 fn simd_sub<S: Simd>(
4184 ctx: &Self::SimdCtx<S>,
4185 lhs: Self::SimdVec<S>,
4186 rhs: Self::SimdVec<S>,
4187 ) -> Self::SimdVec<S> {
4188 ctx.sub_c64s(lhs, rhs)
4189 }
4190
4191 #[inline(always)]
4192 fn simd_neg<S: Simd>(
4193 ctx: &Self::SimdCtx<S>,
4194 value: Self::SimdVec<S>,
4195 ) -> Self::SimdVec<S> {
4196 ctx.neg_c64s(value)
4197 }
4198
4199 #[inline(always)]
4200 fn simd_conj<S: Simd>(
4201 ctx: &Self::SimdCtx<S>,
4202 value: Self::SimdVec<S>,
4203 ) -> Self::SimdVec<S> {
4204 ctx.conj_c64s(value)
4205 }
4206
4207 #[inline(always)]
4208 fn simd_abs1<S: Simd>(
4209 ctx: &Self::SimdCtx<S>,
4210 value: Self::SimdVec<S>,
4211 ) -> Self::SimdVec<S> {
4212 if const {
4213 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4214 } {
4215 bytemuck::cast(ctx.abs_f64s(bytemuck::cast(value)))
4216 } else if const {
4217 core::mem::size_of::<S::c64s>()
4218 == core::mem::size_of::<Complex<f64>>()
4219 } {
4220 let value: Complex<f64> = bytemuck::cast(value);
4221 let v = value.re.abs() + value.im.abs();
4222 bytemuck::cast(Complex { re: v, im: v })
4223 } else {
4224 panic!();
4225 }
4226 }
4227
4228 #[inline(always)]
4229 fn simd_abs_max<S: Simd>(
4230 ctx: &Self::SimdCtx<S>,
4231 value: Self::SimdVec<S>,
4232 ) -> Self::SimdVec<S> {
4233 if const {
4234 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4235 } {
4236 bytemuck::cast(ctx.abs_f64s(bytemuck::cast(value)))
4237 } else if const {
4238 core::mem::size_of::<S::c64s>()
4239 == core::mem::size_of::<Complex<f64>>()
4240 } {
4241 let value: Complex<f64> = bytemuck::cast(value);
4242 let re = value.re.abs();
4243 let im = value.im.abs();
4244 let v = if re.is_nan() {
4247 re
4248 } else if im.is_nan() {
4249 im
4250 } else if re > im {
4251 re
4252 } else {
4253 im
4254 };
4255 bytemuck::cast(Complex { re: v, im: v })
4256 } else {
4257 panic!();
4258 }
4259 }
4260
4261 #[inline(always)]
4262 fn simd_mul_real<S: Simd>(
4263 ctx: &Self::SimdCtx<S>,
4264 lhs: Self::SimdVec<S>,
4265 real_rhs: Self::SimdVec<S>,
4266 ) -> Self::SimdVec<S> {
4267 if const {
4268 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4269 } {
4270 bytemuck::cast(
4271 ctx.mul_f64s(bytemuck::cast(lhs), bytemuck::cast(real_rhs)),
4272 )
4273 } else if const {
4274 core::mem::size_of::<S::c64s>()
4275 == core::mem::size_of::<Complex<f64>>()
4276 } {
4277 let mut lhs: Complex<f64> = bytemuck::cast(lhs);
4278 let rhs: Complex<f64> = bytemuck::cast(real_rhs);
4279 lhs *= rhs.re;
4280 bytemuck::cast(lhs)
4281 } else {
4282 panic!();
4283 }
4284 }
4285
4286 #[inline(always)]
4287 fn simd_mul_pow2<S: Simd>(
4288 ctx: &Self::SimdCtx<S>,
4289 lhs: Self::SimdVec<S>,
4290 real_rhs: Self::SimdVec<S>,
4291 ) -> Self::SimdVec<S> {
4292 Self::simd_mul_real(ctx, lhs, real_rhs)
4293 }
4294
4295 #[inline(always)]
4296 fn simd_mul<S: Simd>(
4297 ctx: &Self::SimdCtx<S>,
4298 lhs: Self::SimdVec<S>,
4299 rhs: Self::SimdVec<S>,
4300 ) -> Self::SimdVec<S> {
4301 ctx.mul_e_c64s(lhs, rhs)
4302 }
4303
4304 #[inline(always)]
4305 fn simd_conj_mul<S: Simd>(
4306 ctx: &Self::SimdCtx<S>,
4307 lhs: Self::SimdVec<S>,
4308 rhs: Self::SimdVec<S>,
4309 ) -> Self::SimdVec<S> {
4310 ctx.conj_mul_e_c64s(lhs, rhs)
4311 }
4312
4313 #[inline(always)]
4314 fn simd_mul_add<S: Simd>(
4315 ctx: &Self::SimdCtx<S>,
4316 lhs: Self::SimdVec<S>,
4317 rhs: Self::SimdVec<S>,
4318 acc: Self::SimdVec<S>,
4319 ) -> Self::SimdVec<S> {
4320 ctx.mul_add_e_c64s(lhs, rhs, acc)
4321 }
4322
4323 #[inline(always)]
4324 fn simd_conj_mul_add<S: Simd>(
4325 ctx: &Self::SimdCtx<S>,
4326 lhs: Self::SimdVec<S>,
4327 rhs: Self::SimdVec<S>,
4328 acc: Self::SimdVec<S>,
4329 ) -> Self::SimdVec<S> {
4330 ctx.conj_mul_add_e_c64s(lhs, rhs, acc)
4331 }
4332
4333 #[inline(always)]
4334 fn simd_abs2<S: Simd>(
4335 ctx: &Self::SimdCtx<S>,
4336 value: Self::SimdVec<S>,
4337 ) -> Self::SimdVec<S> {
4338 if const {
4339 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4340 } {
4341 bytemuck::cast(
4342 ctx.mul_f64s(bytemuck::cast(value), bytemuck::cast(value)),
4343 )
4344 } else if const {
4345 core::mem::size_of::<S::c64s>()
4346 == core::mem::size_of::<Complex<f64>>()
4347 } {
4348 let value: Complex<f64> = bytemuck::cast(value);
4349 let v = value.re * value.re + value.im * value.im;
4350 bytemuck::cast(Complex { re: v, im: v })
4351 } else {
4352 panic!();
4353 }
4354 }
4355
4356 #[inline(always)]
4357 fn simd_abs2_add<S: Simd>(
4358 ctx: &Self::SimdCtx<S>,
4359 value: Self::SimdVec<S>,
4360 acc: Self::SimdVec<S>,
4361 ) -> Self::SimdVec<S> {
4362 if const {
4363 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4364 } {
4365 bytemuck::cast(ctx.mul_add_f64s(
4366 bytemuck::cast(value),
4367 bytemuck::cast(value),
4368 bytemuck::cast(acc),
4369 ))
4370 } else if const {
4371 core::mem::size_of::<S::c64s>()
4372 == core::mem::size_of::<Complex<f64>>()
4373 } {
4374 let value: Complex<f64> = bytemuck::cast(value);
4375 let acc: Complex<f64> = bytemuck::cast(acc);
4376 let v = value.re * value.re + value.im * value.im + acc.re;
4377 bytemuck::cast(Complex { re: v, im: v })
4378 } else {
4379 panic!();
4380 }
4381 }
4382
4383 #[inline(always)]
4384 fn simd_reduce_sum<S: Simd>(
4385 ctx: &Self::SimdCtx<S>,
4386 value: Self::SimdVec<S>,
4387 ) -> Self {
4388 ctx.reduce_sum_c64s(value).into()
4389 }
4390
4391 #[inline(always)]
4392 fn simd_reduce_max<S: Simd>(
4393 ctx: &Self::SimdCtx<S>,
4394 value: Self::SimdVec<S>,
4395 ) -> Self {
4396 ctx.reduce_max_c64s(value).into()
4397 }
4398
4399 #[inline(always)]
4400 fn simd_equal<S: Simd>(
4401 _: &Self::SimdCtx<S>,
4402 _: Self::SimdVec<S>,
4403 _: Self::SimdVec<S>,
4404 ) -> Self::SimdMask<S> {
4405 panic!()
4406 }
4407
4408 #[inline(always)]
4409 fn simd_less_than<S: Simd>(
4410 ctx: &Self::SimdCtx<S>,
4411 real_lhs: Self::SimdVec<S>,
4412 real_rhs: Self::SimdVec<S>,
4413 ) -> Self::SimdMask<S> {
4414 if const {
4415 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4416 } {
4417 ctx.less_than_f64s(
4418 bytemuck::cast(real_lhs),
4419 bytemuck::cast(real_rhs),
4420 )
4421 } else if const {
4422 core::mem::size_of::<S::c64s>()
4423 == core::mem::size_of::<Complex<f64>>()
4424 } {
4425 assert!(
4426 const {
4427 core::mem::size_of::<S::m64s>()
4428 == core::mem::size_of::<bool>()
4429 }
4430 );
4431 let lhs: Complex<f64> = bytemuck::cast(real_lhs);
4432 let rhs: Complex<f64> = bytemuck::cast(real_rhs);
4433 unsafe { core::mem::transmute_copy(&(lhs.re < rhs.re)) }
4434 } else {
4435 panic!();
4436 }
4437 }
4438
4439 #[inline(always)]
4440 fn simd_less_than_or_equal<S: Simd>(
4441 ctx: &Self::SimdCtx<S>,
4442 real_lhs: Self::SimdVec<S>,
4443 real_rhs: Self::SimdVec<S>,
4444 ) -> Self::SimdMask<S> {
4445 if const {
4446 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4447 } {
4448 ctx.less_than_or_equal_f64s(
4449 bytemuck::cast(real_lhs),
4450 bytemuck::cast(real_rhs),
4451 )
4452 } else if const {
4453 core::mem::size_of::<S::c64s>()
4454 == core::mem::size_of::<Complex<f64>>()
4455 } {
4456 assert!(
4457 const {
4458 core::mem::size_of::<S::m64s>()
4459 == core::mem::size_of::<bool>()
4460 }
4461 );
4462 let lhs: Complex<f64> = bytemuck::cast(real_lhs);
4463 let rhs: Complex<f64> = bytemuck::cast(real_rhs);
4464 unsafe { core::mem::transmute_copy(&(lhs.re <= rhs.re)) }
4465 } else {
4466 panic!();
4467 }
4468 }
4469
4470 #[inline(always)]
4471 fn simd_greater_than<S: Simd>(
4472 ctx: &Self::SimdCtx<S>,
4473 real_lhs: Self::SimdVec<S>,
4474 real_rhs: Self::SimdVec<S>,
4475 ) -> Self::SimdMask<S> {
4476 if const {
4477 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4478 } {
4479 ctx.greater_than_f64s(
4480 bytemuck::cast(real_lhs),
4481 bytemuck::cast(real_rhs),
4482 )
4483 } else if const {
4484 core::mem::size_of::<S::c64s>()
4485 == core::mem::size_of::<Complex<f64>>()
4486 } {
4487 assert!(
4488 const {
4489 core::mem::size_of::<S::m64s>()
4490 == core::mem::size_of::<bool>()
4491 }
4492 );
4493 let lhs: Complex<f64> = bytemuck::cast(real_lhs);
4494 let rhs: Complex<f64> = bytemuck::cast(real_rhs);
4495 unsafe { core::mem::transmute_copy(&(lhs.re > rhs.re)) }
4496 } else {
4497 panic!();
4498 }
4499 }
4500
4501 #[inline(always)]
4502 fn simd_greater_than_or_equal<S: Simd>(
4503 ctx: &Self::SimdCtx<S>,
4504 real_lhs: Self::SimdVec<S>,
4505 real_rhs: Self::SimdVec<S>,
4506 ) -> Self::SimdMask<S> {
4507 if const {
4508 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4509 } {
4510 ctx.greater_than_or_equal_f64s(
4511 bytemuck::cast(real_lhs),
4512 bytemuck::cast(real_rhs),
4513 )
4514 } else if const {
4515 core::mem::size_of::<S::c64s>()
4516 == core::mem::size_of::<Complex<f64>>()
4517 } {
4518 assert!(
4519 const {
4520 core::mem::size_of::<S::m64s>()
4521 == core::mem::size_of::<bool>()
4522 }
4523 );
4524 let lhs: Complex<f64> = bytemuck::cast(real_lhs);
4525 let rhs: Complex<f64> = bytemuck::cast(real_rhs);
4526 unsafe { core::mem::transmute_copy(&(lhs.re >= rhs.re)) }
4527 } else {
4528 panic!();
4529 }
4530 }
4531
4532 #[inline(always)]
4533 fn simd_select<S: Simd>(
4534 ctx: &Self::SimdCtx<S>,
4535 mask: Self::SimdMask<S>,
4536 lhs: Self::SimdVec<S>,
4537 rhs: Self::SimdVec<S>,
4538 ) -> Self::SimdVec<S> {
4539 if const {
4540 core::mem::size_of::<S::c64s>() == core::mem::size_of::<S::f64s>()
4541 } {
4542 bytemuck::cast(ctx.select_f64s(
4543 mask,
4544 bytemuck::cast(lhs),
4545 bytemuck::cast(rhs),
4546 ))
4547 } else if const {
4548 core::mem::size_of::<S::c64s>()
4549 == core::mem::size_of::<Complex<f64>>()
4550 } {
4551 assert!(
4552 const {
4553 core::mem::size_of::<S::m64s>()
4554 == core::mem::size_of::<bool>()
4555 }
4556 );
4557 let mask: bool = unsafe { core::mem::transmute_copy(&mask) };
4558 let lhs: Complex<f64> = bytemuck::cast(lhs);
4559 let rhs: Complex<f64> = bytemuck::cast(rhs);
4560 bytemuck::cast(if mask { lhs } else { rhs })
4561 } else {
4562 panic!();
4563 }
4564 }
4565
4566 #[inline(always)]
4567 fn simd_index_select<S: Simd>(
4568 ctx: &Self::SimdCtx<S>,
4569 mask: Self::SimdMask<S>,
4570 lhs: Self::SimdIndex<S>,
4571 rhs: Self::SimdIndex<S>,
4572 ) -> Self::SimdIndex<S> {
4573 f64::simd_index_select(ctx, mask, lhs, rhs)
4574 }
4575
4576 #[inline(always)]
4577 fn simd_index_splat<S: Simd>(
4578 ctx: &Self::SimdCtx<S>,
4579 value: Self::Index,
4580 ) -> Self::SimdIndex<S> {
4581 f64::simd_index_splat(ctx, value)
4582 }
4583
4584 #[inline(always)]
4585 fn simd_index_add<S: Simd>(
4586 ctx: &Self::SimdCtx<S>,
4587 lhs: Self::SimdIndex<S>,
4588 rhs: Self::SimdIndex<S>,
4589 ) -> Self::SimdIndex<S> {
4590 f64::simd_index_add(ctx, lhs, rhs)
4591 }
4592
4593 #[inline(always)]
4594 fn simd_index_less_than<S: Simd>(
4595 ctx: &Self::SimdCtx<S>,
4596 lhs: Self::SimdIndex<S>,
4597 rhs: Self::SimdIndex<S>,
4598 ) -> Self::SimdMask<S> {
4599 f64::simd_index_less_than(ctx, lhs, rhs)
4600 }
4601
4602 #[inline(always)]
4603 fn simd_and_mask<S: Simd>(
4604 ctx: &Self::SimdCtx<S>,
4605 lhs: Self::SimdMask<S>,
4606 rhs: Self::SimdMask<S>,
4607 ) -> Self::SimdMask<S> {
4608 f64::simd_and_mask(ctx, lhs, rhs)
4609 }
4610
4611 #[inline(always)]
4612 fn simd_or_mask<S: Simd>(
4613 ctx: &Self::SimdCtx<S>,
4614 lhs: Self::SimdMask<S>,
4615 rhs: Self::SimdMask<S>,
4616 ) -> Self::SimdMask<S> {
4617 f64::simd_or_mask(ctx, lhs, rhs)
4618 }
4619
4620 #[inline(always)]
4621 fn simd_not_mask<S: Simd>(
4622 ctx: &Self::SimdCtx<S>,
4623 mask: Self::SimdMask<S>,
4624 ) -> Self::SimdMask<S> {
4625 f64::simd_not_mask(ctx, mask)
4626 }
4627
4628 #[inline(always)]
4629 fn simd_first_true_mask<S: Simd>(
4630 ctx: &Self::SimdCtx<S>,
4631 value: Self::SimdMask<S>,
4632 ) -> usize {
4633 f64::simd_first_true_mask(ctx, value)
4634 }
4635
4636 #[inline(always)]
4637 fn simd_mem_mask_between<S: Simd>(
4638 ctx: &Self::SimdCtx<S>,
4639 start: Self::Index,
4640 end: Self::Index,
4641 ) -> Self::SimdMemMask<S> {
4642 ctx.mask_between_m64s((2 * start) as _, (2 * end) as _)
4643 }
4644
4645 #[inline(always)]
4646 fn simd_mask_between<S: Simd>(
4647 ctx: &Self::SimdCtx<S>,
4648 start: Self::Index,
4649 end: Self::Index,
4650 ) -> Self::SimdMask<S> {
4651 ctx.mask_between_m64s((2 * start) as _, (2 * end) as _)
4652 .mask()
4653 }
4654
4655 #[inline(always)]
4656 unsafe fn simd_mask_load_raw<S: Simd>(
4657 ctx: &Self::SimdCtx<S>,
4658 mask: Self::SimdMemMask<S>,
4659 ptr: *const Self::SimdVec<S>,
4660 ) -> Self::SimdVec<S> {
4661 ctx.mask_load_ptr_c64s(mask, ptr as _)
4662 }
4663
4664 #[inline(always)]
4665 unsafe fn simd_mask_store_raw<S: Simd>(
4666 ctx: &Self::SimdCtx<S>,
4667 mask: Self::SimdMemMask<S>,
4668 ptr: *mut Self::SimdVec<S>,
4669 values: Self::SimdVec<S>,
4670 ) {
4671 ctx.mask_store_ptr_c64s(mask, ptr as _, values);
4672 }
4673}
4674#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
4675pub struct Symbolic;
4676impl_op!({
4677 impl Add for Symbolic {
4678 fn add(self, rhs: Self) -> Self::Output {
4679 _ = rhs;
4680 Symbolic
4681 }
4682 }
4683 impl Sub for Symbolic {
4684 fn sub(self, rhs: Self) -> Self::Output {
4685 _ = rhs;
4686 Symbolic
4687 }
4688 }
4689 impl Mul for Symbolic {
4690 fn mul(self, rhs: Self) -> Self::Output {
4691 _ = rhs;
4692 Symbolic
4693 }
4694 }
4695 impl Div for Symbolic {
4696 fn div(self, rhs: Self) -> Self::Output {
4697 _ = rhs;
4698 Symbolic
4699 }
4700 }
4701 impl Rem for Symbolic {
4702 fn rem(self, rhs: Self) -> Self::Output {
4703 _ = rhs;
4704 Symbolic
4705 }
4706 }
4707});
4708impl_assign_op!({
4709 impl AddAssign for Symbolic {
4710 fn add_assign(&mut self, rhs: Self) {
4711 _ = rhs;
4712 }
4713 }
4714 impl SubAssign for Symbolic {
4715 fn sub_assign(&mut self, rhs: Self) {
4716 _ = rhs;
4717 }
4718 }
4719 impl MulAssign for Symbolic {
4720 fn mul_assign(&mut self, rhs: Self) {
4721 _ = rhs;
4722 }
4723 }
4724 impl DivAssign for Symbolic {
4725 fn div_assign(&mut self, rhs: Self) {
4726 _ = rhs;
4727 }
4728 }
4729 impl RemAssign for Symbolic {
4730 fn rem_assign(&mut self, rhs: Self) {
4731 _ = rhs;
4732 }
4733 }
4734});
4735impl core::ops::Neg for Symbolic {
4736 type Output = Self;
4737
4738 fn neg(self) -> Self {
4739 Self
4740 }
4741}
4742impl core::ops::Neg for &Symbolic {
4743 type Output = Symbolic;
4744
4745 fn neg(self) -> Symbolic {
4746 Symbolic
4747 }
4748}
4749impl num_traits::Zero for Symbolic {
4750 fn zero() -> Self {
4751 Self
4752 }
4753
4754 fn is_zero(&self) -> bool {
4755 true
4756 }
4757}
4758impl num_traits::One for Symbolic {
4759 fn one() -> Self {
4760 Self
4761 }
4762
4763 fn is_one(&self) -> bool {
4764 true
4765 }
4766}
4767impl num_traits::Num for Symbolic {
4768 type FromStrRadixErr = core::convert::Infallible;
4769
4770 fn from_str_radix(_: &str, _: u32) -> Result<Self, Self::FromStrRadixErr> {
4771 Ok(Self)
4772 }
4773}
4774impl Symbolic {
4775 #[inline]
4776 pub fn materialize(len: usize) -> &'static mut [Self] {
4777 unsafe {
4778 core::slice::from_raw_parts_mut(
4779 core::ptr::NonNull::dangling().as_ptr(),
4780 len,
4781 )
4782 }
4783 }
4784}
4785impl RealField for Symbolic {
4786 fn epsilon_impl() -> Self {
4787 Self
4788 }
4789
4790 fn nbits_impl() -> usize {
4791 0
4792 }
4793
4794 fn min_positive_impl() -> Self {
4795 Self
4796 }
4797
4798 fn max_positive_impl() -> Self {
4799 Self
4800 }
4801
4802 fn sqrt_min_positive_impl() -> Self {
4803 Self
4804 }
4805
4806 fn sqrt_max_positive_impl() -> Self {
4807 Self
4808 }
4809}
4810impl ComplexField for Symbolic {
4811 type Arch = pulp::Scalar;
4812 type Index = usize;
4813 type Real = Self;
4814 type SimdCtx<S: pulp::Simd> = S;
4815 type SimdIndex<S: pulp::Simd> = ();
4816 type SimdMask<S: pulp::Simd> = ();
4817 type SimdMemMask<S: pulp::Simd> = ();
4818 type SimdVec<S: pulp::Simd> = ();
4819 type Unit = Self;
4820
4821 const IS_REAL: bool = true;
4822 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Copy;
4823
4824 fn zero_impl() -> Self {
4825 Self
4826 }
4827
4828 fn one_impl() -> Self {
4829 Self
4830 }
4831
4832 fn nan_impl() -> Self {
4833 Self
4834 }
4835
4836 fn infinity_impl() -> Self {
4837 Self
4838 }
4839
4840 fn from_real_impl(_: &Self::Real) -> Self {
4841 Self
4842 }
4843
4844 fn from_f64_impl(_: f64) -> Self {
4845 Self
4846 }
4847
4848 fn real_part_impl(_: &Self) -> Self::Real {
4849 Self
4850 }
4851
4852 fn imag_part_impl(_: &Self) -> Self::Real {
4853 Self
4854 }
4855
4856 fn copy_impl(_: &Self) -> Self {
4857 Self
4858 }
4859
4860 fn conj_impl(_: &Self) -> Self {
4861 Self
4862 }
4863
4864 fn recip_impl(_: &Self) -> Self {
4865 Self
4866 }
4867
4868 fn sqrt_impl(_: &Self) -> Self {
4869 Self
4870 }
4871
4872 fn abs_impl(_: &Self) -> Self::Real {
4873 Self
4874 }
4875
4876 fn abs1_impl(_: &Self) -> Self::Real {
4877 Self
4878 }
4879
4880 fn abs2_impl(_: &Self) -> Self::Real {
4881 Self
4882 }
4883
4884 fn mul_real_impl(_: &Self, _: &Self::Real) -> Self {
4885 Self
4886 }
4887
4888 fn mul_pow2_impl(_: &Self, _: &Self::Real) -> Self {
4889 Self
4890 }
4891
4892 fn is_finite_impl(_: &Self) -> bool {
4893 true
4894 }
4895
4896 fn simd_ctx<S: pulp::Simd>(simd: S) -> Self::SimdCtx<S> {
4897 simd
4898 }
4899
4900 fn ctx_from_simd<S: pulp::Simd>(simd: &Self::SimdCtx<S>) -> S {
4901 *simd
4902 }
4903
4904 fn simd_mem_mask_between<S: pulp::Simd>(
4905 _: &Self::SimdCtx<S>,
4906 _: Self::Index,
4907 _: Self::Index,
4908 ) -> Self::SimdMemMask<S> {
4909 }
4910
4911 unsafe fn simd_mask_load_raw<S: pulp::Simd>(
4912 _: &Self::SimdCtx<S>,
4913 _: Self::SimdMemMask<S>,
4914 _: *const Self::SimdVec<S>,
4915 ) -> Self::SimdVec<S> {
4916 }
4917
4918 unsafe fn simd_mask_store_raw<S: pulp::Simd>(
4919 _: &Self::SimdCtx<S>,
4920 _: Self::SimdMemMask<S>,
4921 _: *mut Self::SimdVec<S>,
4922 _: Self::SimdVec<S>,
4923 ) {
4924 }
4925
4926 fn simd_splat<S: pulp::Simd>(
4927 _: &Self::SimdCtx<S>,
4928 _: &Self,
4929 ) -> Self::SimdVec<S> {
4930 }
4931
4932 fn simd_splat_real<S: pulp::Simd>(
4933 _: &Self::SimdCtx<S>,
4934 _: &Self::Real,
4935 ) -> Self::SimdVec<S> {
4936 }
4937
4938 fn simd_add<S: pulp::Simd>(
4939 _: &Self::SimdCtx<S>,
4940 _: Self::SimdVec<S>,
4941 _: Self::SimdVec<S>,
4942 ) -> Self::SimdVec<S> {
4943 }
4944
4945 fn simd_sub<S: pulp::Simd>(
4946 _: &Self::SimdCtx<S>,
4947 _: Self::SimdVec<S>,
4948 _: Self::SimdVec<S>,
4949 ) -> Self::SimdVec<S> {
4950 }
4951
4952 fn simd_neg<S: pulp::Simd>(
4953 _: &Self::SimdCtx<S>,
4954 _: Self::SimdVec<S>,
4955 ) -> Self::SimdVec<S> {
4956 }
4957
4958 fn simd_conj<S: pulp::Simd>(
4959 _: &Self::SimdCtx<S>,
4960 _: Self::SimdVec<S>,
4961 ) -> Self::SimdVec<S> {
4962 }
4963
4964 fn simd_abs1<S: pulp::Simd>(
4965 _: &Self::SimdCtx<S>,
4966 _: Self::SimdVec<S>,
4967 ) -> Self::SimdVec<S> {
4968 }
4969
4970 fn simd_abs_max<S: pulp::Simd>(
4971 _: &Self::SimdCtx<S>,
4972 _: Self::SimdVec<S>,
4973 ) -> Self::SimdVec<S> {
4974 }
4975
4976 fn simd_mul_real<S: pulp::Simd>(
4977 _: &Self::SimdCtx<S>,
4978 _: Self::SimdVec<S>,
4979 _: Self::SimdVec<S>,
4980 ) -> Self::SimdVec<S> {
4981 }
4982
4983 fn simd_mul_pow2<S: pulp::Simd>(
4984 _: &Self::SimdCtx<S>,
4985 _: Self::SimdVec<S>,
4986 _: Self::SimdVec<S>,
4987 ) -> Self::SimdVec<S> {
4988 }
4989
4990 fn simd_mul<S: pulp::Simd>(
4991 _: &Self::SimdCtx<S>,
4992 _: Self::SimdVec<S>,
4993 _: Self::SimdVec<S>,
4994 ) -> Self::SimdVec<S> {
4995 }
4996
4997 fn simd_conj_mul<S: pulp::Simd>(
4998 _: &Self::SimdCtx<S>,
4999 _: Self::SimdVec<S>,
5000 _: Self::SimdVec<S>,
5001 ) -> Self::SimdVec<S> {
5002 }
5003
5004 fn simd_mul_add<S: pulp::Simd>(
5005 _: &Self::SimdCtx<S>,
5006 _: Self::SimdVec<S>,
5007 _: Self::SimdVec<S>,
5008 _: Self::SimdVec<S>,
5009 ) -> Self::SimdVec<S> {
5010 }
5011
5012 fn simd_conj_mul_add<S: pulp::Simd>(
5013 _: &Self::SimdCtx<S>,
5014 _: Self::SimdVec<S>,
5015 _: Self::SimdVec<S>,
5016 _: Self::SimdVec<S>,
5017 ) -> Self::SimdVec<S> {
5018 }
5019
5020 fn simd_abs2<S: pulp::Simd>(
5021 _: &Self::SimdCtx<S>,
5022 _: Self::SimdVec<S>,
5023 ) -> Self::SimdVec<S> {
5024 }
5025
5026 fn simd_abs2_add<S: pulp::Simd>(
5027 _: &Self::SimdCtx<S>,
5028 _: Self::SimdVec<S>,
5029 _: Self::SimdVec<S>,
5030 ) -> Self::SimdVec<S> {
5031 }
5032
5033 fn simd_reduce_sum<S: pulp::Simd>(
5034 _: &Self::SimdCtx<S>,
5035 _: Self::SimdVec<S>,
5036 ) -> Self {
5037 Self
5038 }
5039
5040 fn simd_reduce_max<S: pulp::Simd>(
5041 _: &Self::SimdCtx<S>,
5042 _: Self::SimdVec<S>,
5043 ) -> Self {
5044 Self
5045 }
5046
5047 fn simd_equal<S: pulp::Simd>(
5048 _: &Self::SimdCtx<S>,
5049 _: Self::SimdVec<S>,
5050 _: Self::SimdVec<S>,
5051 ) -> Self::SimdMask<S> {
5052 }
5053
5054 fn simd_less_than<S: pulp::Simd>(
5055 _: &Self::SimdCtx<S>,
5056 _: Self::SimdVec<S>,
5057 _: Self::SimdVec<S>,
5058 ) -> Self::SimdMask<S> {
5059 }
5060
5061 fn simd_less_than_or_equal<S: pulp::Simd>(
5062 _: &Self::SimdCtx<S>,
5063 _: Self::SimdVec<S>,
5064 _: Self::SimdVec<S>,
5065 ) -> Self::SimdMask<S> {
5066 }
5067
5068 fn simd_greater_than<S: pulp::Simd>(
5069 _: &Self::SimdCtx<S>,
5070 _: Self::SimdVec<S>,
5071 _: Self::SimdVec<S>,
5072 ) -> Self::SimdMask<S> {
5073 }
5074
5075 fn simd_greater_than_or_equal<S: pulp::Simd>(
5076 _: &Self::SimdCtx<S>,
5077 _: Self::SimdVec<S>,
5078 _: Self::SimdVec<S>,
5079 ) -> Self::SimdMask<S> {
5080 }
5081
5082 fn simd_select<S: pulp::Simd>(
5083 _: &Self::SimdCtx<S>,
5084 _: Self::SimdMask<S>,
5085 _: Self::SimdVec<S>,
5086 _: Self::SimdVec<S>,
5087 ) -> Self::SimdVec<S> {
5088 }
5089
5090 fn simd_index_select<S: pulp::Simd>(
5091 _: &Self::SimdCtx<S>,
5092 _: Self::SimdMask<S>,
5093 _: Self::SimdIndex<S>,
5094 _: Self::SimdIndex<S>,
5095 ) -> Self::SimdIndex<S> {
5096 }
5097
5098 fn simd_index_splat<S: pulp::Simd>(
5099 _: &Self::SimdCtx<S>,
5100 _: Self::Index,
5101 ) -> Self::SimdIndex<S> {
5102 }
5103
5104 fn simd_index_add<S: pulp::Simd>(
5105 _: &Self::SimdCtx<S>,
5106 _: Self::SimdIndex<S>,
5107 _: Self::SimdIndex<S>,
5108 ) -> Self::SimdIndex<S> {
5109 }
5110
5111 fn simd_and_mask<S: pulp::Simd>(
5112 _: &Self::SimdCtx<S>,
5113 _: Self::SimdMask<S>,
5114 _: Self::SimdMask<S>,
5115 ) -> Self::SimdMask<S> {
5116 }
5117
5118 fn simd_or_mask<S: pulp::Simd>(
5119 _: &Self::SimdCtx<S>,
5120 _: Self::SimdMask<S>,
5121 _: Self::SimdMask<S>,
5122 ) -> Self::SimdMask<S> {
5123 }
5124
5125 fn simd_not_mask<S: Simd>(
5126 _: &Self::SimdCtx<S>,
5127 _: Self::SimdMask<S>,
5128 ) -> Self::SimdMask<S> {
5129 }
5130
5131 fn simd_first_true_mask<S: pulp::Simd>(
5132 _: &Self::SimdCtx<S>,
5133 _: Self::SimdMask<S>,
5134 ) -> usize {
5135 0
5136 }
5137
5138 fn simd_mask_between<S: Simd>(
5139 _: &Self::SimdCtx<S>,
5140 _: Self::Index,
5141 _: Self::Index,
5142 ) -> Self::SimdMask<S> {
5143 }
5144
5145 fn simd_index_less_than<S: Simd>(
5146 _: &Self::SimdCtx<S>,
5147 _: Self::SimdIndex<S>,
5148 _: Self::SimdIndex<S>,
5149 ) -> Self::SimdMask<S> {
5150 }
5151}
5152pub type c64 = Complex<f64>;
5153pub type c32 = Complex<f32>;
5154pub type fx128 = qd::Quad;
5155pub type cx128 = Complex<fx128>;
5156pub extern crate num_traits;
5157pub extern crate pulp;
5158impl ComplexField for fx128 {
5159 type Arch = pulp::Arch;
5160 type Index = u64;
5161 type Real = Self;
5162 type SimdCtx<S: Simd> = S;
5163 type SimdIndex<S: Simd> = S::u64s;
5164 type SimdMask<S: Simd> = S::m64s;
5165 type SimdMemMask<S: Simd> = Quad<pulp::MemMask<S::m64s>>;
5166 type SimdVec<S: Simd> = Quad<S::f64s>;
5167 type Unit = f64;
5168
5169 const IS_REAL: bool = true;
5170 const SIMD_CAPABILITIES: SimdCapabilities = SimdCapabilities::Simd;
5171
5172 #[inline(always)]
5173 fn zero_impl() -> Self {
5174 Self::ZERO
5175 }
5176
5177 #[inline(always)]
5178 fn one_impl() -> Self {
5179 Quad(1.0, 0.0)
5180 }
5181
5182 #[inline(always)]
5183 fn nan_impl() -> Self {
5184 Self::NAN
5185 }
5186
5187 #[inline(always)]
5188 fn infinity_impl() -> Self {
5189 Self::INFINITY
5190 }
5191
5192 #[inline(always)]
5193 fn from_real_impl(real: &Self::Real) -> Self {
5194 *real
5195 }
5196
5197 #[inline(always)]
5198 fn from_f64_impl(real: f64) -> Self {
5199 real.into()
5200 }
5201
5202 #[inline(always)]
5203 fn real_part_impl(value: &Self) -> Self::Real {
5204 *value
5205 }
5206
5207 #[inline(always)]
5208 fn imag_part_impl(_: &Self) -> Self::Real {
5209 Self::ZERO
5210 }
5211
5212 #[inline(always)]
5213 fn copy_impl(value: &Self) -> Self {
5214 *value
5215 }
5216
5217 #[inline(always)]
5218 fn conj_impl(value: &Self) -> Self {
5219 *value
5220 }
5221
5222 #[inline(always)]
5223 fn recip_impl(value: &Self) -> Self {
5224 if value.0.abs() == f64::INFINITY {
5225 Quad::ZERO
5226 } else {
5227 Quad::from(1.0) / *value
5228 }
5229 }
5230
5231 #[inline(always)]
5232 fn sqrt_impl(value: &Self) -> Self {
5233 value.sqrt()
5234 }
5235
5236 #[inline(always)]
5237 fn abs_impl(value: &Self) -> Self::Real {
5238 value.abs()
5239 }
5240
5241 #[inline(always)]
5242 fn abs1_impl(value: &Self) -> Self::Real {
5243 value.abs()
5244 }
5245
5246 #[inline(always)]
5247 fn abs2_impl(value: &Self) -> Self::Real {
5248 value * value
5249 }
5250
5251 #[inline(always)]
5252 fn mul_real_impl(lhs: &Self, rhs: &Self::Real) -> Self {
5253 lhs * rhs
5254 }
5255
5256 #[inline(always)]
5257 fn mul_pow2_impl(lhs: &Self, rhs: &Self::Real) -> Self {
5258 lhs * rhs
5259 }
5260
5261 #[inline(always)]
5262 fn is_finite_impl(value: &Self) -> bool {
5263 value.0.is_finite() && value.1.is_finite()
5264 }
5265
5266 #[inline(always)]
5267 fn simd_ctx<S: Simd>(simd: S) -> Self::SimdCtx<S> {
5268 simd
5269 }
5270
5271 #[inline(always)]
5272 fn ctx_from_simd<S: Simd>(ctx: &Self::SimdCtx<S>) -> S {
5273 *ctx
5274 }
5275
5276 #[inline(always)]
5277 fn simd_mask_between<S: Simd>(
5278 ctx: &Self::SimdCtx<S>,
5279 start: Self::Index,
5280 end: Self::Index,
5281 ) -> Self::SimdMask<S> {
5282 ctx.mask_between_m64s(start as _, end as _).mask()
5283 }
5284
5285 #[inline(always)]
5286 fn simd_mem_mask_between<S: Simd>(
5287 ctx: &Self::SimdCtx<S>,
5288 start: Self::Index,
5289 end: Self::Index,
5290 ) -> Self::SimdMemMask<S> {
5291 let n = (core::mem::size_of::<Self::SimdVec<S>>()
5292 / core::mem::size_of::<Self>()) as u64;
5293 let start = start * 2;
5294 let end = end * 2;
5295 let mut sa = start.min(n);
5296 let mut ea = end.min(n);
5297 let mut sb = start.max(n) - n;
5298 let mut eb = end.max(n) - n;
5299 if sa == ea {
5300 sa = 0;
5301 ea = 0;
5302 }
5303 if sb == eb {
5304 sb = 0;
5305 eb = 0;
5306 }
5307 let a = f64::simd_mem_mask_between(ctx, sa, ea);
5308 let b = f64::simd_mem_mask_between(ctx, sb, eb);
5309 Quad(a, b)
5310 }
5311
5312 #[inline(always)]
5313 unsafe fn simd_mask_load_raw<S: Simd>(
5314 ctx: &Self::SimdCtx<S>,
5315 mask: Self::SimdMemMask<S>,
5316 ptr: *const Self::SimdVec<S>,
5317 ) -> Self::SimdVec<S> {
5318 unsafe {
5319 Quad(
5320 f64::simd_mask_load_raw(ctx, mask.0, &raw const (*ptr).0),
5321 f64::simd_mask_load_raw(ctx, mask.1, &raw const (*ptr).1),
5322 )
5323 }
5324 }
5325
5326 #[inline(always)]
5327 unsafe fn simd_mask_store_raw<S: Simd>(
5328 ctx: &Self::SimdCtx<S>,
5329 mask: Self::SimdMemMask<S>,
5330 ptr: *mut Self::SimdVec<S>,
5331 values: Self::SimdVec<S>,
5332 ) {
5333 unsafe {
5334 f64::simd_mask_store_raw(ctx, mask.0, &raw mut (*ptr).0, values.0);
5335 f64::simd_mask_store_raw(ctx, mask.1, &raw mut (*ptr).1, values.1);
5336 }
5337 }
5338
5339 #[inline(always)]
5340 fn simd_splat<S: Simd>(
5341 ctx: &Self::SimdCtx<S>,
5342 value: &Self,
5343 ) -> Self::SimdVec<S> {
5344 Quad(ctx.splat_f64s(value.0), ctx.splat_f64s(value.1))
5345 }
5346
5347 #[inline(always)]
5348 fn simd_splat_real<S: Simd>(
5349 ctx: &Self::SimdCtx<S>,
5350 value: &Self::Real,
5351 ) -> Self::SimdVec<S> {
5352 Quad(ctx.splat_f64s(value.0), ctx.splat_f64s(value.1))
5353 }
5354
5355 #[inline(always)]
5356 fn simd_add<S: Simd>(
5357 ctx: &Self::SimdCtx<S>,
5358 lhs: Self::SimdVec<S>,
5359 rhs: Self::SimdVec<S>,
5360 ) -> Self::SimdVec<S> {
5361 qd::simd::add_estimate(*ctx, lhs, rhs)
5362 }
5363
5364 #[inline(always)]
5365 fn simd_sub<S: Simd>(
5366 ctx: &Self::SimdCtx<S>,
5367 lhs: Self::SimdVec<S>,
5368 rhs: Self::SimdVec<S>,
5369 ) -> Self::SimdVec<S> {
5370 qd::simd::sub_estimate(*ctx, lhs, rhs)
5371 }
5372
5373 #[inline(always)]
5374 fn simd_neg<S: Simd>(
5375 ctx: &Self::SimdCtx<S>,
5376 value: Self::SimdVec<S>,
5377 ) -> Self::SimdVec<S> {
5378 qd::simd::neg(*ctx, value)
5379 }
5380
5381 #[inline(always)]
5382 fn simd_conj<S: Simd>(
5383 _: &Self::SimdCtx<S>,
5384 value: Self::SimdVec<S>,
5385 ) -> Self::SimdVec<S> {
5386 value
5387 }
5388
5389 #[inline(always)]
5390 fn simd_abs1<S: Simd>(
5391 ctx: &Self::SimdCtx<S>,
5392 value: Self::SimdVec<S>,
5393 ) -> Self::SimdVec<S> {
5394 qd::simd::abs(*ctx, value)
5395 }
5396
5397 #[inline(always)]
5398 fn simd_abs_max<S: Simd>(
5399 ctx: &Self::SimdCtx<S>,
5400 value: Self::SimdVec<S>,
5401 ) -> Self::SimdVec<S> {
5402 qd::simd::abs(*ctx, value)
5403 }
5404
5405 #[inline(always)]
5406 fn simd_mul_real<S: Simd>(
5407 ctx: &Self::SimdCtx<S>,
5408 lhs: Self::SimdVec<S>,
5409 real_rhs: Self::SimdVec<S>,
5410 ) -> Self::SimdVec<S> {
5411 qd::simd::mul(*ctx, lhs, real_rhs)
5412 }
5413
5414 #[inline(always)]
5415 fn simd_mul_pow2<S: Simd>(
5416 ctx: &Self::SimdCtx<S>,
5417 lhs: Self::SimdVec<S>,
5418 real_rhs: Self::SimdVec<S>,
5419 ) -> Self::SimdVec<S> {
5420 qd::simd::mul(*ctx, lhs, real_rhs)
5421 }
5422
5423 #[inline(always)]
5424 fn simd_mul<S: Simd>(
5425 ctx: &Self::SimdCtx<S>,
5426 lhs: Self::SimdVec<S>,
5427 rhs: Self::SimdVec<S>,
5428 ) -> Self::SimdVec<S> {
5429 qd::simd::mul(*ctx, lhs, rhs)
5430 }
5431
5432 #[inline(always)]
5433 fn simd_conj_mul<S: Simd>(
5434 ctx: &Self::SimdCtx<S>,
5435 lhs: Self::SimdVec<S>,
5436 rhs: Self::SimdVec<S>,
5437 ) -> Self::SimdVec<S> {
5438 qd::simd::mul(*ctx, lhs, rhs)
5439 }
5440
5441 #[inline(always)]
5442 fn simd_mul_add<S: Simd>(
5443 ctx: &Self::SimdCtx<S>,
5444 lhs: Self::SimdVec<S>,
5445 rhs: Self::SimdVec<S>,
5446 acc: Self::SimdVec<S>,
5447 ) -> Self::SimdVec<S> {
5448 qd::simd::add_estimate(*ctx, qd::simd::mul(*ctx, lhs, rhs), acc)
5449 }
5450
5451 #[inline(always)]
5452 fn simd_conj_mul_add<S: Simd>(
5453 ctx: &Self::SimdCtx<S>,
5454 lhs: Self::SimdVec<S>,
5455 rhs: Self::SimdVec<S>,
5456 acc: Self::SimdVec<S>,
5457 ) -> Self::SimdVec<S> {
5458 qd::simd::add_estimate(*ctx, qd::simd::mul(*ctx, lhs, rhs), acc)
5459 }
5460
5461 #[inline(always)]
5462 fn simd_abs2<S: Simd>(
5463 ctx: &Self::SimdCtx<S>,
5464 value: Self::SimdVec<S>,
5465 ) -> Self::SimdVec<S> {
5466 qd::simd::mul(*ctx, value, value)
5467 }
5468
5469 #[inline(always)]
5470 fn simd_abs2_add<S: Simd>(
5471 ctx: &Self::SimdCtx<S>,
5472 value: Self::SimdVec<S>,
5473 acc: Self::SimdVec<S>,
5474 ) -> Self::SimdVec<S> {
5475 qd::simd::add_estimate(*ctx, qd::simd::mul(*ctx, value, value), acc)
5476 }
5477
5478 #[inline(always)]
5479 fn simd_reduce_sum<S: Simd>(
5480 _: &Self::SimdCtx<S>,
5481 value: Self::SimdVec<S>,
5482 ) -> Self {
5483 let a = value.0;
5484 let b = value.1;
5485 let a: &[f64] = bytemuck::cast_slice(core::slice::from_ref(&a));
5486 let b: &[f64] = bytemuck::cast_slice(core::slice::from_ref(&b));
5487 let mut acc = Quad::ZERO;
5488 for (&a, &b) in core::iter::zip(a, b) {
5489 acc += Quad(a, b);
5490 }
5491 acc
5492 }
5493
5494 #[inline(always)]
5495 fn simd_reduce_max<S: Simd>(
5496 _: &Self::SimdCtx<S>,
5497 value: Self::SimdVec<S>,
5498 ) -> Self {
5499 let a = value.0;
5500 let b = value.1;
5501 let a: &[f64] = bytemuck::cast_slice(core::slice::from_ref(&a));
5502 let b: &[f64] = bytemuck::cast_slice(core::slice::from_ref(&b));
5503 let mut acc = Quad::NEG_INFINITY;
5504 for (&a, &b) in core::iter::zip(a, b) {
5505 let val = Quad(a, b);
5506 if val > acc {
5507 acc = val
5508 }
5509 }
5510 acc
5511 }
5512
5513 #[inline(always)]
5514 fn simd_equal<S: Simd>(
5515 ctx: &Self::SimdCtx<S>,
5516 real_lhs: Self::SimdVec<S>,
5517 real_rhs: Self::SimdVec<S>,
5518 ) -> Self::SimdMask<S> {
5519 qd::simd::eq(*ctx, real_lhs, real_rhs)
5520 }
5521
5522 #[inline(always)]
5523 fn simd_less_than<S: Simd>(
5524 ctx: &Self::SimdCtx<S>,
5525 real_lhs: Self::SimdVec<S>,
5526 real_rhs: Self::SimdVec<S>,
5527 ) -> Self::SimdMask<S> {
5528 qd::simd::less_than(*ctx, real_lhs, real_rhs)
5529 }
5530
5531 #[inline(always)]
5532 fn simd_less_than_or_equal<S: Simd>(
5533 ctx: &Self::SimdCtx<S>,
5534 real_lhs: Self::SimdVec<S>,
5535 real_rhs: Self::SimdVec<S>,
5536 ) -> Self::SimdMask<S> {
5537 qd::simd::less_than_or_equal(*ctx, real_lhs, real_rhs)
5538 }
5539
5540 #[inline(always)]
5541 fn simd_greater_than<S: Simd>(
5542 ctx: &Self::SimdCtx<S>,
5543 real_lhs: Self::SimdVec<S>,
5544 real_rhs: Self::SimdVec<S>,
5545 ) -> Self::SimdMask<S> {
5546 qd::simd::greater_than(*ctx, real_lhs, real_rhs)
5547 }
5548
5549 #[inline(always)]
5550 fn simd_greater_than_or_equal<S: Simd>(
5551 ctx: &Self::SimdCtx<S>,
5552 real_lhs: Self::SimdVec<S>,
5553 real_rhs: Self::SimdVec<S>,
5554 ) -> Self::SimdMask<S> {
5555 qd::simd::greater_than_or_equal(*ctx, real_lhs, real_rhs)
5556 }
5557
5558 #[inline(always)]
5559 fn simd_select<S: Simd>(
5560 ctx: &Self::SimdCtx<S>,
5561 mask: Self::SimdMask<S>,
5562 lhs: Self::SimdVec<S>,
5563 rhs: Self::SimdVec<S>,
5564 ) -> Self::SimdVec<S> {
5565 Quad(
5566 ctx.select_f64s(mask, lhs.0, rhs.0),
5567 ctx.select_f64s(mask, lhs.1, rhs.1),
5568 )
5569 }
5570
5571 #[inline(always)]
5572 fn simd_index_select<S: Simd>(
5573 ctx: &Self::SimdCtx<S>,
5574 mask: Self::SimdMask<S>,
5575 lhs: Self::SimdIndex<S>,
5576 rhs: Self::SimdIndex<S>,
5577 ) -> Self::SimdIndex<S> {
5578 ctx.select_u64s(mask, lhs, rhs)
5579 }
5580
5581 #[inline(always)]
5582 fn simd_index_splat<S: Simd>(
5583 ctx: &Self::SimdCtx<S>,
5584 value: Self::Index,
5585 ) -> Self::SimdIndex<S> {
5586 ctx.splat_u64s(value)
5587 }
5588
5589 #[inline(always)]
5590 fn simd_index_add<S: Simd>(
5591 ctx: &Self::SimdCtx<S>,
5592 lhs: Self::SimdIndex<S>,
5593 rhs: Self::SimdIndex<S>,
5594 ) -> Self::SimdIndex<S> {
5595 ctx.add_u64s(lhs, rhs)
5596 }
5597
5598 #[inline(always)]
5599 fn simd_index_less_than<S: Simd>(
5600 ctx: &Self::SimdCtx<S>,
5601 lhs: Self::SimdIndex<S>,
5602 rhs: Self::SimdIndex<S>,
5603 ) -> Self::SimdMask<S> {
5604 ctx.less_than_u64s(lhs, rhs)
5605 }
5606
5607 #[inline(always)]
5608 fn simd_and_mask<S: Simd>(
5609 ctx: &Self::SimdCtx<S>,
5610 lhs: Self::SimdMask<S>,
5611 rhs: Self::SimdMask<S>,
5612 ) -> Self::SimdMask<S> {
5613 ctx.and_m64s(lhs, rhs)
5614 }
5615
5616 #[inline(always)]
5617 fn simd_or_mask<S: Simd>(
5618 ctx: &Self::SimdCtx<S>,
5619 lhs: Self::SimdMask<S>,
5620 rhs: Self::SimdMask<S>,
5621 ) -> Self::SimdMask<S> {
5622 ctx.or_m64s(lhs, rhs)
5623 }
5624
5625 #[inline(always)]
5626 fn simd_not_mask<S: Simd>(
5627 ctx: &Self::SimdCtx<S>,
5628 mask: Self::SimdMask<S>,
5629 ) -> Self::SimdMask<S> {
5630 ctx.not_m64s(mask)
5631 }
5632
5633 #[inline(always)]
5634 fn simd_first_true_mask<S: Simd>(
5635 ctx: &Self::SimdCtx<S>,
5636 value: Self::SimdMask<S>,
5637 ) -> usize {
5638 ctx.first_true_m64s(value)
5639 }
5640}
5641impl RealField for fx128 {
5642 #[inline(always)]
5643 fn epsilon_impl() -> Self {
5644 let mut x = Quad::EPSILON;
5645 x.0 *= 8.0;
5646 x.1 *= 8.0;
5647 x
5648 }
5649
5650 #[inline(always)]
5651 fn nbits_impl() -> usize {
5652 100
5653 }
5654
5655 #[inline(always)]
5656 fn min_positive_impl() -> Self {
5657 Quad::MIN_POSITIVE
5658 }
5659
5660 #[inline(always)]
5661 fn max_positive_impl() -> Self {
5662 Quad::MIN_POSITIVE.recip()
5663 }
5664
5665 #[inline(always)]
5666 fn sqrt_min_positive_impl() -> Self {
5667 Quad::MIN_POSITIVE.sqrt()
5668 }
5669
5670 #[inline(always)]
5671 fn sqrt_max_positive_impl() -> Self {
5672 Quad::MIN_POSITIVE.recip().sqrt()
5673 }
5674}
5675pub mod ext {
5676 use super::*;
5677 pub use super::{ComplexField, RealField};
5678 pub trait ComplexFieldExt: ComplexField {
5679 #[inline(always)]
5680 #[must_use]
5681 fn nbits() -> usize {
5682 Self::Real::nbits_impl()
5683 }
5684 #[inline(always)]
5685 #[must_use]
5686 fn zero() -> Self {
5687 Self::zero_impl()
5688 }
5689 #[inline(always)]
5690 #[must_use]
5691 fn one() -> Self {
5692 Self::one_impl()
5693 }
5694 #[inline(always)]
5695 #[must_use]
5696 fn nan() -> Self {
5697 Self::nan_impl()
5698 }
5699 #[inline(always)]
5700 #[must_use]
5701 fn infinity() -> Self {
5702 Self::infinity_impl()
5703 }
5704 #[inline(always)]
5705 #[must_use]
5706 fn as_real(&self) -> Self {
5707 Self::from_real_impl(&Self::real_part_impl(self))
5708 }
5709 #[inline(always)]
5710 #[must_use]
5711 fn real(&self) -> Self::Real {
5712 Self::real_part_impl(self)
5713 }
5714 #[inline(always)]
5715 #[must_use]
5716 fn imag(&self) -> Self::Real {
5717 Self::imag_part_impl(self)
5718 }
5719 #[inline(always)]
5720 #[must_use]
5721 fn copy(&self) -> Self {
5722 Self::copy_impl(self)
5723 }
5724 #[inline(always)]
5725 #[must_use]
5726 fn conj(&self) -> Self {
5727 Self::conj_impl(self)
5728 }
5729 #[inline(always)]
5730 #[must_use]
5731 fn mul_real(&self, rhs: impl ByRef<Self::Real>) -> Self {
5732 Self::mul_real_impl(self, rhs.by_ref())
5733 }
5734 #[inline(always)]
5735 #[must_use]
5736 fn mul_pow2(&self, rhs: impl ByRef<Self::Real>) -> Self {
5737 Self::mul_real_impl(self, rhs.by_ref())
5738 }
5739 #[inline(always)]
5740 #[must_use]
5741 fn abs1(&self) -> Self::Real {
5742 Self::abs1_impl(self)
5743 }
5744 #[inline(always)]
5745 #[must_use]
5746 fn absmax(&self) -> Self::Real {
5747 if const { Self::IS_REAL } {
5748 Self::abs1_impl(self)
5749 } else {
5750 &Self::Real::abs1_impl(&real(self))
5751 + &Self::Real::abs1_impl(&imag(self))
5752 }
5753 }
5754 #[inline(always)]
5755 #[must_use]
5756 fn abs(&self) -> Self::Real {
5757 Self::abs_impl(self)
5758 }
5759 #[inline(always)]
5760 #[must_use]
5761 fn abs2(&self) -> Self::Real {
5762 Self::abs2_impl(self)
5763 }
5764 #[inline(always)]
5765 #[must_use]
5766 fn is_nan(&self) -> bool {
5767 Self::is_nan_impl(self)
5768 }
5769 #[inline(always)]
5770 #[must_use]
5771 fn is_finite(&self) -> bool {
5772 Self::is_finite_impl(self)
5773 }
5774 #[inline(always)]
5775 #[must_use]
5776 fn sqrt(&self) -> Self {
5777 Self::sqrt_impl(self)
5778 }
5779 #[inline(always)]
5780 #[must_use]
5781 fn recip(&self) -> Self {
5782 Self::recip_impl(self)
5783 }
5784 #[inline(always)]
5785 #[must_use]
5786 fn from_f64(value: f64) -> Self {
5787 Self::from_f64_impl(value)
5788 }
5789 }
5790 pub trait RealFieldExt: RealField {
5791 #[inline(always)]
5792 #[must_use]
5793 fn eps<T: RealField>() -> T {
5794 T::Real::epsilon_impl()
5795 }
5796 #[inline(always)]
5797 #[must_use]
5798 fn nbits<T: ComplexField>() -> usize {
5799 T::Real::nbits_impl()
5800 }
5801 #[inline(always)]
5802 #[must_use]
5803 fn min_positive<T: RealField>() -> T {
5804 T::min_positive_impl()
5805 }
5806 #[inline(always)]
5807 #[must_use]
5808 fn max_positive<T: RealField>() -> T {
5809 T::max_positive_impl()
5810 }
5811 #[inline(always)]
5812 #[must_use]
5813 fn sqrt_min_positive<T: RealField>() -> T {
5814 T::sqrt_min_positive_impl()
5815 }
5816 #[inline(always)]
5817 #[must_use]
5818 fn sqrt_max_positive<T: RealField>() -> T {
5819 T::sqrt_max_positive_impl()
5820 }
5821 #[inline(always)]
5822 #[must_use]
5823 fn to_cplx<T: ComplexField<Real = Self>>(&self) -> T {
5824 T::from_real_impl(self)
5825 }
5826 #[inline(always)]
5827 #[must_use]
5828 fn fmax(&self, rhs: impl ByRef<Self>) -> Self {
5829 math_utils::max(self, rhs.by_ref())
5830 }
5831 #[inline(always)]
5832 #[must_use]
5833 fn fmin(&self, rhs: impl ByRef<Self>) -> Self {
5834 math_utils::min(self, rhs.by_ref())
5835 }
5836 #[inline(always)]
5837 #[must_use]
5838 fn hypot(&self, rhs: impl ByRef<Self>) -> Self {
5839 math_utils::hypot(self, rhs.by_ref())
5840 }
5841 }
5842 impl<T: ComplexField> ComplexFieldExt for T {}
5843 impl<T: RealField> RealFieldExt for T {}
5844}
5845#[cfg(test)]
5846mod tests {
5847 use super::*;
5848 #[test]
5851 fn recip_of_subnormal() {
5852 let (re, im) = recip_impl(0.0f64, f64::MIN_POSITIVE / 2.0);
5853 assert_eq!((re, im), (0.0, -2.0 / f64::MIN_POSITIVE));
5854 let (re, im) = recip_impl(f64::MIN_POSITIVE / 4.0, 0.0);
5855 assert_eq!((re, im), (4.0 / f64::MIN_POSITIVE, 0.0));
5856 let (re, im) = recip_impl(0.0f32, f32::MIN_POSITIVE / 2.0);
5857 assert_eq!((re, im), (0.0, -2.0 / f32::MIN_POSITIVE));
5858 let (re, _) = recip_impl(f64::from_bits(1), 0.0);
5860 assert_eq!(re, f64::INFINITY);
5861 }
5862
5863 #[test]
5867 fn complex_abs_max_propagates_nan() {
5868 struct AbsMax {
5869 re: f64,
5870 im: f64,
5871 }
5872 impl pulp::WithSimd for AbsMax {
5873 type Output = f64;
5874 #[inline(always)]
5875 fn with_simd<S: pulp::Simd>(self, simd: S) -> f64 {
5876 let ctx = <Complex<f64> as ComplexField>::simd_ctx(simd);
5877 let ctx = SimdCtx::<Complex<f64>, S>::new(&ctx);
5878 let value = ctx.splat(Complex {
5879 re: self.re,
5880 im: self.im,
5881 });
5882 ctx.reduce_max_real(ctx.abs_max(value))
5883 }
5884 }
5885 let arch = pulp::Arch::new();
5886 let abs_max = |re, im| arch.dispatch(AbsMax { re, im });
5887 assert_eq!(abs_max(1.0, 2.0), 2.0);
5888 assert_eq!(abs_max(-3.0, 2.0), 3.0);
5889 assert_eq!(abs_max(-3.0, -4.0), 4.0);
5890 assert!(abs_max(f64::NAN, 1.0).is_nan());
5891 assert!(abs_max(1.0, f64::NAN).is_nan());
5892 assert!(abs_max(f64::NAN, f64::NAN).is_nan());
5893 }
5894
5895 macro_rules! assert_abs_is_hypot {
5898 ($t:ty, $cases:expr) => {
5899 for (re, im) in $cases {
5900 let expected = <$t>::hypot(re, im).to_bits();
5901 let z = Complex::<$t> { re, im };
5902 assert_eq!(abs(&z).to_bits(), expected, "abs({re:e}, {im:e})");
5903 let z = ComplexImpl(z);
5904 assert_eq!(abs(&z).to_bits(), expected, "abs({re:e}, {im:e})");
5905 assert_eq!(
5906 hypot(&re, &im).to_bits(),
5907 expected,
5908 "hypot({re:e}, {im:e})"
5909 );
5910 }
5911 };
5912 }
5913
5914 #[cfg_attr(miri, ignore)]
5919 #[test]
5920 fn abs_of_subnormal_complex() {
5921 let s = f32::from_bits;
5922 let d = f64::from_bits;
5923 assert_abs_is_hypot!(
5924 f32,
5925 [
5926 (0.0, s(1 << 19)),
5928 (0.0, s(1 << 9)),
5929 (-0.0, -s(1)),
5930 (s(3), s(4)),
5932 (s(3 * 2_372_667), s(4 * 2_372_667)),
5935 (-s(3 << 12), s(4 << 12)),
5937 (s(3 * 1_900_000), s(4 * 1_900_000)),
5938 ]
5939 );
5940 assert_abs_is_hypot!(
5941 f64,
5942 [
5943 (0.0, d(1 << 44)),
5944 (0.0, d(1 << 4)),
5945 (d(3), -d(4)),
5946 (d(3 << 50), d(4 << 50)),
5949 (d(3 * 1_000_000_000_000_000), d(4 * 1_000_000_000_000_000)),
5950 ]
5951 );
5952 }
5953
5954 #[cfg_attr(miri, ignore)]
5959 #[test]
5960 fn abs_near_max_positive() {
5961 assert_abs_is_hypot!(
5962 f32,
5963 [
5964 (f32::MAX, 0.0),
5965 (0.0, -f32::MAX),
5966 (3.0 * 2.0f32.powi(125), 4.0 * 2.0f32.powi(125)),
5967 ]
5968 );
5969 assert_abs_is_hypot!(
5970 f64,
5971 [
5972 (f64::MAX, 0.0),
5973 (-3.0 * 2.0f64.powi(1021), 4.0 * 2.0f64.powi(1021)),
5974 ]
5975 );
5976 assert_eq!(
5977 abs(&Complex {
5978 re: f64::MAX,
5979 im: f64::MAX
5980 }),
5981 f64::INFINITY
5982 );
5983 }
5984
5985 #[test]
5987 fn abs_of_zero_inf_nan() {
5988 assert_eq!(
5989 abs(&Complex {
5990 re: 0.0f64,
5991 im: -0.0
5992 })
5993 .to_bits(),
5994 0
5995 );
5996 assert_eq!(
5997 abs(&Complex {
5998 re: f32::INFINITY,
5999 im: 1.0
6000 }),
6001 f32::INFINITY
6002 );
6003 assert_eq!(
6004 abs(&Complex {
6005 re: 1.0,
6006 im: f64::NEG_INFINITY
6007 }),
6008 f64::INFINITY
6009 );
6010 assert!(
6011 abs(&Complex {
6012 re: f64::NAN,
6013 im: 1e-320
6014 })
6015 .is_nan()
6016 );
6017 assert!(
6018 abs(&Complex {
6019 re: f32::MAX,
6020 im: f32::NAN
6021 })
6022 .is_nan()
6023 );
6024 }
6025
6026 #[cfg_attr(miri, ignore)]
6031 #[test]
6032 fn sqrt_of_subnormal_complex() {
6033 let s = 2.0f32.powi(-74);
6034 let z = Complex {
6035 re: 3.0 * s * s,
6036 im: 4.0 * s * s,
6037 };
6038 assert_eq!(sqrt(&z), Complex { re: 2.0 * s, im: s });
6039 let s = 2.0f64.powi(-537);
6040 let z = Complex {
6041 re: 3.0 * s * s,
6042 im: -4.0 * s * s,
6043 };
6044 assert_eq!(
6045 sqrt(&z),
6046 Complex {
6047 re: 2.0 * s,
6048 im: -s
6049 }
6050 );
6051 }
6052}