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

Side by Side Diff: src/objects-inl.h

Issue 150813004: In-heap small typed arrays (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Patch for review Created 6 years, 9 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 1703 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 1714
1715 void JSObject::initialize_elements() { 1715 void JSObject::initialize_elements() {
1716 if (map()->has_fast_smi_or_object_elements() || 1716 if (map()->has_fast_smi_or_object_elements() ||
1717 map()->has_fast_double_elements()) { 1717 map()->has_fast_double_elements()) {
1718 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array())); 1718 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array()));
1719 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array()); 1719 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array());
1720 } else if (map()->has_external_array_elements()) { 1720 } else if (map()->has_external_array_elements()) {
1721 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map()); 1721 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map());
1722 ASSERT(!GetHeap()->InNewSpace(empty_array)); 1722 ASSERT(!GetHeap()->InNewSpace(empty_array));
1723 WRITE_FIELD(this, kElementsOffset, empty_array); 1723 WRITE_FIELD(this, kElementsOffset, empty_array);
1724 } else if (map()->has_fixed_typed_array_elements()) {
1725 FixedTypedArrayBase* empty_array =
1726 GetHeap()->EmptyFixedTypedArrayForMap(map());
1727 ASSERT(!GetHeap()->InNewSpace(empty_array));
1728 WRITE_FIELD(this, kElementsOffset, empty_array);
1724 } else { 1729 } else {
1725 UNREACHABLE(); 1730 UNREACHABLE();
1726 } 1731 }
1727 } 1732 }
1728 1733
1729 1734
1730 MaybeObject* JSObject::ResetElements() { 1735 MaybeObject* JSObject::ResetElements() {
1731 if (map()->is_observed()) { 1736 if (map()->is_observed()) {
1732 // Maintain invariant that observed elements are always in dictionary mode. 1737 // Maintain invariant that observed elements are always in dictionary mode.
1733 SeededNumberDictionary* dictionary; 1738 SeededNumberDictionary* dictionary;
(...skipping 1891 matching lines...) Expand 10 before | Expand all | Expand 10 after
3625 } 3630 }
3626 3631
3627 3632
3628 void ExternalFloat64Array::set(int index, double value) { 3633 void ExternalFloat64Array::set(int index, double value) {
3629 ASSERT((index >= 0) && (index < this->length())); 3634 ASSERT((index >= 0) && (index < this->length()));
3630 double* ptr = static_cast<double*>(external_pointer()); 3635 double* ptr = static_cast<double*>(external_pointer());
3631 ptr[index] = value; 3636 ptr[index] = value;
3632 } 3637 }
3633 3638
3634 3639
3640 void* FixedTypedArrayBase::DataPtr() {
3641 return FIELD_ADDR(this, kDataOffset);
3642 }
3643
3644
3645 int FixedTypedArrayBase::DataSize() {
3646 InstanceType instance_type = map()->instance_type();
3647 int element_size;
3648 switch (instance_type) {
3649 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
3650 case FIXED_##TYPE##_ARRAY_TYPE: \
3651 element_size = size; \
3652 break;
3653
3654 TYPED_ARRAYS(TYPED_ARRAY_CASE)
3655 #undef TYPED_ARRAY_CASE
3656 default:
3657 UNREACHABLE();
3658 return 0;
3659 }
3660 return length() * element_size;
3661 }
3662
3663
3635 int FixedTypedArrayBase::size() { 3664 int FixedTypedArrayBase::size() {
3636 InstanceType instance_type = map()->instance_type(); 3665 return OBJECT_POINTER_ALIGN(kDataOffset + DataSize());
3637 int element_size;
3638 switch (instance_type) {
3639 case FIXED_UINT8_ARRAY_TYPE:
3640 case FIXED_INT8_ARRAY_TYPE:
3641 case FIXED_UINT8_CLAMPED_ARRAY_TYPE:
3642 element_size = 1;
3643 break;
3644 case FIXED_UINT16_ARRAY_TYPE:
3645 case FIXED_INT16_ARRAY_TYPE:
3646 element_size = 2;
3647 break;
3648 case FIXED_UINT32_ARRAY_TYPE:
3649 case FIXED_INT32_ARRAY_TYPE:
3650 case FIXED_FLOAT32_ARRAY_TYPE:
3651 element_size = 4;
3652 break;
3653 case FIXED_FLOAT64_ARRAY_TYPE:
3654 element_size = 8;
3655 break;
3656 default:
3657 UNREACHABLE();
3658 return 0;
3659 }
3660 return OBJECT_POINTER_ALIGN(kDataOffset + length() * element_size);
3661 } 3666 }
3662 3667
3663 3668
3669 uint8_t Uint8ArrayTraits::defaultValue() { return 0; }
3670
3671
3672 uint8_t Uint8ClampedArrayTraits::defaultValue() { return 0; }
3673
3674
3675 int8_t Int8ArrayTraits::defaultValue() { return 0; }
3676
3677
3678 uint16_t Uint16ArrayTraits::defaultValue() { return 0; }
3679
3680
3681 int16_t Int16ArrayTraits::defaultValue() { return 0; }
3682
3683
3684 uint32_t Uint32ArrayTraits::defaultValue() { return 0; }
3685
3686
3687 int32_t Int32ArrayTraits::defaultValue() { return 0; }
3688
3689
3690 float Float32ArrayTraits::defaultValue() {
3691 return static_cast<double>(OS::nan_value());
3692 }
3693
3694
3695 double Float64ArrayTraits::defaultValue() { return OS::nan_value(); }
3696
3697
3664 template <class Traits> 3698 template <class Traits>
3665 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) { 3699 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) {
3666 ASSERT((index >= 0) && (index < this->length())); 3700 ASSERT((index >= 0) && (index < this->length()));
3667 ElementType* ptr = reinterpret_cast<ElementType*>( 3701 ElementType* ptr = reinterpret_cast<ElementType*>(
3668 FIELD_ADDR(this, kDataOffset)); 3702 FIELD_ADDR(this, kDataOffset));
3669 return ptr[index]; 3703 return ptr[index];
3670 } 3704 }
3671 3705
3672 3706
3673 template<> inline 3707 template<> inline
(...skipping 15 matching lines...) Expand all
3689 3723
3690 template<> inline 3724 template<> inline
3691 void FixedTypedArray<Float64ArrayTraits>::set( 3725 void FixedTypedArray<Float64ArrayTraits>::set(
3692 int index, Float64ArrayTraits::ElementType value) { 3726 int index, Float64ArrayTraits::ElementType value) {
3693 ASSERT((index >= 0) && (index < this->length())); 3727 ASSERT((index >= 0) && (index < this->length()));
3694 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); 3728 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value);
3695 } 3729 }
3696 3730
3697 3731
3698 template <class Traits> 3732 template <class Traits>
3733 typename Traits::ElementType FixedTypedArray<Traits>::from_int(int value) {
3734 return static_cast<ElementType>(value);
3735 }
3736
3737
3738 template <> inline
3739 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_int(int value) {
3740 if (value < 0) return 0;
3741 if (value > 0xFF) return 0xFF;
3742 return static_cast<uint8_t>(value);
3743 }
3744
3745
3746 template <class Traits>
3747 typename Traits::ElementType FixedTypedArray<Traits>::from_double(
3748 double value) {
3749 return static_cast<ElementType>(DoubleToInt32(value));
3750 }
3751
3752
3753 template<> inline
3754 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_double(double value) {
3755 if (value < 0) return 0;
3756 if (value > 0xFF) return 0xFF;
3757 return static_cast<uint8_t>(lrint(value));
3758 }
3759
3760
3761 template<> inline
3762 float FixedTypedArray<Float32ArrayTraits>::from_double(double value) {
3763 return static_cast<float>(value);
3764 }
3765
3766
3767 template<> inline
3768 double FixedTypedArray<Float64ArrayTraits>::from_double(double value) {
3769 return value;
3770 }
3771
3772
3773 template <class Traits>
3699 MaybeObject* FixedTypedArray<Traits>::get(int index) { 3774 MaybeObject* FixedTypedArray<Traits>::get(int index) {
3700 return Traits::ToObject(GetHeap(), get_scalar(index)); 3775 return Traits::ToObject(GetHeap(), get_scalar(index));
3701 } 3776 }
3702 3777
3703 template <class Traits> 3778 template <class Traits>
3704 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { 3779 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) {
3705 ElementType cast_value = Traits::defaultValue(); 3780 ElementType cast_value = Traits::defaultValue();
3706 if (index < static_cast<uint32_t>(length())) { 3781 if (index < static_cast<uint32_t>(length())) {
3707 if (value->IsSmi()) { 3782 if (value->IsSmi()) {
3708 int int_value = Smi::cast(value)->value(); 3783 int int_value = Smi::cast(value)->value();
3709 cast_value = static_cast<ElementType>(int_value); 3784 cast_value = from_int(int_value);
3710 } else if (value->IsHeapNumber()) { 3785 } else if (value->IsHeapNumber()) {
3711 double double_value = HeapNumber::cast(value)->value(); 3786 double double_value = HeapNumber::cast(value)->value();
3712 cast_value = static_cast<ElementType>(DoubleToInt32(double_value)); 3787 cast_value = from_double(double_value);
3713 } else { 3788 } else {
3714 // Clamp undefined to the default value. All other types have been 3789 // Clamp undefined to the default value. All other types have been
3715 // converted to a number type further up in the call chain. 3790 // converted to a number type further up in the call chain.
3716 ASSERT(value->IsUndefined()); 3791 ASSERT(value->IsUndefined());
3717 } 3792 }
3718 set(index, cast_value); 3793 set(index, cast_value);
3719 } 3794 }
3720 return Traits::ToObject(GetHeap(), cast_value); 3795 return Traits::ToObject(GetHeap(), cast_value);
3721 } 3796 }
3722 3797
(...skipping 2260 matching lines...) Expand 10 before | Expand all | Expand 10 after
5983 #undef EXTERNAL_ELEMENTS_CHECK 6058 #undef EXTERNAL_ELEMENTS_CHECK
5984 6059
5985 6060
5986 bool JSObject::HasFixedTypedArrayElements() { 6061 bool JSObject::HasFixedTypedArrayElements() {
5987 HeapObject* array = elements(); 6062 HeapObject* array = elements();
5988 ASSERT(array != NULL); 6063 ASSERT(array != NULL);
5989 return array->IsFixedTypedArrayBase(); 6064 return array->IsFixedTypedArrayBase();
5990 } 6065 }
5991 6066
5992 6067
6068 #define FIXED_TYPED_ELEMENTS_CHECK(Type, type, TYPE, ctype, size) \
6069 bool JSObject::HasFixed##Type##Elements() { \
6070 HeapObject* array = elements(); \
6071 ASSERT(array != NULL); \
6072 if (!array->IsHeapObject()) \
6073 return false; \
6074 return array->map()->instance_type() == FIXED_##TYPE##_ARRAY_TYPE; \
6075 }
6076
6077 TYPED_ARRAYS(FIXED_TYPED_ELEMENTS_CHECK)
6078
6079 #undef FIXED_TYPED_ELEMENTS_CHECK
6080
6081
5993 bool JSObject::HasNamedInterceptor() { 6082 bool JSObject::HasNamedInterceptor() {
5994 return map()->has_named_interceptor(); 6083 return map()->has_named_interceptor();
5995 } 6084 }
5996 6085
5997 6086
5998 bool JSObject::HasIndexedInterceptor() { 6087 bool JSObject::HasIndexedInterceptor() {
5999 return map()->has_indexed_interceptor(); 6088 return map()->has_indexed_interceptor();
6000 } 6089 }
6001 6090
6002 6091
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
6753 #undef READ_UINT32_FIELD 6842 #undef READ_UINT32_FIELD
6754 #undef WRITE_UINT32_FIELD 6843 #undef WRITE_UINT32_FIELD
6755 #undef READ_SHORT_FIELD 6844 #undef READ_SHORT_FIELD
6756 #undef WRITE_SHORT_FIELD 6845 #undef WRITE_SHORT_FIELD
6757 #undef READ_BYTE_FIELD 6846 #undef READ_BYTE_FIELD
6758 #undef WRITE_BYTE_FIELD 6847 #undef WRITE_BYTE_FIELD
6759 6848
6760 } } // namespace v8::internal 6849 } } // namespace v8::internal
6761 6850
6762 #endif // V8_OBJECTS_INL_H_ 6851 #endif // V8_OBJECTS_INL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698