Skip to content

Commit

Permalink
Use intermOut.cpp's IsNan and IsInfinity for parse-time constant folding
Browse files Browse the repository at this point in the history
There were two implementations of isInf() and isNan(), in Constant.cpp
and in intermOut.cpp. The former only works on little-endian systems,
the latter is a wrapper for library functions and works regardless of
endianness. Move the second version into Common.h and adopt it in both
places. Thereby avoid the duplication and fix for big-endian systems.

On s390x, this fixes the test case
Glsl/CompileToAstTest.FromFile/constFold_frag.

Fixes KhronosGroup#2802
  • Loading branch information
mhillenbrand committed Nov 9, 2021
1 parent ed2f23f commit 7c57c41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 62 deletions.
29 changes: 29 additions & 0 deletions glslang/Include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <list>
Expand Down Expand Up @@ -302,6 +303,34 @@ template <class T> int IntLog2(T n)
return result;
}

inline bool IsInfinity(double x) {
#ifdef _MSC_VER
switch (_fpclass(x)) {
case _FPCLASS_NINF:
case _FPCLASS_PINF:
return true;
default:
return false;
}
#else
return std::isinf(x);
#endif
}

inline bool IsNan(double x) {
#ifdef _MSC_VER
switch (_fpclass(x)) {
case _FPCLASS_SNAN:
case _FPCLASS_QNAN:
return true;
default:
return false;
}
#else
return std::isnan(x);
#endif
}

} // end namespace glslang

#endif // _COMMON_INCLUDED_
33 changes: 2 additions & 31 deletions glslang/MachineIndependent/Constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,6 @@ namespace {

using namespace glslang;

typedef union {
double d;
int i[2];
} DoubleIntUnion;

// Some helper functions

bool isNan(double x)
{
DoubleIntUnion u;
// tough to find a platform independent library function, do it directly
u.d = x;
int bitPatternL = u.i[0];
int bitPatternH = u.i[1];
return (bitPatternH & 0x7ff80000) == 0x7ff80000 &&
((bitPatternH & 0xFFFFF) != 0 || bitPatternL != 0);
}

bool isInf(double x)
{
DoubleIntUnion u;
// tough to find a platform independent library function, do it directly
u.d = x;
int bitPatternL = u.i[0];
int bitPatternH = u.i[1];
return (bitPatternH & 0x7ff00000) == 0x7ff00000 &&
(bitPatternH & 0xFFFFF) == 0 && bitPatternL == 0;
}

const double pi = 3.1415926535897932384626433832795;

} // end anonymous namespace
Expand Down Expand Up @@ -663,12 +634,12 @@ TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType)

case EOpIsNan:
{
newConstArray[i].setBConst(isNan(unionArray[i].getDConst()));
newConstArray[i].setBConst(IsNan(unionArray[i].getDConst()));
break;
}
case EOpIsInf:
{
newConstArray[i].setBConst(isInf(unionArray[i].getDConst()));
newConstArray[i].setBConst(IsInfinity(unionArray[i].getDConst()));
break;
}

Expand Down
31 changes: 0 additions & 31 deletions glslang/MachineIndependent/intermOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,6 @@
#endif
#include <cstdint>

namespace {

bool IsInfinity(double x) {
#ifdef _MSC_VER
switch (_fpclass(x)) {
case _FPCLASS_NINF:
case _FPCLASS_PINF:
return true;
default:
return false;
}
#else
return std::isinf(x);
#endif
}

bool IsNan(double x) {
#ifdef _MSC_VER
switch (_fpclass(x)) {
case _FPCLASS_SNAN:
case _FPCLASS_QNAN:
return true;
default:
return false;
}
#else
return std::isnan(x);
#endif
}

}

namespace glslang {

Expand Down

0 comments on commit 7c57c41

Please sign in to comment.