| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <ctype.h> // isspace. | 5 #include <ctype.h> // isspace. |
| 6 | 6 |
| 7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
| 8 | 8 |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/exceptions.h" | 10 #include "vm/exceptions.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // seed = (seed + 0x5A17) & _Random._MASK_64; | 116 // seed = (seed + 0x5A17) & _Random._MASK_64; |
| 117 // } while (seed == 0); | 117 // } while (seed == 0); |
| 118 // _state[kSTATE_LO] = seed & _MASK_32; | 118 // _state[kSTATE_LO] = seed & _MASK_32; |
| 119 // _state[kSTATE_HI] = seed >> 32; | 119 // _state[kSTATE_HI] = seed >> 32; |
| 120 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) { | 120 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) { |
| 121 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); | 121 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); |
| 122 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1)); | 122 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1)); |
| 123 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); | 123 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); |
| 124 ASSERT(!seed_int.IsNull()); | 124 ASSERT(!seed_int.IsNull()); |
| 125 ASSERT(!array.IsNull()); | 125 ASSERT(!array.IsNull()); |
| 126 // TODO(srdjan): Reduce Bigint to 64 bit value. | 126 int64_t seed = 0; |
| 127 int64_t seed = seed_int.IsBigint() ? 0 : seed_int.AsInt64Value(); | 127 if (seed_int.IsBigint()) { |
| 128 const Bigint& mask64 = Bigint::Handle( |
| 129 BigintOperations::NewFromUint64(0xffffffffffffffff)); |
| 130 Bigint& big_seed = Bigint::Handle(); |
| 131 big_seed ^= seed_int.raw(); |
| 132 Bigint& low64 = Bigint::Handle(); |
| 133 while (!big_seed.IsZero()) { |
| 134 low64 = BigintOperations::BitAnd(big_seed, mask64); |
| 135 ASSERT(BigintOperations::FitsIntoUint64(low64)); |
| 136 seed ^= BigintOperations::ToUint64(low64); |
| 137 big_seed = BigintOperations::ShiftRight(big_seed, 64); |
| 138 } |
| 139 } else { |
| 140 seed = seed_int.AsInt64Value(); |
| 141 } |
| 142 |
| 128 do { | 143 do { |
| 129 seed = seed + 0x5A17; | 144 seed = seed + 0x5A17; |
| 130 } while (seed == 0); | 145 } while (seed == 0); |
| 131 array.SetUint32(0, static_cast<uint32_t>(seed)); | 146 array.SetUint32(0, static_cast<uint32_t>(seed)); |
| 132 array.SetUint32(array.ElementSizeInBytes(), | 147 array.SetUint32(array.ElementSizeInBytes(), |
| 133 static_cast<uint32_t>(seed >> 32)); | 148 static_cast<uint32_t>(seed >> 32)); |
| 134 return Object::null(); | 149 return Object::null(); |
| 135 } | 150 } |
| 136 | 151 |
| 137 } // namespace dart | 152 } // namespace dart |
| OLD | NEW |