Skip to content

Commit 56a43a8

Browse files
committed
ENHL: Add SIMD implementation for deg2rad
1 parent 5ae53e9 commit 56a43a8

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

numpy/core/src/_simd/_simd.dispatch.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ SIMD_IMPL_INTRIN_1(sumup_@sfx@, @esfx@, v@sfx@)
381381
***************************/
382382
#if @fp_only@
383383
/**begin repeat1
384-
* #intrin = sqrt, recip, abs, square#
384+
* #intrin = sqrt, recip, abs, square, deg2rad#
385385
*/
386386
SIMD_IMPL_INTRIN_1(@intrin@_@sfx@, v@sfx@, v@sfx@)
387387
/**end repeat1**/
@@ -615,7 +615,7 @@ SIMD_INTRIN_DEF(sumup_@sfx@)
615615
***************************/
616616
#if @fp_only@
617617
/**begin repeat1
618-
* #intrin = sqrt, recip, abs, square#
618+
* #intrin = sqrt, recip, abs, square, deg2rad#
619619
*/
620620
SIMD_INTRIN_DEF(@intrin@_@sfx@)
621621
/**end repeat1**/

numpy/core/src/common/simd/avx2/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b)
105105
return _mm256_blendv_epi8(a, b, _mm256_cmpgt_epi64(a, b));
106106
}
107107

108+
// deg2rad
109+
NPY_FINLINE npyv_f32 npyv_deg2rad_f32(npyv_f32 a)
110+
{ return _mm256_div_ps(_mm256_mul_ps(a, _mm256_set1_ps(3.1415926535897931)), _mm256_set1_ps(180)); }
111+
NPY_FINLINE npyv_f64 npyv_deg2rad_f64(npyv_f64 a)
112+
{ return _mm256_div_pd(_mm256_mul_pd(a, _mm256_set1_pd(3.1415926535897931)), _mm256_set1_pd(180)); }
113+
108114
#endif // _NPY_SIMD_AVX2_MATH_H

numpy/core/src/common/simd/avx512/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,10 @@ NPY_FINLINE npyv_f64 npyv_minp_f64(npyv_f64 a, npyv_f64 b)
112112
#define npyv_min_u64 _mm512_min_epu64
113113
#define npyv_min_s64 _mm512_min_epi64
114114

115+
// deg2rad
116+
NPY_FINLINE npyv_f32 npyv_deg2rad_f32(npyv_f32 a)
117+
{ return _mm512_div_ps(_mm512_mul_ps(a, _mm512_set1_ps(3.1415926535897931)), _mm512_set1_ps(180)); }
118+
NPY_FINLINE npyv_f64 npyv_deg2rad_f64(npyv_f64 a)
119+
{ return _mm512_div_pd(_mm512_mul_pd(a, _mm512_set1_pd(3.1415926535897931)), _mm512_set1_pd(180)); }
120+
115121
#endif // _NPY_SIMD_AVX512_MATH_H

numpy/core/src/common/simd/neon/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,10 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b)
153153
return vbslq_s64(npyv_cmplt_s64(a, b), a, b);
154154
}
155155

156+
// deg2rad
157+
NPY_FINLINE npyv_f32 npyv_deg2rad_f32(npyv_f32 a)
158+
{ return npyv_div_f32(npyv_mul_f32(a, vdupq_n_f32(3.1415926535897931)), vdupq_n_f32(180)); }
159+
NPY_FINLINE npyv_f64 npyv_deg2rad_f64(npyv_f64 a)
160+
{ return npyv_div_f64(npyv_mul_f64(a, vdupq_n_f64(3.1415926535897931)), vdupq_n_f64(180)); }
161+
156162
#endif // _NPY_SIMD_NEON_MATH_H

numpy/core/src/common/simd/sse/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,10 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b)
143143
return npyv_select_s64(npyv_cmplt_s64(a, b), a, b);
144144
}
145145

146+
// deg2rad
147+
NPY_FINLINE npyv_f32 npyv_deg2rad_f32(npyv_f32 a)
148+
{ return _mm_div_ps(_mm_mul_ps(a, _mm_set1_ps(3.1415926535897931)), _mm_set1_ps(180)); }
149+
NPY_FINLINE npyv_f64 npyv_deg2rad_f64(npyv_f64 a)
150+
{ return _mm_div_pd(_mm_mul_pd(a, _mm_set1_pd(3.1415926535897931)), _mm_set1_pd(180)); }
151+
146152
#endif // _NPY_SIMD_SSE_MATH_H

numpy/core/src/common/simd/vsx/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ NPY_FINLINE npyv_f64 npyv_square_f64(npyv_f64 a)
6969
#define npyv_min_u64 vec_min
7070
#define npyv_min_s64 vec_min
7171

72+
// deg2rad
73+
NPY_FINLINE npyv_f32 npyv_deg2rad_f32(npyv_f32 a)
74+
{ return npyv_div_f32(npyv_mul_f32(a, npyv_setall_f32(3.1415926535897931)), npyv_setall_f32(180)); }
75+
NPY_FINLINE npyv_f64 npyv_deg2rad_f64(npyv_f64 a)
76+
{ return npyv_div_f64(npyv_mul_f64(a, npyv_setall_f64(3.1415926535897931)), npyv_setall_f64(180)); }
77+
7278
#endif // _NPY_SIMD_VSX_MATH_H

numpy/core/tests/test_simd.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,22 @@ def test_special_cases(self):
415415
nnan = self.notnan(self.setall(self._nan()))
416416
assert nnan == [0]*self.nlanes
417417

418+
def test_deg2rad(self):
419+
pinf, ninf, nan = self._pinfinity(), self._ninfinity(), self._nan()
420+
data = self._data()
421+
vdata = self.load(self._data())
422+
423+
deg2rad_cases = ((-0.0, -0.0), (0.0, 0.0), (nan, nan),
424+
(pinf, pinf), (ninf, ninf))
425+
for case, desired in deg2rad_cases:
426+
data_deg2rad = [desired]*self.nlanes
427+
deg2rad = self.deg2rad(self.setall(case))
428+
assert deg2rad == pytest.approx(data_deg2rad, nan_ok=True)
429+
430+
data_deg2rad = self.load([x*math.pi/180 for x in data])
431+
deg2rad = self.deg2rad(vdata)
432+
assert deg2rad == pytest.approx(data_deg2rad)
433+
418434
class _SIMD_ALL(_Test_Utility):
419435
"""
420436
To test all vector types at once

0 commit comments

Comments
 (0)