Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: src/ia32/stub-cache-ia32.cc

Issue 6315004: Truncate rather than round to nearest when performing float-to-integer... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3267 matching lines...) Expand 10 before | Expand all | Expand 10 after
3278 __ bind(&generic_stub_call); 3278 __ bind(&generic_stub_call);
3279 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric); 3279 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
3280 Handle<Code> generic_construct_stub(code); 3280 Handle<Code> generic_construct_stub(code);
3281 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET); 3281 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
3282 3282
3283 // Return the generated code. 3283 // Return the generated code.
3284 return GetCode(); 3284 return GetCode();
3285 } 3285 }
3286 3286
3287 3287
3288 MaybeObject* ExternalArrayStubCompiler::CompileKeyedLoadStub(
3289 ExternalArrayType array_type, Code::Flags flags) {
3290 // ----------- S t a t e -------------
3291 // -- eax : key
3292 // -- edx : receiver
3293 // -- esp[0] : return address
3294 // -----------------------------------
3295 Label slow, failed_allocation;
3296
3297 // Check that the object isn't a smi.
3298 __ test(edx, Immediate(kSmiTagMask));
3299 __ j(zero, &slow, not_taken);
3300
3301 // Check that the key is a smi.
3302 __ test(eax, Immediate(kSmiTagMask));
3303 __ j(not_zero, &slow, not_taken);
3304
3305 // Get the map of the receiver.
3306 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3307 // Check that the receiver does not require access checks. We need
3308 // to check this explicitly since this generic stub does not perform
3309 // map checks.
3310 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3311 1 << Map::kIsAccessCheckNeeded);
3312 __ j(not_zero, &slow, not_taken);
3313
3314 __ CmpInstanceType(ecx, JS_OBJECT_TYPE);
3315 __ j(not_equal, &slow, not_taken);
3316
3317 // Check that the elements array is the appropriate type of
3318 // ExternalArray.
3319 __ mov(ebx, FieldOperand(edx, JSObject::kElementsOffset));
3320 Handle<Map> map(Heap::MapForExternalArrayType(array_type));
3321 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
3322 Immediate(map));
3323 __ j(not_equal, &slow, not_taken);
3324
3325 // eax: key, known to be a smi.
3326 // edx: receiver, known to be a JSObject.
3327 // ebx: elements object, known to be an external array.
3328 // Check that the index is in range.
3329 __ mov(ecx, eax);
3330 __ SmiUntag(ecx); // Untag the index.
3331 __ cmp(ecx, FieldOperand(ebx, ExternalArray::kLengthOffset));
3332 // Unsigned comparison catches both negative and too-large values.
3333 __ j(above_equal, &slow);
3334
3335 __ mov(ebx, FieldOperand(ebx, ExternalArray::kExternalPointerOffset));
3336 // ebx: base pointer of external storage
3337 switch (array_type) {
3338 case kExternalByteArray:
3339 __ movsx_b(ecx, Operand(ebx, ecx, times_1, 0));
3340 break;
3341 case kExternalUnsignedByteArray:
3342 __ movzx_b(ecx, Operand(ebx, ecx, times_1, 0));
3343 break;
3344 case kExternalShortArray:
3345 __ movsx_w(ecx, Operand(ebx, ecx, times_2, 0));
3346 break;
3347 case kExternalUnsignedShortArray:
3348 __ movzx_w(ecx, Operand(ebx, ecx, times_2, 0));
3349 break;
3350 case kExternalIntArray:
3351 case kExternalUnsignedIntArray:
3352 __ mov(ecx, Operand(ebx, ecx, times_4, 0));
3353 break;
3354 case kExternalFloatArray:
3355 __ fld_s(Operand(ebx, ecx, times_4, 0));
3356 break;
3357 default:
3358 UNREACHABLE();
3359 break;
3360 }
3361
3362 // For integer array types:
3363 // ecx: value
3364 // For floating-point array type:
3365 // FP(0): value
3366
3367 if (array_type == kExternalIntArray ||
3368 array_type == kExternalUnsignedIntArray) {
3369 // For the Int and UnsignedInt array types, we need to see whether
3370 // the value can be represented in a Smi. If not, we need to convert
3371 // it to a HeapNumber.
3372 Label box_int;
3373 if (array_type == kExternalIntArray) {
3374 __ cmp(ecx, 0xC0000000);
3375 __ j(sign, &box_int);
3376 } else {
3377 ASSERT_EQ(array_type, kExternalUnsignedIntArray);
3378 // The test is different for unsigned int values. Since we need
3379 // the value to be in the range of a positive smi, we can't
3380 // handle either of the top two bits being set in the value.
3381 __ test(ecx, Immediate(0xC0000000));
3382 __ j(not_zero, &box_int);
3383 }
3384
3385 __ mov(eax, ecx);
3386 __ SmiTag(eax);
3387 __ ret(0);
3388
3389 __ bind(&box_int);
3390
3391 // Allocate a HeapNumber for the int and perform int-to-double
3392 // conversion.
3393 if (array_type == kExternalIntArray) {
3394 __ push(ecx);
3395 __ fild_s(Operand(esp, 0));
3396 __ pop(ecx);
3397 } else {
3398 ASSERT(array_type == kExternalUnsignedIntArray);
3399 // Need to zero-extend the value.
3400 // There's no fild variant for unsigned values, so zero-extend
3401 // to a 64-bit int manually.
3402 __ push(Immediate(0));
3403 __ push(ecx);
3404 __ fild_d(Operand(esp, 0));
3405 __ pop(ecx);
3406 __ pop(ecx);
3407 }
3408 // FP(0): value
3409 __ AllocateHeapNumber(ecx, ebx, edi, &failed_allocation);
3410 // Set the value.
3411 __ mov(eax, ecx);
3412 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
3413 __ ret(0);
3414 } else if (array_type == kExternalFloatArray) {
3415 // For the floating-point array type, we need to always allocate a
3416 // HeapNumber.
3417 __ AllocateHeapNumber(ecx, ebx, edi, &failed_allocation);
3418 // Set the value.
3419 __ mov(eax, ecx);
3420 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
3421 __ ret(0);
3422 } else {
3423 __ mov(eax, ecx);
3424 __ SmiTag(eax);
3425 __ ret(0);
3426 }
3427
3428 // If we fail allocation of the HeapNumber, we still have a value on
3429 // top of the FPU stack. Remove it.
3430 __ bind(&failed_allocation);
3431 __ ffree();
3432 __ fincstp();
3433 // Fall through to slow case.
3434
3435 // Slow case: Jump to runtime.
3436 __ bind(&slow);
3437 __ IncrementCounter(&Counters::keyed_load_external_array_slow, 1);
3438 // ----------- S t a t e -------------
3439 // -- eax : key
3440 // -- edx : receiver
3441 // -- esp[0] : return address
3442 // -----------------------------------
3443
3444 __ pop(ebx);
3445 __ push(edx); // receiver
3446 __ push(eax); // name
3447 __ push(ebx); // return address
3448
3449 // Perform tail call to the entry.
3450 __ TailCallRuntime(Runtime::kKeyedGetProperty, 2, 1);
3451
3452 // Return the generated code.
3453 return GetCode(flags);
3454 }
3455
3456
3457 MaybeObject* ExternalArrayStubCompiler::CompileKeyedStoreStub(
3458 ExternalArrayType array_type, Code::Flags flags) {
3459 // ----------- S t a t e -------------
3460 // -- eax : value
3461 // -- ecx : key
3462 // -- edx : receiver
3463 // -- esp[0] : return address
3464 // -----------------------------------
3465 Label slow, check_heap_number;
3466
3467 // Check that the object isn't a smi.
3468 __ test(edx, Immediate(kSmiTagMask));
3469 __ j(zero, &slow);
3470 // Get the map from the receiver.
3471 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
3472 // Check that the receiver does not require access checks. We need
3473 // to do this because this generic stub does not perform map checks.
3474 __ test_b(FieldOperand(edi, Map::kBitFieldOffset),
3475 1 << Map::kIsAccessCheckNeeded);
3476 __ j(not_zero, &slow);
3477 // Check that the key is a smi.
3478 __ test(ecx, Immediate(kSmiTagMask));
3479 __ j(not_zero, &slow);
3480 // Get the instance type from the map of the receiver.
3481 __ CmpInstanceType(edi, JS_OBJECT_TYPE);
3482 __ j(not_equal, &slow);
3483
3484 // Check that the elements array is the appropriate type of
3485 // ExternalArray.
3486 // eax: value
3487 // edx: receiver, a JSObject
3488 // ecx: key, a smi
3489 __ mov(edi, FieldOperand(edx, JSObject::kElementsOffset));
3490 __ CheckMap(edi, Handle<Map>(Heap::MapForExternalArrayType(array_type)),
3491 &slow, true);
3492
3493 // Check that the index is in range.
3494 __ mov(ebx, ecx);
3495 __ SmiUntag(ebx);
3496 __ cmp(ebx, FieldOperand(edi, ExternalArray::kLengthOffset));
3497 // Unsigned comparison catches both negative and too-large values.
3498 __ j(above_equal, &slow);
3499
3500 // Handle both smis and HeapNumbers in the fast path. Go to the
3501 // runtime for all other kinds of values.
3502 // eax: value
3503 // edx: receiver
3504 // ecx: key
3505 // edi: elements array
3506 // ebx: untagged index
3507 __ test(eax, Immediate(kSmiTagMask));
3508 __ j(not_equal, &check_heap_number);
3509 // smi case
3510 __ mov(ecx, eax); // Preserve the value in eax. Key is no longer needed.
3511 __ SmiUntag(ecx);
3512 __ mov(edi, FieldOperand(edi, ExternalArray::kExternalPointerOffset));
3513 // ecx: base pointer of external storage
3514 switch (array_type) {
3515 case kExternalByteArray:
3516 case kExternalUnsignedByteArray:
3517 __ mov_b(Operand(edi, ebx, times_1, 0), ecx);
3518 break;
3519 case kExternalShortArray:
3520 case kExternalUnsignedShortArray:
3521 __ mov_w(Operand(edi, ebx, times_2, 0), ecx);
3522 break;
3523 case kExternalIntArray:
3524 case kExternalUnsignedIntArray:
3525 __ mov(Operand(edi, ebx, times_4, 0), ecx);
3526 break;
3527 case kExternalFloatArray:
3528 // Need to perform int-to-float conversion.
3529 __ push(ecx);
3530 __ fild_s(Operand(esp, 0));
3531 __ pop(ecx);
3532 __ fstp_s(Operand(edi, ebx, times_4, 0));
3533 break;
3534 default:
3535 UNREACHABLE();
3536 break;
3537 }
3538 __ ret(0); // Return the original value.
3539
3540 __ bind(&check_heap_number);
3541 // eax: value
3542 // edx: receiver
3543 // ecx: key
3544 // edi: elements array
3545 // ebx: untagged index
3546 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
3547 Immediate(Factory::heap_number_map()));
3548 __ j(not_equal, &slow);
3549
3550 // The WebGL specification leaves the behavior of storing NaN and
3551 // +/-Infinity into integer arrays basically undefined. For more
3552 // reproducible behavior, convert these to zero.
3553 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
3554 __ mov(edi, FieldOperand(edi, ExternalArray::kExternalPointerOffset));
3555 // ebx: untagged index
3556 // edi: base pointer of external storage
3557 // top of FPU stack: value
3558 if (array_type == kExternalFloatArray) {
3559 __ fstp_s(Operand(edi, ebx, times_4, 0));
3560 __ ret(0);
3561 } else {
3562 // Test the top of the FP stack for NaN.
3563 Label is_nan;
3564 __ fucomi(0);
3565 __ j(parity_even, &is_nan);
3566
3567 // Perform float-to-int conversion with truncation (round-to-zero)
3568 // behavior.
3569 if (array_type != kExternalUnsignedIntArray) {
3570 if (CpuFeatures::IsSupported(SSE3)) {
3571 CpuFeatures::Scope scope(SSE3);
3572 __ push(ecx); // Make room on stack
3573 __ fisttp_s(Operand(esp, 0));
3574 __ pop(ecx);
William Hesse 2011/01/14 14:58:17 Why are SSE3 features used on ia32, but not on x64
Ken Russell (switch to Gerrit) 2011/01/14 22:37:27 x64 can assume SSE2. This plus the availability of
3575 } else if (CpuFeatures::IsSupported(SSE2)) {
3576 CpuFeatures::Scope scope(SSE2);
3577 // Free the top of the FP stack, which we don't use in this code
3578 // path.
3579 __ ffree();
3580 __ fincstp();
3581
3582 __ cvttsd2si(ecx, FieldOperand(eax, HeapNumber::kValueOffset));
3583 } else {
3584 // TODO(kbr): consider supporting non-SSE2 processors properly.
3585 // The code in IntegerConvert (code-stubs-ia32.cc) is roughly what
3586 // is needed though the conversion failure case does not need to be
3587 // handled. The code below is not correct; it doesn't truncate, it
3588 // rounds.
3589 __ push(ecx); // Make room on stack
3590 __ fistp_s(Operand(esp, 0));
3591 __ pop(ecx);
3592 }
3593 } else {
3594 // fisttp stores values as signed integers. To represent the
3595 // entire range, we need to store as a 64-bit int and discard
3596 // the high 32 bits.
3597 bool have_sse3 = CpuFeatures::IsSupported(SSE3);
3598 if (have_sse3 || !CpuFeatures::IsSupported(SSE2)) {
3599 __ sub(Operand(esp), Immediate(2 * kPointerSize));
3600 if (have_sse3) {
3601 CpuFeatures::Scope scope(SSE3);
3602 __ fisttp_d(Operand(esp, 0));
3603 } else {
3604 // TODO(kbr): consider supporting non-SSE2 processors properly.
3605 __ fistp_d(Operand(esp, 0));
3606 }
3607 __ pop(ecx);
3608 __ add(Operand(esp), Immediate(kPointerSize));
3609 } else {
3610 ASSERT(CpuFeatures::IsSupported(SSE2));
3611 CpuFeatures::Scope scope(SSE2);
3612 // We can easily implement the correct rounding behavior for the
3613 // range [0, 2^31-1]. For the time being, to keep this code simple,
3614 // use the wrong rounding behavior for values outside this range.
3615 __ movd(xmm0, FieldOperand(eax, HeapNumber::kValueOffset));
3616 __ LoadPowerOf2(xmm1, ecx, 31);
3617 Label is_outside_range;
3618 Label continuation_point;
3619 __ ucomisd(xmm0, xmm1);
3620 __ j(above_equal, &is_outside_range);
3621 // Free the top of the FP stack, which we don't use in this code
3622 // path.
3623 __ ffree();
3624 __ fincstp();
3625 __ cvttsd2si(ecx, FieldOperand(eax, HeapNumber::kValueOffset));
3626 __ jmp(&continuation_point);
3627 __ bind(&is_outside_range);
3628 __ sub(Operand(esp), Immediate(2 * kPointerSize));
3629 __ fistp_d(Operand(esp, 0));
3630 __ pop(ecx);
3631 __ add(Operand(esp), Immediate(kPointerSize));
3632 __ bind(&continuation_point);
3633 }
3634 }
3635 // ecx: untagged integer value
3636 switch (array_type) {
3637 case kExternalByteArray:
3638 case kExternalUnsignedByteArray:
3639 __ mov_b(Operand(edi, ebx, times_1, 0), ecx);
3640 break;
3641 case kExternalShortArray:
3642 case kExternalUnsignedShortArray:
3643 __ mov_w(Operand(edi, ebx, times_2, 0), ecx);
3644 break;
3645 case kExternalIntArray:
3646 case kExternalUnsignedIntArray: {
3647 // We also need to explicitly check for +/-Infinity. These are
3648 // converted to MIN_INT, but we need to be careful not to
3649 // confuse with legal uses of MIN_INT.
3650 Label not_infinity;
William Hesse 2011/01/14 14:58:17 I don't see why numbers outside the 32-bit int ran
Ken Russell (switch to Gerrit) 2011/01/14 22:37:27 You're right, and the x64 code's behavior is what
3651 // This test would apparently detect both NaN and Infinity,
3652 // but we've already checked for NaN using the FPU hardware
3653 // above.
3654 __ mov_w(edx, FieldOperand(eax, HeapNumber::kValueOffset + 6));
3655 __ and_(edx, 0x7FF0);
3656 __ cmp(edx, 0x7FF0);
3657 __ j(not_equal, &not_infinity);
3658 __ mov(ecx, 0);
3659 __ bind(&not_infinity);
3660 __ mov(Operand(edi, ebx, times_4, 0), ecx);
3661 break;
3662 }
3663 default:
3664 UNREACHABLE();
3665 break;
3666 }
3667 __ ret(0); // Return original value.
3668
3669 __ bind(&is_nan);
3670 __ ffree();
3671 __ fincstp();
3672 switch (array_type) {
3673 case kExternalByteArray:
3674 case kExternalUnsignedByteArray:
3675 __ mov_b(Operand(edi, ebx, times_1, 0), 0);
3676 break;
3677 case kExternalShortArray:
3678 case kExternalUnsignedShortArray:
3679 __ Set(ecx, Immediate(0));
3680 __ mov_w(Operand(edi, ebx, times_2, 0), ecx);
3681 break;
3682 case kExternalIntArray:
3683 case kExternalUnsignedIntArray:
3684 __ mov(Operand(edi, ebx, times_4, 0), Immediate(0));
3685 break;
3686 default:
3687 UNREACHABLE();
3688 break;
3689 }
3690 __ ret(0); // Return the original value.
3691 }
3692
3693 // Slow case: call runtime.
3694 __ bind(&slow);
3695 // ----------- S t a t e -------------
3696 // -- eax : value
3697 // -- ecx : key
3698 // -- edx : receiver
3699 // -- esp[0] : return address
3700 // -----------------------------------
3701
3702 __ pop(ebx);
3703 __ push(edx);
3704 __ push(ecx);
3705 __ push(eax);
3706 __ push(ebx);
3707
3708 // Do tail-call to runtime routine.
3709 __ TailCallRuntime(Runtime::kSetProperty, 3, 1);
3710
3711 return GetCode(flags);
3712 }
3713
3288 #undef __ 3714 #undef __
3289 3715
3290 } } // namespace v8::internal 3716 } } // namespace v8::internal
3291 3717
3292 #endif // V8_TARGET_ARCH_IA32 3718 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/ic-ia32.cc ('k') | src/ic.h » ('j') | src/x64/assembler-x64.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698