68dfa49af839abb99a6027e021a400e83f77f674
[platform/upstream/nodejs.git] / deps / v8 / src / runtime / runtime-maths.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #include "src/arguments.h"
8 #include "src/assembler.h"
9 #include "src/codegen.h"
10 #include "src/runtime/runtime-utils.h"
11 #include "src/third_party/fdlibm/fdlibm.h"
12
13
14 namespace v8 {
15 namespace internal {
16
17 #define RUNTIME_UNARY_MATH(Name, name)                       \
18   RUNTIME_FUNCTION(Runtime_Math##Name) {                     \
19     HandleScope scope(isolate);                              \
20     DCHECK(args.length() == 1);                              \
21     isolate->counters()->math_##name()->Increment();         \
22     CONVERT_DOUBLE_ARG_CHECKED(x, 0);                        \
23     return *isolate->factory()->NewHeapNumber(std::name(x)); \
24   }
25
26 RUNTIME_UNARY_MATH(Acos, acos)
27 RUNTIME_UNARY_MATH(Asin, asin)
28 RUNTIME_UNARY_MATH(Atan, atan)
29 RUNTIME_UNARY_MATH(LogRT, log)
30 #undef RUNTIME_UNARY_MATH
31
32
33 RUNTIME_FUNCTION(Runtime_DoubleHi) {
34   HandleScope scope(isolate);
35   DCHECK(args.length() == 1);
36   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
37   uint64_t integer = double_to_uint64(x);
38   integer = (integer >> 32) & 0xFFFFFFFFu;
39   return *isolate->factory()->NewNumber(static_cast<int32_t>(integer));
40 }
41
42
43 RUNTIME_FUNCTION(Runtime_DoubleLo) {
44   HandleScope scope(isolate);
45   DCHECK(args.length() == 1);
46   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
47   return *isolate->factory()->NewNumber(
48       static_cast<int32_t>(double_to_uint64(x) & 0xFFFFFFFFu));
49 }
50
51
52 RUNTIME_FUNCTION(Runtime_ConstructDouble) {
53   HandleScope scope(isolate);
54   DCHECK(args.length() == 2);
55   CONVERT_NUMBER_CHECKED(uint32_t, hi, Uint32, args[0]);
56   CONVERT_NUMBER_CHECKED(uint32_t, lo, Uint32, args[1]);
57   uint64_t result = (static_cast<uint64_t>(hi) << 32) | lo;
58   return *isolate->factory()->NewNumber(uint64_to_double(result));
59 }
60
61
62 RUNTIME_FUNCTION(Runtime_RemPiO2) {
63   SealHandleScope shs(isolate);
64   DisallowHeapAllocation no_gc;
65   DCHECK(args.length() == 2);
66   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
67   CONVERT_ARG_CHECKED(JSTypedArray, result, 1);
68   RUNTIME_ASSERT(result->byte_length() == Smi::FromInt(2 * sizeof(double)));
69   void* backing_store = JSArrayBuffer::cast(result->buffer())->backing_store();
70   double* y = static_cast<double*>(backing_store);
71   return Smi::FromInt(fdlibm::rempio2(x, y));
72 }
73
74
75 static const double kPiDividedBy4 = 0.78539816339744830962;
76
77
78 RUNTIME_FUNCTION(Runtime_MathAtan2) {
79   HandleScope scope(isolate);
80   DCHECK(args.length() == 2);
81   isolate->counters()->math_atan2()->Increment();
82
83   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
84   CONVERT_DOUBLE_ARG_CHECKED(y, 1);
85   double result;
86   if (std::isinf(x) && std::isinf(y)) {
87     // Make sure that the result in case of two infinite arguments
88     // is a multiple of Pi / 4. The sign of the result is determined
89     // by the first argument (x) and the sign of the second argument
90     // determines the multiplier: one or three.
91     int multiplier = (x < 0) ? -1 : 1;
92     if (y < 0) multiplier *= 3;
93     result = multiplier * kPiDividedBy4;
94   } else {
95     result = std::atan2(x, y);
96   }
97   return *isolate->factory()->NewNumber(result);
98 }
99
100
101 RUNTIME_FUNCTION(Runtime_MathExpRT) {
102   HandleScope scope(isolate);
103   DCHECK(args.length() == 1);
104   isolate->counters()->math_exp()->Increment();
105
106   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
107   lazily_initialize_fast_exp();
108   return *isolate->factory()->NewNumber(fast_exp(x));
109 }
110
111
112 RUNTIME_FUNCTION(Runtime_MathFloorRT) {
113   HandleScope scope(isolate);
114   DCHECK(args.length() == 1);
115   isolate->counters()->math_floor()->Increment();
116
117   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
118   return *isolate->factory()->NewNumber(Floor(x));
119 }
120
121
122 // Slow version of Math.pow.  We check for fast paths for special cases.
123 // Used if VFP3 is not available.
124 RUNTIME_FUNCTION(Runtime_MathPowSlow) {
125   HandleScope scope(isolate);
126   DCHECK(args.length() == 2);
127   isolate->counters()->math_pow()->Increment();
128
129   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
130
131   // If the second argument is a smi, it is much faster to call the
132   // custom powi() function than the generic pow().
133   if (args[1]->IsSmi()) {
134     int y = args.smi_at(1);
135     return *isolate->factory()->NewNumber(power_double_int(x, y));
136   }
137
138   CONVERT_DOUBLE_ARG_CHECKED(y, 1);
139   double result = power_helper(x, y);
140   if (std::isnan(result)) return isolate->heap()->nan_value();
141   return *isolate->factory()->NewNumber(result);
142 }
143
144
145 // Fast version of Math.pow if we know that y is not an integer and y is not
146 // -0.5 or 0.5.  Used as slow case from full codegen.
147 RUNTIME_FUNCTION(Runtime_MathPowRT) {
148   HandleScope scope(isolate);
149   DCHECK(args.length() == 2);
150   isolate->counters()->math_pow()->Increment();
151
152   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
153   CONVERT_DOUBLE_ARG_CHECKED(y, 1);
154   if (y == 0) {
155     return Smi::FromInt(1);
156   } else {
157     double result = power_double_double(x, y);
158     if (std::isnan(result)) return isolate->heap()->nan_value();
159     return *isolate->factory()->NewNumber(result);
160   }
161 }
162
163
164 RUNTIME_FUNCTION(Runtime_RoundNumber) {
165   HandleScope scope(isolate);
166   DCHECK(args.length() == 1);
167   CONVERT_NUMBER_ARG_HANDLE_CHECKED(input, 0);
168   isolate->counters()->math_round()->Increment();
169
170   if (!input->IsHeapNumber()) {
171     DCHECK(input->IsSmi());
172     return *input;
173   }
174
175   Handle<HeapNumber> number = Handle<HeapNumber>::cast(input);
176
177   double value = number->value();
178   int exponent = number->get_exponent();
179   int sign = number->get_sign();
180
181   if (exponent < -1) {
182     // Number in range ]-0.5..0.5[. These always round to +/-zero.
183     if (sign) return isolate->heap()->minus_zero_value();
184     return Smi::FromInt(0);
185   }
186
187   // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and
188   // should be rounded to 2^30, which is not smi (for 31-bit smis, similar
189   // argument holds for 32-bit smis).
190   if (!sign && exponent < kSmiValueSize - 2) {
191     return Smi::FromInt(static_cast<int>(value + 0.5));
192   }
193
194   // If the magnitude is big enough, there's no place for fraction part. If we
195   // try to add 0.5 to this number, 1.0 will be added instead.
196   if (exponent >= 52) {
197     return *number;
198   }
199
200   if (sign && value >= -0.5) return isolate->heap()->minus_zero_value();
201
202   // Do not call NumberFromDouble() to avoid extra checks.
203   return *isolate->factory()->NewNumber(Floor(value + 0.5));
204 }
205
206
207 RUNTIME_FUNCTION(Runtime_MathSqrtRT) {
208   HandleScope scope(isolate);
209   DCHECK(args.length() == 1);
210   isolate->counters()->math_sqrt()->Increment();
211
212   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
213   return *isolate->factory()->NewNumber(fast_sqrt(x));
214 }
215
216
217 RUNTIME_FUNCTION(Runtime_MathFround) {
218   HandleScope scope(isolate);
219   DCHECK(args.length() == 1);
220
221   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
222   float xf = DoubleToFloat32(x);
223   return *isolate->factory()->NewNumber(xf);
224 }
225
226
227 RUNTIME_FUNCTION(RuntimeReference_MathPow) {
228   SealHandleScope shs(isolate);
229   return __RT_impl_Runtime_MathPowSlow(args, isolate);
230 }
231
232
233 RUNTIME_FUNCTION(RuntimeReference_IsMinusZero) {
234   SealHandleScope shs(isolate);
235   DCHECK(args.length() == 1);
236   CONVERT_ARG_CHECKED(Object, obj, 0);
237   if (!obj->IsHeapNumber()) return isolate->heap()->false_value();
238   HeapNumber* number = HeapNumber::cast(obj);
239   return isolate->heap()->ToBoolean(IsMinusZero(number->value()));
240 }
241 }
242 }  // namespace v8::internal