| 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/exceptions.h" | 9 #include "vm/exceptions.h" |
| 10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 DEFINE_NATIVE_ENTRY(Math_log, 1) { | 63 DEFINE_NATIVE_ENTRY(Math_log, 1) { |
| 64 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 64 GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 65 return Double::New(log(operand.value())); | 65 return Double::New(log(operand.value())); |
| 66 } | 66 } |
| 67 | 67 |
| 68 DEFINE_NATIVE_ENTRY(Math_doublePow, 2) { | 68 DEFINE_NATIVE_ENTRY(Math_doublePow, 2) { |
| 69 const double operand = | 69 const double operand = |
| 70 Double::CheckedHandle(arguments->NativeArgAt(0)).value(); | 70 Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
| 71 GET_NON_NULL_NATIVE_ARGUMENT( | 71 GET_NON_NULL_NATIVE_ARGUMENT(Double, exponent_object, |
| 72 Double, exponent_object, arguments->NativeArgAt(1)); | 72 arguments->NativeArgAt(1)); |
| 73 const double exponent = exponent_object.value(); | 73 const double exponent = exponent_object.value(); |
| 74 return Double::New(pow(operand, exponent)); | 74 return Double::New(pow(operand, exponent)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 | 77 |
| 78 // Returns the typed-data array store in '_Random._state' field. | 78 // Returns the typed-data array store in '_Random._state' field. |
| 79 static RawTypedData* GetRandomStateArray(const Instance& receiver) { | 79 static RawTypedData* GetRandomStateArray(const Instance& receiver) { |
| 80 const Class& random_class = Class::Handle(receiver.clazz()); | 80 const Class& random_class = Class::Handle(receiver.clazz()); |
| 81 const Field& state_field = | 81 const Field& state_field = |
| 82 Field::Handle(random_class.LookupFieldAllowPrivate(Symbols::_state())); | 82 Field::Handle(random_class.LookupFieldAllowPrivate(Symbols::_state())); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 99 // _state[_kSTATE_HI] = state >> 32; | 99 // _state[_kSTATE_HI] = state >> 32; |
| 100 DEFINE_NATIVE_ENTRY(Random_nextState, 1) { | 100 DEFINE_NATIVE_ENTRY(Random_nextState, 1) { |
| 101 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); | 101 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); |
| 102 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); | 102 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); |
| 103 const uint64_t state_lo = array.GetUint32(0); | 103 const uint64_t state_lo = array.GetUint32(0); |
| 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(Zone* zone, uint64_t seed) { | 114 RawTypedData* CreateRandomState(Zone* zone, uint64_t seed) { |
| 115 const TypedData& result = TypedData::Handle( | 115 const TypedData& result = |
| 116 zone, TypedData::New(kTypedDataUint32ArrayCid, 2)); | 116 TypedData::Handle(zone, TypedData::New(kTypedDataUint32ArrayCid, 2)); |
| 117 result.SetUint32(0, static_cast<uint32_t>(seed)); | 117 result.SetUint32(0, static_cast<uint32_t>(seed)); |
| 118 result.SetUint32(result.ElementSizeInBytes(), | 118 result.SetUint32(result.ElementSizeInBytes(), |
| 119 static_cast<uint32_t>(seed >> 32)); | 119 static_cast<uint32_t>(seed >> 32)); |
| 120 return result.raw(); | 120 return result.raw(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 | 123 |
| 124 uint64_t mix64(uint64_t n) { | 124 uint64_t mix64(uint64_t n) { |
| 125 // Thomas Wang 64-bit mix. | 125 // Thomas Wang 64-bit mix. |
| 126 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 126 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 127 // 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 |
| 128 n = (~n) + (n << 21); // n = (n << 21) - n - 1; | 128 n = (~n) + (n << 21); // n = (n << 21) - n - 1; |
| 129 n = n ^ (n >> 24); | 129 n = n ^ (n >> 24); |
| 130 n = n * 265; // n = (n + (n << 3)) + (n << 8); | 130 n = n * 265; // n = (n + (n << 3)) + (n << 8); |
| 131 n = n ^ (n >> 14); | 131 n = n ^ (n >> 14); |
| 132 n = n * 21; // n = (n + (n << 2)) + (n << 4); | 132 n = n * 21; // n = (n + (n << 2)) + (n << 4); |
| 133 n = n ^ (n >> 28); | 133 n = n ^ (n >> 28); |
| 134 n = n + (n << 31); | 134 n = n + (n << 31); |
| 135 return n; | 135 return n; |
| 136 } | 136 } |
| 137 | 137 |
| 138 | 138 |
| 139 // Implements: | 139 // Implements: |
| 140 // uint64_t hash = 0; | 140 // uint64_t hash = 0; |
| 141 // do { | 141 // do { |
| 142 // hash = hash * 1037 ^ mix64((uint64_t)seed); | 142 // hash = hash * 1037 ^ mix64((uint64_t)seed); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 Exceptions::ThrowByType(Exceptions::kUnsupported, args); | 214 Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
| 215 } | 215 } |
| 216 uint64_t result = 0; | 216 uint64_t result = 0; |
| 217 for (intptr_t i = 0; i < n; i++) { | 217 for (intptr_t i = 0; i < n; i++) { |
| 218 result = (result << 8) | buffer[i]; | 218 result = (result << 8) | buffer[i]; |
| 219 } | 219 } |
| 220 return Integer::New(result); | 220 return Integer::New(result); |
| 221 } | 221 } |
| 222 | 222 |
| 223 } // namespace dart | 223 } // namespace dart |
| OLD | NEW |