diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jsifier.js | 4 | ||||
-rw-r--r-- | src/library.js | 30 | ||||
-rw-r--r-- | src/preamble.js | 28 |
3 files changed, 36 insertions, 26 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 4bc20758..2c33eba1 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -788,6 +788,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { if (!correctSigns() && !CHECK_SIGNS) return value; if (type in Runtime.INT_TYPES) { var bits = parseInt(type.substr(1)); + if (isNumber(value)) { + // Sign/unsign constants at compile time + return eval(op + 'Sign(' + value + ', ' + bits + ', 1)').toString(); + } // shortcuts for 32-bit case if (bits === 32 && !CHECK_SIGNS) { if (op === 're') { diff --git a/src/library.js b/src/library.js index b875e131..08daceb1 100644 --- a/src/library.js +++ b/src/library.js @@ -1230,3 +1230,33 @@ var Library = { } }; + +// 'Runtime' functions that need preprocessor parsing like library functions + +// Converts a value we have as signed, into an unsigned value. For +// example, -1 in int32 would be a very large number as unsigned. +function unSign(value, bits, ignore) { + if (value >= 0) return value; +#if CHECK_SIGNS + if (!ignore) CorrectionsMonitor.note('UnSign'); +#endif + return bits <= 32 ? 2*Math.abs(1 << (bits-1)) + value // Need some trickery, since if bits == 32, we are right at the limit of the bits JS uses in bitshifts + : Math.pow(2, bits) + value; + // TODO: clean up previous line +} + +// Converts a value we have as unsigned, into a signed value. For +// example, 200 in a uint8 would be a negative number. +function reSign(value, bits, ignore) { + if (value <= 0) return value; + var half = bits <= 32 ? Math.abs(1 << (bits-1)) // abs is needed if bits == 32 + : Math.pow(2, bits-1); + if (value >= half) { +#if CHECK_SIGNS + if (!ignore) CorrectionsMonitor.note('ReSign'); +#endif + value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts + } + return value; +} + diff --git a/src/preamble.js b/src/preamble.js index 2eb745a5..9f457a5c 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -383,32 +383,8 @@ function intArrayToString(array) { return ret; } -// Converts a value we have as signed, into an unsigned value. For -// example, -1 in int32 would be a very large number as unsigned. -function unSign(value, bits, ignore) { - if (value >= 0) return value; -#if CHECK_SIGNS - if (!ignore) CorrectionsMonitor.note('UnSign'); -#endif - return bits <= 32 ? 2*Math.abs(1 << (bits-1)) + value // Need some trickery, since if bits == 32, we are right at the limit of the bits JS uses in bitshifts - : Math.pow(2, bits) + value; - // TODO: clean up previous line -} - -// Converts a value we have as unsigned, into a signed value. For -// example, 200 in a uint8 would be a negative number. -function reSign(value, bits, ignore) { - if (value <= 0) return value; - var half = bits <= 32 ? Math.abs(1 << (bits-1)) // abs is needed if bits == 32 - : Math.pow(2, bits-1); - if (value >= half) { -#if CHECK_SIGNS - if (!ignore) CorrectionsMonitor.note('ReSign'); -#endif - value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts - } - return value; -} +var unSign = {{{ unSign.toString() }}} +var reSign = {{{ reSign.toString() }}} // === Body === |