Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: runtime/lib/math.cc

Issue 156533002: Add more complex mixing of random seed. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 uint64_t mix64(uint64_t n) {
115 // Thomas Wang 64-bit mix.
116 // 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
118 n = (~n) + (n << 21); // n = (n << 21) - n - 1;
floitsch 2014/02/06 15:39:48 Shouldn't these bit-optimizations be done by the c
Ivan Posva 2014/02/07 00:15:20 In my experience it does not hurt to do it by hand
Lasse Reichstein Nielsen 2014/02/07 08:38:54 The multiplication is simpler than the shifts, so
119 n = n ^ (n >> 24);
120 n = (n + (n << 3)) + (n << 8); // n * 265
floitsch 2014/02/06 15:39:48 This can definitely be done by the compiler.
Ivan Posva 2014/02/07 00:15:20 ditto
Lasse Reichstein Nielsen 2014/02/07 08:38:54 I'll trust the original author's comment here and
121 n = n ^ (n >> 14);
122 n = (n + (n << 2)) + (n << 4); // n * 21
123 n = n ^ (n >> 28);
124 n = n + (n << 31);
125 return n;
126 }
127
128
114 // Implements: 129 // Implements:
130 // uint64_t hash = 0;
115 // do { 131 // do {
116 // seed = (seed + 0x5A17) & _Random._MASK_64; 132 // hash = hash * 1037 ^ mix64((uint64_t)seed);
117 // } while (seed == 0); 133 // seed >>= 64;
118 // _state[kSTATE_LO] = seed & _MASK_32; 134 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative.
119 // _state[kSTATE_HI] = seed >> 32; 135 // if (hash == 0) {
136 // hash = 0x5A17;
137 // }
138 // _state[kSTATE_LO] = hash & _MASK_32;
139 // _state[kSTATE_HI] = hash >> 32;
120 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) { 140 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) {
121 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); 141 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
122 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1)); 142 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1));
123 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); 143 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
124 ASSERT(!seed_int.IsNull()); 144 ASSERT(!seed_int.IsNull());
125 ASSERT(!array.IsNull()); 145 ASSERT(!array.IsNull());
126 int64_t seed = 0; 146 uint64_t seed = 0;
127 if (seed_int.IsBigint()) { 147 if (seed_int.IsBigint()) {
128 const Bigint& mask64 = Bigint::Handle( 148 const Bigint& mask64 = Bigint::Handle(
129 BigintOperations::NewFromUint64(0xffffffffffffffffLL)); 149 BigintOperations::NewFromUint64(0xffffffffffffffffLL));
130 Bigint& big_seed = Bigint::Handle(); 150 Bigint& big_seed = Bigint::Handle();
131 big_seed ^= seed_int.raw(); 151 big_seed ^= seed_int.raw();
152 uint64_t negate_mask = 0;
153 if (big_seed.IsNegative()) {
154 // Negate bits to make seed positive.
155 // Negate bits again (by xor with negatE_mask) when extracted below,
Ivan Posva 2014/02/07 00:15:20 negatE_mask -> negate_mask
Lasse Reichstein Nielsen 2014/02/07 08:38:54 Good catch
156 // to get original bits.
157 negate_mask = 0xffffffffffffffffLL;
158 big_seed ^= BigintOperations::BitNot(big_seed);
159 }
132 Bigint& low64 = Bigint::Handle(); 160 Bigint& low64 = Bigint::Handle();
133 while (!big_seed.IsZero()) { 161 do {
134 low64 = BigintOperations::BitAnd(big_seed, mask64); 162 low64 = BigintOperations::BitAnd(big_seed, mask64);
135 ASSERT(BigintOperations::FitsIntoUint64(low64)); 163 ASSERT(BigintOperations::FitsIntoUint64(low64));
136 seed ^= BigintOperations::ToUint64(low64); 164 uint64_t chunk = BigintOperations::ToUint64(low64) ^ negate_mask;
165 seed = (seed * 1037) ^ mix64(chunk);
137 big_seed = BigintOperations::ShiftRight(big_seed, 64); 166 big_seed = BigintOperations::ShiftRight(big_seed, 64);
138 } 167 } while (!big_seed.IsZero());
139 } else { 168 } else {
140 seed = seed_int.AsInt64Value(); 169 seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value()));
141 } 170 }
142 171
143 do { 172 if (seed == 0) {
144 seed = seed + 0x5A17; 173 seed = 0x5a17;
145 } while (seed == 0); 174 }
146 array.SetUint32(0, static_cast<uint32_t>(seed)); 175 array.SetUint32(0, static_cast<uint32_t>(seed));
147 array.SetUint32(array.ElementSizeInBytes(), 176 array.SetUint32(array.ElementSizeInBytes(),
148 static_cast<uint32_t>(seed >> 32)); 177 static_cast<uint32_t>(seed >> 32));
149 return Object::null(); 178 return Object::null();
150 } 179 }
151 180
152 } // namespace dart 181 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/math_patch.dart » ('j') | sdk/lib/_internal/lib/math_patch.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698