From: oleg@chromium.org Date: Tue, 16 Mar 2010 13:23:41 +0000 (+0000) Subject: Add a special case in Math.round for a SMI result. Also change the implementation... X-Git-Tag: upstream/4.7.83~22238 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=78f19f1ae94930b920e5e93ad098382abaf7ac6a;p=platform%2Fupstream%2Fv8.git Add a special case in Math.round for a SMI result. Also change the implementation for non-SMI case. Review URL: http://codereview.chromium.org/981002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4146 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/runtime.cc b/src/runtime.cc index 4e4856dd8..492bca794 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -5325,12 +5325,21 @@ static Object* Runtime_Math_round(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 1); Counters::math_round.Increment(); - CONVERT_DOUBLE_CHECKED(x, args[0]); + + if (x > 0 && x < Smi::kMaxValue) { + return Smi::FromInt(static_cast(x + 0.5)); + } + if (signbit(x) && x >= -0.5) return Heap::minus_zero_value(); - double integer = ceil(x); - if (integer - x > 0.5) { integer -= 1.0; } - return Heap::NumberFromDouble(integer); + + // if the magnitude is big enough, there's no place for fraction part. If we + // try to add 0.5 to this number, 1.0 will be added instead. + if (x >= 9007199254740991.0 || x <= -9007199254740991.0) { + return args[0]; + } + + return Heap::NumberFromDouble(floor(x + 0.5)); }