Chromium Code Reviews| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); | 104 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); |
| 105 const uint64_t A = 0xffffda61; | 105 const uint64_t A = 0xffffda61; |
| 106 uint64_t state = (A * state_lo) + state_hi; | 106 uint64_t state = (A * state_lo) + state_hi; |
| 107 array.SetUint32(0, static_cast<uint32_t>(state)); | 107 array.SetUint32(0, static_cast<uint32_t>(state)); |
| 108 array.SetUint32(array.ElementSizeInBytes(), | 108 array.SetUint32(array.ElementSizeInBytes(), |
| 109 static_cast<uint32_t>(state >> 32)); | 109 static_cast<uint32_t>(state >> 32)); |
| 110 return Object::null(); | 110 return Object::null(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 | 113 |
| 114 RawTypedData* CreateRandomState(Isolate* isolate, uint64_t seed) { | |
| 115 const TypedData& result = TypedData::Handle( | |
| 116 isolate, TypedData::New(kTypedDataUint32ArrayCid, 2)); | |
| 117 result.SetUint32(0, static_cast<uint32_t>(seed)); | |
| 118 result.SetUint32(result.ElementSizeInBytes(), | |
| 119 static_cast<uint32_t>(seed >> 32)); | |
| 120 return result.raw(); | |
| 121 } | |
| 122 | |
| 123 | |
| 114 uint64_t mix64(uint64_t n) { | 124 uint64_t mix64(uint64_t n) { |
| 115 // Thomas Wang 64-bit mix. | 125 // Thomas Wang 64-bit mix. |
| 116 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 126 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 117 // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~T twang/tech/inthash.htm | 127 // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~T twang/tech/inthash.htm |
| 118 n = (~n) + (n << 21); // n = (n << 21) - n - 1; | 128 n = (~n) + (n << 21); // n = (n << 21) - n - 1; |
| 119 n = n ^ (n >> 24); | 129 n = n ^ (n >> 24); |
| 120 n = n * 265; // n = (n + (n << 3)) + (n << 8); | 130 n = n * 265; // n = (n + (n << 3)) + (n << 8); |
| 121 n = n ^ (n >> 14); | 131 n = n ^ (n >> 14); |
| 122 n = n * 21; // n = (n + (n << 2)) + (n << 4); | 132 n = n * 21; // n = (n + (n << 2)) + (n << 4); |
| 123 n = n ^ (n >> 28); | 133 n = n ^ (n >> 28); |
| 124 n = n + (n << 31); | 134 n = n + (n << 31); |
| 125 return n; | 135 return n; |
| 126 } | 136 } |
| 127 | 137 |
| 128 | 138 |
| 129 // Implements: | 139 // Implements: |
| 130 // uint64_t hash = 0; | 140 // uint64_t hash = 0; |
| 131 // do { | 141 // do { |
| 132 // hash = hash * 1037 ^ mix64((uint64_t)seed); | 142 // hash = hash * 1037 ^ mix64((uint64_t)seed); |
| 133 // seed >>= 64; | 143 // seed >>= 64; |
| 134 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. | 144 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. |
| 135 // if (hash == 0) { | 145 // if (hash == 0) { |
| 136 // hash = 0x5A17; | 146 // hash = 0x5A17; |
| 137 // } | 147 // } |
| 138 // _state[kSTATE_LO] = hash & _MASK_32; | 148 // var result = new Uint32List(2); |
| 139 // _state[kSTATE_HI] = hash >> 32; | 149 // result[kSTATE_LO] = seed & _MASK_32; |
| 140 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) { | 150 // result[kSTATE_HI] = seed >> 32; |
| 141 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); | 151 // return result; |
| 142 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1)); | 152 DEFINE_NATIVE_ENTRY(Random_setupSeed, 1) { |
| 143 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); | 153 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(0)); |
| 144 ASSERT(!seed_int.IsNull()); | |
| 145 ASSERT(!array.IsNull()); | |
| 146 uint64_t seed = 0; | 154 uint64_t seed = 0; |
| 147 if (seed_int.IsBigint()) { | 155 if (seed_int.IsBigint()) { |
| 148 const Bigint& mask64 = Bigint::Handle( | 156 const Bigint& mask64 = Bigint::Handle( |
| 149 BigintOperations::NewFromUint64(0xffffffffffffffffLL)); | 157 BigintOperations::NewFromUint64(0xffffffffffffffffLL)); |
| 150 Bigint& big_seed = Bigint::Handle(); | 158 Bigint& big_seed = Bigint::Handle(); |
| 151 big_seed ^= seed_int.raw(); | 159 big_seed ^= seed_int.raw(); |
| 152 uint64_t negate_mask = 0; | 160 uint64_t negate_mask = 0; |
| 153 if (big_seed.IsNegative()) { | 161 if (big_seed.IsNegative()) { |
| 154 // Negate bits to make seed positive. | 162 // Negate bits to make seed positive. |
| 155 // Negate bits again (by xor with negate_mask) when extracted below, | 163 // Negate bits again (by xor with negate_mask) when extracted below, |
| 156 // to get original bits. | 164 // to get original bits. |
| 157 negate_mask = 0xffffffffffffffffLL; | 165 negate_mask = 0xffffffffffffffffLL; |
| 158 big_seed ^= BigintOperations::BitNot(big_seed); | 166 big_seed ^= BigintOperations::BitNot(big_seed); |
| 159 } | 167 } |
| 160 Bigint& low64 = Bigint::Handle(); | 168 Bigint& low64 = Bigint::Handle(); |
| 161 do { | 169 do { |
| 162 low64 = BigintOperations::BitAnd(big_seed, mask64); | 170 low64 = BigintOperations::BitAnd(big_seed, mask64); |
| 163 ASSERT(BigintOperations::FitsIntoUint64(low64)); | 171 ASSERT(BigintOperations::FitsIntoUint64(low64)); |
| 164 uint64_t chunk = BigintOperations::ToUint64(low64) ^ negate_mask; | 172 uint64_t chunk = BigintOperations::ToUint64(low64) ^ negate_mask; |
| 165 seed = (seed * 1037) ^ mix64(chunk); | 173 seed = (seed * 1037) ^ mix64(chunk); |
| 166 big_seed = BigintOperations::ShiftRight(big_seed, 64); | 174 big_seed = BigintOperations::ShiftRight(big_seed, 64); |
| 167 } while (!big_seed.IsZero()); | 175 } while (!big_seed.IsZero()); |
| 168 } else { | 176 } else { |
| 169 seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value())); | 177 seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value())); |
| 170 } | 178 } |
| 171 | 179 |
| 172 if (seed == 0) { | 180 if (seed == 0) { |
| 173 seed = 0x5a17; | 181 seed = 0x5a17; |
| 174 } | 182 } |
| 175 array.SetUint32(0, static_cast<uint32_t>(seed)); | 183 return CreateRandomState(isolate, seed); |
| 176 array.SetUint32(array.ElementSizeInBytes(), | |
| 177 static_cast<uint32_t>(seed >> 32)); | |
| 178 return Object::null(); | |
| 179 } | 184 } |
| 180 | 185 |
| 181 | 186 |
| 182 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) { | 187 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) { |
| 183 Random* rnd = isolate->random(); | 188 Random* rnd = isolate->random(); |
| 184 int64_t seed = rnd->NextUInt32(); | 189 uint64_t seed = rnd->NextUInt32(); |
| 185 seed |= (static_cast<int64_t>(rnd->NextUInt32())) << 32; | 190 seed |= (static_cast<uint64_t>(rnd->NextUInt32())) << 32; |
|
siva
2014/03/17 21:19:04
I would write this as :
seed |= (static_cast<uint
| |
| 186 return Integer::New(seed); | 191 return CreateRandomState(isolate, seed); |
| 187 } | 192 } |
| 188 | 193 |
| 189 } // namespace dart | 194 } // namespace dart |
| OLD | NEW |