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

Unified Diff: runtime/lib/math.cc

Issue 21966003: Adapt Random class to be able to run in javascript integer compatibility mode. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | runtime/vm/object.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/math.cc
===================================================================
--- runtime/lib/math.cc (revision 25742)
+++ runtime/lib/math.cc (working copy)
@@ -66,4 +66,63 @@
return Double::New(log(operand.value()));
}
+
+// Returns the typed-data array store in '_Random._state' field.
+static RawTypedData* GetRandomStateArray(const Instance& receiver) {
+ const Class& random_class = Class::Handle(receiver.clazz());
+ const Field& state_field =
+ Field::Handle(random_class.LookupField(Symbols::_state()));
+ ASSERT(!state_field.IsNull());
+ const Instance& state_field_value =
+ Instance::Cast(Object::Handle(receiver.GetField(state_field)));
siva 2013/08/02 22:46:13 Why cast this to an Instance, it is being cast to
+ ASSERT(!state_field_value.IsNull());
+ ASSERT(state_field_value.IsTypedData());
+ const TypedData& array = TypedData::Cast(state_field_value);
+ ASSERT(array.Length() == 2);
+ ASSERT(array.ElementType() == kUint32ArrayElement);
+ return array.raw();
+}
+
+
+// Implements:
+// var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64;
+// _state[kSTATE_LO] = state & _MASK_32;
+// _state[kSTATE_HI] = state >> 32;
+DEFINE_NATIVE_ENTRY(Random_nextState, 1) {
+ GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
+ const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
+ const uint64_t state_lo = array.GetUint32(0);
+ const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes());
siva 2013/08/02 22:46:13 might be more readable to have: const intptr_t sta
+ const uint64_t A = 0xffffda61;
+ uint64_t state = (A * state_lo) + state_hi;
+ array.SetUint32(0, static_cast<uint32_t>(state));
+ array.SetUint32(array.ElementSizeInBytes(),
+ static_cast<uint32_t>(state >> 32));
+ return Object::null();
+}
+
+
+// Implements:
+// do {
+// seed = (seed + 0x5A17) & _Random._MASK_64;
+// } while (seed == 0);
+// _state[kSTATE_LO] = seed & _MASK_32;
+// _state[kSTATE_HI] = seed >> 32;
+DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) {
+ GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
+ GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1));
+ const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
+ ASSERT(!seed_int.IsNull());
+ ASSERT(!array.IsNull());
+ // TODO(srdjan): Reduce Bigint to 64 bit value.
+ int64_t seed = seed_int.IsBigint() ? 0 : seed_int.AsInt64Value();
+ do {
+ seed = seed + 0x5A17;
+ } while (seed == 0);
+ array.SetUint32(0, static_cast<uint32_t>(seed));
+ array.SetUint32(array.ElementSizeInBytes(),
siva 2013/08/02 22:46:13 Ditto comment about offsets.
+ static_cast<uint32_t>(seed >> 32));
+ return Object::null();
+}
+
} // namespace dart
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | runtime/vm/object.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698