From b67b4a70720ec09c7d6e90a14f42377ad1a8e1a5 Mon Sep 17 00:00:00 2001
From: John Kessenich <cepheus@frii.com>
Date: Tue, 28 Feb 2017 12:40:40 -0700
Subject: [PATCH] PP: Address #737: accept 'h'/'H' floating-point suffix more
 broadly.

---
 Test/120.frag                                 |   4 +-
 Test/baseResults/120.frag.out                 |   5 +-
 .../baseResults/hlsl.numericsuffixes.frag.out | 131 ++++++++++++------
 Test/hlsl.numericsuffixes.frag                |   4 +
 glslang/Include/revision.h                    |   4 +-
 glslang/MachineIndependent/Versions.cpp       |   2 +-
 .../preprocessor/PpScanner.cpp                |  52 ++++---
 .../preprocessor/PpTokens.h                   |   2 -
 8 files changed, 142 insertions(+), 62 deletions(-)

diff --git a/Test/120.frag b/Test/120.frag
index 028d30810..ea35b80e4 100644
--- a/Test/120.frag
+++ b/Test/120.frag
@@ -243,4 +243,6 @@ void voidTernary()
 	b ? foo121111() : foo12111();
 	b ? foo121111() : 4;  // ERROR
 	b ? 3 : foo12111();   // ERROR
-}
\ No newline at end of file
+}
+
+float halfFloat1 = 1.0h;   // syntax ERROR
diff --git a/Test/baseResults/120.frag.out b/Test/baseResults/120.frag.out
index 5028b7514..c64eacc75 100644
--- a/Test/baseResults/120.frag.out
+++ b/Test/baseResults/120.frag.out
@@ -52,7 +52,8 @@ ERROR: 0:209: 'assign' :  cannot convert from 'const float' to 'temp 4-component
 ERROR: 0:212: 'sampler2DRect' : Reserved word. 
 ERROR: 0:244: ':' :  wrong operand types: no operation ':' exists that takes a left-hand operand of type 'global void' and a right operand of type 'const int' (or there is no acceptable conversion)
 ERROR: 0:245: ':' :  wrong operand types: no operation ':' exists that takes a left-hand operand of type 'const int' and a right operand of type 'global void' (or there is no acceptable conversion)
-ERROR: 53 compilation errors.  No code generated.
+ERROR: 0:248: '' :  syntax error
+ERROR: 54 compilation errors.  No code generated.
 
 
 Shader version: 120
@@ -645,6 +646,7 @@ ERROR: node is still EOpNull!
 0:?     's2DRbad' (uniform sampler2DRect)
 0:?     's2DR' (uniform sampler2DRect)
 0:?     's2DRS' (uniform sampler2DRectShadow)
+0:?     'halfFloat1' (global float)
 
 
 Linked fragment stage:
@@ -688,4 +690,5 @@ ERROR: node is still EOpNull!
 0:?     's2DRbad' (uniform sampler2DRect)
 0:?     's2DR' (uniform sampler2DRect)
 0:?     's2DRS' (uniform sampler2DRectShadow)
+0:?     'halfFloat1' (global float)
 
diff --git a/Test/baseResults/hlsl.numericsuffixes.frag.out b/Test/baseResults/hlsl.numericsuffixes.frag.out
index fee642686..3637a2cd9 100644
--- a/Test/baseResults/hlsl.numericsuffixes.frag.out
+++ b/Test/baseResults/hlsl.numericsuffixes.frag.out
@@ -50,16 +50,36 @@ gl_FragCoord origin is upper left
 0:15          'r08' (temp uint)
 0:15          Constant:
 0:15            58 (const uint)
-0:18      move second child to first child (temp 4-component vector of float)
-0:18        color: direct index for structure (temp 4-component vector of float)
-0:18          'ps_output' (temp structure{temp 4-component vector of float color})
+0:16      Sequence
+0:16        move second child to first child (temp float)
+0:16          'r09' (temp float)
+0:16          Constant:
+0:16            1.000000
+0:17      Sequence
+0:17        move second child to first child (temp float)
+0:17          'r10' (temp float)
+0:17          Constant:
+0:17            1.000000
+0:18      Sequence
+0:18        move second child to first child (temp float)
+0:18          'r11' (temp float)
 0:18          Constant:
-0:18            0 (const int)
-0:18        Construct vec4 (temp 4-component vector of float)
-0:18          Convert int to float (temp float)
-0:18            'r07' (temp int)
-0:19      Branch: Return with expression
-0:19        'ps_output' (temp structure{temp 4-component vector of float color})
+0:18            1.100000
+0:19      Sequence
+0:19        move second child to first child (temp float)
+0:19          'r12' (temp float)
+0:19          Constant:
+0:19            1.100000
+0:22      move second child to first child (temp 4-component vector of float)
+0:22        color: direct index for structure (temp 4-component vector of float)
+0:22          'ps_output' (temp structure{temp 4-component vector of float color})
+0:22          Constant:
+0:22            0 (const int)
+0:22        Construct vec4 (temp 4-component vector of float)
+0:22          Convert int to float (temp float)
+0:22            'r07' (temp int)
+0:23      Branch: Return with expression
+0:23        'ps_output' (temp structure{temp 4-component vector of float color})
 0:5  Function Definition: main( (temp void)
 0:5    Function Parameters: 
 0:?     Sequence
@@ -128,16 +148,36 @@ gl_FragCoord origin is upper left
 0:15          'r08' (temp uint)
 0:15          Constant:
 0:15            58 (const uint)
-0:18      move second child to first child (temp 4-component vector of float)
-0:18        color: direct index for structure (temp 4-component vector of float)
-0:18          'ps_output' (temp structure{temp 4-component vector of float color})
+0:16      Sequence
+0:16        move second child to first child (temp float)
+0:16          'r09' (temp float)
+0:16          Constant:
+0:16            1.000000
+0:17      Sequence
+0:17        move second child to first child (temp float)
+0:17          'r10' (temp float)
+0:17          Constant:
+0:17            1.000000
+0:18      Sequence
+0:18        move second child to first child (temp float)
+0:18          'r11' (temp float)
 0:18          Constant:
-0:18            0 (const int)
-0:18        Construct vec4 (temp 4-component vector of float)
-0:18          Convert int to float (temp float)
-0:18            'r07' (temp int)
-0:19      Branch: Return with expression
-0:19        'ps_output' (temp structure{temp 4-component vector of float color})
+0:18            1.100000
+0:19      Sequence
+0:19        move second child to first child (temp float)
+0:19          'r12' (temp float)
+0:19          Constant:
+0:19            1.100000
+0:22      move second child to first child (temp 4-component vector of float)
+0:22        color: direct index for structure (temp 4-component vector of float)
+0:22          'ps_output' (temp structure{temp 4-component vector of float color})
+0:22          Constant:
+0:22            0 (const int)
+0:22        Construct vec4 (temp 4-component vector of float)
+0:22          Convert int to float (temp float)
+0:22            'r07' (temp int)
+0:23      Branch: Return with expression
+0:23        'ps_output' (temp structure{temp 4-component vector of float color})
 0:5  Function Definition: main( (temp void)
 0:5    Function Parameters: 
 0:?     Sequence
@@ -153,12 +193,12 @@ gl_FragCoord origin is upper left
 
 // Module Version 10000
 // Generated by (magic number): 80001
-// Id's are bound by 49
+// Id's are bound by 54
 
                               Capability Shader
                1:             ExtInstImport  "GLSL.std.450"
                               MemoryModel Logical GLSL450
-                              EntryPoint Fragment 4  "main" 46
+                              EntryPoint Fragment 4  "main" 51
                               ExecutionMode 4 OriginUpperLeft
                               Name 4  "main"
                               Name 8  "PS_OUTPUT"
@@ -173,9 +213,13 @@ gl_FragCoord origin is upper left
                               Name 28  "r06"
                               Name 30  "r07"
                               Name 32  "r08"
-                              Name 35  "ps_output"
-                              Name 46  "color"
-                              Decorate 46(color) Location 0
+                              Name 34  "r09"
+                              Name 35  "r10"
+                              Name 36  "r11"
+                              Name 38  "r12"
+                              Name 40  "ps_output"
+                              Name 51  "color"
+                              Decorate 51(color) Location 0
                2:             TypeVoid
                3:             TypeFunction 2
                6:             TypeFloat 32
@@ -195,16 +239,17 @@ gl_FragCoord origin is upper left
               29:     24(int) Constant 6
               31:     24(int) Constant 57
               33:     15(int) Constant 58
-              34:             TypePointer Function 8(PS_OUTPUT)
-              36:     24(int) Constant 0
-              40:             TypePointer Function 7(fvec4)
-              45:             TypePointer Output 7(fvec4)
-       46(color):     45(ptr) Variable Output
+              37:    6(float) Constant 1066192077
+              39:             TypePointer Function 8(PS_OUTPUT)
+              41:     24(int) Constant 0
+              45:             TypePointer Function 7(fvec4)
+              50:             TypePointer Output 7(fvec4)
+       51(color):     50(ptr) Variable Output
          4(main):           2 Function None 3
                5:             Label
-              47:8(PS_OUTPUT) FunctionCall 10(@main()
-              48:    7(fvec4) CompositeExtract 47 0
-                              Store 46(color) 48
+              52:8(PS_OUTPUT) FunctionCall 10(@main()
+              53:    7(fvec4) CompositeExtract 52 0
+                              Store 51(color) 53
                               Return
                               FunctionEnd
       10(@main():8(PS_OUTPUT) Function None 9
@@ -218,7 +263,11 @@ gl_FragCoord origin is upper left
          28(r06):     25(ptr) Variable Function
          30(r07):     25(ptr) Variable Function
          32(r08):     16(ptr) Variable Function
-   35(ps_output):     34(ptr) Variable Function
+         34(r09):     12(ptr) Variable Function
+         35(r10):     12(ptr) Variable Function
+         36(r11):     12(ptr) Variable Function
+         38(r12):     12(ptr) Variable Function
+   40(ps_output):     39(ptr) Variable Function
                               Store 13(r00) 14
                               Store 17(r01) 18
                               Store 19(r02) 20
@@ -228,11 +277,15 @@ gl_FragCoord origin is upper left
                               Store 28(r06) 29
                               Store 30(r07) 31
                               Store 32(r08) 33
-              37:     24(int) Load 30(r07)
-              38:    6(float) ConvertSToF 37
-              39:    7(fvec4) CompositeConstruct 38 38 38 38
-              41:     40(ptr) AccessChain 35(ps_output) 36
-                              Store 41 39
-              42:8(PS_OUTPUT) Load 35(ps_output)
-                              ReturnValue 42
+                              Store 34(r09) 14
+                              Store 35(r10) 14
+                              Store 36(r11) 37
+                              Store 38(r12) 37
+              42:     24(int) Load 30(r07)
+              43:    6(float) ConvertSToF 42
+              44:    7(fvec4) CompositeConstruct 43 43 43 43
+              46:     45(ptr) AccessChain 40(ps_output) 41
+                              Store 46 44
+              47:8(PS_OUTPUT) Load 40(ps_output)
+                              ReturnValue 47
                               FunctionEnd
diff --git a/Test/hlsl.numericsuffixes.frag b/Test/hlsl.numericsuffixes.frag
index 60b2572f3..bccb786da 100644
--- a/Test/hlsl.numericsuffixes.frag
+++ b/Test/hlsl.numericsuffixes.frag
@@ -13,6 +13,10 @@ PS_OUTPUT main()
     int   r06 = 6L;      // upper long int
     int   r07 = 071;     // octal
     uint  r08 = 072u;    // unsigned octal
+    float r09 = 1.h;     // half
+    float r10 = 1.H;     // half
+    float r11 = 1.1h;    // half
+    float r12 = 1.1H;    // half
 
     PS_OUTPUT ps_output;
     ps_output.color = r07; // gets 71 octal = 57 decimal
diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h
index 76c6c5aa6..f9f4ec50a 100644
--- a/glslang/Include/revision.h
+++ b/glslang/Include/revision.h
@@ -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.1854"
-#define GLSLANG_DATE "27-Feb-2017"
+#define GLSLANG_REVISION "Overload400-PrecQual.1858"
+#define GLSLANG_DATE "28-Feb-2017"
diff --git a/glslang/MachineIndependent/Versions.cpp b/glslang/MachineIndependent/Versions.cpp
index 637ed9344..27c42938f 100644
--- a/glslang/MachineIndependent/Versions.cpp
+++ b/glslang/MachineIndependent/Versions.cpp
@@ -687,7 +687,7 @@ void TParseVersions::doubleCheck(const TSourceLoc& loc, const char* op)
 }
 
 #ifdef AMD_EXTENSIONS
-// Call for any operation needing GLSL float16 data-type support.
+// Call for any operation needing float16 data-type support.
 void TParseVersions::float16Check(const TSourceLoc& loc, const char* op, bool builtIn)
 {
     if (!builtIn) {
diff --git a/glslang/MachineIndependent/preprocessor/PpScanner.cpp b/glslang/MachineIndependent/preprocessor/PpScanner.cpp
index a30442400..89748fb2f 100644
--- a/glslang/MachineIndependent/preprocessor/PpScanner.cpp
+++ b/glslang/MachineIndependent/preprocessor/PpScanner.cpp
@@ -101,9 +101,16 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
 {
     bool HasDecimalOrExponent = false;
     int isDouble = 0;
+    bool generateFloat16 = false;
+    bool acceptFloat16 = parseContext.intermediate.getSource() == EShSourceHlsl;
+    bool isFloat16 = false;
+    bool requireHF = false;
 #ifdef AMD_EXTENSIONS
-    int isFloat16 = 0;
-    bool enableFloat16 = parseContext.version >= 450 && parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float);
+    if (parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float)) {
+        acceptFloat16 = true;
+        generateFloat16 = true;
+        requireHF = true;
+    }
 #endif
 
     const auto saveName = [&](int ch) {
@@ -158,21 +165,27 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
             saveName(ch2);
             isDouble = 1;
         }
+    } else if (acceptFloat16 && (ch == 'h' || ch == 'H')) {
 #ifdef AMD_EXTENSIONS
-    } else if (enableFloat16 && (ch == 'h' || ch == 'H')) {
-        parseContext.float16Check(ppToken->loc, "half floating-point suffix");
+        if (generateFloat16)
+            parseContext.float16Check(ppToken->loc, "half floating-point suffix");
+#endif
         if (!HasDecimalOrExponent)
             parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
-        int ch2 = getChar();
-        if (ch2 != 'f' && ch2 != 'F') {
-            ungetChar();
-            ungetChar();
+        if (requireHF) {
+            int ch2 = getChar();
+            if (ch2 != 'f' && ch2 != 'F') {
+                ungetChar();
+                ungetChar();
+            } else {
+                saveName(ch);
+                saveName(ch2);
+                isFloat16 = generateFloat16;
+            }
         } else {
             saveName(ch);
-            saveName(ch2);
-            isFloat16 = 1;
+            isFloat16 = generateFloat16;
         }
-#endif
     } else if (ch == 'f' || ch == 'F') {
         parseContext.profileRequires(ppToken->loc,  EEsProfile, 300, nullptr, "floating-point suffix");
         if (! parseContext.relaxedErrors())
@@ -197,10 +210,8 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
     // Return the right token type
     if (isDouble)
         return PpAtomConstDouble;
-#ifdef AMD_EXTENSIONS
     else if (isFloat16)
         return PpAtomConstFloat16;
-#endif
     else
         return PpAtomConstFloat;
 }
@@ -216,6 +227,15 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken)
     int ii = 0;
     unsigned long long ival = 0;
     bool enableInt64 = pp->parseContext.version >= 450 && pp->parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_int64);
+    bool acceptHalf = pp->parseContext.intermediate.getSource() == EShSourceHlsl;
+#ifdef AMD_EXTENSIONS
+    if (pp->parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float))
+        acceptHalf = true;
+#endif
+
+    const auto floatingPointChar = [&](int ch) { return ch == '.' || ch == 'e' || ch == 'E' ||
+                                                                     ch == 'f' || ch == 'F' ||
+                                                     (acceptHalf && (ch == 'h' || ch == 'H')); };
 
     ppToken->ival = 0;
     ppToken->i64val = 0;
@@ -380,7 +400,7 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken)
                         ch = getch();
                     } while (ch >= '0' && ch <= '9');
                 }
-                if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'E' || ch == 'F')
+                if (floatingPointChar(ch))
                     return pp->lFloatConst(len, ch, ppToken);
 
                 // wasn't a float, so must be octal...
@@ -435,9 +455,9 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken)
                 }
                 ch = getch();
             } while (ch >= '0' && ch <= '9');
-            if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'E' || ch == 'F') {
+            if (floatingPointChar(ch))
                 return pp->lFloatConst(len, ch, ppToken);
-            } else {
+            else {
                 // Finish handling signed and unsigned integers
                 int numericLen = len;
                 bool isUnsigned = false;
diff --git a/glslang/MachineIndependent/preprocessor/PpTokens.h b/glslang/MachineIndependent/preprocessor/PpTokens.h
index 9695c2fcc..923fd9fa0 100644
--- a/glslang/MachineIndependent/preprocessor/PpTokens.h
+++ b/glslang/MachineIndependent/preprocessor/PpTokens.h
@@ -127,9 +127,7 @@ enum EFixedAtoms {
     PpAtomConstUint64,
     PpAtomConstFloat,
     PpAtomConstDouble,
-#ifdef AMD_EXTENSIONS
     PpAtomConstFloat16,
-#endif
     PpAtomConstString,
 
     // Identifiers
-- 
GitLab