diff options
author | alon@honor <none@none> | 2010-09-24 20:04:29 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-09-24 20:04:29 -0700 |
commit | 9352899596e6ee328824117031fc56fd2123336e (patch) | |
tree | 3f435cd040e587ea0d37085a98a2f516ab45c8d0 | |
parent | b170e6ca85023a5d572153b5e151935b6873ff7d (diff) |
IEEE floats fixes
-rw-r--r-- | src/parseTools.js | 40 | ||||
-rw-r--r-- | tests/runner.py | 14 |
2 files changed, 29 insertions, 25 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index 3221a829..456b44f1 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -254,7 +254,7 @@ function parseParamTokens(params) { intertype: 'value', type: segment[0], value: segment[1], - ident: segment[1].text, + ident: parseNumerical(segment[1].text), }); // } else { // throw "what is this params token? " + JSON.stringify(segment); @@ -308,22 +308,6 @@ function cleanOutTokens(filterOut, tokens, index) { } } -function _HexToInt(stringy) { - var ret = 0; - var mul = 1; - var base; - for (var i = (stringy.length - 1); i >= 0; i = i - 1) { - if (stringy.charCodeAt(i) >= "A".charCodeAt(0)) { - base = "A".charCodeAt(0) - 10; - } else { - base = "0".charCodeAt(0); - } - ret = ret + (mul*(stringy.charCodeAt(i) - base)); - mul = mul * 16; - } - return ret; -} - function _IntToHex(x) { assert(x >= 0 && x <= 15); if (x <= 9) { @@ -334,15 +318,23 @@ function _IntToHex(x) { } function IEEEUnHex(stringy) { - stringy = stringy.substr(2); // '0x'; - var top = _HexToInt(stringy[0]); - var neg = !!(top & 8); + stringy = stringy.substr(2); // leading '0x'; + var top = eval('0x' + stringy[0]); + var neg = !!(top & 8); // sign if (neg) { stringy = _IntToHex(top & ~8) + stringy.substr(1); } - var a = _HexToInt(stringy.substr(0, 8)); - var b = _HexToInt(stringy.substr(8)); - var e = (a >> ((52 - 32) & 0x7ff)) - 1023; + var a = eval('0x' + stringy.substr(0, 8)); // top half + var b = eval('0x' + stringy.substr(8)); // bottom half + var e = a >> ((52 - 32) & 0x7ff); // exponent + if (e === 0x7ff) { + if (a == 0 && b == 0) { + return Infinity; + } else { + return NaN; + } + } + e -= 1023; // offset return (((((a & 0xfffff | 0x100000) * 1.0) / Math.pow(2,52-32)) * Math.pow(2, e)) + (((b * 1.0) / Math.pow(2, 52)) * Math.pow(2, e)) * (neg ? -1 : 1)).toString(); } @@ -370,7 +362,7 @@ function parseLLVMString(str) { ret.push(chr.charCodeAt(0)); i++; } else { - ret.push(_HexToInt(str[i+1]+str[i+2])); + ret.push(eval('0x' + str[i+1]+str[i+2])); i += 3; } } diff --git a/tests/runner.py b/tests/runner.py index 186d4bbc..28f136ca 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -74,7 +74,7 @@ class T(unittest.TestCase): output_processor(output) if output is not None and 'Traceback' in output: print output; assert (0) # 'generating JavaScript failed' if DEBUG: print "\nGenerated JavaScript:\n\n===\n\n%s\n\n===\n\n" % output - #assert(0) + #assert(0) # XXX js_output = timeout_run(Popen([JS_ENGINE] + JS_ENGINE_OPTS + [filename + '.o.js'] + args, stdout=PIPE, stderr=STDOUT), 60, 'Execution') if output_nicerizer is not None: js_output = output_nicerizer(js_output) @@ -147,6 +147,18 @@ class T(unittest.TestCase): ''' self.do_test(src, '*1,10,10.5,1,1.2339') + def test_math(self): + src = ''' + #include <stdio.h> + #include <cmath> + int main() + { + printf("*%f\\n", M_PI); + return 0; + } + ''' + self.do_test(src, '*3.14159') + def test_if(self): src = ''' #include <stdio.h> |