Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/runtime.cc

Issue 1008004: Improve Math.round(). Fix the bug in r4146. Further improve performance by ch... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/runtime.h ('k') | test/mjsunit/math-round.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 5332 matching lines...) Expand 10 before | Expand all | Expand 10 after
5343 if (y == 0) { 5343 if (y == 0) {
5344 return Smi::FromInt(1); 5344 return Smi::FromInt(1);
5345 } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { 5345 } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) {
5346 return Heap::nan_value(); 5346 return Heap::nan_value();
5347 } else { 5347 } else {
5348 return Heap::AllocateHeapNumber(pow(x, y)); 5348 return Heap::AllocateHeapNumber(pow(x, y));
5349 } 5349 }
5350 } 5350 }
5351 5351
5352 5352
5353 static Object* Runtime_Math_round(Arguments args) { 5353 static Object* Runtime_RoundNumber(Arguments args) {
5354 NoHandleAllocation ha; 5354 NoHandleAllocation ha;
5355 ASSERT(args.length() == 1); 5355 ASSERT(args.length() == 1);
5356 Counters::math_round.Increment(); 5356 Counters::math_round.Increment();
5357 5357
5358 CONVERT_DOUBLE_CHECKED(x, args[0]); 5358 if (!args[0]->IsHeapNumber()) {
5359 if (signbit(x) && x >= -0.5) return Heap::minus_zero_value(); 5359 // Must be smi. Return the argument unchanged for all the other types
5360 double integer = ceil(x); 5360 // to make fuzz-natives test happy.
5361 if (integer - x > 0.5) { integer -= 1.0; } 5361 return args[0];
5362 return Heap::NumberFromDouble(integer); 5362 }
5363
5364 HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]);
5365
5366 double value = number->value();
5367 int exponent = number->get_exponent();
5368 int sign = number->get_sign();
5369
5370 // We compare with kSmiValueSize - 3 because (2^30 - 0.1) has exponent 29 and
5371 // should be rounded to 2^30, which is not smi.
5372 if (!sign && exponent <= kSmiValueSize - 3) {
5373 return Smi::FromInt(static_cast<int>(value + 0.5));
5374 }
5375
5376 // If the magnitude is big enough, there's no place for fraction part. If we
5377 // try to add 0.5 to this number, 1.0 will be added instead.
5378 if (exponent >= 52) {
5379 return number;
5380 }
5381
5382 if (sign && value >= -0.5) return Heap::minus_zero_value();
5383
5384 return Heap::NumberFromDouble(floor(value + 0.5));
5363 } 5385 }
5364 5386
5365 5387
5366 static Object* Runtime_Math_sin(Arguments args) { 5388 static Object* Runtime_Math_sin(Arguments args) {
5367 NoHandleAllocation ha; 5389 NoHandleAllocation ha;
5368 ASSERT(args.length() == 1); 5390 ASSERT(args.length() == 1);
5369 Counters::math_sin.Increment(); 5391 Counters::math_sin.Increment();
5370 5392
5371 CONVERT_DOUBLE_CHECKED(x, args[0]); 5393 CONVERT_DOUBLE_CHECKED(x, args[0]);
5372 return TranscendentalCache::Get(TranscendentalCache::SIN, x); 5394 return TranscendentalCache::Get(TranscendentalCache::SIN, x);
(...skipping 4029 matching lines...) Expand 10 before | Expand all | Expand 10 after
9402 } else { 9424 } else {
9403 // Handle last resort GC and make sure to allow future allocations 9425 // Handle last resort GC and make sure to allow future allocations
9404 // to grow the heap without causing GCs (if possible). 9426 // to grow the heap without causing GCs (if possible).
9405 Counters::gc_last_resort_from_js.Increment(); 9427 Counters::gc_last_resort_from_js.Increment();
9406 Heap::CollectAllGarbage(false); 9428 Heap::CollectAllGarbage(false);
9407 } 9429 }
9408 } 9430 }
9409 9431
9410 9432
9411 } } // namespace v8::internal 9433 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | test/mjsunit/math-round.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698