diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-12-12 16:53:54 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-12-12 16:53:54 -0800 |
commit | ed95ef39551d6f70f6c003ebb57e8e5c80221404 (patch) | |
tree | 45cff9812e63ee29478ed5b4912a1dc83c8ecd12 | |
parent | 8f42941647a97122aa5b357aeff5970f72a30019 (diff) |
refactor makeRounding in asm
-rw-r--r-- | src/parseTools.js | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index 0bacebd9..74211112 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -1707,34 +1707,36 @@ function makeSignOp(value, type, op, force, ignore) { function makeRounding(value, bits, signed, floatConversion) { // TODO: handle roundings of i64s assert(bits); - if (ASM_JS && floatConversion && bits <= 32) { - return '(~~(' + value + '))'; // explicit float-to-int conversion - } - - // C rounds to 0 (-5.5 to -5, +5.5 to 5), while JS has no direct way to do that. - if (bits <= 32 && signed) return '((' + value + ')&-1)'; // This is fast and even correct, for all cases. Note that it is the same - // as |0, but &-1 hints to the js optimizer that this is a rounding correction - // Do Math.floor, which is reasonably fast, if we either don't care, or if we can be sure - // the value is non-negative - if (!correctRoundings() || (!signed && !floatConversion)) return 'Math.floor(' + value + ')'; - // We are left with >32 bits signed, or a float conversion. Check and correct inline - // Note that if converting a float, we may have the wrong sign at this point! But, we have - // been rounded properly regardless, and we will be sign-corrected later when actually used, if - // necessary. - return makeInlineCalculation('VALUE >= 0 ? Math.floor(VALUE) : Math.ceil(VALUE)', value, 'tempBigIntR'); -/* refactored version - needs perf testing TODO - if (bits <= 32) { - if (signed) { - return '((' + value + ')&-1)'; // &-1 (instead of |0) hints to the js optimizer that this is a rounding correction - } else { - return '((' + value + ')>>>0)'; + if (!ASM_JS) { + // C rounds to 0 (-5.5 to -5, +5.5 to 5), while JS has no direct way to do that. + if (bits <= 32 && signed) return '((' + value + ')&-1)'; // This is fast and even correct, for all cases. Note that it is the same + // as |0, but &-1 hints to the js optimizer that this is a rounding correction + // Do Math.floor, which is reasonably fast, if we either don't care, or if we can be sure + // the value is non-negative + if (!correctRoundings() || (!signed && !floatConversion)) return 'Math.floor(' + value + ')'; + // We are left with >32 bits signed, or a float conversion. Check and correct inline + // Note that if converting a float, we may have the wrong sign at this point! But, we have + // been rounded properly regardless, and we will be sign-corrected later when actually used, if + // necessary. + return makeInlineCalculation('VALUE >= 0 ? Math.floor(VALUE) : Math.ceil(VALUE)', value, 'tempBigIntR'); + } else { + // asm.js mode, cleaner refactoring of this function as well. TODO: use in non-asm case, most of this + if (floatConversion && bits <= 32) { + return '(~~(' + value + '))'; // explicit float-to-int conversion + } + + if (bits <= 32) { + if (signed) { + return '((' + value + ')&-1)'; // &-1 (instead of |0) hints to the js optimizer that this is a rounding correction + } else { + return '((' + value + ')>>>0)'; + } } + // Math.floor is reasonably fast if we don't care about corrections (and even correct if unsigned) + if (!correctRoundings() || !signed) return 'Math.floor(' + value + ')'; + // We are left with >32 bits + return makeInlineCalculation('VALUE >= 0 ? Math.floor(VALUE) : Math.ceil(VALUE)', value, 'tempBigIntR'); } - // Math.floor is reasonably fast if we don't care about corrections (and even correct if unsigned) - if (!correctRoundings() || !signed) return 'Math.floor(' + value + ')'; - // We are left with >32 bits - return makeInlineCalculation('VALUE >= 0 ? Math.floor(VALUE) : Math.ceil(VALUE)', value, 'tempBigIntR'); -*/ } // fptoui and fptosi are not in these, because we need to be careful about what we do there. We can't |