 Chromium Code Reviews
 Chromium Code Reviews Issue 6303012:
  Truncate rather than round to nearest when performing float-to-integer...  (Closed) 
  Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
    
  
    Issue 6303012:
  Truncate rather than round to nearest when performing float-to-integer...  (Closed) 
  Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/| OLD | NEW | 
|---|---|
| 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 3288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3299 __ bind(&generic_stub_call); | 3299 __ bind(&generic_stub_call); | 
| 3300 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric); | 3300 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric); | 
| 3301 Handle<Code> generic_construct_stub(code); | 3301 Handle<Code> generic_construct_stub(code); | 
| 3302 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET); | 3302 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET); | 
| 3303 | 3303 | 
| 3304 // Return the generated code. | 3304 // Return the generated code. | 
| 3305 return GetCode(); | 3305 return GetCode(); | 
| 3306 } | 3306 } | 
| 3307 | 3307 | 
| 3308 | 3308 | 
| 3309 MaybeObject* ExternalArrayStubCompiler::CompileKeyedLoadStub( | |
| 3310 ExternalArrayType array_type, Code::Flags flags) { | |
| 3311 // ----------- S t a t e ------------- | |
| 3312 // -- eax : key | |
| 3313 // -- edx : receiver | |
| 3314 // -- esp[0] : return address | |
| 3315 // ----------------------------------- | |
| 3316 Label slow, failed_allocation; | |
| 3317 | |
| 3318 // Check that the object isn't a smi. | |
| 3319 __ test(edx, Immediate(kSmiTagMask)); | |
| 3320 __ j(zero, &slow, not_taken); | |
| 3321 | |
| 3322 // Check that the key is a smi. | |
| 3323 __ test(eax, Immediate(kSmiTagMask)); | |
| 3324 __ j(not_zero, &slow, not_taken); | |
| 3325 | |
| 3326 // Get the map of the receiver. | |
| 3327 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset)); | |
| 3328 // Check that the receiver does not require access checks. We need | |
| 3329 // to check this explicitly since this generic stub does not perform | |
| 3330 // map checks. | |
| 3331 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset), | |
| 3332 1 << Map::kIsAccessCheckNeeded); | |
| 3333 __ j(not_zero, &slow, not_taken); | |
| 3334 | |
| 3335 __ CmpInstanceType(ecx, JS_OBJECT_TYPE); | |
| 3336 __ j(not_equal, &slow, not_taken); | |
| 3337 | |
| 3338 // Check that the elements array is the appropriate type of | |
| 3339 // ExternalArray. | |
| 3340 __ mov(ebx, FieldOperand(edx, JSObject::kElementsOffset)); | |
| 3341 Handle<Map> map(Heap::MapForExternalArrayType(array_type)); | |
| 3342 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset), | |
| 3343 Immediate(map)); | |
| 3344 __ j(not_equal, &slow, not_taken); | |
| 3345 | |
| 3346 // eax: key, known to be a smi. | |
| 3347 // edx: receiver, known to be a JSObject. | |
| 3348 // ebx: elements object, known to be an external array. | |
| 3349 // Check that the index is in range. | |
| 3350 __ mov(ecx, eax); | |
| 3351 __ SmiUntag(ecx); // Untag the index. | |
| 3352 __ cmp(ecx, FieldOperand(ebx, ExternalArray::kLengthOffset)); | |
| 3353 // Unsigned comparison catches both negative and too-large values. | |
| 3354 __ j(above_equal, &slow); | |
| 3355 | |
| 3356 __ mov(ebx, FieldOperand(ebx, ExternalArray::kExternalPointerOffset)); | |
| 3357 // ebx: base pointer of external storage | |
| 3358 switch (array_type) { | |
| 3359 case kExternalByteArray: | |
| 3360 __ movsx_b(ecx, Operand(ebx, ecx, times_1, 0)); | |
| 3361 break; | |
| 3362 case kExternalUnsignedByteArray: | |
| 3363 __ movzx_b(ecx, Operand(ebx, ecx, times_1, 0)); | |
| 3364 break; | |
| 3365 case kExternalShortArray: | |
| 3366 __ movsx_w(ecx, Operand(ebx, ecx, times_2, 0)); | |
| 3367 break; | |
| 3368 case kExternalUnsignedShortArray: | |
| 3369 __ movzx_w(ecx, Operand(ebx, ecx, times_2, 0)); | |
| 3370 break; | |
| 3371 case kExternalIntArray: | |
| 3372 case kExternalUnsignedIntArray: | |
| 3373 __ mov(ecx, Operand(ebx, ecx, times_4, 0)); | |
| 3374 break; | |
| 3375 case kExternalFloatArray: | |
| 3376 __ fld_s(Operand(ebx, ecx, times_4, 0)); | |
| 3377 break; | |
| 3378 default: | |
| 3379 UNREACHABLE(); | |
| 3380 break; | |
| 3381 } | |
| 3382 | |
| 3383 // For integer array types: | |
| 3384 // ecx: value | |
| 3385 // For floating-point array type: | |
| 3386 // FP(0): value | |
| 3387 | |
| 3388 if (array_type == kExternalIntArray || | |
| 3389 array_type == kExternalUnsignedIntArray) { | |
| 3390 // For the Int and UnsignedInt array types, we need to see whether | |
| 3391 // the value can be represented in a Smi. If not, we need to convert | |
| 3392 // it to a HeapNumber. | |
| 3393 Label box_int; | |
| 3394 if (array_type == kExternalIntArray) { | |
| 3395 __ cmp(ecx, 0xC0000000); | |
| 3396 __ j(sign, &box_int); | |
| 3397 } else { | |
| 3398 ASSERT_EQ(array_type, kExternalUnsignedIntArray); | |
| 3399 // The test is different for unsigned int values. Since we need | |
| 3400 // the value to be in the range of a positive smi, we can't | |
| 3401 // handle either of the top two bits being set in the value. | |
| 3402 __ test(ecx, Immediate(0xC0000000)); | |
| 3403 __ j(not_zero, &box_int); | |
| 3404 } | |
| 3405 | |
| 3406 __ mov(eax, ecx); | |
| 3407 __ SmiTag(eax); | |
| 3408 __ ret(0); | |
| 3409 | |
| 3410 __ bind(&box_int); | |
| 3411 | |
| 3412 // Allocate a HeapNumber for the int and perform int-to-double | |
| 3413 // conversion. | |
| 3414 if (array_type == kExternalIntArray) { | |
| 3415 __ push(ecx); | |
| 3416 __ fild_s(Operand(esp, 0)); | |
| 3417 __ pop(ecx); | |
| 3418 } else { | |
| 3419 ASSERT(array_type == kExternalUnsignedIntArray); | |
| 3420 // Need to zero-extend the value. | |
| 3421 // There's no fild variant for unsigned values, so zero-extend | |
| 3422 // to a 64-bit int manually. | |
| 3423 __ push(Immediate(0)); | |
| 3424 __ push(ecx); | |
| 3425 __ fild_d(Operand(esp, 0)); | |
| 3426 __ pop(ecx); | |
| 3427 __ pop(ecx); | |
| 3428 } | |
| 3429 // FP(0): value | |
| 3430 __ AllocateHeapNumber(ecx, ebx, edi, &failed_allocation); | |
| 3431 // Set the value. | |
| 3432 __ mov(eax, ecx); | |
| 3433 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3434 __ ret(0); | |
| 3435 } else if (array_type == kExternalFloatArray) { | |
| 3436 // For the floating-point array type, we need to always allocate a | |
| 3437 // HeapNumber. | |
| 3438 __ AllocateHeapNumber(ecx, ebx, edi, &failed_allocation); | |
| 3439 // Set the value. | |
| 3440 __ mov(eax, ecx); | |
| 3441 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3442 __ ret(0); | |
| 3443 } else { | |
| 3444 __ mov(eax, ecx); | |
| 3445 __ SmiTag(eax); | |
| 3446 __ ret(0); | |
| 3447 } | |
| 3448 | |
| 3449 // If we fail allocation of the HeapNumber, we still have a value on | |
| 3450 // top of the FPU stack. Remove it. | |
| 3451 __ bind(&failed_allocation); | |
| 3452 __ ffree(); | |
| 3453 __ fincstp(); | |
| 3454 // Fall through to slow case. | |
| 3455 | |
| 3456 // Slow case: Jump to runtime. | |
| 3457 __ bind(&slow); | |
| 3458 __ IncrementCounter(&Counters::keyed_load_external_array_slow, 1); | |
| 3459 // ----------- S t a t e ------------- | |
| 3460 // -- eax : key | |
| 3461 // -- edx : receiver | |
| 3462 // -- esp[0] : return address | |
| 3463 // ----------------------------------- | |
| 3464 | |
| 3465 __ pop(ebx); | |
| 3466 __ push(edx); // receiver | |
| 3467 __ push(eax); // name | |
| 3468 __ push(ebx); // return address | |
| 3469 | |
| 3470 // Perform tail call to the entry. | |
| 3471 __ TailCallRuntime(Runtime::kKeyedGetProperty, 2, 1); | |
| 3472 | |
| 3473 // Return the generated code. | |
| 3474 return GetCode(flags); | |
| 3475 } | |
| 3476 | |
| 3477 | |
| 3478 MaybeObject* ExternalArrayStubCompiler::CompileKeyedStoreStub( | |
| 3479 ExternalArrayType array_type, Code::Flags flags) { | |
| 3480 // ----------- S t a t e ------------- | |
| 3481 // -- eax : value | |
| 3482 // -- ecx : key | |
| 3483 // -- edx : receiver | |
| 3484 // -- esp[0] : return address | |
| 3485 // ----------------------------------- | |
| 3486 Label slow, check_heap_number; | |
| 3487 | |
| 3488 // Check that the object isn't a smi. | |
| 3489 __ test(edx, Immediate(kSmiTagMask)); | |
| 3490 __ j(zero, &slow); | |
| 3491 // Get the map from the receiver. | |
| 3492 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset)); | |
| 3493 // Check that the receiver does not require access checks. We need | |
| 3494 // to do this because this generic stub does not perform map checks. | |
| 3495 __ test_b(FieldOperand(edi, Map::kBitFieldOffset), | |
| 3496 1 << Map::kIsAccessCheckNeeded); | |
| 3497 __ j(not_zero, &slow); | |
| 3498 // Check that the key is a smi. | |
| 3499 __ test(ecx, Immediate(kSmiTagMask)); | |
| 3500 __ j(not_zero, &slow); | |
| 3501 // Get the instance type from the map of the receiver. | |
| 3502 __ CmpInstanceType(edi, JS_OBJECT_TYPE); | |
| 3503 __ j(not_equal, &slow); | |
| 3504 | |
| 3505 // Check that the elements array is the appropriate type of | |
| 3506 // ExternalArray. | |
| 3507 // eax: value | |
| 3508 // edx: receiver, a JSObject | |
| 3509 // ecx: key, a smi | |
| 3510 __ mov(edi, FieldOperand(edx, JSObject::kElementsOffset)); | |
| 3511 __ CheckMap(edi, Handle<Map>(Heap::MapForExternalArrayType(array_type)), | |
| 3512 &slow, true); | |
| 3513 | |
| 3514 // Check that the index is in range. | |
| 3515 __ mov(ebx, ecx); | |
| 3516 __ SmiUntag(ebx); | |
| 3517 __ cmp(ebx, FieldOperand(edi, ExternalArray::kLengthOffset)); | |
| 3518 // Unsigned comparison catches both negative and too-large values. | |
| 3519 __ j(above_equal, &slow); | |
| 3520 | |
| 3521 // Handle both smis and HeapNumbers in the fast path. Go to the | |
| 3522 // runtime for all other kinds of values. | |
| 3523 // eax: value | |
| 3524 // edx: receiver | |
| 3525 // ecx: key | |
| 3526 // edi: elements array | |
| 3527 // ebx: untagged index | |
| 3528 __ test(eax, Immediate(kSmiTagMask)); | |
| 3529 __ j(not_equal, &check_heap_number); | |
| 3530 // smi case | |
| 3531 __ mov(ecx, eax); // Preserve the value in eax. Key is no longer needed. | |
| 3532 __ SmiUntag(ecx); | |
| 3533 __ mov(edi, FieldOperand(edi, ExternalArray::kExternalPointerOffset)); | |
| 3534 // ecx: base pointer of external storage | |
| 3535 switch (array_type) { | |
| 3536 case kExternalByteArray: | |
| 3537 case kExternalUnsignedByteArray: | |
| 3538 __ mov_b(Operand(edi, ebx, times_1, 0), ecx); | |
| 3539 break; | |
| 3540 case kExternalShortArray: | |
| 3541 case kExternalUnsignedShortArray: | |
| 3542 __ mov_w(Operand(edi, ebx, times_2, 0), ecx); | |
| 3543 break; | |
| 3544 case kExternalIntArray: | |
| 3545 case kExternalUnsignedIntArray: | |
| 3546 __ mov(Operand(edi, ebx, times_4, 0), ecx); | |
| 3547 break; | |
| 3548 case kExternalFloatArray: | |
| 3549 // Need to perform int-to-float conversion. | |
| 3550 __ push(ecx); | |
| 3551 __ fild_s(Operand(esp, 0)); | |
| 3552 __ pop(ecx); | |
| 3553 __ fstp_s(Operand(edi, ebx, times_4, 0)); | |
| 3554 break; | |
| 3555 default: | |
| 3556 UNREACHABLE(); | |
| 3557 break; | |
| 3558 } | |
| 3559 __ ret(0); // Return the original value. | |
| 3560 | |
| 3561 __ bind(&check_heap_number); | |
| 3562 // eax: value | |
| 3563 // edx: receiver | |
| 3564 // ecx: key | |
| 3565 // edi: elements array | |
| 3566 // ebx: untagged index | |
| 3567 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), | |
| 3568 Immediate(Factory::heap_number_map())); | |
| 3569 __ j(not_equal, &slow); | |
| 3570 | |
| 3571 // The WebGL specification leaves the behavior of storing NaN and | |
| 3572 // +/-Infinity into integer arrays basically undefined. For more | |
| 3573 // reproducible behavior, convert these to zero. | |
| 3574 __ mov(edi, FieldOperand(edi, ExternalArray::kExternalPointerOffset)); | |
| 3575 // ebx: untagged index | |
| 3576 // edi: base pointer of external storage | |
| 3577 if (array_type == kExternalFloatArray) { | |
| 3578 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3579 __ fstp_s(Operand(edi, ebx, times_4, 0)); | |
| 3580 __ ret(0); | |
| 3581 } else { | |
| 3582 // Perform float-to-int conversion with truncation (round-to-zero) | |
| 3583 // behavior. | |
| 3584 | |
| 3585 // For the moment we make the slow call to the runtime on | |
| 3586 // processors that don't support SSE2. The code in IntegerConvert | |
| 3587 // (code-stubs-ia32.cc) is roughly what is needed here though the | |
| 3588 // conversion failure case does not need to be handled. | |
| 3589 if (!CpuFeatures::IsSupported(SSE2)) { | |
| 3590 __ jmp(&slow); | |
| 
Erik Corry
2011/01/21 08:20:29
This jmp is not needed.
 | |
| 3591 } else { | |
| 3592 if (array_type != kExternalIntArray && | |
| 3593 array_type != kExternalUnsignedIntArray) { | |
| 3594 ASSERT(CpuFeatures::IsSupported(SSE2)); | |
| 3595 CpuFeatures::Scope scope(SSE2); | |
| 3596 __ cvttsd2si(ecx, FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3597 // ecx: untagged integer value | |
| 3598 switch (array_type) { | |
| 3599 case kExternalByteArray: | |
| 3600 case kExternalUnsignedByteArray: | |
| 3601 __ mov_b(Operand(edi, ebx, times_1, 0), ecx); | |
| 3602 break; | |
| 3603 case kExternalShortArray: | |
| 3604 case kExternalUnsignedShortArray: | |
| 3605 __ mov_w(Operand(edi, ebx, times_2, 0), ecx); | |
| 3606 break; | |
| 3607 default: | |
| 3608 UNREACHABLE(); | |
| 3609 break; | |
| 3610 } | |
| 3611 } else { | |
| 3612 if (CpuFeatures::IsSupported(SSE3)) { | |
| 3613 CpuFeatures::Scope scope(SSE3); | |
| 3614 // fisttp stores values as signed integers. To represent the | |
| 3615 // entire range of int and unsigned int arrays, store as a | |
| 3616 // 64-bit int and discard the high 32 bits. | |
| 3617 // If the value is NaN or +/-infinity, the result is 0x80000000, | |
| 3618 // which is automatically zero when taken mod 2^n, n < 32. | |
| 3619 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3620 __ sub(Operand(esp), Immediate(2 * kPointerSize)); | |
| 3621 __ fisttp_d(Operand(esp, 0)); | |
| 3622 __ pop(ecx); | |
| 3623 __ add(Operand(esp), Immediate(kPointerSize)); | |
| 3624 } else { | |
| 3625 ASSERT(CpuFeatures::IsSupported(SSE2)); | |
| 3626 CpuFeatures::Scope scope(SSE2); | |
| 3627 // We can easily implement the correct rounding behavior for the | |
| 3628 // range [0, 2^31-1]. For the time being, to keep this code simple, | |
| 3629 // make the slow runtime call for values outside this range. | |
| 3630 __ movd(xmm0, FieldOperand(eax, HeapNumber::kValueOffset)); | |
| 3631 // We will need the key if we have to make the slow runtime call. | |
| 3632 __ push(ecx); | |
| 3633 __ LoadPowerOf2(xmm1, ecx, 31); | |
| 3634 __ pop(ecx); | |
| 3635 __ ucomisd(xmm1, xmm0); | |
| 3636 __ j(above_equal, &slow); | |
| 3637 __ cvttsd2si(ecx, Operand(xmm0)); | |
| 3638 } | |
| 3639 // ecx: untagged integer value | |
| 3640 __ mov(Operand(edi, ebx, times_4, 0), ecx); | |
| 3641 } | |
| 3642 __ ret(0); // Return original value. | |
| 3643 } | |
| 3644 } | |
| 3645 | |
| 3646 // Slow case: call runtime. | |
| 3647 __ bind(&slow); | |
| 3648 // ----------- S t a t e ------------- | |
| 3649 // -- eax : value | |
| 3650 // -- ecx : key | |
| 3651 // -- edx : receiver | |
| 3652 // -- esp[0] : return address | |
| 3653 // ----------------------------------- | |
| 3654 | |
| 3655 __ pop(ebx); | |
| 3656 __ push(edx); | |
| 3657 __ push(ecx); | |
| 3658 __ push(eax); | |
| 3659 __ push(ebx); | |
| 3660 | |
| 3661 // Do tail-call to runtime routine. | |
| 3662 __ TailCallRuntime(Runtime::kSetProperty, 3, 1); | |
| 3663 | |
| 3664 return GetCode(flags); | |
| 3665 } | |
| 3666 | |
| 3667 | |
| 3309 #undef __ | 3668 #undef __ | 
| 3310 | 3669 | 
| 3311 } } // namespace v8::internal | 3670 } } // namespace v8::internal | 
| 3312 | 3671 | 
| 3313 #endif // V8_TARGET_ARCH_IA32 | 3672 #endif // V8_TARGET_ARCH_IA32 | 
| OLD | NEW |