Skip to content

Commit ee009a4

Browse files
committed
GPU Math: Make constexpr what possible
1 parent 5848069 commit ee009a4

File tree

1 file changed

+77
-77
lines changed

1 file changed

+77
-77
lines changed

GPU/Common/GPUCommonMath.h

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class GPUCommonMath
5353
GPUd() static float2 MakeFloat2(float x, float y); // TODO: Find better appraoch that is constexpr
5454

5555
template <class T>
56-
GPUhd() static T Min(const T x, const T y)
56+
GPUhd() constexpr static T Min(const T x, const T y)
5757
{
5858
return GPUCA_CHOICE(std::min(x, y), min(x, y), min(x, y));
5959
}
6060
template <class T>
61-
GPUhd() static T Max(const T x, const T y)
61+
GPUhd() constexpr static T Max(const T x, const T y)
6262
{
6363
return GPUCA_CHOICE(std::max(x, y), max(x, y), max(x, y));
6464
}
@@ -69,51 +69,51 @@ class GPUCommonMath
6969
template <class T, class S, class R>
7070
GPUd() static T MaxWithRef(T x, T y, T z, T w, S refX, S refY, S refZ, S refW, R& r);
7171
template <class T>
72-
GPUdi() static T Clamp(const T v, const T lo, const T hi)
72+
GPUdi() constexpr static T Clamp(const T v, const T lo, const T hi)
7373
{
7474
return Max(lo, Min(v, hi));
7575
}
76-
GPUhdni() static float Sqrt(float x);
76+
GPUhdni() constexpr static float Sqrt(float x);
7777
GPUd() static float InvSqrt(float x);
7878
template <class T>
79-
GPUhd() static T Abs(T x);
80-
GPUd() static float ASin(float x);
81-
GPUd() static float ACos(float x);
82-
GPUd() static float ATan(float x);
83-
GPUhd() static float ATan2(float y, float x);
84-
GPUd() static float Sin(float x);
85-
GPUd() static float Cos(float x);
79+
GPUhd() constexpr static T Abs(T x);
80+
GPUd() constexpr static float ASin(float x);
81+
GPUd() constexpr static float ACos(float x);
82+
GPUd() constexpr static float ATan(float x);
83+
GPUhd() constexpr static float ATan2(float y, float x);
84+
GPUd() constexpr static float Sin(float x);
85+
GPUd() constexpr static float Cos(float x);
8686
GPUhdni() static void SinCos(float x, float& s, float& c);
8787
GPUhdni() static void SinCosd(double x, double& s, double& c);
88-
GPUd() static float Tan(float x);
89-
GPUd() static float Pow(float x, float y);
90-
GPUd() static float Log(float x);
91-
GPUd() static float Exp(float x);
92-
GPUhdni() static float Copysign(float x, float y) { return GPUCA_CHOICE(std::copysignf(x, y), copysignf(x, y), copysign(x, y)); }
93-
GPUd() static constexpr float TwoPi() { return 6.2831853f; }
94-
GPUd() static constexpr float Pi() { return 3.1415927f; }
95-
GPUd() static float Round(float x);
96-
GPUd() static float Floor(float x) { return GPUCA_CHOICE(floorf(x), floorf(x), floor(x)); }
97-
GPUd() static uint32_t Float2UIntReint(const float& x);
98-
GPUd() static uint32_t Float2UIntRn(float x) { return (uint32_t)(int32_t)(x + 0.5f); }
99-
GPUd() static int32_t Float2IntRn(float x);
100-
GPUd() static float Modf(float x, float y);
101-
GPUd() static bool Finite(float x) { return GPUCA_CHOICE(std::isfinite(x), isfinite(x), isfinite(x)); }
102-
GPUd() static bool IsNaN(float x) { return GPUCA_CHOICE(std::isnan(x), isnan(x), isnan(x)); }
103-
GPUd() static bool FiniteRelaxed(float x); // always true if not using NO_FAST_MATH
104-
GPUd() static bool IsNaNRelaxed(float x); // always true if not using NO_FAST_MATH
105-
GPUd() static float QuietNaN() { return GPUCA_CHOICE(std::numeric_limits<float>::quiet_NaN(), __builtin_nanf(""), nan(0u)); }
106-
GPUd() static uint32_t Clz(uint32_t val);
107-
GPUd() static uint32_t Popcount(uint32_t val);
88+
GPUd() constexpr static float Tan(float x);
89+
GPUd() constexpr static float Pow(float x, float y);
90+
GPUd() constexpr static float Log(float x);
91+
GPUd() constexpr static float Exp(float x);
92+
GPUhdni() constexpr static float Copysign(float x, float y) { return GPUCA_CHOICE(std::copysignf(x, y), copysignf(x, y), copysign(x, y)); }
93+
GPUd() constexpr static float TwoPi() { return 6.2831853f; }
94+
GPUd() constexpr static float Pi() { return 3.1415927f; }
95+
GPUd() constexpr static float Round(float x);
96+
GPUd() constexpr static float Floor(float x) { return GPUCA_CHOICE(floorf(x), floorf(x), floor(x)); }
97+
GPUd() static uint32_t Float2UIntReint(float x);
98+
GPUd() constexpr static uint32_t Float2UIntRn(float x) { return (uint32_t)(int32_t)(x + 0.5f); }
99+
GPUd() constexpr static int32_t Float2IntRn(float x);
100+
GPUd() constexpr static float Modf(float x, float y);
101+
GPUd() constexpr static bool Finite(float x) { return GPUCA_CHOICE(std::isfinite(x), isfinite(x), isfinite(x)); }
102+
GPUd() constexpr static bool IsNaN(float x) { return GPUCA_CHOICE(std::isnan(x), isnan(x), isnan(x)); }
103+
GPUd() constexpr static bool FiniteRelaxed(float x); // always true if not using NO_FAST_MATH
104+
GPUd() constexpr static bool IsNaNRelaxed(float x); // always true if not using NO_FAST_MATH
105+
GPUd() constexpr static float QuietNaN() { return GPUCA_CHOICE(std::numeric_limits<float>::quiet_NaN(), __builtin_nanf(""), nan(0u)); }
106+
GPUd() constexpr static uint32_t Clz(uint32_t val);
107+
GPUd() constexpr static uint32_t Popcount(uint32_t val);
108108

109109
GPUd() static void memcpy(void* dst, const void* src, size_t size);
110110

111-
GPUhdi() static float Hypot(float x, float y) { return Sqrt(x * x + y * y); }
112-
GPUhdi() static float Hypot(float x, float y, float z) { return Sqrt(x * x + y * y + z * z); }
113-
GPUhdi() static float Hypot(float x, float y, float z, float w) { return Sqrt(x * x + y * y + z * z + w * w); }
111+
GPUhdi() constexpr static float Hypot(float x, float y) { return Sqrt(x * x + y * y); }
112+
GPUhdi() constexpr static float Hypot(float x, float y, float z) { return Sqrt(x * x + y * y + z * z); }
113+
GPUhdi() constexpr static float Hypot(float x, float y, float z, float w) { return Sqrt(x * x + y * y + z * z + w * w); }
114114

115115
template <typename T>
116-
GPUhd() static void Swap(T& a, T& b);
116+
GPUhd() constexpr static void Swap(T& a, T& b);
117117

118118
template <class T>
119119
GPUdi() static T AtomicExch(GPUglobalref() GPUgeneric() GPUAtomic(T) * addr, T val)
@@ -162,14 +162,14 @@ class GPUCommonMath
162162
{
163163
GPUCommonMath::AtomicMinInternal(addr, val);
164164
}
165-
GPUd() static int32_t Mul24(int32_t a, int32_t b);
166-
GPUd() static float FMulRZ(float a, float b);
165+
GPUd() constexpr static int32_t Mul24(int32_t a, int32_t b);
166+
GPUd() constexpr static float FMulRZ(float a, float b);
167167

168168
template <int32_t I, class T>
169169
GPUd() constexpr static T nextMultipleOf(T val);
170170

171171
template <typename... Args>
172-
GPUhdni() static float Sum2(float w, Args... args);
172+
GPUhdni() constexpr static float Sum2(float w, Args... args);
173173

174174
private:
175175
template <class S, class T>
@@ -187,7 +187,7 @@ class GPUCommonMath
187187
typedef GPUCommonMath CAMath;
188188

189189
template <typename... Args>
190-
GPUhdi() float GPUCommonMath::Sum2(float w, Args... args)
190+
GPUhdi() constexpr float GPUCommonMath::Sum2(float w, Args... args)
191191
{
192192
if constexpr (sizeof...(Args) == 0) {
193193
return w * w;
@@ -239,9 +239,9 @@ GPUdi() float2 GPUCommonMath::MakeFloat2(float x, float y)
239239
#endif // GPUCA_GPUCODE
240240
}
241241

242-
GPUdi() float GPUCommonMath::Modf(float x, float y) { return GPUCA_CHOICE(fmodf(x, y), fmodf(x, y), fmod(x, y)); }
242+
GPUdi() constexpr float GPUCommonMath::Modf(float x, float y) { return GPUCA_CHOICE(fmodf(x, y), fmodf(x, y), fmod(x, y)); }
243243

244-
GPUdi() uint32_t GPUCommonMath::Float2UIntReint(const float& x)
244+
GPUdi() uint32_t GPUCommonMath::Float2UIntReint(float x)
245245
{
246246
#if defined(GPUCA_GPUCODE_DEVICE) && (defined(__CUDACC__) || defined(__HIPCC__))
247247
return __float_as_uint(x);
@@ -253,37 +253,37 @@ GPUdi() uint32_t GPUCommonMath::Float2UIntReint(const float& x)
253253
}
254254

255255
#ifdef GPUCA_NO_FAST_MATH
256-
GPUdi() float GPUCommonMath::Round(float x) { return GPUCA_CHOICE(roundf(x), roundf(x), round(x)); }
257-
GPUdi() int32_t GPUCommonMath::Float2IntRn(float x) { return (int32_t)Round(x); }
258-
GPUhdi() float GPUCommonMath::Sqrt(float x) { return GPUCA_CHOICE(sqrtf(x), (float)sqrt((double)x), sqrt(x)); }
259-
GPUdi() float GPUCommonMath::ATan(float x) { return GPUCA_CHOICE((float)atan((double)x), (float)atan((double)x), atan(x)); }
260-
GPUhdi() float GPUCommonMath::ATan2(float y, float x) { return GPUCA_CHOICE((float)atan2((double)y, (double)x), (float)atan2((double)y, (double)x), atan2(y, x)); }
261-
GPUdi() float GPUCommonMath::Sin(float x) { return GPUCA_CHOICE((float)sin((double)x), (float)sin((double)x), sin(x)); }
262-
GPUdi() float GPUCommonMath::Cos(float x) { return GPUCA_CHOICE((float)cos((double)x), (float)cos((double)x), cos(x)); }
263-
GPUdi() float GPUCommonMath::Tan(float x) { return GPUCA_CHOICE((float)tanf((double)x), (float)tanf((double)x), tan(x)); }
264-
GPUdi() float GPUCommonMath::Pow(float x, float y) { return GPUCA_CHOICE((float)pow((double)x, (double)y), pow((double)x, (double)y), pow(x, y)); }
265-
GPUdi() float GPUCommonMath::ASin(float x) { return GPUCA_CHOICE((float)asin((double)x), (float)asin((double)x), asin(x)); }
266-
GPUdi() float GPUCommonMath::ACos(float x) { return GPUCA_CHOICE((float)acos((double)x), (float)acos((double)x), acos(x)); }
267-
GPUdi() float GPUCommonMath::Log(float x) { return GPUCA_CHOICE((float)log((double)x), (float)log((double)x), log(x)); }
268-
GPUdi() float GPUCommonMath::Exp(float x) { return GPUCA_CHOICE((float)exp((double)x), (float)exp((double)x), exp(x)); }
269-
GPUdi() bool GPUCommonMath::FiniteRelaxed(float x) { return Finite(x); }
270-
GPUdi() bool GPUCommonMath::IsNaNRelaxed(float x) { return IsNaN(x); }
256+
GPUdi() constexpr float GPUCommonMath::Round(float x) { return GPUCA_CHOICE(roundf(x), roundf(x), round(x)); }
257+
GPUdi() constexpr int32_t GPUCommonMath::Float2IntRn(float x) { return (int32_t)Round(x); }
258+
GPUhdi() constexpr float GPUCommonMath::Sqrt(float x) { return GPUCA_CHOICE(sqrtf(x), (float)sqrt((double)x), sqrt(x)); }
259+
GPUdi() constexpr float GPUCommonMath::ATan(float x) { return GPUCA_CHOICE((float)atan((double)x), (float)atan((double)x), atan(x)); }
260+
GPUhdi() constexpr float GPUCommonMath::ATan2(float y, float x) { return GPUCA_CHOICE((float)atan2((double)y, (double)x), (float)atan2((double)y, (double)x), atan2(y, x)); }
261+
GPUdi() constexpr float GPUCommonMath::Sin(float x) { return GPUCA_CHOICE((float)sin((double)x), (float)sin((double)x), sin(x)); }
262+
GPUdi() constexpr float GPUCommonMath::Cos(float x) { return GPUCA_CHOICE((float)cos((double)x), (float)cos((double)x), cos(x)); }
263+
GPUdi() constexpr float GPUCommonMath::Tan(float x) { return GPUCA_CHOICE((float)tanf((double)x), (float)tanf((double)x), tan(x)); }
264+
GPUdi() constexpr float GPUCommonMath::Pow(float x, float y) { return GPUCA_CHOICE((float)pow((double)x, (double)y), pow((double)x, (double)y), pow(x, y)); }
265+
GPUdi() constexpr float GPUCommonMath::ASin(float x) { return GPUCA_CHOICE((float)asin((double)x), (float)asin((double)x), asin(x)); }
266+
GPUdi() constexpr float GPUCommonMath::ACos(float x) { return GPUCA_CHOICE((float)acos((double)x), (float)acos((double)x), acos(x)); }
267+
GPUdi() constexpr float GPUCommonMath::Log(float x) { return GPUCA_CHOICE((float)log((double)x), (float)log((double)x), log(x)); }
268+
GPUdi() constexpr float GPUCommonMath::Exp(float x) { return GPUCA_CHOICE((float)exp((double)x), (float)exp((double)x), exp(x)); }
269+
GPUdi() constexpr bool GPUCommonMath::FiniteRelaxed(float x) { return Finite(x); }
270+
GPUdi() constexpr bool GPUCommonMath::IsNaNRelaxed(float x) { return IsNaN(x); }
271271
#else
272-
GPUdi() float GPUCommonMath::Round(float x) { return GPUCA_CHOICE(roundf(x), rintf(x), rint(x)); }
273-
GPUdi() int32_t GPUCommonMath::Float2IntRn(float x) { return GPUCA_CHOICE((int32_t)Round(x), __float2int_rn(x), (int32_t)Round(x)); }
274-
GPUhdi() float GPUCommonMath::Sqrt(float x) { return GPUCA_CHOICE(sqrtf(x), sqrtf(x), sqrt(x)); }
275-
GPUdi() float GPUCommonMath::ATan(float x) { return GPUCA_CHOICE(atanf(x), atanf(x), atan(x)); }
276-
GPUhdi() float GPUCommonMath::ATan2(float y, float x) { return GPUCA_CHOICE(atan2f(y, x), atan2f(y, x), atan2(y, x)); }
277-
GPUdi() float GPUCommonMath::Sin(float x) { return GPUCA_CHOICE(sinf(x), sinf(x), sin(x)); }
278-
GPUdi() float GPUCommonMath::Cos(float x) { return GPUCA_CHOICE(cosf(x), cosf(x), cos(x)); }
279-
GPUdi() float GPUCommonMath::Tan(float x) { return GPUCA_CHOICE(tanf(x), tanf(x), tan(x)); }
280-
GPUdi() float GPUCommonMath::Pow(float x, float y) { return GPUCA_CHOICE(powf(x, y), powf(x, y), pow(x, y)); }
281-
GPUdi() float GPUCommonMath::ASin(float x) { return GPUCA_CHOICE(asinf(x), asinf(x), asin(x)); }
282-
GPUdi() float GPUCommonMath::ACos(float x) { return GPUCA_CHOICE(acosf(x), acosf(x), acos(x)); }
283-
GPUdi() float GPUCommonMath::Log(float x) { return GPUCA_CHOICE(logf(x), logf(x), log(x)); }
284-
GPUdi() float GPUCommonMath::Exp(float x) { return GPUCA_CHOICE(expf(x), expf(x), exp(x)); }
285-
GPUdi() bool GPUCommonMath::FiniteRelaxed(float x) { return true; }
286-
GPUdi() bool GPUCommonMath::IsNaNRelaxed(float x) { return false; }
272+
GPUdi() constexpr float GPUCommonMath::Round(float x) { return GPUCA_CHOICE(roundf(x), rintf(x), rint(x)); }
273+
GPUdi() constexpr int32_t GPUCommonMath::Float2IntRn(float x) { return GPUCA_CHOICE((int32_t)Round(x), __float2int_rn(x), (int32_t)Round(x)); }
274+
GPUhdi() constexpr float GPUCommonMath::Sqrt(float x) { return GPUCA_CHOICE(sqrtf(x), sqrtf(x), sqrt(x)); }
275+
GPUdi() constexpr float GPUCommonMath::ATan(float x) { return GPUCA_CHOICE(atanf(x), atanf(x), atan(x)); }
276+
GPUhdi() constexpr float GPUCommonMath::ATan2(float y, float x) { return GPUCA_CHOICE(atan2f(y, x), atan2f(y, x), atan2(y, x)); }
277+
GPUdi() constexpr float GPUCommonMath::Sin(float x) { return GPUCA_CHOICE(sinf(x), sinf(x), sin(x)); }
278+
GPUdi() constexpr float GPUCommonMath::Cos(float x) { return GPUCA_CHOICE(cosf(x), cosf(x), cos(x)); }
279+
GPUdi() constexpr float GPUCommonMath::Tan(float x) { return GPUCA_CHOICE(tanf(x), tanf(x), tan(x)); }
280+
GPUdi() constexpr float GPUCommonMath::Pow(float x, float y) { return GPUCA_CHOICE(powf(x, y), powf(x, y), pow(x, y)); }
281+
GPUdi() constexpr float GPUCommonMath::ASin(float x) { return GPUCA_CHOICE(asinf(x), asinf(x), asin(x)); }
282+
GPUdi() constexpr float GPUCommonMath::ACos(float x) { return GPUCA_CHOICE(acosf(x), acosf(x), acos(x)); }
283+
GPUdi() constexpr float GPUCommonMath::Log(float x) { return GPUCA_CHOICE(logf(x), logf(x), log(x)); }
284+
GPUdi() constexpr float GPUCommonMath::Exp(float x) { return GPUCA_CHOICE(expf(x), expf(x), exp(x)); }
285+
GPUdi() constexpr bool GPUCommonMath::FiniteRelaxed(float x) { return true; }
286+
GPUdi() constexpr bool GPUCommonMath::IsNaNRelaxed(float x) { return false; }
287287
#endif
288288

289289
GPUhdi() void GPUCommonMath::SinCos(float x, float& s, float& c)
@@ -311,7 +311,7 @@ GPUhdi() void GPUCommonMath::SinCosd(double x, double& s, double& c)
311311
#endif
312312
}
313313

314-
GPUdi() uint32_t GPUCommonMath::Clz(uint32_t x)
314+
GPUdi() constexpr uint32_t GPUCommonMath::Clz(uint32_t x)
315315
{
316316
#if (defined(__GNUC__) || defined(__clang__) || defined(__CUDACC__) || defined(__HIPCC__))
317317
return x == 0 ? 32 : GPUCA_CHOICE(__builtin_clz(x), __clz(x), __builtin_clz(x)); // use builtin if available
@@ -325,7 +325,7 @@ GPUdi() uint32_t GPUCommonMath::Clz(uint32_t x)
325325
#endif
326326
}
327327

328-
GPUdi() uint32_t GPUCommonMath::Popcount(uint32_t x)
328+
GPUdi() constexpr uint32_t GPUCommonMath::Popcount(uint32_t x)
329329
{
330330
#if (defined(__GNUC__) || defined(__clang__) || defined(__CUDACC__) || defined(__HIPCC__)) && !defined(__OPENCL__) // TODO: remove OPENCL when reported SPIR-V bug is fixed
331331
// use builtin if available
@@ -338,7 +338,7 @@ GPUdi() uint32_t GPUCommonMath::Popcount(uint32_t x)
338338
}
339339

340340
template <typename T>
341-
GPUhdi() void GPUCommonMath::Swap(T& a, T& b)
341+
GPUhdi() constexpr void GPUCommonMath::Swap(T& a, T& b)
342342
{
343343
#ifndef GPUCA_GPUCODE_DEVICE
344344
std::swap(a, b);
@@ -413,19 +413,19 @@ GPUdi() float GPUCommonMath::InvSqrt(float _x)
413413
}
414414

415415
template <>
416-
GPUhdi() float GPUCommonMath::Abs<float>(float x)
416+
GPUhdi() constexpr float GPUCommonMath::Abs<float>(float x)
417417
{
418418
return GPUCA_CHOICE(fabsf(x), fabsf(x), fabs(x));
419419
}
420420

421421
template <>
422-
GPUhdi() double GPUCommonMath::Abs<double>(double x)
422+
GPUhdi() constexpr double GPUCommonMath::Abs<double>(double x)
423423
{
424424
return GPUCA_CHOICE(fabs(x), fabs(x), fabs(x));
425425
}
426426

427427
template <>
428-
GPUhdi() int32_t GPUCommonMath::Abs<int32_t>(int32_t x)
428+
GPUhdi() constexpr int32_t GPUCommonMath::Abs<int32_t>(int32_t x)
429429
{
430430
return GPUCA_CHOICE(abs(x), abs(x), abs(x));
431431
}

0 commit comments

Comments
 (0)