| OLD | NEW |
| 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 4150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4161 CONVERT_DOUBLE_CHECKED(y, args[1]); | 4161 CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 4162 if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { | 4162 if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
| 4163 return Heap::nan_value(); | 4163 return Heap::nan_value(); |
| 4164 } else if (y == 0) { | 4164 } else if (y == 0) { |
| 4165 return Smi::FromInt(1); | 4165 return Smi::FromInt(1); |
| 4166 } else { | 4166 } else { |
| 4167 return Heap::AllocateHeapNumber(pow(x, y)); | 4167 return Heap::AllocateHeapNumber(pow(x, y)); |
| 4168 } | 4168 } |
| 4169 } | 4169 } |
| 4170 | 4170 |
| 4171 // Returns a number value with positive sign, greater than or equal to | |
| 4172 // 0 but less than 1, chosen randomly. | |
| 4173 static Object* Runtime_Math_random(Arguments args) { | |
| 4174 NoHandleAllocation ha; | |
| 4175 ASSERT(args.length() == 0); | |
| 4176 | |
| 4177 // To get much better precision, we combine the results of two | |
| 4178 // invocations of random(). The result is computed by normalizing a | |
| 4179 // double in the range [0, RAND_MAX + 1) obtained by adding the | |
| 4180 // high-order bits in the range [0, RAND_MAX] with the low-order | |
| 4181 // bits in the range [0, 1). | |
| 4182 double lo = static_cast<double>(random()) * (1.0 / (RAND_MAX + 1.0)); | |
| 4183 double hi = static_cast<double>(random()); | |
| 4184 double result = (hi + lo) * (1.0 / (RAND_MAX + 1.0)); | |
| 4185 ASSERT(result >= 0 && result < 1); | |
| 4186 return Heap::AllocateHeapNumber(result); | |
| 4187 } | |
| 4188 | |
| 4189 | 4171 |
| 4190 static Object* Runtime_Math_round(Arguments args) { | 4172 static Object* Runtime_Math_round(Arguments args) { |
| 4191 NoHandleAllocation ha; | 4173 NoHandleAllocation ha; |
| 4192 ASSERT(args.length() == 1); | 4174 ASSERT(args.length() == 1); |
| 4193 | 4175 |
| 4194 CONVERT_DOUBLE_CHECKED(x, args[0]); | 4176 CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 4195 if (signbit(x) && x >= -0.5) return Heap::minus_zero_value(); | 4177 if (signbit(x) && x >= -0.5) return Heap::minus_zero_value(); |
| 4196 return Heap::NumberFromDouble(floor(x + 0.5)); | 4178 return Heap::NumberFromDouble(floor(x + 0.5)); |
| 4197 } | 4179 } |
| 4198 | 4180 |
| (...skipping 3274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7473 } else { | 7455 } else { |
| 7474 // Handle last resort GC and make sure to allow future allocations | 7456 // Handle last resort GC and make sure to allow future allocations |
| 7475 // to grow the heap without causing GCs (if possible). | 7457 // to grow the heap without causing GCs (if possible). |
| 7476 Counters::gc_last_resort_from_js.Increment(); | 7458 Counters::gc_last_resort_from_js.Increment(); |
| 7477 Heap::CollectAllGarbage(); | 7459 Heap::CollectAllGarbage(); |
| 7478 } | 7460 } |
| 7479 } | 7461 } |
| 7480 | 7462 |
| 7481 | 7463 |
| 7482 } } // namespace v8::internal | 7464 } } // namespace v8::internal |
| OLD | NEW |