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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
6 | 6 |
7 #include "platform/math.h" | 7 #include "platform/math.h" |
8 | 8 |
9 #include "vm/code_generator.h" // DartModulo. | 9 #include "vm/code_generator.h" // DartModulo. |
10 #include "vm/dart_entry.h" | 10 #include "vm/dart_entry.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 DEFINE_NATIVE_ENTRY(Double_doubleFromInteger, 2) { | 22 DEFINE_NATIVE_ENTRY(Double_doubleFromInteger, 2) { |
23 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); | 23 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); |
24 const Integer& value = Integer::CheckedHandle(arguments->NativeArgAt(1)); | 24 const Integer& value = Integer::CheckedHandle(arguments->NativeArgAt(1)); |
25 if (FLAG_trace_intrinsified_natives) { | 25 if (FLAG_trace_intrinsified_natives) { |
26 OS::Print("Double_doubleFromInteger %s\n", value.ToCString()); | 26 OS::Print("Double_doubleFromInteger %s\n", value.ToCString()); |
27 } | 27 } |
28 return Double::New(value.AsDoubleValue()); | 28 return Double::New(value.AsDoubleValue()); |
29 } | 29 } |
30 | 30 |
31 | 31 |
| 32 DEFINE_NATIVE_ENTRY(Double_doubleFromPixels, 1) { |
| 33 const Integer& pixels = Integer::CheckedHandle(arguments->NativeArgAt(0)); |
| 34 // TODO(regis): Recognize +/- infinity. |
| 35 return Double::New(pixels.AsDoubleValue() / Pixels::kScaling); |
| 36 } |
| 37 |
| 38 |
32 DEFINE_NATIVE_ENTRY(Double_add, 2) { | 39 DEFINE_NATIVE_ENTRY(Double_add, 2) { |
33 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); | 40 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
34 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); | 41 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
35 double right = right_object.value(); | 42 double right = right_object.value(); |
36 if (FLAG_trace_intrinsified_natives) { | 43 if (FLAG_trace_intrinsified_natives) { |
37 OS::Print("Double_add %f + %f\n", left, right); | 44 OS::Print("Double_add %f + %f\n", left, right); |
38 } | 45 } |
39 return Double::New(left + right); | 46 return Double::New(left + right); |
40 } | 47 } |
41 | 48 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (exponent == 0) { | 111 if (exponent == 0) { |
105 // The double fits in a Smi or Mint. | 112 // The double fits in a Smi or Mint. |
106 return Integer::New(ival); | 113 return Integer::New(ival); |
107 } | 114 } |
108 Integer& result = Integer::Handle(); | 115 Integer& result = Integer::Handle(); |
109 result = Bigint::NewFromShiftedInt64(ival, exponent); | 116 result = Bigint::NewFromShiftedInt64(ival, exponent); |
110 return result.AsValidInteger(); | 117 return result.AsValidInteger(); |
111 } | 118 } |
112 | 119 |
113 | 120 |
| 121 static RawInteger* DoubleToPixels(double val, const char* error_msg) { |
| 122 if (isnan(val)) { |
| 123 const Array& args = Array::Handle(Array::New(1)); |
| 124 args.SetAt(0, String::Handle(String::New(error_msg))); |
| 125 Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
| 126 } |
| 127 if (isinf(val)) { |
| 128 if (val < 0) { |
| 129 return Integer::New(Pixels::kMinusInfinity); |
| 130 } else { |
| 131 return Integer::New(Pixels::kPlusInfinity); |
| 132 } |
| 133 } |
| 134 DoubleInternals internals = DoubleInternals(val); |
| 135 ASSERT(!internals.IsSpecial()); // Only Infinity and NaN are special. |
| 136 uint64_t significand = internals.Significand(); |
| 137 intptr_t exponent = internals.Exponent() + Pixels::kFractionalBits; |
| 138 if (exponent <= 0) { |
| 139 significand >>= -exponent; |
| 140 exponent = 0; |
| 141 } else if (exponent <= 10) { |
| 142 // A double significand has at most 53 bits. The following shift will |
| 143 // hence not overflow, and yield an integer of at most 63 bits. |
| 144 significand <<= exponent; |
| 145 exponent = 0; |
| 146 } |
| 147 // A significand has at most 63 bits (after the shift above). |
| 148 // The cast to int64_t is hence safe. |
| 149 int64_t ival = static_cast<int64_t>(significand); |
| 150 if (internals.Sign() < 0) { |
| 151 ival = -ival; |
| 152 } |
| 153 if (exponent == 0) { |
| 154 // The double fits in a Smi or Mint. |
| 155 return Integer::New(ival); |
| 156 } |
| 157 Integer& result = Integer::Handle(); |
| 158 result = Bigint::NewFromShiftedInt64(ival, exponent); |
| 159 return result.AsValidInteger(); |
| 160 } |
| 161 |
| 162 |
114 DEFINE_NATIVE_ENTRY(Double_trunc_div, 2) { | 163 DEFINE_NATIVE_ENTRY(Double_trunc_div, 2) { |
115 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); | 164 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
116 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); | 165 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
117 double right = right_object.value(); | 166 double right = right_object.value(); |
118 if (FLAG_trace_intrinsified_natives) { | 167 if (FLAG_trace_intrinsified_natives) { |
119 OS::Print("Double_trunc_div %f ~/ %f\n", left, right); | 168 OS::Print("Double_trunc_div %f ~/ %f\n", left, right); |
120 } | 169 } |
121 return DoubleToInteger(trunc(left / right), | 170 return DoubleToInteger(trunc(left / right), |
122 "Result of truncating division is Infinity or NaN"); | 171 "Result of truncating division is Infinity or NaN"); |
123 } | 172 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 // MAC OSX math library produces old style cast warning. | 252 // MAC OSX math library produces old style cast warning. |
204 #pragma GCC diagnostic ignored "-Wold-style-cast" | 253 #pragma GCC diagnostic ignored "-Wold-style-cast" |
205 #endif | 254 #endif |
206 | 255 |
207 DEFINE_NATIVE_ENTRY(Double_toInt, 1) { | 256 DEFINE_NATIVE_ENTRY(Double_toInt, 1) { |
208 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 257 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
209 return DoubleToInteger(arg.value(), "Infinity or NaN toInt"); | 258 return DoubleToInteger(arg.value(), "Infinity or NaN toInt"); |
210 } | 259 } |
211 | 260 |
212 | 261 |
| 262 DEFINE_NATIVE_ENTRY(Double_toPixels, 1) { |
| 263 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
| 264 return DoubleToPixels(arg.value(), "NaN toPixels"); |
| 265 } |
| 266 |
| 267 |
213 DEFINE_NATIVE_ENTRY(Double_parse, 3) { | 268 DEFINE_NATIVE_ENTRY(Double_parse, 3) { |
214 GET_NON_NULL_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); | 269 GET_NON_NULL_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); |
215 GET_NON_NULL_NATIVE_ARGUMENT(Integer, startValue, arguments->NativeArgAt(1)); | 270 GET_NON_NULL_NATIVE_ARGUMENT(Integer, startValue, arguments->NativeArgAt(1)); |
216 GET_NON_NULL_NATIVE_ARGUMENT(Integer, endValue, arguments->NativeArgAt(2)); | 271 GET_NON_NULL_NATIVE_ARGUMENT(Integer, endValue, arguments->NativeArgAt(2)); |
217 | 272 |
218 const intptr_t start = startValue.AsTruncatedUint32Value(); | 273 const intptr_t start = startValue.AsTruncatedUint32Value(); |
219 const intptr_t end = endValue.AsTruncatedUint32Value(); | 274 const intptr_t end = endValue.AsTruncatedUint32Value(); |
220 const intptr_t len = value.Length(); | 275 const intptr_t len = value.Length(); |
221 | 276 |
222 // Indices should be inside the string, and 0 <= start < end <= len. | 277 // Indices should be inside the string, and 0 <= start < end <= len. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 DEFINE_NATIVE_ENTRY(Double_flipSignBit, 1) { | 365 DEFINE_NATIVE_ENTRY(Double_flipSignBit, 1) { |
311 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 366 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
312 const double in_val = arg.value(); | 367 const double in_val = arg.value(); |
313 const int64_t bits = bit_cast<int64_t, double>(in_val) ^ kSignBitDouble; | 368 const int64_t bits = bit_cast<int64_t, double>(in_val) ^ kSignBitDouble; |
314 return Double::New(bit_cast<double, int64_t>(bits)); | 369 return Double::New(bit_cast<double, int64_t>(bits)); |
315 } | 370 } |
316 | 371 |
317 // Add here only functions using/referring to old-style casts. | 372 // Add here only functions using/referring to old-style casts. |
318 | 373 |
319 } // namespace dart | 374 } // namespace dart |
OLD | NEW |