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

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 landing 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
« no previous file with comments | « src/objects-debug.cc ('k') | src/runtime.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 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 1718 matching lines...) Expand 10 before | Expand all | Expand 10 after
1729 1729
1730 void JSObject::initialize_elements() { 1730 void JSObject::initialize_elements() {
1731 if (map()->has_fast_smi_or_object_elements() || 1731 if (map()->has_fast_smi_or_object_elements() ||
1732 map()->has_fast_double_elements()) { 1732 map()->has_fast_double_elements()) {
1733 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array())); 1733 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array()));
1734 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array()); 1734 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array());
1735 } else if (map()->has_external_array_elements()) { 1735 } else if (map()->has_external_array_elements()) {
1736 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map()); 1736 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map());
1737 ASSERT(!GetHeap()->InNewSpace(empty_array)); 1737 ASSERT(!GetHeap()->InNewSpace(empty_array));
1738 WRITE_FIELD(this, kElementsOffset, empty_array); 1738 WRITE_FIELD(this, kElementsOffset, empty_array);
1739 } else if (map()->has_fixed_typed_array_elements()) {
1740 FixedTypedArrayBase* empty_array =
1741 GetHeap()->EmptyFixedTypedArrayForMap(map());
1742 ASSERT(!GetHeap()->InNewSpace(empty_array));
1743 WRITE_FIELD(this, kElementsOffset, empty_array);
1739 } else { 1744 } else {
1740 UNREACHABLE(); 1745 UNREACHABLE();
1741 } 1746 }
1742 } 1747 }
1743 1748
1744 1749
1745 MaybeObject* JSObject::ResetElements() { 1750 MaybeObject* JSObject::ResetElements() {
1746 if (map()->is_observed()) { 1751 if (map()->is_observed()) {
1747 // Maintain invariant that observed elements are always in dictionary mode. 1752 // Maintain invariant that observed elements are always in dictionary mode.
1748 SeededNumberDictionary* dictionary; 1753 SeededNumberDictionary* dictionary;
(...skipping 1920 matching lines...) Expand 10 before | Expand all | Expand 10 after
3669 } 3674 }
3670 3675
3671 3676
3672 void ExternalFloat64Array::set(int index, double value) { 3677 void ExternalFloat64Array::set(int index, double value) {
3673 ASSERT((index >= 0) && (index < this->length())); 3678 ASSERT((index >= 0) && (index < this->length()));
3674 double* ptr = static_cast<double*>(external_pointer()); 3679 double* ptr = static_cast<double*>(external_pointer());
3675 ptr[index] = value; 3680 ptr[index] = value;
3676 } 3681 }
3677 3682
3678 3683
3684 void* FixedTypedArrayBase::DataPtr() {
3685 return FIELD_ADDR(this, kDataOffset);
3686 }
3687
3688
3689 int FixedTypedArrayBase::DataSize() {
3690 InstanceType instance_type = map()->instance_type();
3691 int element_size;
3692 switch (instance_type) {
3693 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
3694 case FIXED_##TYPE##_ARRAY_TYPE: \
3695 element_size = size; \
3696 break;
3697
3698 TYPED_ARRAYS(TYPED_ARRAY_CASE)
3699 #undef TYPED_ARRAY_CASE
3700 default:
3701 UNREACHABLE();
3702 return 0;
3703 }
3704 return length() * element_size;
3705 }
3706
3707
3679 int FixedTypedArrayBase::size() { 3708 int FixedTypedArrayBase::size() {
3680 InstanceType instance_type = map()->instance_type(); 3709 return OBJECT_POINTER_ALIGN(kDataOffset + DataSize());
3681 int element_size;
3682 switch (instance_type) {
3683 case FIXED_UINT8_ARRAY_TYPE:
3684 case FIXED_INT8_ARRAY_TYPE:
3685 case FIXED_UINT8_CLAMPED_ARRAY_TYPE:
3686 element_size = 1;
3687 break;
3688 case FIXED_UINT16_ARRAY_TYPE:
3689 case FIXED_INT16_ARRAY_TYPE:
3690 element_size = 2;
3691 break;
3692 case FIXED_UINT32_ARRAY_TYPE:
3693 case FIXED_INT32_ARRAY_TYPE:
3694 case FIXED_FLOAT32_ARRAY_TYPE:
3695 element_size = 4;
3696 break;
3697 case FIXED_FLOAT64_ARRAY_TYPE:
3698 element_size = 8;
3699 break;
3700 default:
3701 UNREACHABLE();
3702 return 0;
3703 }
3704 return OBJECT_POINTER_ALIGN(kDataOffset + length() * element_size);
3705 } 3710 }
3706 3711
3707 3712
3713 uint8_t Uint8ArrayTraits::defaultValue() { return 0; }
3714
3715
3716 uint8_t Uint8ClampedArrayTraits::defaultValue() { return 0; }
3717
3718
3719 int8_t Int8ArrayTraits::defaultValue() { return 0; }
3720
3721
3722 uint16_t Uint16ArrayTraits::defaultValue() { return 0; }
3723
3724
3725 int16_t Int16ArrayTraits::defaultValue() { return 0; }
3726
3727
3728 uint32_t Uint32ArrayTraits::defaultValue() { return 0; }
3729
3730
3731 int32_t Int32ArrayTraits::defaultValue() { return 0; }
3732
3733
3734 float Float32ArrayTraits::defaultValue() {
3735 return static_cast<float>(OS::nan_value());
3736 }
3737
3738
3739 double Float64ArrayTraits::defaultValue() { return OS::nan_value(); }
3740
3741
3708 template <class Traits> 3742 template <class Traits>
3709 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) { 3743 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) {
3710 ASSERT((index >= 0) && (index < this->length())); 3744 ASSERT((index >= 0) && (index < this->length()));
3711 ElementType* ptr = reinterpret_cast<ElementType*>( 3745 ElementType* ptr = reinterpret_cast<ElementType*>(
3712 FIELD_ADDR(this, kDataOffset)); 3746 FIELD_ADDR(this, kDataOffset));
3713 return ptr[index]; 3747 return ptr[index];
3714 } 3748 }
3715 3749
3716 3750
3717 template<> inline 3751 template<> inline
(...skipping 15 matching lines...) Expand all
3733 3767
3734 template<> inline 3768 template<> inline
3735 void FixedTypedArray<Float64ArrayTraits>::set( 3769 void FixedTypedArray<Float64ArrayTraits>::set(
3736 int index, Float64ArrayTraits::ElementType value) { 3770 int index, Float64ArrayTraits::ElementType value) {
3737 ASSERT((index >= 0) && (index < this->length())); 3771 ASSERT((index >= 0) && (index < this->length()));
3738 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); 3772 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value);
3739 } 3773 }
3740 3774
3741 3775
3742 template <class Traits> 3776 template <class Traits>
3777 typename Traits::ElementType FixedTypedArray<Traits>::from_int(int value) {
3778 return static_cast<ElementType>(value);
3779 }
3780
3781
3782 template <> inline
3783 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_int(int value) {
3784 if (value < 0) return 0;
3785 if (value > 0xFF) return 0xFF;
3786 return static_cast<uint8_t>(value);
3787 }
3788
3789
3790 template <class Traits>
3791 typename Traits::ElementType FixedTypedArray<Traits>::from_double(
3792 double value) {
3793 return static_cast<ElementType>(DoubleToInt32(value));
3794 }
3795
3796
3797 template<> inline
3798 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_double(double value) {
3799 if (value < 0) return 0;
3800 if (value > 0xFF) return 0xFF;
3801 return static_cast<uint8_t>(lrint(value));
3802 }
3803
3804
3805 template<> inline
3806 float FixedTypedArray<Float32ArrayTraits>::from_double(double value) {
3807 return static_cast<float>(value);
3808 }
3809
3810
3811 template<> inline
3812 double FixedTypedArray<Float64ArrayTraits>::from_double(double value) {
3813 return value;
3814 }
3815
3816
3817 template <class Traits>
3743 MaybeObject* FixedTypedArray<Traits>::get(int index) { 3818 MaybeObject* FixedTypedArray<Traits>::get(int index) {
3744 return Traits::ToObject(GetHeap(), get_scalar(index)); 3819 return Traits::ToObject(GetHeap(), get_scalar(index));
3745 } 3820 }
3746 3821
3747 template <class Traits> 3822 template <class Traits>
3748 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { 3823 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) {
3749 ElementType cast_value = Traits::defaultValue(); 3824 ElementType cast_value = Traits::defaultValue();
3750 if (index < static_cast<uint32_t>(length())) { 3825 if (index < static_cast<uint32_t>(length())) {
3751 if (value->IsSmi()) { 3826 if (value->IsSmi()) {
3752 int int_value = Smi::cast(value)->value(); 3827 int int_value = Smi::cast(value)->value();
3753 cast_value = static_cast<ElementType>(int_value); 3828 cast_value = from_int(int_value);
3754 } else if (value->IsHeapNumber()) { 3829 } else if (value->IsHeapNumber()) {
3755 double double_value = HeapNumber::cast(value)->value(); 3830 double double_value = HeapNumber::cast(value)->value();
3756 cast_value = static_cast<ElementType>(DoubleToInt32(double_value)); 3831 cast_value = from_double(double_value);
3757 } else { 3832 } else {
3758 // Clamp undefined to the default value. All other types have been 3833 // Clamp undefined to the default value. All other types have been
3759 // converted to a number type further up in the call chain. 3834 // converted to a number type further up in the call chain.
3760 ASSERT(value->IsUndefined()); 3835 ASSERT(value->IsUndefined());
3761 } 3836 }
3762 set(index, cast_value); 3837 set(index, cast_value);
3763 } 3838 }
3764 return Traits::ToObject(GetHeap(), cast_value); 3839 return Traits::ToObject(GetHeap(), cast_value);
3765 } 3840 }
3766 3841
(...skipping 2247 matching lines...) Expand 10 before | Expand all | Expand 10 after
6014 #undef EXTERNAL_ELEMENTS_CHECK 6089 #undef EXTERNAL_ELEMENTS_CHECK
6015 6090
6016 6091
6017 bool JSObject::HasFixedTypedArrayElements() { 6092 bool JSObject::HasFixedTypedArrayElements() {
6018 HeapObject* array = elements(); 6093 HeapObject* array = elements();
6019 ASSERT(array != NULL); 6094 ASSERT(array != NULL);
6020 return array->IsFixedTypedArrayBase(); 6095 return array->IsFixedTypedArrayBase();
6021 } 6096 }
6022 6097
6023 6098
6099 #define FIXED_TYPED_ELEMENTS_CHECK(Type, type, TYPE, ctype, size) \
6100 bool JSObject::HasFixed##Type##Elements() { \
6101 HeapObject* array = elements(); \
6102 ASSERT(array != NULL); \
6103 if (!array->IsHeapObject()) \
6104 return false; \
6105 return array->map()->instance_type() == FIXED_##TYPE##_ARRAY_TYPE; \
6106 }
6107
6108 TYPED_ARRAYS(FIXED_TYPED_ELEMENTS_CHECK)
6109
6110 #undef FIXED_TYPED_ELEMENTS_CHECK
6111
6112
6024 bool JSObject::HasNamedInterceptor() { 6113 bool JSObject::HasNamedInterceptor() {
6025 return map()->has_named_interceptor(); 6114 return map()->has_named_interceptor();
6026 } 6115 }
6027 6116
6028 6117
6029 bool JSObject::HasIndexedInterceptor() { 6118 bool JSObject::HasIndexedInterceptor() {
6030 return map()->has_indexed_interceptor(); 6119 return map()->has_indexed_interceptor();
6031 } 6120 }
6032 6121
6033 6122
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after
6789 #undef READ_UINT32_FIELD 6878 #undef READ_UINT32_FIELD
6790 #undef WRITE_UINT32_FIELD 6879 #undef WRITE_UINT32_FIELD
6791 #undef READ_SHORT_FIELD 6880 #undef READ_SHORT_FIELD
6792 #undef WRITE_SHORT_FIELD 6881 #undef WRITE_SHORT_FIELD
6793 #undef READ_BYTE_FIELD 6882 #undef READ_BYTE_FIELD
6794 #undef WRITE_BYTE_FIELD 6883 #undef WRITE_BYTE_FIELD
6795 6884
6796 } } // namespace v8::internal 6885 } } // namespace v8::internal
6797 6886
6798 #endif // V8_OBJECTS_INL_H_ 6887 #endif // V8_OBJECTS_INL_H_
OLDNEW
« no previous file with comments | « src/objects-debug.cc ('k') | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698