From 4fd99d23ccc02d9f40b4429e20ea94c231276b9e Mon Sep 17 00:00:00 2001 From: "serya@chromium.org" Date: Thu, 11 Mar 2010 08:31:15 +0000 Subject: [PATCH] Math.abs rewrited to not use Runtime. Review URL: http://codereview.chromium.org/799006 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4093 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/math.js | 3 ++- src/runtime.cc | 10 ---------- src/runtime.h | 1 - src/v8-counters.h | 1 - test/mjsunit/abs.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 test/mjsunit/abs.js diff --git a/src/math.js b/src/math.js index 034f32d..f6cd055 100644 --- a/src/math.js +++ b/src/math.js @@ -45,7 +45,8 @@ $Math.__proto__ = global.Object.prototype; function MathAbs(x) { if (%_IsSmi(x)) return x >= 0 ? x : -x; if (!IS_NUMBER(x)) x = ToNumber(x); - return %Math_abs(x); + if (x === 0) return 0; // To handle -0. + return x > 0 ? x : -x; } // ECMA 262 - 15.8.2.2 diff --git a/src/runtime.cc b/src/runtime.cc index 6d3a158..20049df 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -4847,16 +4847,6 @@ static Object* Runtime_StringCompare(Arguments args) { } -static Object* Runtime_Math_abs(Arguments args) { - NoHandleAllocation ha; - ASSERT(args.length() == 1); - Counters::math_abs.Increment(); - - CONVERT_DOUBLE_CHECKED(x, args[0]); - return Heap::AllocateHeapNumber(fabs(x)); -} - - static Object* Runtime_Math_acos(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 1); diff --git a/src/runtime.h b/src/runtime.h index 8c2f86d..b8891bd 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -132,7 +132,6 @@ namespace internal { F(StringCompare, 2, 1) \ \ /* Math */ \ - F(Math_abs, 1, 1) \ F(Math_acos, 1, 1) \ F(Math_asin, 1, 1) \ F(Math_atan, 1, 1) \ diff --git a/src/v8-counters.h b/src/v8-counters.h index 2b493fb..b595cd4 100644 --- a/src/v8-counters.h +++ b/src/v8-counters.h @@ -170,7 +170,6 @@ namespace internal { SC(regexp_entry_native, V8.RegExpEntryNative) \ SC(number_to_string_native, V8.NumberToStringNative) \ SC(number_to_string_runtime, V8.NumberToStringRuntime) \ - SC(math_abs, V8.MathAbs) \ SC(math_acos, V8.MathAcos) \ SC(math_asin, V8.MathAsin) \ SC(math_atan, V8.MathAtan) \ diff --git a/test/mjsunit/abs.js b/test/mjsunit/abs.js new file mode 100644 index 0000000..d1c453c --- /dev/null +++ b/test/mjsunit/abs.js @@ -0,0 +1,48 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Test Math.sin and Math.abs. + +assertEquals(1, Math.abs(1)); // Positive SMI. +assertEquals(1, Math.abs(-1)); // Negative SMI. +assertEquals(0.5, Math.abs(0.5)); // Positive double. +assertEquals(0.5, Math.abs(-0.5)); // Negative double. +assertEquals('Infinity', Math.abs(Number('+Infinity').toString())); +assertEquals('Infinity', Math.abs(Number('-Infinity').toString())); +assertEquals('NaN', Math.abs(NaN).toString()); +assertEquals('NaN', Math.abs(-NaN).toString()); + +var minusZero = 1 / (-1 / 0); +function isMinusZero(x) { + return x === 0 && 1 / x < 0; +} + +assertTrue(!isMinusZero(0)); +assertTrue(isMinusZero(minusZero)); +assertEquals(0, Math.abs(minusZero)); +assertTrue(!isMinusZero(Math.abs(minusZero))); +assertTrue(!isMinusZero(Math.abs(0.0))); -- 2.7.4