summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-06-27 14:28:01 -0400
committerGitHub <noreply@github.com>2023-06-27 14:28:01 -0400
commit9ddbea318d347f55c81c82e71ee09c45aeb89c59 (patch)
treed4f9458f9518c9f0754e190beee4078df74ee474
parent1b01ff909afa1eb6700c0dc947e679b9c3890880 (diff)
Support for infinite literal of from 34.2432#INF (#2944)
-rw-r--r--prelude/slang-cpp-prelude.h5
-rw-r--r--source/compiler-core/slang-lexer.cpp34
-rw-r--r--tests/bugs/inf-float-literal.slang26
-rw-r--r--tests/bugs/inf-float-literal.slang.expected.txt4
4 files changed, 69 insertions, 0 deletions
diff --git a/prelude/slang-cpp-prelude.h b/prelude/slang-cpp-prelude.h
index d15abdb88..2b848dc3b 100644
--- a/prelude/slang-cpp-prelude.h
+++ b/prelude/slang-cpp-prelude.h
@@ -43,6 +43,11 @@
#define SLANG_PRELUDE_EXPORT_START SLANG_PRELUDE_EXTERN_C_START SLANG_PRELUDE_SHARED_LIB_EXPORT
#define SLANG_PRELUDE_EXPORT_END SLANG_PRELUDE_EXTERN_C_END
+#ifndef INFINITY
+// Must overflow for double
+# define INFINITY float(1e+300 * 1e+300)
+#endif
+
#ifndef SLANG_INFINITY
# define SLANG_INFINITY INFINITY
#endif
diff --git a/source/compiler-core/slang-lexer.cpp b/source/compiler-core/slang-lexer.cpp
index 7bb9aa84d..24cd3034b 100644
--- a/source/compiler-core/slang-lexer.cpp
+++ b/source/compiler-core/slang-lexer.cpp
@@ -438,6 +438,22 @@ namespace Slang
static bool _maybeLexNumberExponent(Lexer* lexer, int base)
{
+ if (_peek(lexer) == '#')
+ {
+ // Special case #INF
+ const auto inf = toSlice("#INF");
+ for (auto c : inf)
+ {
+ if (_peek(lexer) != c)
+ {
+ return false;
+ }
+ _advance(lexer);
+ }
+
+ return true;
+ }
+
if(!_isNumberExponent(_peek(lexer), base))
return false;
@@ -626,6 +642,24 @@ namespace Slang
}
}
+ if (*cursor == '#')
+ {
+ // It must be INF
+ const auto inf = toSlice("#INF");
+
+ if (UnownedStringSlice(cursor, end).startsWith(inf))
+ {
+ if(outSuffix)
+ {
+ *outSuffix = UnownedStringSlice(cursor + inf.getLength(), end);
+ }
+
+ value = INFINITY;
+
+ return value;
+ }
+ }
+
// Now read optional exponent
if(_isNumberExponent(*cursor, radix))
{
diff --git a/tests/bugs/inf-float-literal.slang b/tests/bugs/inf-float-literal.slang
new file mode 100644
index 000000000..f40a4e8d4
--- /dev/null
+++ b/tests/bugs/inf-float-literal.slang
@@ -0,0 +1,26 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj
+//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj
+//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute -shaderobj
+
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int idx = int(dispatchThreadID.x);
+
+ float a;
+
+ switch (idx)
+ {
+ default:
+ case 0: a = 1.#INF; break;
+ case 1: a = 2.4#INFf; break;
+ case 2: a = float(234.5#INFl); break;
+ case 3: a = -1.#INF; break;
+ }
+
+ outputBuffer[idx] = a;
+} \ No newline at end of file
diff --git a/tests/bugs/inf-float-literal.slang.expected.txt b/tests/bugs/inf-float-literal.slang.expected.txt
new file mode 100644
index 000000000..7448e6a47
--- /dev/null
+++ b/tests/bugs/inf-float-literal.slang.expected.txt
@@ -0,0 +1,4 @@
+7F800000
+7F800000
+7F800000
+FF800000