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 4143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4154 | 4154 |
4155 static Object* Runtime_Math_log(Arguments args) { | 4155 static Object* Runtime_Math_log(Arguments args) { |
4156 NoHandleAllocation ha; | 4156 NoHandleAllocation ha; |
4157 ASSERT(args.length() == 1); | 4157 ASSERT(args.length() == 1); |
4158 | 4158 |
4159 CONVERT_DOUBLE_CHECKED(x, args[0]); | 4159 CONVERT_DOUBLE_CHECKED(x, args[0]); |
4160 return Heap::AllocateHeapNumber(log(x)); | 4160 return Heap::AllocateHeapNumber(log(x)); |
4161 } | 4161 } |
4162 | 4162 |
4163 | 4163 |
| 4164 // Helper function to compute x^y, where y is known to be an |
| 4165 // integer. Uses binary decomposition to limit the number of |
| 4166 // multiplications; see the discussion in "Hacker's Delight" by Henry |
| 4167 // S. Warren, Jr., figure 11-6, page 213. |
| 4168 static double powi(double x, int y) { |
| 4169 ASSERT(y != kMinInt); |
| 4170 unsigned n = (y < 0) ? -y : y; |
| 4171 double m = x; |
| 4172 double p = 1; |
| 4173 while (true) { |
| 4174 if ((n & 1) != 0) p *= m; |
| 4175 n >>= 1; |
| 4176 if (n == 0) { |
| 4177 if (y < 0) { |
| 4178 // Unfortunately, we have to be careful when p has reached |
| 4179 // infinity in the computation, because sometimes the higher |
| 4180 // internal precision in the pow() implementation would have |
| 4181 // given us a finite p. This happens very rarely. |
| 4182 double result = 1.0 / p; |
| 4183 return (result == 0 && isinf(p)) ? pow(x, y) : result; |
| 4184 } else { |
| 4185 return p; |
| 4186 } |
| 4187 } |
| 4188 m *= m; |
| 4189 } |
| 4190 } |
| 4191 |
| 4192 |
4164 static Object* Runtime_Math_pow(Arguments args) { | 4193 static Object* Runtime_Math_pow(Arguments args) { |
4165 NoHandleAllocation ha; | 4194 NoHandleAllocation ha; |
4166 ASSERT(args.length() == 2); | 4195 ASSERT(args.length() == 2); |
4167 | 4196 |
4168 CONVERT_DOUBLE_CHECKED(x, args[0]); | 4197 CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 4198 |
| 4199 // If the second argument is a smi, it is much faster to call the |
| 4200 // custom powi() function than the generic pow(). |
| 4201 if (args[1]->IsSmi()) { |
| 4202 int y = Smi::cast(args[1])->value(); |
| 4203 return Heap::AllocateHeapNumber(powi(x, y)); |
| 4204 } |
| 4205 |
4169 CONVERT_DOUBLE_CHECKED(y, args[1]); | 4206 CONVERT_DOUBLE_CHECKED(y, args[1]); |
4170 if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { | 4207 if (y == 0.5) { |
4171 return Heap::nan_value(); | 4208 // It's not uncommon to use Math.pow(x, 0.5) to compute the square |
| 4209 // root of a number. To speed up such computations, we explictly |
| 4210 // check for this case and use the sqrt() function which is faster |
| 4211 // than pow(). |
| 4212 return Heap::AllocateHeapNumber(sqrt(x)); |
| 4213 } else if (y == -0.5) { |
| 4214 // Optimized using Math.pow(x, -0.5) == 1 / Math.pow(x, 0.5). |
| 4215 return Heap::AllocateHeapNumber(1.0 / sqrt(x)); |
4172 } else if (y == 0) { | 4216 } else if (y == 0) { |
4173 return Smi::FromInt(1); | 4217 return Smi::FromInt(1); |
| 4218 } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
| 4219 return Heap::nan_value(); |
4174 } else { | 4220 } else { |
4175 return Heap::AllocateHeapNumber(pow(x, y)); | 4221 return Heap::AllocateHeapNumber(pow(x, y)); |
4176 } | 4222 } |
4177 } | 4223 } |
4178 | 4224 |
4179 | 4225 |
4180 static Object* Runtime_Math_round(Arguments args) { | 4226 static Object* Runtime_Math_round(Arguments args) { |
4181 NoHandleAllocation ha; | 4227 NoHandleAllocation ha; |
4182 ASSERT(args.length() == 1); | 4228 ASSERT(args.length() == 1); |
4183 | 4229 |
(...skipping 3279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7463 } else { | 7509 } else { |
7464 // Handle last resort GC and make sure to allow future allocations | 7510 // Handle last resort GC and make sure to allow future allocations |
7465 // to grow the heap without causing GCs (if possible). | 7511 // to grow the heap without causing GCs (if possible). |
7466 Counters::gc_last_resort_from_js.Increment(); | 7512 Counters::gc_last_resort_from_js.Increment(); |
7467 Heap::CollectAllGarbage(); | 7513 Heap::CollectAllGarbage(); |
7468 } | 7514 } |
7469 } | 7515 } |
7470 | 7516 |
7471 | 7517 |
7472 } } // namespace v8::internal | 7518 } } // namespace v8::internal |
OLD | NEW |