Reviewers: ulan, danno, Paul Lind, kisg,

Description:
MIPS: Fix rounding in Uint8ClampedArray setter.

Port r12364 (31e40def)

Original commit message:
According to Web IDL spec, we should round to
the nearest integer, choosing the even integer
if it lies halfway between two.

BUG=
TEST=


Please review this at https://chromiumcodereview.appspot.com/10870049/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/mips/macro-assembler-mips.cc
  M src/mips/simulator-mips.cc


Index: src/mips/macro-assembler-mips.cc
diff --git a/src/mips/macro-assembler-mips.cc b/src/mips/macro-assembler-mips.cc index 515685590eaadf5347dfbd2bb85b95281219e61e..d99fbdcaa4f426f4e140ca8bb3613d0c325c3f34 100644
--- a/src/mips/macro-assembler-mips.cc
+++ b/src/mips/macro-assembler-mips.cc
@@ -5398,7 +5398,7 @@ void MacroAssembler::ClampDoubleToUint8(Register result_reg,

   // In 0-255 range, round and truncate.
   bind(&in_bounds);
-  round_w_d(temp_double_reg, input_reg);
+  cvt_w_d(temp_double_reg, input_reg);
   mfc1(result_reg, temp_double_reg);
   bind(&done);
 }
Index: src/mips/simulator-mips.cc
diff --git a/src/mips/simulator-mips.cc b/src/mips/simulator-mips.cc
index 66d0da71fa4260e086b77b99f8f3773466db5d44..77d64f0782a3c832faa3be86a505b3633d7a1386 100644
--- a/src/mips/simulator-mips.cc
+++ b/src/mips/simulator-mips.cc
@@ -2068,10 +2068,15 @@ void Simulator::DecodeTypeRegister(Instruction* instr) {
               // Rounding modes are not yet supported.
               ASSERT((FCSR_ & 3) == 0);
               // In rounding mode 0 it should behave like ROUND.
-            case ROUND_W_D:  // Round double to word.
+            case ROUND_W_D:  // Round double to word (round half to even).
               {
                 double rounded = fs > 0 ? floor(fs + 0.5) : ceil(fs - 0.5);
                 int32_t result = static_cast<int32_t>(rounded);
+                if ((result & 1) != 0 && result - fs == 0.5) {
+                  // If the number is halfway between two integers,
+                  // round to the even one.
+                  result--;
+                }
                 set_fpu_register(fd_reg, result);
                 if (set_fcsr_round_error(fs, rounded)) {
                   set_fpu_register(fd_reg, kFPUInvalidResult);


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to