[llvm-commits] [hlvm] r38316 - /hlvm/trunk/hlvm/Pass/Validate.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 17:02:17 PDT 2007


Author: reid
Date: Sat Jul  7 19:02:17 2007
New Revision: 38316

URL: http://llvm.org/viewvc/llvm-project?rev=38316&view=rev
Log:
Implement checking of floating point constant value ranges.

Modified:
    hlvm/trunk/hlvm/Pass/Validate.cpp

Modified: hlvm/trunk/hlvm/Pass/Validate.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Validate.cpp?rev=38316&r1=38315&r2=38316&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Pass/Validate.cpp (original)
+++ hlvm/trunk/hlvm/Pass/Validate.cpp Sat Jul  7 19:02:17 2007
@@ -43,6 +43,7 @@
 #include <llvm/Support/MathExtras.h>
 #include <llvm/ADT/StringExtras.h>
 #include <iostream>
+#include <cfloat>
 
 using namespace hlvm;
 using namespace llvm;
@@ -498,9 +499,49 @@
   if (checkConstant(CR,ConstantRealID)) {
     const char* startp = CR->getValue().c_str();
     char* endp = 0;
-    double val = strtod(startp,&endp);
+    long double val = strtold(startp,&endp);
     if (!endp || startp == endp || *endp != '\0')
       error(CR,"Invalid real constant. Conversion failed.");
+    else {
+      // It converted to a double okay, check that it is in range
+      unsigned numBits = 0;
+      switch (CR->getType()->getID()) {
+        case Float32TypeID: numBits = 32; break;
+        case Float44TypeID: numBits = 44; break;
+        case Float64TypeID: numBits = 64; break;
+        case Float80TypeID: numBits = 80; break;
+        case Float128TypeID: numBits = 128; break;
+        case RealTypeID: 
+        {
+          const RealType* Ty = llvm::cast<RealType>(CR->getType());
+          numBits = Ty->getMantissa() + Ty->getExponent() + 1; 
+          break;
+        }
+        default:
+          error(CR,"Invalid floating point type");
+          return;
+      }
+      bool isValid = false;
+      if (numBits <= sizeof(float)*8) {
+        float x = val;
+        long double x2 = x;
+        isValid = (val <= FLT_MAX && val >= FLT_MIN && val == x2);
+      } 
+      else if (numBits <= sizeof(double)*8)
+      {
+        double x = val;
+        long double x2 = x;
+        isValid = (val <= DBL_MAX && val >= DBL_MIN && val == x2);
+      }
+      else if (numBits > sizeof(long double)*8)
+      {
+        warning(CR,"Don't know how to check range of real type > 128 bits");
+        isValid = true;
+      }
+      if (!isValid)
+        error(CR,"Real constant out of range for real requiring " +
+          utostr(numBits) + " bits");
+    }
   }
 }
 





More information about the llvm-commits mailing list