Index: src/x64/builtins-x64.cc |
diff --git a/src/x64/builtins-x64.cc b/src/x64/builtins-x64.cc |
index 89e8078c2035f629adc2d70f5661da363461248f..7d24df5fa0f73d1df81e56db57fd4cf81c3b7343 100644 |
--- a/src/x64/builtins-x64.cc |
+++ b/src/x64/builtins-x64.cc |
@@ -1480,6 +1480,118 @@ void Builtins::Generate_ArrayCode(MacroAssembler* masm) { |
// static |
+void Builtins::Generate_MathMaxMin(MacroAssembler* masm, MathMaxMinKind kind) { |
+ // ----------- S t a t e ------------- |
+ // -- rax : number of arguments |
+ // -- rsp[0] : return address |
+ // -- rsp[(argc - n) * 8] : arg[n] (zero-based) |
+ // -- rsp[(argc + 1) * 8] : receiver |
+ // ----------------------------------- |
+ Condition const cc = (kind == MathMaxMinKind::kMin) ? below : above; |
+ Heap::RootListIndex const root_index = |
+ (kind == MathMaxMinKind::kMin) ? Heap::kInfinityValueRootIndex |
+ : Heap::kMinusInfinityValueRootIndex; |
+ XMMRegister const reg = (kind == MathMaxMinKind::kMin) ? xmm1 : xmm0; |
+ |
+ // Load the accumulator with the default return value (either -Infinity or |
+ // +Infinity), with the tagged value in rdx and the double value in xmm0. |
+ __ LoadRoot(rdx, root_index); |
+ __ Movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset)); |
+ __ Move(rcx, rax); |
+ |
+ Label done_loop, loop; |
+ __ bind(&loop); |
+ { |
+ // Check if all parameters done. |
+ __ testp(rcx, rcx); |
+ __ j(zero, &done_loop); |
+ |
+ // Load the next parameter tagged value into rbx. |
+ __ movp(rbx, Operand(rsp, rcx, times_pointer_size, 0)); |
+ |
+ // Load the double value of the parameter into xmm1, maybe converting the |
+ // parameter to a number first using the ToNumberStub if necessary. |
+ Label convert, convert_smi, convert_number, done_convert; |
+ __ bind(&convert); |
+ __ JumpIfSmi(rbx, &convert_smi); |
+ __ JumpIfRoot(FieldOperand(rbx, HeapObject::kMapOffset), |
+ Heap::kHeapNumberMapRootIndex, &convert_number); |
+ { |
+ // Parameter is not a Number, use the ToNumberStub to convert it. |
+ FrameScope scope(masm, StackFrame::INTERNAL); |
+ __ Integer32ToSmi(rax, rax); |
+ __ Integer32ToSmi(rcx, rcx); |
+ __ Push(rax); |
+ __ Push(rcx); |
+ __ Push(rdx); |
+ __ movp(rax, rbx); |
+ ToNumberStub stub(masm->isolate()); |
+ __ CallStub(&stub); |
+ __ movp(rbx, rax); |
+ __ Pop(rdx); |
+ __ Pop(rcx); |
+ __ Pop(rax); |
+ { |
+ // Restore the double accumulator value (xmm0). |
+ Label restore_smi, done_restore; |
+ __ JumpIfSmi(rdx, &restore_smi, Label::kNear); |
+ __ Movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset)); |
+ __ jmp(&done_restore, Label::kNear); |
+ __ bind(&restore_smi); |
+ __ SmiToDouble(xmm0, rdx); |
+ __ bind(&done_restore); |
+ } |
+ __ SmiToInteger32(rcx, rcx); |
+ __ SmiToInteger32(rax, rax); |
+ } |
+ __ jmp(&convert); |
+ __ bind(&convert_number); |
+ __ Movsd(xmm1, FieldOperand(rbx, HeapNumber::kValueOffset)); |
+ __ jmp(&done_convert, Label::kNear); |
+ __ bind(&convert_smi); |
+ __ SmiToDouble(xmm1, rbx); |
+ __ bind(&done_convert); |
+ |
+ // Perform the actual comparison with the accumulator value on the left hand |
+ // side (xmm0) and the next parameter value on the right hand side (xmm1). |
+ Label compare_equal, compare_nan, compare_swap, done_compare; |
+ __ Ucomisd(xmm0, xmm1); |
+ __ j(parity_even, &compare_nan, Label::kNear); |
+ __ j(cc, &done_compare, Label::kNear); |
+ __ j(equal, &compare_equal, Label::kNear); |
+ |
+ // Result is on the right hand side. |
+ __ bind(&compare_swap); |
+ __ Movaps(xmm0, xmm1); |
+ __ Move(rdx, rbx); |
+ __ jmp(&done_compare, Label::kNear); |
+ |
+ // At least one side is NaN, which means that the result will be NaN too. |
+ __ bind(&compare_nan); |
+ __ LoadRoot(rdx, Heap::kNanValueRootIndex); |
+ __ Movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset)); |
+ __ jmp(&done_compare, Label::kNear); |
+ |
+ // Left and right hand side are equal, check for -0 vs. +0. |
+ __ bind(&compare_equal); |
+ __ Movmskpd(kScratchRegister, reg); |
+ __ testl(kScratchRegister, Immediate(1)); |
+ __ j(not_zero, &compare_swap); |
+ |
+ __ bind(&done_compare); |
+ __ decp(rcx); |
+ __ jmp(&loop); |
+ } |
+ |
+ __ bind(&done_loop); |
+ __ PopReturnAddressTo(rcx); |
+ __ leap(rsp, Operand(rsp, rax, times_pointer_size, kPointerSize)); |
+ __ PushReturnAddressFrom(rcx); |
+ __ movp(rax, rdx); |
+ __ Ret(); |
+} |
+ |
+// static |
void Builtins::Generate_NumberConstructor(MacroAssembler* masm) { |
// ----------- S t a t e ------------- |
// -- rax : number of arguments |