2525#include < cmath>
2626#include < algorithm>
2727#include < atomic>
28+ #include < limits>
29+ #include < cstring>
2830#endif
2931
3032#if !defined(GPUCA_GPUCODE_COMPILEKERNELS) && (!defined(GPUCA_GPUCODE_DEVICE) || defined(__CUDACC__) || defined(__HIPCC__))
@@ -83,9 +85,14 @@ class GPUCommonMath
8385 GPUd () static float Modf (float x, float y);
8486 GPUd () static bool Finite (float x);
8587 GPUd () static bool IsNaN (float x);
88+ GPUd () static bool FiniteRelaxed (float x); // always true if not using NO_FAST_MATH
89+ GPUd () static bool IsNaNRelaxed (float x); // always true if not using NO_FAST_MATH
90+ GPUd () static float QuietNaN ();
8691 GPUd () static uint32_t Clz (uint32_t val);
8792 GPUd () static uint32_t Popcount (uint32_t val);
8893
94+ GPUd () static void memcpy (void * dst, const void * src, size_t size);
95+
8996 GPUhdni () static float Hypot (float x, float y);
9097 GPUhdni () static float Hypot (float x, float y, float z);
9198 GPUhdni () static float Hypot (float x, float y, float z, float w);
@@ -181,6 +188,23 @@ typedef GPUCommonMath CAMath;
181188 #define CHOICE (c1, c2, c3 ) (c1) // Select first option for Host
182189#endif // clang-format on
183190
191+ GPUdi () void GPUCommonMath::memcpy (void * dst, const void * src, size_t size)
192+ {
193+ #ifndef GPUCA_GPUCODE_DEVICE
194+ std::memcpy (dst, src, size);
195+ #elif defined(__CUDACC__) || defined(__HIPCC__)
196+ ::memcpy (dst, src, size);
197+ #elif defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
198+ __builtin_memcpy (dst, src, size);
199+ #else
200+ char * d = (char *)dst;
201+ const char * s = (const char *)src;
202+ for (size_t i = 0 ; i < size; i++) {
203+ d[i] = s[i];
204+ }
205+ #endif
206+ }
207+
184208template <int32_t I, class T >
185209GPUdi () constexpr T GPUCommonMath::nextMultipleOf (T val)
186210{
@@ -222,11 +246,12 @@ GPUdi() uint32_t GPUCommonMath::Float2UIntReint(const float& x)
222246GPUdi () uint32_t GPUCommonMath::Float2UIntRn (float x) { return (uint32_t )(int32_t )(x + 0 .5f ); }
223247GPUdi () float GPUCommonMath::Floor (float x) { return CHOICE (floorf (x), floorf (x), floor (x)); }
224248
249+ GPUdi () bool GPUCommonMath::Finite (float x) { return CHOICE (std::isfinite (x), isfinite (x), isfinite (x)); }
250+ GPUdi () bool GPUCommonMath::IsNaN (float x) { return CHOICE (std::isnan (x), isnan (x), isnan (x)); }
251+ GPUdi () float GPUCommonMath::QuietNaN () { return CHOICE (std::numeric_limits<float >::quiet_NaN (), __builtin_nanf (" " ), nan (0u )); }
225252#ifdef GPUCA_NO_FAST_MATH
226253GPUdi () float GPUCommonMath::Round(float x) { return CHOICE (roundf (x), roundf (x), round (x)); }
227254GPUdi () int32_t GPUCommonMath::Float2IntRn(float x) { return (int32_t )Round (x); }
228- GPUdi () bool GPUCommonMath::Finite(float x) { return CHOICE (std::isfinite (x), isfinite (x), true ); } // Fixme: fix these 2 for OpenCL
229- GPUdi () bool GPUCommonMath::IsNaN(float x) { return CHOICE (std::isnan (x), isnan (x), false ); }
230255GPUhdi () float GPUCommonMath::Sqrt(float x) { return CHOICE (sqrtf (x), (float )sqrt ((double )x), sqrt (x)); }
231256GPUdi () float GPUCommonMath::ATan(float x) { return CHOICE ((float )atan ((double )x), (float )atan ((double )x), atan (x)); }
232257GPUhdi () float GPUCommonMath::ATan2(float y, float x) { return CHOICE ((float )atan2 ((double )y, (double )x), (float )atan2 ((double )y, (double )x), atan2 (y, x)); }
@@ -238,10 +263,11 @@ GPUdi() float GPUCommonMath::ASin(float x) { return CHOICE((float)asin((double)x
238263GPUdi () float GPUCommonMath::ACos(float x) { return CHOICE ((float )acos ((double )x), (float )acos ((double )x), acos (x)); }
239264GPUdi () float GPUCommonMath::Log(float x) { return CHOICE ((float )log ((double )x), (float )log ((double )x), log (x)); }
240265GPUdi () float GPUCommonMath::Exp(float x) { return CHOICE ((float )exp ((double )x), (float )exp ((double )x), exp (x)); }
266+ GPUdi () bool GPUCommonMath::FiniteRelaxed(float x) { return Finite (x); }
267+ GPUdi () bool GPUCommonMath::IsNaNRelaxed(float x) { return IsNaN (x); }
241268#else
242269GPUdi () float GPUCommonMath::Round(float x) { return CHOICE (roundf (x), rintf (x), rint (x)); }
243270GPUdi () int32_t GPUCommonMath::Float2IntRn(float x) { return CHOICE ((int32_t )Round (x), __float2int_rn (x), (int32_t )Round (x)); }
244- GPUdi () bool GPUCommonMath::Finite(float x) { return CHOICE (std::isfinite (x), true , true ); }
245271GPUhdi () float GPUCommonMath::Sqrt(float x) { return CHOICE (sqrtf (x), sqrtf (x), sqrt (x)); }
246272GPUdi () float GPUCommonMath::ATan(float x) { return CHOICE (atanf (x), atanf (x), atan (x)); }
247273GPUhdi () float GPUCommonMath::ATan2(float y, float x) { return CHOICE (atan2f (y, x), atan2f (y, x), atan2 (y, x)); }
@@ -253,6 +279,8 @@ GPUdi() float GPUCommonMath::ASin(float x) { return CHOICE(asinf(x), asinf(x), a
253279GPUdi () float GPUCommonMath::ACos(float x) { return CHOICE (acosf (x), acosf (x), acos (x)); }
254280GPUdi () float GPUCommonMath::Log(float x) { return CHOICE (logf (x), logf (x), log (x)); }
255281GPUdi () float GPUCommonMath::Exp(float x) { return CHOICE (expf (x), expf (x), exp (x)); }
282+ GPUdi () bool GPUCommonMath::FiniteRelaxed(float x) { return true ; }
283+ GPUdi () bool GPUCommonMath::IsNaNRelaxed(float x) { return false ; }
256284#endif
257285
258286GPUhdi () void GPUCommonMath::SinCos (float x, float & s, float & c)
0 commit comments