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 |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "vm/bootstrap_natives.h" |
6 | |
7 | #include <math.h> |
8 | |
9 | #include "vm/dart_entry.h" |
10 | #include "vm/double_conversion.h" |
11 | #include "vm/double_internals.h" |
12 | #include "vm/exceptions.h" |
13 | #include "vm/native_entry.h" |
14 | #include "vm/object.h" |
15 | #include "vm/runtime_entry.h" // DartModulo. |
16 | #include "vm/symbols.h" |
17 | |
18 | namespace dart { |
19 | |
20 | DEFINE_NATIVE_ENTRY(Double_doubleFromInteger, 0, 2) { |
21 | ASSERT( |
22 | TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(0)).IsNull()); |
23 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, value, arguments->NativeArgAt(1)); |
24 | if (FLAG_trace_intrinsified_natives) { |
25 | OS::PrintErr("Double_doubleFromInteger %s\n" , value.ToCString()); |
26 | } |
27 | return Double::New(value.AsDoubleValue()); |
28 | } |
29 | |
30 | DEFINE_NATIVE_ENTRY(Double_add, 0, 2) { |
31 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
32 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
33 | double right = right_object.value(); |
34 | if (FLAG_trace_intrinsified_natives) { |
35 | OS::PrintErr("Double_add %f + %f\n" , left, right); |
36 | } |
37 | return Double::New(left + right); |
38 | } |
39 | |
40 | DEFINE_NATIVE_ENTRY(Double_sub, 0, 2) { |
41 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
42 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
43 | double right = right_object.value(); |
44 | if (FLAG_trace_intrinsified_natives) { |
45 | OS::PrintErr("Double_sub %f - %f\n" , left, right); |
46 | } |
47 | return Double::New(left - right); |
48 | } |
49 | |
50 | DEFINE_NATIVE_ENTRY(Double_mul, 0, 2) { |
51 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
52 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
53 | double right = right_object.value(); |
54 | if (FLAG_trace_intrinsified_natives) { |
55 | OS::PrintErr("Double_mul %f * %f\n" , left, right); |
56 | } |
57 | return Double::New(left * right); |
58 | } |
59 | |
60 | DEFINE_NATIVE_ENTRY(Double_div, 0, 2) { |
61 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
62 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
63 | double right = right_object.value(); |
64 | if (FLAG_trace_intrinsified_natives) { |
65 | OS::PrintErr("Double_div %f / %f\n" , left, right); |
66 | } |
67 | return Double::New(left / right); |
68 | } |
69 | |
70 | static IntegerPtr DoubleToInteger(double val, const char* error_msg) { |
71 | if (isinf(val) || isnan(val)) { |
72 | const Array& args = Array::Handle(Array::New(1)); |
73 | args.SetAt(0, String::Handle(String::New(error_msg))); |
74 | Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
75 | } |
76 | int64_t ival = 0; |
77 | if (val <= static_cast<double>(kMinInt64)) { |
78 | ival = kMinInt64; |
79 | } else if (val >= static_cast<double>(kMaxInt64)) { |
80 | ival = kMaxInt64; |
81 | } else { // Representable in int64_t. |
82 | ival = static_cast<int64_t>(val); |
83 | } |
84 | return Integer::New(ival); |
85 | } |
86 | |
87 | DEFINE_NATIVE_ENTRY(Double_hashCode, 0, 1) { |
88 | double val = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
89 | if (FLAG_trace_intrinsified_natives) { |
90 | OS::PrintErr("Double_hashCode %f\n" , val); |
91 | } |
92 | if ((val >= kMinInt64RepresentableAsDouble) && |
93 | (val <= kMaxInt64RepresentableAsDouble)) { |
94 | int64_t ival = static_cast<int64_t>(val); |
95 | if (static_cast<double>(ival) == val) { |
96 | return Integer::New(ival); |
97 | } |
98 | } |
99 | |
100 | uint64_t uval = bit_cast<uint64_t>(val); |
101 | return Smi::New(((uval >> 32) ^ (uval)) & kSmiMax); |
102 | } |
103 | |
104 | DEFINE_NATIVE_ENTRY(Double_trunc_div, 0, 2) { |
105 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
106 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
107 | double right = right_object.value(); |
108 | if (FLAG_trace_intrinsified_natives) { |
109 | OS::PrintErr("Double_trunc_div %f ~/ %f\n" , left, right); |
110 | } |
111 | return DoubleToInteger(trunc(left / right), |
112 | "Result of truncating division is Infinity or NaN" ); |
113 | } |
114 | |
115 | DEFINE_NATIVE_ENTRY(Double_modulo, 0, 2) { |
116 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
117 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
118 | double right = right_object.value(); |
119 | return Double::New(DartModulo(left, right)); |
120 | } |
121 | |
122 | DEFINE_NATIVE_ENTRY(Double_remainder, 0, 2) { |
123 | double left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
124 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
125 | double right = right_object.value(); |
126 | return Double::New(fmod_ieee(left, right)); |
127 | } |
128 | |
129 | DEFINE_NATIVE_ENTRY(Double_greaterThan, 0, 2) { |
130 | const Double& left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
131 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right, arguments->NativeArgAt(1)); |
132 | bool result = right.IsNull() ? false : (left.value() > right.value()); |
133 | if (FLAG_trace_intrinsified_natives) { |
134 | OS::PrintErr("Double_greaterThan %s > %s\n" , left.ToCString(), |
135 | right.ToCString()); |
136 | } |
137 | return Bool::Get(result).raw(); |
138 | } |
139 | |
140 | DEFINE_NATIVE_ENTRY(Double_greaterThanFromInteger, 0, 2) { |
141 | const Double& right = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
142 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, left, arguments->NativeArgAt(1)); |
143 | return Bool::Get(left.AsDoubleValue() > right.value()).raw(); |
144 | } |
145 | |
146 | DEFINE_NATIVE_ENTRY(Double_equal, 0, 2) { |
147 | const Double& left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
148 | GET_NON_NULL_NATIVE_ARGUMENT(Double, right, arguments->NativeArgAt(1)); |
149 | bool result = right.IsNull() ? false : (left.value() == right.value()); |
150 | if (FLAG_trace_intrinsified_natives) { |
151 | OS::PrintErr("Double_equal %s == %s\n" , left.ToCString(), |
152 | right.ToCString()); |
153 | } |
154 | return Bool::Get(result).raw(); |
155 | } |
156 | |
157 | DEFINE_NATIVE_ENTRY(Double_equalToInteger, 0, 2) { |
158 | const Double& left = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
159 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, right, arguments->NativeArgAt(1)); |
160 | return Bool::Get(left.value() == right.AsDoubleValue()).raw(); |
161 | } |
162 | |
163 | DEFINE_NATIVE_ENTRY(Double_round, 0, 1) { |
164 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
165 | return Double::New(round(arg.value())); |
166 | } |
167 | |
168 | DEFINE_NATIVE_ENTRY(Double_floor, 0, 1) { |
169 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
170 | return Double::New(floor(arg.value())); |
171 | } |
172 | |
173 | DEFINE_NATIVE_ENTRY(Double_ceil, 0, 1) { |
174 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
175 | return Double::New(ceil(arg.value())); |
176 | } |
177 | |
178 | DEFINE_NATIVE_ENTRY(Double_truncate, 0, 1) { |
179 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
180 | return Double::New(trunc(arg.value())); |
181 | } |
182 | |
183 | #if defined(HOST_OS_MACOS) |
184 | // MAC OSX math library produces old style cast warning. |
185 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
186 | #endif |
187 | |
188 | DEFINE_NATIVE_ENTRY(Double_toInt, 0, 1) { |
189 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
190 | return DoubleToInteger(arg.value(), "Infinity or NaN toInt" ); |
191 | } |
192 | |
193 | DEFINE_NATIVE_ENTRY(Double_parse, 0, 3) { |
194 | GET_NON_NULL_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); |
195 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, startValue, arguments->NativeArgAt(1)); |
196 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, endValue, arguments->NativeArgAt(2)); |
197 | |
198 | const intptr_t start = startValue.AsTruncatedUint32Value(); |
199 | const intptr_t end = endValue.AsTruncatedUint32Value(); |
200 | const intptr_t len = value.Length(); |
201 | |
202 | // Indices should be inside the string, and 0 <= start < end <= len. |
203 | if (0 <= start && start < end && end <= len) { |
204 | double double_value; |
205 | if (String::ParseDouble(value, start, end, &double_value)) { |
206 | return Double::New(double_value); |
207 | } |
208 | } |
209 | return Object::null(); |
210 | } |
211 | |
212 | DEFINE_NATIVE_ENTRY(Double_toString, 0, 1) { |
213 | const Number& number = Number::CheckedHandle(zone, arguments->NativeArgAt(0)); |
214 | return number.ToString(Heap::kNew); |
215 | } |
216 | |
217 | DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 0, 2) { |
218 | // The boundaries are exclusive. |
219 | static const double kLowerBoundary = -1e21; |
220 | static const double kUpperBoundary = 1e21; |
221 | |
222 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
223 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); |
224 | double d = arg.value(); |
225 | intptr_t fraction_digits_value = fraction_digits.Value(); |
226 | if (0 <= fraction_digits_value && fraction_digits_value <= 20 && |
227 | kLowerBoundary < d && d < kUpperBoundary) { |
228 | return DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value)); |
229 | } else { |
230 | Exceptions::ThrowArgumentError(String::Handle( |
231 | String::New("Illegal arguments to double.toStringAsFixed" ))); |
232 | return Object::null(); |
233 | } |
234 | } |
235 | |
236 | DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 0, 2) { |
237 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
238 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); |
239 | double d = arg.value(); |
240 | intptr_t fraction_digits_value = fraction_digits.Value(); |
241 | if (-1 <= fraction_digits_value && fraction_digits_value <= 20) { |
242 | return DoubleToStringAsExponential(d, |
243 | static_cast<int>(fraction_digits_value)); |
244 | } else { |
245 | Exceptions::ThrowArgumentError(String::Handle( |
246 | String::New("Illegal arguments to double.toStringAsExponential" ))); |
247 | return Object::null(); |
248 | } |
249 | } |
250 | |
251 | DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 0, 2) { |
252 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
253 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, precision, arguments->NativeArgAt(1)); |
254 | double d = arg.value(); |
255 | intptr_t precision_value = precision.Value(); |
256 | if (1 <= precision_value && precision_value <= 21) { |
257 | return DoubleToStringAsPrecision(d, static_cast<int>(precision_value)); |
258 | } else { |
259 | Exceptions::ThrowArgumentError(String::Handle( |
260 | String::New("Illegal arguments to double.toStringAsPrecision" ))); |
261 | return Object::null(); |
262 | } |
263 | } |
264 | |
265 | DEFINE_NATIVE_ENTRY(Double_getIsInfinite, 0, 1) { |
266 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
267 | return Bool::Get(isinf(arg.value())).raw(); |
268 | } |
269 | |
270 | DEFINE_NATIVE_ENTRY(Double_getIsNaN, 0, 1) { |
271 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
272 | return Bool::Get(isnan(arg.value())).raw(); |
273 | } |
274 | |
275 | DEFINE_NATIVE_ENTRY(Double_getIsNegative, 0, 1) { |
276 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
277 | // Include negative zero, infinity. |
278 | double dval = arg.value(); |
279 | return Bool::Get(signbit(dval) && !isnan(dval)).raw(); |
280 | } |
281 | |
282 | DEFINE_NATIVE_ENTRY(Double_flipSignBit, 0, 1) { |
283 | const Double& arg = Double::CheckedHandle(zone, arguments->NativeArgAt(0)); |
284 | const double in_val = arg.value(); |
285 | const int64_t bits = bit_cast<int64_t, double>(in_val) ^ kSignBitDouble; |
286 | return Double::New(bit_cast<double, int64_t>(bits)); |
287 | } |
288 | |
289 | // Add here only functions using/referring to old-style casts. |
290 | |
291 | } // namespace dart |
292 | |