HLSL: Support 1.#INF and -1.#INF syntax.

This commit is contained in:
John Kessenich
2017-04-12 13:26:57 -06:00
parent b5e739c20e
commit 776c515ea5
8 changed files with 256 additions and 16 deletions

View File

@@ -2,5 +2,5 @@
// For the version, it uses the latest git tag followed by the number of commits.
// For the date, it uses the current date (when then script is run).
#define GLSLANG_REVISION "Overload400-PrecQual.1994"
#define GLSLANG_DATE "11-Apr-2017"
#define GLSLANG_REVISION "Overload400-PrecQual.1995"
#define GLSLANG_DATE "12-Apr-2017"

View File

@@ -45,11 +45,31 @@
namespace {
bool is_positive_infinity(double x) {
bool IsInfinity(double x) {
#ifdef _MSC_VER
return _fpclass(x) == _FPCLASS_PINF;
switch (_fpclass(x)) {
case _FPCLASS_NINF:
case _FPCLASS_PINF:
return true;
default:
return false;
}
#else
return std::isinf(x) && (x >= 0);
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
}
@@ -694,11 +714,14 @@ static void OutputConstantUnion(TInfoSink& out, const TIntermTyped* node, const
#endif
{
const double value = constUnion[i].getDConst();
// Print infinity in a portable way, for test stability.
// Other cases may be needed in the future: negative infinity,
// and NaNs.
if (is_positive_infinity(value))
out.debug << "inf\n";
// Print infinities and NaNs in a portable way.
if (IsInfinity(value)) {
if (value < 0)
out.debug << "-1.#INF\n";
else
out.debug << "+1.#INF\n";
} else if (IsNan(value))
out.debug << "1.#IND\n";
else {
const int maxSize = 300;
char buf[maxSize];

View File

@@ -92,7 +92,7 @@ namespace glslang {
class TPpToken {
public:
TPpToken() : space(false), ival(0), dval(0.0), i64val(0)
TPpToken() : space(false), i64val(0)
{
loc.init();
name[0] = 0;
@@ -108,10 +108,14 @@ public:
bool operator!=(const TPpToken& right) { return ! operator==(right); }
TSourceLoc loc;
bool space; // true if a space (for white space or a removed comment) should also be recognized, in front of the token returned
int ival;
double dval;
long long i64val;
bool space; // true if a space (for white space or a removed comment) should also be recognized, in front of the token returned
union {
int ival;
double dval;
long long i64val;
};
char name[MaxTokenLength + 1];
};

View File

@@ -124,6 +124,35 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
HasDecimalOrExponent = true;
saveName(ch);
ch = getChar();
// 1.#INF or -1.#INF
if (ch == '#') {
if ((len < 2) ||
(len == 2 && ppToken->name[0] != '1') ||
(len == 3 && ppToken->name[1] != '1' && !(ppToken->name[0] == '-' || ppToken->name[0] == '+')) ||
(len > 3))
parseContext.ppError(ppToken->loc, "unexpected use of", "#", "");
else {
// we have 1.# or -1.# or +1.#, check for 'INF'
if ((ch = getChar()) != 'I' ||
(ch = getChar()) != 'N' ||
(ch = getChar()) != 'F')
parseContext.ppError(ppToken->loc, "expected 'INF'", "#", "");
else {
// we have [+-].#INF, and we are targeting IEEE 754, so wrap it up:
saveName('I');
saveName('N');
saveName('F');
ppToken->name[len] = '\0';
if (ppToken->name[0] == '-')
ppToken->i64val = 0xfff0000000000000; // -Infinity
else
ppToken->i64val = 0x7ff0000000000000; // +Infinity
return PpAtomConstFloat;
}
}
}
while (ch >= '0' && ch <= '9') {
saveName(ch);
ch = getChar();