Skip to content

Commit

Permalink
Support for infinite literal of from 34.2432#INF (shader-slang#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmall-zzz authored Jun 27, 2023
1 parent 1b01ff9 commit 9ddbea3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions prelude/slang-cpp-prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions source/compiler-core/slang-lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
{
Expand Down
26 changes: 26 additions & 0 deletions tests/bugs/inf-float-literal.slang
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions tests/bugs/inf-float-literal.slang.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
7F800000
7F800000
7F800000
FF800000

0 comments on commit 9ddbea3

Please sign in to comment.