diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-12-02 11:14:28 -0500 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-12-02 08:14:28 -0800 |
| commit | a3651d99fb8f3a046365d60751d1f3f806e48f7a (patch) | |
| tree | 8950724957bb7f2cdaa45bfa2b75cbe4e38e947b | |
| parent | c6393465795e700a68458ff618041104f89ed42b (diff) | |
Fix bug in calcSafeRadians. (#1138)
| -rw-r--r-- | prelude/slang-cpp-scalar-intrinsics.h | 18 | ||||
| -rw-r--r-- | tests/compute/transcendental.slang | 31 | ||||
| -rw-r--r-- | tests/compute/transcendental.slang.expected.txt | 16 |
3 files changed, 59 insertions, 6 deletions
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 diff --git a/tests/compute/transcendental.slang b/tests/compute/transcendental.slang new file mode 100644 index 000000000..aa40da752 --- /dev/null +++ b/tests/compute/transcendental.slang @@ -0,0 +1,31 @@ +//TEST(compute):COMPARE_COMPUTE:-cpu +//TEST(compute):COMPARE_COMPUTE: +//TEST(compute,vulcan):COMPARE_COMPUTE:-vk + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer : register(u0); + +int quantize(double value) +{ + return int(value * 256); +} + +int quantize(float value) +{ + return int(value * 256); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + float values[] = { -9, 9, -3, 3 }; + + int tid = int(dispatchThreadID.x); + float value = values[tid]; + + outputBuffer[tid * 4] = quantize(sin(value)); + outputBuffer[tid * 4 + 1] = quantize(cos(value)); + + outputBuffer[tid * 4 + 2] = quantize(sin(double(value))); + outputBuffer[tid * 4 + 3] = quantize(cos(double(value))); +}
\ No newline at end of file diff --git a/tests/compute/transcendental.slang.expected.txt b/tests/compute/transcendental.slang.expected.txt new file mode 100644 index 000000000..4a525cc7c --- /dev/null +++ b/tests/compute/transcendental.slang.expected.txt @@ -0,0 +1,16 @@ +FFFFFF97 +FFFFFF17 +FFFFFF97 +FFFFFF17 +69 +FFFFFF17 +69 +FFFFFF17 +FFFFFFDC +FFFFFF03 +FFFFFFDC +FFFFFF03 +24 +FFFFFF03 +24 +FFFFFF03 |
