VecCore 0.8.1
C++ Library for Portable SIMD Vectorization
Loading...
Searching...
No Matches
VecMath.h
Go to the documentation of this file.
1#ifndef VECCORE_MATH_H
2#define VECCORE_MATH_H
3
4#ifdef _MSC_VER
5#define _USE_MATH_DEFINES
6#include <math.h>
7#endif
8
9#include <cmath>
10
11#if defined(__APPLE__) && !defined(__NVCC__)
13void sincosf(const float &x, float *s, float *c)
14{
15 __sincosf(x, s, c);
16}
17
19void sincos(const double &x, double *s, double *c)
20{
21 __sincos(x, s, c);
22}
23#elif defined(_MSC_VER)
25void sincosf(const float &x, float *s, float *c)
26{
27 *s = sinf(x);
28 *c = cosf(x);
29}
30
32void sincos(const double &x, double *s, double *c)
33{
34 *s = sin(x);
35 *c = cos(x);
36}
37#endif
38
39namespace vecCore {
40inline namespace math {
41
42#define VECCORE_MATH_UNARY_FUNCTION(F, f) \
43template <typename T> \
44VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE \
45T F(const T &x) \
46{ \
47 T ret(x); \
48 for(size_t i = 0; i < VectorSize<T>(); ++i) \
49 Set(ret, i, std::f(Get(x,i))); \
50 return ret; \
51} \
52
53#define VECCORE_MATH_BINARY_FUNCTION(F, f) \
54template <typename T> \
55VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE \
56T F(const T &x, const T &y) \
57{ \
58 T ret(x); \
59 for(size_t i = 0; i < VectorSize<T>(); ++i) \
60 Set(ret, i, std::f(Get(x,i), Get(y,i))); \
61 return ret; \
62} \
63
64// Abs, Min, Max, Sign
65
67
68template <class T>
71T Min(const T &a, const T &b)
72{
73 return Blend(a < b, a, b);
74}
75
76template <class T>
79T Max(const T &a, const T &b)
80{
81 return Blend(a > b, a, b);
82}
83
84template <typename T>
87T Min(const T &a, const T &b, const T &c)
88{
89 return Min(a, Min(b, c));
90}
91
92template <typename T>
95T Max(const T &a, const T &b, const T &c)
96{
97 return Max(a, Max(b, c));
98}
99
100template <typename T, template <typename> class Wrapper>
103Wrapper<T> Min(const Wrapper<T> &a, const Wrapper<T> &b)
104{
105 return Blend(a < b, a, b);
106}
107
108template <typename T, template <typename> class Wrapper>
111Wrapper<T> Max(const Wrapper<T> &a, const Wrapper<T> &b)
112{
113 return Blend(a > b, a, b);
114}
115
117
118template <typename T>
121T Sign(const T &x)
122{
123 return CopySign(T(1), x);
124}
125
126// Trigonometric Functions
127
131
136
137template <typename T>
140void SinCos(const T &x, T *s, T *c)
141{
142 sincos(x, s, c);
143}
144
145template <>
148void SinCos(const float &x, float *s, float *c)
149{
150 ::sincosf(x, s, c);
151}
152
153template <>
156void SinCos(const double &x, double *s, double *c)
157{
158 ::sincos(x, s, c);
159}
160
161// Hyperbolic Functions
162
166
167VECCORE_MATH_UNARY_FUNCTION(ASinh, asinh)
168VECCORE_MATH_UNARY_FUNCTION(ACosh, acosh)
169VECCORE_MATH_UNARY_FUNCTION(ATanh, atanh)
170
171// Exponential and Logarithmic Functions
172
175VECCORE_MATH_UNARY_FUNCTION(Expm1, expm1)
179VECCORE_MATH_UNARY_FUNCTION(Log10, log10)
180VECCORE_MATH_UNARY_FUNCTION(Log1p, log1p)
181VECCORE_MATH_UNARY_FUNCTION(Ilogb, ilogb)
182
183// Gamma Functions
184VECCORE_MATH_UNARY_FUNCTION(TGamma, tgamma)
185
186template <typename T>
189T Frexp(const T &x, int *exp)
190{
191 T ret;
192 for(size_t i = 0; i < VectorSize<T>(); ++i)
193 Set(ret, i, std::frexp(Get(x,i), &exp[i]));
194 return ret;
195}
196
197template <typename T>
200T Ldexp(const T &x, int exp)
201{
202 T ret;
203 for(size_t i = 0; i < VectorSize<T>(); ++i)
204 Set(ret, i, std::ldexp(Get(x,i), exp));
205 return ret;
206}
207
208template <typename T>
211T Modf(const T &x, T *intpart)
212{
213 T ret;
214 for(size_t i = 0; i < VectorSize<T>(); ++i)
215 Set(ret, i, std::modf(Get(x,i), &intpart[i]));
216 return ret;
217}
218
219template <typename T>
222T Rsqrt(const T &x)
223{
224 T ret(1.0 / x);
225 for(size_t i = 0; i < VectorSize<T>(); ++i)
226 Set(ret, i, std::sqrt(Get(ret,i)));
227 return ret;
228}
229
230template <typename T>
233T Scalbn(const T &x, int n)
234{
235 T ret;
236 for(size_t i = 0; i < VectorSize<T>(); ++i)
237 Set(ret, i, std::scalbn(Get(x,i), n));
238 return ret;
239}
240
241template <typename T>
244T Scalbln(const T &x, long int n)
245{
246 T ret;
247 for(size_t i = 0; i < VectorSize<T>(); ++i)
248 Set(ret, i, std::scalbln(Get(x,i), n));
249 return ret;
250}
251
252// Power Functions
253
258
259// Rounding and Remainder Functions
260
262VECCORE_MATH_UNARY_FUNCTION(Floor, floor)
264VECCORE_MATH_UNARY_FUNCTION(Round, round)
265VECCORE_MATH_UNARY_FUNCTION(Trunc, trunc)
266
267// Miscellaneous Utilities
268
269template <typename T>
272Mask<T> IsInf(const T &x)
273{
274#ifndef VECCORE_CUDA
275 using std::isinf;
276#endif
277 return isinf(x);
278}
279
280template <typename T>
284{
285 return x < T(0);
286}
287
288template <typename T>
291constexpr T Deg(const T &x)
292{
293 return (x * T(180.0 / M_PI));
294}
295
296template <typename T>
299constexpr T Rad(const T &x)
300{
301 return (x * T(M_PI / 180.0));
302}
303
304#undef VECCORE_MATH_UNARY_FUNCTION
305#undef VECCORE_MATH_BINARY_FUNCTION
306
307} // namespace math
308} // namespace vecCore
309
310#endif
#define VECCORE_ATT_HOST_DEVICE
Definition: CUDA.h:10
#define VECCORE_FORCE_INLINE
Definition: Common.h:32
#define VECCORE_MATH_UNARY_FUNCTION(F, f)
Definition: VecMath.h:42
#define VECCORE_MATH_BINARY_FUNCTION(F, f)
Definition: VecMath.h:53
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE constexpr T Rad(const T &x)
Definition: VecMath.h:299
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Sign(const T &x)
Definition: VecMath.h:121
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE constexpr T Deg(const T &x)
Definition: VecMath.h:291
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Mask< T > IsNegative(const T &x)
Definition: VecMath.h:283
VECCORE_FORCE_INLINE void SinCos(const UME::SIMD::SIMDVec_f< T, N > &x, UME::SIMD::SIMDVec_f< T, N > *s, UME::SIMD::SIMDVec_f< T, N > *c)
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Max(const T &a, const T &b)
Definition: VecMath.h:79
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Scalbn(const T &x, int n)
Definition: VecMath.h:233
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Scalbln(const T &x, long int n)
Definition: VecMath.h:244
VECCORE_FORCE_INLINE UME::SIMD::SIMDVecMask< N > IsInf(const UME::SIMD::SIMDVec_f< T, N > &x)
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Frexp(const T &x, int *exp)
Definition: VecMath.h:189
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Modf(const T &x, T *intpart)
Definition: VecMath.h:211
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Ldexp(const T &x, int exp)
Definition: VecMath.h:200
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Rsqrt(const T &x)
Definition: VecMath.h:222
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Min(const T &a, const T &b)
Definition: VecMath.h:71
VECCORE_FORCE_INLINE Vc::SimdArray< T, N > CopySign(const Vc::SimdArray< T, N > &x, const Vc::SimdArray< T, N > &y)
Definition: VcSimdArray.h:117
VECCORE_FORCE_INLINE Vc::SimdArray< T, N > Tan(const Vc::SimdArray< T, N > &x)
Definition: VcSimdArray.h:148
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Scalar< T > Get(const T &v, size_t i)
typename TypeTraits< T >::MaskType Mask
Definition: Interface.h:10
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE T Blend(const Mask< T > &mask, const T &src1, const T &src2)
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void Set(T &v, size_t i, Scalar< T > const val)