OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
8 #include "src/code-stub-assembler.h" | 8 #include "src/code-stub-assembler.h" |
9 #include "src/counters.h" | 9 #include "src/counters.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 // ----------------------------------------------------------------------------- | 15 // ----------------------------------------------------------------------------- |
16 // ES6 section 20.2.2 Function Properties of the Math Object | 16 // ES6 section 20.2.2 Function Properties of the Math Object |
17 | 17 |
18 class MathBuiltinsAssembler : public CodeStubAssembler { | 18 class MathBuiltinsAssembler : public CodeStubAssembler { |
19 public: | 19 public: |
20 explicit MathBuiltinsAssembler(compiler::CodeAssemblerState* state) | 20 explicit MathBuiltinsAssembler(compiler::CodeAssemblerState* state) |
21 : CodeStubAssembler(state) {} | 21 : CodeStubAssembler(state) {} |
22 | 22 |
23 protected: | 23 protected: |
24 void MathRoundingOperation(Node* (CodeStubAssembler::*float64op)(Node*)); | 24 void MathRoundingOperation(Node* (CodeStubAssembler::*float64op)(Node*)); |
25 void MathUnaryOperation(Node* (CodeStubAssembler::*float64op)(Node*)); | 25 void MathUnaryOperation(Node* (CodeStubAssembler::*float64op)(Node*)); |
| 26 void MathMaxMin(Node* (CodeStubAssembler::*float64op)(Node*, Node*), |
| 27 double default_val); |
26 }; | 28 }; |
27 | 29 |
28 // ES6 section - 20.2.2.1 Math.abs ( x ) | 30 // ES6 section - 20.2.2.1 Math.abs ( x ) |
29 TF_BUILTIN(MathAbs, CodeStubAssembler) { | 31 TF_BUILTIN(MathAbs, CodeStubAssembler) { |
30 Node* context = Parameter(4); | 32 Node* context = Parameter(4); |
31 | 33 |
32 // We might need to loop once for ToNumber conversion. | 34 // We might need to loop once for ToNumber conversion. |
33 Variable var_x(this, MachineRepresentation::kTagged); | 35 Variable var_x(this, MachineRepresentation::kTagged); |
34 Label loop(this, &var_x); | 36 Label loop(this, &var_x); |
35 var_x.Bind(Parameter(1)); | 37 var_x.Bind(Parameter(1)); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 void MathBuiltinsAssembler::MathUnaryOperation( | 158 void MathBuiltinsAssembler::MathUnaryOperation( |
157 Node* (CodeStubAssembler::*float64op)(Node*)) { | 159 Node* (CodeStubAssembler::*float64op)(Node*)) { |
158 Node* x = Parameter(1); | 160 Node* x = Parameter(1); |
159 Node* context = Parameter(4); | 161 Node* context = Parameter(4); |
160 Node* x_value = TruncateTaggedToFloat64(context, x); | 162 Node* x_value = TruncateTaggedToFloat64(context, x); |
161 Node* value = (this->*float64op)(x_value); | 163 Node* value = (this->*float64op)(x_value); |
162 Node* result = AllocateHeapNumberWithValue(value); | 164 Node* result = AllocateHeapNumberWithValue(value); |
163 Return(result); | 165 Return(result); |
164 } | 166 } |
165 | 167 |
| 168 void MathBuiltinsAssembler::MathMaxMin( |
| 169 Node* (CodeStubAssembler::*float64op)(Node*, Node*), double default_val) { |
| 170 Node* argc = Parameter(BuiltinDescriptor::kArgumentsCount); |
| 171 Node* context = Parameter(BuiltinDescriptor::kContext); |
| 172 |
| 173 CodeStubArguments arguments(this, ChangeInt32ToIntPtr(argc)); |
| 174 argc = arguments.GetLength(); |
| 175 |
| 176 Variable result(this, MachineRepresentation::kFloat64); |
| 177 result.Bind(Float64Constant(default_val)); |
| 178 |
| 179 CodeStubAssembler::VariableList vars({&result}, zone()); |
| 180 arguments.ForEach(vars, [this, float64op, context, &result](Node* arg) { |
| 181 Node* float_value = TruncateTaggedToFloat64(context, arg); |
| 182 result.Bind((this->*float64op)(result.value(), float_value)); |
| 183 }); |
| 184 |
| 185 arguments.PopAndReturn(ChangeFloat64ToTagged(result.value())); |
| 186 } |
| 187 |
166 // ES6 section 20.2.2.2 Math.acos ( x ) | 188 // ES6 section 20.2.2.2 Math.acos ( x ) |
167 TF_BUILTIN(MathAcos, MathBuiltinsAssembler) { | 189 TF_BUILTIN(MathAcos, MathBuiltinsAssembler) { |
168 MathUnaryOperation(&CodeStubAssembler::Float64Acos); | 190 MathUnaryOperation(&CodeStubAssembler::Float64Acos); |
169 } | 191 } |
170 | 192 |
171 // ES6 section 20.2.2.3 Math.acosh ( x ) | 193 // ES6 section 20.2.2.3 Math.acosh ( x ) |
172 TF_BUILTIN(MathAcosh, MathBuiltinsAssembler) { | 194 TF_BUILTIN(MathAcosh, MathBuiltinsAssembler) { |
173 MathUnaryOperation(&CodeStubAssembler::Float64Acosh); | 195 MathUnaryOperation(&CodeStubAssembler::Float64Acosh); |
174 } | 196 } |
175 | 197 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 // ES6 section 20.2.2.34 Math.tanh ( x ) | 509 // ES6 section 20.2.2.34 Math.tanh ( x ) |
488 TF_BUILTIN(MathTanh, MathBuiltinsAssembler) { | 510 TF_BUILTIN(MathTanh, MathBuiltinsAssembler) { |
489 MathUnaryOperation(&CodeStubAssembler::Float64Tanh); | 511 MathUnaryOperation(&CodeStubAssembler::Float64Tanh); |
490 } | 512 } |
491 | 513 |
492 // ES6 section 20.2.2.35 Math.trunc ( x ) | 514 // ES6 section 20.2.2.35 Math.trunc ( x ) |
493 TF_BUILTIN(MathTrunc, MathBuiltinsAssembler) { | 515 TF_BUILTIN(MathTrunc, MathBuiltinsAssembler) { |
494 MathRoundingOperation(&CodeStubAssembler::Float64Trunc); | 516 MathRoundingOperation(&CodeStubAssembler::Float64Trunc); |
495 } | 517 } |
496 | 518 |
497 void Builtins::Generate_MathMax(MacroAssembler* masm) { | 519 // ES6 section 20.2.2.24 Math.max ( value1, value2 , ...values ) |
498 Generate_MathMaxMin(masm, MathMaxMinKind::kMax); | 520 TF_BUILTIN(MathMax, MathBuiltinsAssembler) { |
| 521 MathMaxMin(&CodeStubAssembler::Float64Max, -1.0 * V8_INFINITY); |
499 } | 522 } |
500 | 523 |
501 void Builtins::Generate_MathMin(MacroAssembler* masm) { | 524 // ES6 section 20.2.2.25 Math.min ( value1, value2 , ...values ) |
502 Generate_MathMaxMin(masm, MathMaxMinKind::kMin); | 525 TF_BUILTIN(MathMin, MathBuiltinsAssembler) { |
| 526 MathMaxMin(&CodeStubAssembler::Float64Min, V8_INFINITY); |
503 } | 527 } |
504 | 528 |
505 } // namespace internal | 529 } // namespace internal |
506 } // namespace v8 | 530 } // namespace v8 |
OLD | NEW |