diff options
author | Alon Zakai <azakai@mozilla.com> | 2011-01-27 21:31:20 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2011-01-27 21:31:20 -0800 |
commit | 5958a6a754324de9eeff39fe1f21ba2b7042f833 (patch) | |
tree | b9a5f9929c3cbe64f9b45ae7be4b6037f2b7b33d /src/preamble.js | |
parent | 8e51dd91884b48f9c59fed12e995ed9fdbdcd5f8 (diff) |
reSign parallel to unSign to fix rare signing issues; CHECK_SIGNS option
Diffstat (limited to 'src/preamble.js')
-rw-r--r-- | src/preamble.js | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/preamble.js b/src/preamble.js index e375b90a..ade02496 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -349,7 +349,27 @@ function intArrayToString(array) { // example, -1 in int32 would be a very large number as unsigned. function unSign(value, bits) { if (value >= 0) return value; - return 2*Math.abs(1 << (bits-1)) + value; +#if CHECK_SIGNS + print('WARNING: unSign needed, ' + [value, bits] + ' at ' + new Error().stack); +#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) { + 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 + print('WARNING: reSign needed, ' + [value, bits] + ' at ' + new Error().stack); +#endif + value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts + } + return value; } // === Body === |