| 1 | /* |
| 2 | Copyright (c) 2005-2019 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "tbb/tbb_config.h" |
| 18 | |
| 19 | #if __TBB_GCC_STRICT_ALIASING_BROKEN |
| 20 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 21 | #endif |
| 22 | |
| 23 | //! Wrapper around T where all members are private. |
| 24 | /** Used to prove that aligned_space<T,N> never calls member of T. */ |
| 25 | template<typename T> |
| 26 | class Minimal { |
| 27 | Minimal(); |
| 28 | Minimal( Minimal& min ); |
| 29 | ~Minimal(); |
| 30 | void operator=( const Minimal& ); |
| 31 | T pad; |
| 32 | template<typename U> |
| 33 | friend void AssignToCheckAlignment( Minimal<U>& dst, const Minimal<U>& src ) ; |
| 34 | }; |
| 35 | |
| 36 | template<typename T> |
| 37 | void AssignToCheckAlignment( Minimal<T>& dst, const Minimal<T>& src ) { |
| 38 | dst.pad = src.pad; |
| 39 | } |
| 40 | |
| 41 | #include "tbb/aligned_space.h" |
| 42 | #include "harness_assert.h" |
| 43 | |
| 44 | static bool SpaceWasted; |
| 45 | |
| 46 | template<typename U, size_t N> |
| 47 | void TestAlignedSpaceN() { |
| 48 | typedef Minimal<U> T; |
| 49 | struct { |
| 50 | //! Pad byte increases chance that subsequent member will be misaligned if there is a problem. |
| 51 | char pad; |
| 52 | tbb::aligned_space<T ,N> space; |
| 53 | } x; |
| 54 | AssertSameType( static_cast< T *>(0), x.space.begin() ); |
| 55 | AssertSameType( static_cast< T *>(0), x.space.end() ); |
| 56 | ASSERT( reinterpret_cast<void *>(x.space.begin())==reinterpret_cast< void *>(&x.space), NULL ); |
| 57 | ASSERT( x.space.end()-x.space.begin()==N, NULL ); |
| 58 | ASSERT( reinterpret_cast<void *>(x.space.begin())>=reinterpret_cast< void *>(&x.space), NULL ); |
| 59 | ASSERT( x.space.end()<=reinterpret_cast< T *>(&x.space+1), NULL ); |
| 60 | // Though not required, a good implementation of aligned_space<T,N> does not use any more space than a T[N]. |
| 61 | SpaceWasted |= sizeof(x.space)!=sizeof(T)*N; |
| 62 | for( size_t k=1; k<N; ++k ) |
| 63 | AssignToCheckAlignment( x.space.begin()[k-1], x.space.begin()[k] ); |
| 64 | } |
| 65 | |
| 66 | #define HARNESS_NO_PARSE_COMMAND_LINE 1 |
| 67 | #include "harness.h" |
| 68 | |
| 69 | #include <typeinfo> |
| 70 | template<typename T> |
| 71 | void PrintSpaceWastingWarning() { |
| 72 | REPORT( "Consider rewriting aligned_space<%s,N> to waste less space\n" , typeid(T).name() ); |
| 73 | } |
| 74 | |
| 75 | // RTTI for long double (128 bit) is broken in libc++ up-to NDK11c. Check on newer versions of NDK. |
| 76 | #if ( __ANDROID__ && __clang__ && _LIBCPP_VERSION && __TBB_x86_64 ) |
| 77 | template<> |
| 78 | void PrintSpaceWastingWarning<long double>() { |
| 79 | REPORT( "Consider rewriting aligned_space<ld,N> to waste less space\n" ); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | template<typename T> |
| 84 | void TestAlignedSpace() { |
| 85 | SpaceWasted = false; |
| 86 | TestAlignedSpaceN<T,1>(); |
| 87 | TestAlignedSpaceN<T,2>(); |
| 88 | TestAlignedSpaceN<T,3>(); |
| 89 | TestAlignedSpaceN<T,4>(); |
| 90 | TestAlignedSpaceN<T,5>(); |
| 91 | TestAlignedSpaceN<T,6>(); |
| 92 | TestAlignedSpaceN<T,7>(); |
| 93 | TestAlignedSpaceN<T,8>(); |
| 94 | if( SpaceWasted ) |
| 95 | PrintSpaceWastingWarning<T>(); |
| 96 | } |
| 97 | |
| 98 | #include "harness_m128.h" |
| 99 | |
| 100 | int TestMain () { |
| 101 | TestAlignedSpace<char>(); |
| 102 | TestAlignedSpace<short>(); |
| 103 | TestAlignedSpace<int>(); |
| 104 | TestAlignedSpace<float>(); |
| 105 | TestAlignedSpace<double>(); |
| 106 | TestAlignedSpace<long double>(); |
| 107 | TestAlignedSpace<size_t>(); |
| 108 | #if HAVE_m128 |
| 109 | TestAlignedSpace<__m128>(); |
| 110 | #endif |
| 111 | #if HAVE_m256 |
| 112 | if (have_AVX()) TestAlignedSpace<__m256>(); |
| 113 | #endif |
| 114 | return Harness::Done; |
| 115 | } |
| 116 | |