From a3651d99fb8f3a046365d60751d1f3f806e48f7a Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 2 Dec 2019 11:14:28 -0500 Subject: Fix bug in calcSafeRadians. (#1138) --- prelude/slang-cpp-scalar-intrinsics.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'prelude') diff --git a/prelude/slang-cpp-scalar-intrinsics.h b/prelude/slang-cpp-scalar-intrinsics.h index 081b31265..70f5ec01a 100644 --- a/prelude/slang-cpp-scalar-intrinsics.h +++ b/prelude/slang-cpp-scalar-intrinsics.h @@ -30,9 +30,12 @@ union Union64 // Helpers SLANG_FORCE_INLINE float F32_calcSafeRadians(float radians) { - float a = radians * (1.0f / float(SLANG_PRELUDE_PI)); - a = (a < 0.0f) ? (::ceilf(a) - a) : (a - ::floorf(a)); - return (a * float(SLANG_PRELUDE_PI)); + // Put 0 to 2pi cycles to cycle around 0 to 1 + float a = radians * (1.0f / float(SLANG_PRELUDE_PI * 2)); + // Get truncated fraction, as value in 0 - 1 range + a = a - ::floorf(a); + // Convert back to 0 - 2pi range + return (a * float(SLANG_PRELUDE_PI * 2)); } // Unary @@ -79,9 +82,12 @@ SLANG_FORCE_INLINE int32_t F32_asint(float f) { Union32 u; u.f = f; return u.i; SLANG_FORCE_INLINE double F64_calcSafeRadians(double radians) { - double a = radians * (1.0 / SLANG_PRELUDE_PI); - a = (a < 0.0) ? (::ceil(a) - a) : (a - ::floor(a)); - return (a * SLANG_PRELUDE_PI); + // Put 0 to 2pi cycles to cycle around 0 to 1 + double a = radians * (1.0f / (SLANG_PRELUDE_PI * 2)); + // Get truncated fraction, as value in 0 - 1 range + a = a - ::floor(a); + // Convert back to 0 - 2pi range + return (a * (SLANG_PRELUDE_PI * 2)); } // Unary -- cgit v1.2.3