| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/assembler.h" | 8 #include "src/assembler.h" |
| 9 #include "src/base/utils/random-number-generator.h" | 9 #include "src/base/utils/random-number-generator.h" |
| 10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 if (y == 0) { | 154 if (y == 0) { |
| 155 return Smi::FromInt(1); | 155 return Smi::FromInt(1); |
| 156 } else { | 156 } else { |
| 157 double result = power_double_double(x, y); | 157 double result = power_double_double(x, y); |
| 158 if (std::isnan(result)) return isolate->heap()->nan_value(); | 158 if (std::isnan(result)) return isolate->heap()->nan_value(); |
| 159 return *isolate->factory()->NewNumber(result); | 159 return *isolate->factory()->NewNumber(result); |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 | 163 |
| 164 RUNTIME_FUNCTION(Runtime_RoundNumber) { | |
| 165 HandleScope scope(isolate); | |
| 166 DCHECK(args.length() == 1); | |
| 167 CONVERT_NUMBER_ARG_HANDLE_CHECKED(input, 0); | |
| 168 isolate->counters()->math_round_runtime()->Increment(); | |
| 169 | |
| 170 if (!input->IsHeapNumber()) { | |
| 171 DCHECK(input->IsSmi()); | |
| 172 return *input; | |
| 173 } | |
| 174 | |
| 175 Handle<HeapNumber> number = Handle<HeapNumber>::cast(input); | |
| 176 | |
| 177 double value = number->value(); | |
| 178 int exponent = number->get_exponent(); | |
| 179 int sign = number->get_sign(); | |
| 180 | |
| 181 if (exponent < -1) { | |
| 182 // Number in range ]-0.5..0.5[. These always round to +/-zero. | |
| 183 if (sign) return isolate->heap()->minus_zero_value(); | |
| 184 return Smi::FromInt(0); | |
| 185 } | |
| 186 | |
| 187 // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and | |
| 188 // should be rounded to 2^30, which is not smi (for 31-bit smis, similar | |
| 189 // argument holds for 32-bit smis). | |
| 190 if (!sign && exponent < kSmiValueSize - 2) { | |
| 191 return Smi::FromInt(static_cast<int>(value + 0.5)); | |
| 192 } | |
| 193 | |
| 194 // If the magnitude is big enough, there's no place for fraction part. If we | |
| 195 // try to add 0.5 to this number, 1.0 will be added instead. | |
| 196 if (exponent >= 52) { | |
| 197 return *number; | |
| 198 } | |
| 199 | |
| 200 if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); | |
| 201 | |
| 202 // Do not call NumberFromDouble() to avoid extra checks. | |
| 203 return *isolate->factory()->NewNumber(Floor(value + 0.5)); | |
| 204 } | |
| 205 | |
| 206 | |
| 207 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { | 164 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { |
| 208 HandleScope scope(isolate); | 165 HandleScope scope(isolate); |
| 209 DCHECK(args.length() == 1); | 166 DCHECK(args.length() == 1); |
| 210 if (isolate->serializer_enabled()) { | 167 if (isolate->serializer_enabled()) { |
| 211 // Random numbers in the snapshot are not really that random. And we cannot | 168 // Random numbers in the snapshot are not really that random. And we cannot |
| 212 // return a typed array as it cannot be serialized. To make calling | 169 // return a typed array as it cannot be serialized. To make calling |
| 213 // Math.random possible when creating a custom startup snapshot, we simply | 170 // Math.random possible when creating a custom startup snapshot, we simply |
| 214 // return a normal array with a single random number. | 171 // return a normal array with a single random number. |
| 215 Handle<HeapNumber> random_number = isolate->factory()->NewHeapNumber( | 172 Handle<HeapNumber> random_number = isolate->factory()->NewHeapNumber( |
| 216 isolate->random_number_generator()->NextDouble()); | 173 isolate->random_number_generator()->NextDouble()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 base::RandomNumberGenerator::XorShift128(&state0, &state1); | 211 base::RandomNumberGenerator::XorShift128(&state0, &state1); |
| 255 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); | 212 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); |
| 256 } | 213 } |
| 257 // Persist current state. | 214 // Persist current state. |
| 258 array[kState0Offset] = uint64_to_double(state0); | 215 array[kState0Offset] = uint64_to_double(state0); |
| 259 array[kState1Offset] = uint64_to_double(state1); | 216 array[kState1Offset] = uint64_to_double(state1); |
| 260 return *typed_array; | 217 return *typed_array; |
| 261 } | 218 } |
| 262 } // namespace internal | 219 } // namespace internal |
| 263 } // namespace v8 | 220 } // namespace v8 |
| OLD | NEW |