summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-05-11 14:57:05 -0700
committerGitHub <noreply@github.com>2020-05-11 14:57:05 -0700
commitf5dfa1ed6a51809e8593f5f2abc292ab39f35dcb (patch)
tree1ed0ec39036e65241e64be40ff07a418a23d891c
parent798f3bc2236ce81499b05662dc11e7c071e7cde8 (diff)
Add GLSL translation for HLSL fmod() (#1342)
The existing code was assuming `fmod()` was available as a builtin in GLSL, which isn't true. It also isn't possible to translate the HLSL `fmod()` to the GLSL `mod()` since the two have slightly different semantics. This change introduces a definition for `fmod(x,y)` that amounts to `x - y*trunc(x/y)` which should agree with the HLSL version except in corner cases (e.g., there are some cases where the HLSL version returns `-0` and this one will return `0`).
-rw-r--r--source/slang/hlsl.meta.slang7
-rw-r--r--tests/cross-compile/fmod.slang28
-rw-r--r--tests/cross-compile/fmod.slang.expected.txt4
3 files changed, 36 insertions, 3 deletions
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang
index c0dba51e3..fcd028b6e 100644
--- a/source/slang/hlsl.meta.slang
+++ b/source/slang/hlsl.meta.slang
@@ -1325,14 +1325,15 @@ matrix<double, N, M> fma(matrix<double, N, M> a, matrix<double, N, M> b, matrix<
// Floating point remainder of x/y
__generic<T : __BuiltinFloatingPointType>
__target_intrinsic(hlsl)
-__target_intrinsic(glsl)
__target_intrinsic(cuda, "$P_fmod($0, $1)")
__target_intrinsic(cpp, "$P_fmod($0, $1)")
-T fmod(T x, T y);
+T fmod(T x, T y)
+{
+ return x - y * trunc(x/y);
+}
__generic<T : __BuiltinFloatingPointType, let N : int>
__target_intrinsic(hlsl)
-__target_intrinsic(glsl)
vector<T, N> fmod(vector<T, N> x, vector<T, N> y)
{
VECTOR_MAP_BINARY(T, N, fmod, x, y);
diff --git a/tests/cross-compile/fmod.slang b/tests/cross-compile/fmod.slang
new file mode 100644
index 000000000..94d733e66
--- /dev/null
+++ b/tests/cross-compile/fmod.slang
@@ -0,0 +1,28 @@
+// fmod.slang
+
+// Ensure that HLSL `fmod` works and produces
+// expected output on Vulkan/GLSL.
+
+//TEST(compute):COMPARE_COMPUTE:-dx11 -compute
+//TEST(compute):COMPARE_COMPUTE:-vk -compute
+
+//TEST_INPUT:cbuffer(data=[4 0 0 0]):name=C
+cbuffer C
+{
+ int y;
+}
+
+int test(int x)
+{
+ return int(fmod(float(x), float(y)));
+}
+
+//TEST_INPUT:ubuffer(data=[-7 -2 2 7], stride=4):out,name outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ uint tid = dispatchThreadID.x;
+ outputBuffer[tid] = test(outputBuffer[tid]);
+}
diff --git a/tests/cross-compile/fmod.slang.expected.txt b/tests/cross-compile/fmod.slang.expected.txt
new file mode 100644
index 000000000..02a433b0b
--- /dev/null
+++ b/tests/cross-compile/fmod.slang.expected.txt
@@ -0,0 +1,4 @@
+FFFFFFFD
+FFFFFFFE
+2
+3