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

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
« no previous file with comments | « src/ia32/ic-ia32.cc ('k') | src/ic.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Perform float-to-int conversion with truncation (round-to-zero)
3563 // behavior.
3564 if (array_type != kExternalIntArray &&
3565 array_type != kExternalUnsignedIntArray) {
3566 if (CpuFeatures::IsSupported(SSE3)) {
3567 CpuFeatures::Scope scope(SSE3);
3568 __ push(ecx); // Make room on stack
3569 __ fisttp_s(Operand(esp, 0));
3570 __ pop(ecx);
3571 } else if (CpuFeatures::IsSupported(SSE2)) {
3572 CpuFeatures::Scope scope(SSE2);
3573 // Free the top of the FP stack, which we don't use in this code
3574 // path.
3575 __ ffree();
3576 __ fincstp();
3577
3578 __ cvttsd2si(ecx, FieldOperand(eax, HeapNumber::kValueOffset));
3579 } else {
3580 // TODO(kbr): consider supporting non-SSE2 processors properly.
3581 // The code in IntegerConvert (code-stubs-ia32.cc) is roughly what
3582 // is needed though the conversion failure case does not need to be
3583 // handled. The code below is not correct; it doesn't truncate, it
3584 // rounds.
3585 __ push(ecx); // Make room on stack
3586 __ fistp_s(Operand(esp, 0));
3587 __ pop(ecx);
3588 }
3589 } else {
3590 bool have_sse3 = CpuFeatures::IsSupported(SSE3);
3591 if (have_sse3 || !CpuFeatures::IsSupported(SSE2)) {
3592 // fisttp stores values as signed integers. To represent the
3593 // entire range of unsigned int arrays, store as a 64-bit
3594 // int and discard the high 32 bits.
3595 // If the value is NaN or +/-infinity, the result is 0x80000000,
3596 // which is automatically zero when taken mod 2^n, n < 32.
3597 __ sub(Operand(esp), Immediate(2 * kPointerSize));
3598 if (have_sse3) {
3599 CpuFeatures::Scope scope(SSE3);
3600 __ fisttp_d(Operand(esp, 0));
3601 } else {
3602 // TODO(kbr): consider supporting non-SSE2 processors properly.
3603 __ fistp_d(Operand(esp, 0));
3604 }
3605 __ pop(ecx);
3606 __ add(Operand(esp), Immediate(kPointerSize));
3607 } else {
3608 ASSERT(CpuFeatures::IsSupported(SSE2));
3609 CpuFeatures::Scope scope(SSE2);
3610 // We can easily implement the correct rounding behavior for the
3611 // range [0, 2^31-1]. For the time being, to keep this code simple,
3612 // use the wrong rounding behavior for values outside this range.
3613 __ movd(xmm0, FieldOperand(eax, HeapNumber::kValueOffset));
3614 __ LoadPowerOf2(xmm1, ecx, 31);
3615 Label is_outside_range;
3616 Label continuation_point;
3617 __ ucomisd(xmm0, xmm1);
3618 __ j(above_equal, &is_outside_range);
3619 // Free the top of the FP stack, which we don't use in this code
3620 // path.
3621 __ ffree();
3622 __ fincstp();
3623 __ cvttsd2si(ecx, FieldOperand(eax, HeapNumber::kValueOffset));
3624 __ jmp(&continuation_point);
3625 __ bind(&is_outside_range);
3626 __ sub(Operand(esp), Immediate(2 * kPointerSize));
3627 __ fistp_d(Operand(esp, 0));
3628 __ pop(ecx);
3629 __ add(Operand(esp), Immediate(kPointerSize));
3630 __ bind(&continuation_point);
3631 }
3632 }
3633 // ecx: untagged integer value
3634 switch (array_type) {
3635 case kExternalByteArray:
3636 case kExternalUnsignedByteArray:
3637 __ mov_b(Operand(edi, ebx, times_1, 0), ecx);
3638 break;
3639 case kExternalShortArray:
3640 case kExternalUnsignedShortArray:
3641 __ mov_w(Operand(edi, ebx, times_2, 0), ecx);
3642 break;
3643 case kExternalIntArray:
3644 case kExternalUnsignedIntArray: {
3645 __ mov(Operand(edi, ebx, times_4, 0), ecx);
3646 break;
3647 }
3648 default:
3649 UNREACHABLE();
3650 break;
3651 }
3652 __ ret(0); // Return original value.
3653 }
3654
3655 // Slow case: call runtime.
3656 __ bind(&slow);
3657 // ----------- S t a t e -------------
3658 // -- eax : value
3659 // -- ecx : key
3660 // -- edx : receiver
3661 // -- esp[0] : return address
3662 // -----------------------------------
3663
3664 __ pop(ebx);
3665 __ push(edx);
3666 __ push(ecx);
3667 __ push(eax);
3668 __ push(ebx);
3669
3670 // Do tail-call to runtime routine.
3671 __ TailCallRuntime(Runtime::kSetProperty, 3, 1);
3672
3673 return GetCode(flags);
3674 }
3675
3676
3288 #undef __ 3677 #undef __
3289 3678
3290 } } // namespace v8::internal 3679 } } // namespace v8::internal
3291 3680
3292 #endif // V8_TARGET_ARCH_IA32 3681 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/ic-ia32.cc ('k') | src/ic.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698