From 9bd9e56c1f82bc91579a150179188ff779125df3 Mon Sep 17 00:00:00 2001 From: Mark Nefedov Date: Fri, 14 Aug 2026 23:41:28 +0300 Subject: [PATCH] Use paired sin_cos in the complex from_polar helpers complex_from_polar and the three SIMD backends' simd_complex_from_polar called sine and cosine separately on the same angle, performing the argument reduction twice. They now call sin_cos / simd_sin_cos once and use both outputs. This feeds complex exp, sqrt, powf and powc. --- src/scalar/complex.rs | 3 ++- src/simd/auto_simd_impl.rs | 3 ++- src/simd/portable_simd_impl.rs | 3 ++- src/simd/wide_simd_impl.rs | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/scalar/complex.rs b/src/scalar/complex.rs index 4c58ea3..df2fb22 100644 --- a/src/scalar/complex.rs +++ b/src/scalar/complex.rs @@ -1455,5 +1455,6 @@ impl ComplexField for num_complex::Complex { #[inline] fn complex_from_polar(r: N, theta: N) -> num_complex::Complex { - num_complex::Complex::new(r.clone() * theta.clone().cos(), r * theta.sin()) + let (sin, cos) = theta.sin_cos(); + num_complex::Complex::new(r.clone() * cos, r * sin) } diff --git a/src/simd/auto_simd_impl.rs b/src/simd/auto_simd_impl.rs index 9526d08..108d58f 100644 --- a/src/simd/auto_simd_impl.rs +++ b/src/simd/auto_simd_impl.rs @@ -1479,7 +1479,8 @@ macro_rules! impl_float_simd ( #[inline] fn simd_complex_from_polar(r: N, theta: N) -> num_complex::Complex { - num_complex::Complex::new(r.clone() * theta.clone().simd_cos(), r * theta.simd_sin()) + let (sin, cos) = theta.simd_sin_cos(); + num_complex::Complex::new(r.clone() * cos, r * sin) } impl_float_simd!( diff --git a/src/simd/portable_simd_impl.rs b/src/simd/portable_simd_impl.rs index 68ad1ac..c64e508 100644 --- a/src/simd/portable_simd_impl.rs +++ b/src/simd/portable_simd_impl.rs @@ -1501,7 +1501,8 @@ macro_rules! impl_float_simd ( #[inline] fn simd_complex_from_polar(r: N, theta: N) -> num_complex::Complex { - num_complex::Complex::new(r.clone() * theta.clone().simd_cos(), r * theta.simd_sin()) + let (sin, cos) = theta.simd_sin_cos(); + num_complex::Complex::new(r.clone() * cos, r * sin) } impl_float_simd!( diff --git a/src/simd/wide_simd_impl.rs b/src/simd/wide_simd_impl.rs index 70f042d..5c430a3 100644 --- a/src/simd/wide_simd_impl.rs +++ b/src/simd/wide_simd_impl.rs @@ -1584,5 +1584,6 @@ impl_wide_f32!(f32, f32x8, WideF32x8, WideBoolF32x8, 8; 1, 2, 3, 4, 5, 6, 7); #[inline] fn simd_complex_from_polar(r: N, theta: N) -> num_complex::Complex { - num_complex::Complex::new(r.clone() * theta.clone().simd_cos(), r * theta.simd_sin()) + let (sin, cos) = theta.simd_sin_cos(); + num_complex::Complex::new(r.clone() * cos, r * sin) }