Bug 1122344: Fix rounding of big negative float32 values in Ion on x86; r=mjrosenb

--HG--
extra : rebase_source : 9278fbb2516a3f0d9e287570ee752ecb9d2c5b04
This commit is contained in:
Benjamin Bouvier 2015-02-06 14:13:42 +01:00
parent d2c5d3a86c
commit 4cd6736afd
4 changed files with 119 additions and 29 deletions

View File

@ -1,20 +0,0 @@
function roundf(y) {
return Math.round(Math.fround(y));
}
var x = -1;
assertEq(roundf(x), x);
assertEq(roundf(x), x);
var x = -2;
assertEq(roundf(x), x);
assertEq(roundf(x), x);
var x = -1024;
assertEq(roundf(x), x);
var x = -14680050;
assertEq(roundf(x), Math.fround(x));
var x = -8388610;
assertEq(roundf(x), Math.fround(x));

View File

@ -1,7 +0,0 @@
function f(x) {
var y = Math.fround(x);
assertEq(y, Math.pow(y, 1));
}
f(0);
f(2147483647);

View File

@ -0,0 +1,103 @@
// Bug 1073910
(function() {
function roundf(y) {
return Math.round(Math.fround(y));
}
var x = -1;
assertEq(roundf(x), x);
assertEq(roundf(x), x);
var x = -2;
assertEq(roundf(x), x);
assertEq(roundf(x), x);
var x = -1024;
assertEq(roundf(x), x);
var x = -14680050;
assertEq(roundf(x), Math.fround(x));
var x = -8388610;
assertEq(roundf(x), Math.fround(x));
})();
// Bug 1000606
(function() {
function f() {
var d = Math.fround(0.4999999701976776);
return Math.round(d);
}
assertEq(f(), f());
function g() {
var c = Math.fround(8886111);
return Math.round(c);
}
assertEq(g(), g());
})();
// Bug 1124485
(function() {
function h(x) {
var y = Math.fround(x);
assertEq(y, Math.pow(y, 1));
}
h(0);
h(2147483647);
})();
// Bug 1122344
(function() {
function f() {
return Math.round(Math.fround(-13527757));
};
assertEq(f(), f());
})();
(function() {
// Test values around -0.5 and +0.5
var f32 = new Float32Array(1);
var i32 = new Int32Array(f32.buffer);
function round(x) { return Math.round(x); }
function roundf(x) { return Math.round(Math.fround(x)); }
// Warm up
round(2.5);
round(3.5);
roundf(2.5);
roundf(3.5);
f32[0] = 0.5;
i32[0] += 1;
print('0.5+e =', f32[0]);
var x = f32[0];
assertEq(round(x), 1);
assertEq(roundf(x), 1);
f32[0] = 0.5;
i32[0] -= 1;
print('0.5-e =', f32[0]);
var x = f32[0];
assertEq(round(x), 0);
assertEq(roundf(x), 0);
f32[0] = -0.5;
i32[0] += 1;
print('-0.5-e =', f32[0]);
var x = f32[0];
assertEq(round(x), -1);
assertEq(roundf(x), -1);
f32[0] = -0.5;
i32[0] -= 1;
print('-0.5+e =', f32[0]);
var x = f32[0];
assertEq(round(x), -0);
assertEq(roundf(x), -0);
})();

View File

@ -1812,6 +1812,7 @@ CodeGeneratorX86Shared::visitRound(LRound *lir)
// Branch to a slow path for non-positive inputs. Doesn't catch NaN.
masm.zeroDouble(scratch);
masm.loadConstantDouble(GetBiggestNumberLessThan(0.5), temp);
masm.branchDouble(Assembler::DoubleLessThanOrEqual, input, scratch, &negativeOrZero);
// Input is positive. Add the biggest double less than 0.5 and
@ -1819,7 +1820,6 @@ CodeGeneratorX86Shared::visitRound(LRound *lir)
// than 0.5, adding 0.5 would undesirably round up to 1). Note that we have
// to add the input to the temp register because we're not allowed to
// modify the input register.
masm.loadConstantDouble(GetBiggestNumberLessThan(0.5), temp);
masm.addDouble(input, temp);
bailoutCvttsd2si(temp, output, lir->snapshot());
@ -1840,7 +1840,14 @@ CodeGeneratorX86Shared::visitRound(LRound *lir)
// Input is negative.
masm.bind(&negative);
// Inputs in ]-0.5; 0] need to be added 0.5, other negative inputs need to
// be added the biggest double less than 0.5.
Label loadJoin;
masm.loadConstantDouble(-0.5, scratch);
masm.branchDouble(Assembler::DoubleLessThan, input, scratch, &loadJoin);
masm.loadConstantDouble(0.5, temp);
masm.bind(&loadJoin);
if (AssemblerX86Shared::HasSSE41()) {
// Add 0.5 and round toward -Infinity. The result is stored in the temp
@ -1894,6 +1901,7 @@ CodeGeneratorX86Shared::visitRoundF(LRoundF *lir)
// Branch to a slow path for non-positive inputs. Doesn't catch NaN.
masm.zeroFloat32(scratch);
masm.loadConstantFloat32(GetBiggestNumberLessThan(0.5f), temp);
masm.branchFloat(Assembler::DoubleLessThanOrEqual, input, scratch, &negativeOrZero);
// Input is non-negative. Add the biggest float less than 0.5 and truncate,
@ -1901,7 +1909,6 @@ CodeGeneratorX86Shared::visitRoundF(LRoundF *lir)
// adding 0.5 would undesirably round up to 1). Note that we have to add
// the input to the temp register because we're not allowed to modify the
// input register.
masm.loadConstantFloat32(GetBiggestNumberLessThan(0.5f), temp);
masm.addFloat32(input, temp);
bailoutCvttss2si(temp, output, lir->snapshot());
@ -1923,7 +1930,14 @@ CodeGeneratorX86Shared::visitRoundF(LRoundF *lir)
// Input is negative.
masm.bind(&negative);
// Inputs in ]-0.5; 0] need to be added 0.5, other negative inputs need to
// be added the biggest double less than 0.5.
Label loadJoin;
masm.loadConstantFloat32(-0.5f, scratch);
masm.branchFloat(Assembler::DoubleLessThan, input, scratch, &loadJoin);
masm.loadConstantFloat32(0.5f, temp);
masm.bind(&loadJoin);
if (AssemblerX86Shared::HasSSE41()) {
// Add 0.5 and round toward -Infinity. The result is stored in the temp