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

Side by Side 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, 4 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
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | runtime/vm/object.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 DEFINE_NATIVE_ENTRY(Math_exp, 1) { 59 DEFINE_NATIVE_ENTRY(Math_exp, 1) {
60 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); 60 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0));
61 return Double::New(exp(operand.value())); 61 return Double::New(exp(operand.value()));
62 } 62 }
63 63
64 DEFINE_NATIVE_ENTRY(Math_log, 1) { 64 DEFINE_NATIVE_ENTRY(Math_log, 1) {
65 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); 65 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0));
66 return Double::New(log(operand.value())); 66 return Double::New(log(operand.value()));
67 } 67 }
68 68
69
70 // Returns the typed-data array store in '_Random._state' field.
71 static RawTypedData* GetRandomStateArray(const Instance& receiver) {
72 const Class& random_class = Class::Handle(receiver.clazz());
73 const Field& state_field =
74 Field::Handle(random_class.LookupField(Symbols::_state()));
75 ASSERT(!state_field.IsNull());
76 const Instance& state_field_value =
77 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
78 ASSERT(!state_field_value.IsNull());
79 ASSERT(state_field_value.IsTypedData());
80 const TypedData& array = TypedData::Cast(state_field_value);
81 ASSERT(array.Length() == 2);
82 ASSERT(array.ElementType() == kUint32ArrayElement);
83 return array.raw();
84 }
85
86
87 // Implements:
88 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64;
89 // _state[kSTATE_LO] = state & _MASK_32;
90 // _state[kSTATE_HI] = state >> 32;
91 DEFINE_NATIVE_ENTRY(Random_nextState, 1) {
92 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
93 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
94 const uint64_t state_lo = array.GetUint32(0);
95 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
96 const uint64_t A = 0xffffda61;
97 uint64_t state = (A * state_lo) + state_hi;
98 array.SetUint32(0, static_cast<uint32_t>(state));
99 array.SetUint32(array.ElementSizeInBytes(),
100 static_cast<uint32_t>(state >> 32));
101 return Object::null();
102 }
103
104
105 // Implements:
106 // do {
107 // seed = (seed + 0x5A17) & _Random._MASK_64;
108 // } while (seed == 0);
109 // _state[kSTATE_LO] = seed & _MASK_32;
110 // _state[kSTATE_HI] = seed >> 32;
111 DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) {
112 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
113 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1));
114 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
115 ASSERT(!seed_int.IsNull());
116 ASSERT(!array.IsNull());
117 // TODO(srdjan): Reduce Bigint to 64 bit value.
118 int64_t seed = seed_int.IsBigint() ? 0 : seed_int.AsInt64Value();
119 do {
120 seed = seed + 0x5A17;
121 } while (seed == 0);
122 array.SetUint32(0, static_cast<uint32_t>(seed));
123 array.SetUint32(array.ElementSizeInBytes(),
siva 2013/08/02 22:46:13 Ditto comment about offsets.
124 static_cast<uint32_t>(seed >> 32));
125 return Object::null();
126 }
127
69 } // namespace dart 128 } // namespace dart
OLDNEW
« 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