1 | /* Copyright (C) 2014 Povilas Kanapickas <povilas@radix.lt> |
2 | |
3 | Distributed under the Boost Software License, Version 1.0. |
4 | (See accompanying file LICENSE_1_0.txt or copy at |
5 | http://www.boost.org/LICENSE_1_0.txt) |
6 | */ |
7 | |
8 | #ifndef LIBSIMDPP_SIMDPP_DETAIL_ARRAY_H |
9 | #define LIBSIMDPP_SIMDPP_DETAIL_ARRAY_H |
10 | |
11 | #ifndef LIBSIMDPP_SIMD_H |
12 | #error "This file must be included through simd.h" |
13 | #endif |
14 | |
15 | #include <simdpp/types.h> |
16 | |
17 | namespace simdpp { |
18 | namespace SIMDPP_ARCH_NAMESPACE { |
19 | namespace detail { |
20 | |
21 | /* A compile-time array that uses variables instead of array for underlying |
22 | storage when element count is small. |
23 | |
24 | Variables are used as a workaround because arrays are too opaque for most |
25 | older compilers to be able to figure out whith store corresponds to which |
26 | load. |
27 | */ |
28 | template<class T, unsigned N> |
29 | class vararray { |
30 | public: |
31 | SIMDPP_INL T& operator[](unsigned id) { return d[id]; } |
32 | SIMDPP_INL const T& operator[](unsigned id) const { return d[id]; } |
33 | private: |
34 | T d[N]; |
35 | }; |
36 | |
37 | |
38 | template<class T> |
39 | class vararray<T,2> { |
40 | public: |
41 | SIMDPP_INL T& operator[](unsigned id) { return id == 0 ? d0 : d1; } |
42 | SIMDPP_INL const T& operator[](unsigned id) const { return id == 0 ? d0 : d1; } |
43 | private: |
44 | T d0, d1; |
45 | }; |
46 | |
47 | template<class T> |
48 | class vararray<T,4> { |
49 | public: |
50 | SIMDPP_INL T& operator[](unsigned id) |
51 | { |
52 | switch (id) { |
53 | case 0: return d0; |
54 | case 1: return d1; |
55 | case 2: return d2; |
56 | default: return d3; |
57 | } |
58 | } |
59 | |
60 | SIMDPP_INL const T& operator[](unsigned id) const |
61 | { |
62 | switch (id) { |
63 | case 0: return d0; |
64 | case 1: return d1; |
65 | case 2: return d2; |
66 | default: return d3; |
67 | } |
68 | } |
69 | |
70 | private: |
71 | T d0, d1, d2, d3; |
72 | }; |
73 | |
74 | } // namespace detail |
75 | } // namespace SIMDPP_ARCH_NAMESPACE |
76 | } // namespace simdpp |
77 | |
78 | #endif |
79 | |