File indexing completed on 2025-09-18 09:03:36
0001 #ifndef BOOST_QVM_DETAIL_VEC_REGISTER_IMPL_HPP
0002 #define BOOST_QVM_DETAIL_VEC_REGISTER_IMPL_HPP
0003
0004
0005
0006
0007
0008
0009 #include <boost/qvm/assert.hpp>
0010 #include <boost/qvm/config.hpp>
0011 #include <boost/qvm/static_assert.hpp>
0012 #include <boost/qvm/vec_traits.hpp>
0013
0014 namespace boost { namespace qvm { namespace qvm_detail {
0015
0016 template<class VecType, class ScalarType, int Dim>
0017 struct vec_register_common
0018 {
0019 typedef VecType vec_type;
0020 typedef ScalarType scalar_type;
0021 static int const dim = Dim;
0022 };
0023
0024 template<class VecType, class ScalarType, int Dim>
0025 struct vec_register_read
0026 {
0027 template<int I> static ScalarType read_element(VecType const& v);
0028
0029 template<int I, int N> struct read_element_idx_detail
0030 {
0031 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL ScalarType impl(int const i, VecType const& v)
0032 {
0033 return I == i
0034 ? read_element<I>(v)
0035 : read_element_idx_detail<I + 1, N>::impl(i, v);
0036 }
0037 };
0038
0039 template<int N> struct read_element_idx_detail<N, N>
0040 {
0041 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL ScalarType impl(int, VecType const& v)
0042 {
0043 BOOST_QVM_ASSERT(0);
0044 return read_element<0>(v);
0045 }
0046 };
0047
0048 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL ScalarType read_element_idx(int const i, VecType const& v)
0049 {
0050 return read_element_idx_detail<0, Dim>::impl(i, v);
0051 }
0052 };
0053
0054 template<class VecType, class ScalarType, int Dim>
0055 struct vec_register_write
0056 {
0057 template<int I> static ScalarType& write_element(VecType& v);
0058
0059 template<int I, int N> struct write_element_idx_detail
0060 {
0061 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL ScalarType& impl(int const i, VecType& v)
0062 {
0063 return I == i
0064 ? write_element<I>(v)
0065 : write_element_idx_detail<I + 1, N>::impl(i, v);
0066 }
0067 };
0068
0069 template<int N> struct write_element_idx_detail<N, N>
0070 {
0071 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL ScalarType& impl(int, VecType& v)
0072 {
0073 BOOST_QVM_ASSERT(0);
0074 return write_element<0>(v);
0075 }
0076 };
0077
0078 static BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL ScalarType& write_element_idx(int const i, VecType& v)
0079 {
0080 return write_element_idx_detail<0, Dim>::impl(i, v);
0081 }
0082 };
0083
0084 }}}
0085
0086 #define BOOST_QVM_DETAIL_SPECIALIZE_QVM_DETAIL_VEC_REGISTER_READ(VecType, ScalarType, Dim, I, Read) \
0087 namespace boost { namespace qvm {namespace qvm_detail{ \
0088 template<> \
0089 template<> \
0090 BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL \
0091 ScalarType vec_register_read<VecType, ScalarType, Dim>::read_element<I>(VecType const& v) \
0092 { \
0093 BOOST_QVM_STATIC_ASSERT(I>=0); \
0094 BOOST_QVM_STATIC_ASSERT(I<Dim); \
0095 return v. Read; \
0096 } \
0097 }}}
0098
0099 #define BOOST_QVM_DETAIL_SPECIALIZE_QVM_DETAIL_VEC_REGISTER_WRITE(VecType, ScalarType, Dim, I, Write) \
0100 namespace boost { namespace qvm {namespace qvm_detail{ \
0101 template<> \
0102 template<> \
0103 BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL \
0104 ScalarType& vec_register_write<VecType, ScalarType, Dim>::write_element<I>(VecType& v) \
0105 { \
0106 BOOST_QVM_STATIC_ASSERT(I>=0); \
0107 BOOST_QVM_STATIC_ASSERT(I<Dim); \
0108 return v. Write; \
0109 }; \
0110 }}}
0111
0112 #define BOOST_QVM_DETAIL_SPECIALIZE_QVM_DETAIL_VEC_REGISTER_READ_WRITE(VecType, ScalarType, Dim, I, Read, Write)\
0113 BOOST_QVM_DETAIL_SPECIALIZE_QVM_DETAIL_VEC_REGISTER_READ(VecType, ScalarType, Dim, I, Read) \
0114 BOOST_QVM_DETAIL_SPECIALIZE_QVM_DETAIL_VEC_REGISTER_WRITE(VecType, ScalarType, Dim, I, Write)
0115
0116 #define BOOST_QVM_DETAIL_REGISTER_VEC_SPECIALIZE_VEC_TRAITS_READ(VecType, ScalarType, Dim) \
0117 namespace boost { namespace qvm { \
0118 template<> \
0119 struct vec_traits<VecType> \
0120 : qvm_detail::vec_register_common<VecType, ScalarType, Dim> \
0121 , qvm_detail::vec_register_read<VecType, ScalarType, Dim> \
0122 { \
0123 }; \
0124 }}
0125
0126 #define BOOST_QVM_DETAIL_REGISTER_VEC_SPECIALIZE_VEC_TRAITS_READ_WRITE(VecType, ScalarType, Dim)\
0127 namespace boost { namespace qvm { \
0128 template<> \
0129 struct vec_traits<VecType> \
0130 : qvm_detail::vec_register_common<VecType, ScalarType, Dim> \
0131 , qvm_detail::vec_register_read<VecType, ScalarType, Dim> \
0132 , qvm_detail::vec_register_write<VecType, ScalarType, Dim> \
0133 { \
0134 }; \
0135 }}
0136
0137 #endif