Fix determining the loop iteration count.

Bug b/34128224

Change-Id: I4aebcda840baa8ceef2ae99c98a80a83e32b89b3
Reviewed-on: https://swiftshader-review.googlesource.com/8376
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/compiler/OutputASM.cpp b/src/OpenGL/compiler/OutputASM.cpp
index acd8fbf..49f62b1 100644
--- a/src/OpenGL/compiler/OutputASM.cpp
+++ b/src/OpenGL/compiler/OutputASM.cpp
@@ -24,6 +24,8 @@
 #include <GLES2/gl2ext.h>
 #include <GLES3/gl3.h>
 
+#include <stdlib.h>
+
 namespace glsl
 {
 	// Integer to TString conversion
@@ -3599,11 +3601,16 @@
 
 			if(comparator == EOpLessThan)
 			{
-				int iterations = (limit - initial) / increment;
-
-				if(iterations <= 0)
+				if(!(initial < limit))   // Never loops
 				{
-					iterations = 0;
+					return 0;
+				}
+
+				int iterations = (limit - initial + abs(increment) - 1) / increment;   // Ceiling division
+
+				if(iterations < 0)
+				{
+					return ~0u;
 				}
 
 				return iterations;