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