Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:39:29

0001 #ifndef DataFormat_Math_ExtVec_H
0002 #define DataFormat_Math_ExtVec_H
0003 
0004 #include <type_traits>
0005 
0006 #define VECTOR_EXT(N) __attribute__((vector_size(N)))
0007 
0008 typedef float VECTOR_EXT(8) cms_float32x2_t;
0009 typedef float VECTOR_EXT(16) cms_float32x4_t;
0010 typedef float VECTOR_EXT(32) cms_float32x8_t;
0011 typedef double VECTOR_EXT(16) cms_float64x2_t;
0012 typedef double VECTOR_EXT(32) cms_float64x4_t;
0013 typedef double VECTOR_EXT(64) cms_float64x8_t;
0014 
0015 typedef long double VECTOR_EXT(32) cms_float128x2_t;
0016 typedef long double VECTOR_EXT(64) cms_float128x4_t;
0017 typedef long double VECTOR_EXT(128) cms_float128x8_t;
0018 
0019 // template<typename T, int N> using ExtVec =  T __attribute__( ( vector_size( N*sizeof(T) ) ) );
0020 
0021 template <typename T, int N>
0022 struct ExtVecTraits {
0023   //  typedef T __attribute__( ( vector_size( N*sizeof(T) ) ) ) type;
0024 };
0025 
0026 template <>
0027 struct ExtVecTraits<float, 2> {
0028   typedef float VECTOR_EXT(2 * sizeof(float)) type;
0029 };
0030 
0031 template <>
0032 struct ExtVecTraits<float, 4> {
0033   typedef float VECTOR_EXT(4 * sizeof(float)) type;
0034 };
0035 
0036 template <>
0037 struct ExtVecTraits<double, 2> {
0038   typedef double VECTOR_EXT(2 * sizeof(double)) type;
0039 };
0040 
0041 template <>
0042 struct ExtVecTraits<double, 4> {
0043   typedef double VECTOR_EXT(4 * sizeof(double)) type;
0044 };
0045 
0046 template <>
0047 struct ExtVecTraits<long double, 2> {
0048   typedef long double VECTOR_EXT(2 * sizeof(long double)) type;
0049 };
0050 
0051 template <>
0052 struct ExtVecTraits<long double, 4> {
0053   typedef long double VECTOR_EXT(4 * sizeof(long double)) type;
0054 };
0055 
0056 template <typename T, int N>
0057 using ExtVec = typename ExtVecTraits<T, N>::type;
0058 
0059 template <typename T>
0060 using Vec4 = ExtVec<T, 4>;
0061 template <typename T>
0062 using Vec2 = ExtVec<T, 2>;
0063 
0064 template <typename V>
0065 inline auto xy(V v) -> Vec2<typename std::remove_reference<decltype(v[0])>::type> {
0066   typedef typename std::remove_reference<decltype(v[0])>::type T;
0067   return Vec2<T>{v[0], v[1]};
0068 }
0069 
0070 template <typename V>
0071 inline auto zw(V v) -> Vec2<typename std::remove_reference<decltype(v[0])>::type> {
0072   typedef typename std::remove_reference<decltype(v[0])>::type T;
0073   return Vec2<T>{v[2], v[3]};
0074 }
0075 
0076 template <typename Vec, typename F>
0077 inline Vec apply(Vec v, F f) {
0078   typedef typename std::remove_reference<decltype(v[0])>::type T;
0079   constexpr int N = sizeof(Vec) / sizeof(T);
0080   Vec ret;
0081   for (int i = 0; i != N; ++i)
0082     ret[i] = f(v[i]);
0083   return ret;
0084 }
0085 
0086 template <typename Vec>
0087 inline Vec cross3(Vec x, Vec y) {
0088   //  typedef Vec4<T> Vec;
0089   // yz - zy, zx - xz, xy - yx, 0
0090   Vec x1200{x[1], x[2], x[0], x[0]};
0091   Vec y2010{y[2], y[0], y[1], y[0]};
0092   Vec x2010{x[2], x[0], x[1], x[0]};
0093   Vec y1200{y[1], y[2], y[0], y[0]};
0094   return x1200 * y2010 - x2010 * y1200;
0095 }
0096 
0097 template <typename V1, typename V2>
0098 inline auto cross2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
0099   return x[0] * y[1] - x[1] * y[0];
0100 }
0101 
0102 /*
0103 template<typename T> 
0104 T dot_product(Vec4<T>  x, Vec4<T>  y) {
0105   auto res = x*y;
0106   T ret=0;
0107   for (int i=0;i!=4;++i) ret+=res[i];
0108   return ret;
0109 }
0110 */
0111 
0112 /*
0113 template<typename V, int K> 
0114 inline
0115 V get1(V v) { return (V){v[K]}; }
0116 */
0117 
0118 /*
0119 template<typename T, int N> 
0120 inline
0121 T dot(ExtVec<T,N>  x, ExtVec<T,N>  y) {
0122   T ret=0;
0123   for (int i=0;i!=N;++i) ret+=x[i]*y[i];
0124   return ret;
0125 }
0126 */
0127 
0128 template <typename V>
0129 inline auto dot(V x, V y) -> typename std::remove_reference<decltype(x[0])>::type {
0130   typedef typename std::remove_reference<decltype(x[0])>::type T;
0131   constexpr int N = sizeof(V) / sizeof(T);
0132   T ret = 0;
0133   for (int i = 0; i != N; ++i)
0134     ret += x[i] * y[i];
0135   return ret;
0136 }
0137 
0138 template <typename V1, typename V2>
0139 inline auto dot2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
0140   typedef typename std::remove_reference<decltype(x[0])>::type T;
0141   T ret = 0;
0142   for (int i = 0; i != 2; ++i)
0143     ret += x[i] * y[i];
0144   return ret;
0145 }
0146 
0147 typedef Vec2<float> Vec2F;
0148 typedef Vec4<float> Vec4F;
0149 typedef Vec4<float> Vec3F;
0150 typedef Vec2<double> Vec2D;
0151 typedef Vec4<double> Vec3D;
0152 typedef Vec4<double> Vec4D;
0153 
0154 /*
0155 template<typename T>
0156 struct As3D {
0157   Vec4<T> const & v;
0158   As3D(Vec4<T> const &iv ) : v(iv){}
0159 };
0160 template<typename T>
0161 inline As3D<T> as3D(Vec4<T> const &v ) { return v;}
0162 */
0163 
0164 template <typename V>
0165 struct As3D {
0166   V const& v;
0167   As3D(V const& iv) : v(iv) {}
0168 };
0169 template <typename V>
0170 inline As3D<V> as3D(V const& v) {
0171   return v;
0172 }
0173 
0174 // rotations
0175 
0176 template <typename T>
0177 struct Rot3 {
0178   typedef Vec4<T> Vec;
0179   Vec axis[3];
0180 
0181   constexpr Rot3() : axis{{(Vec){T(1), 0, 0, 0}}, {(Vec){0, T(1), 0, 0}}, {(Vec){0, 0, T(1), 0}}} {}
0182 
0183   constexpr Rot3(Vec4<T> ix, Vec4<T> iy, Vec4<T> iz) : axis{ix, iy, iz} {}
0184 
0185   constexpr Rot3(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
0186       : axis{{(Vec){xx, xy, xz, 0}}, {(Vec){yx, yy, yz, 0}}, {(Vec){zx, zy, zz, 0}}} {}
0187 
0188   constexpr Rot3 transpose() const {
0189     return Rot3(
0190         axis[0][0], axis[1][0], axis[2][0], axis[0][1], axis[1][1], axis[2][1], axis[0][2], axis[1][2], axis[2][2]);
0191   }
0192 
0193   constexpr Vec4<T> x() const { return axis[0]; }
0194   constexpr Vec4<T> y() const { return axis[1]; }
0195   constexpr Vec4<T> z() const { return axis[2]; }
0196 
0197   // toLocal...
0198   constexpr Vec4<T> rotate(Vec4<T> v) const { return transpose().rotateBack(v); }
0199 
0200   // toGlobal...
0201   constexpr Vec4<T> rotateBack(Vec4<T> v) const { return v[0] * axis[0] + v[1] * axis[1] + v[2] * axis[2]; }
0202 
0203   Rot3 rotate(Rot3 const& r) const {
0204     Rot3 tr = transpose();
0205     return Rot3(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]), tr.rotateBack(r.axis[2]));
0206   }
0207 
0208   constexpr Rot3 rotateBack(Rot3 const& r) const {
0209     return Rot3(rotateBack(r.axis[0]), rotateBack(r.axis[1]), rotateBack(r.axis[2]));
0210   }
0211 };
0212 
0213 typedef Rot3<float> Rot3F;
0214 
0215 typedef Rot3<double> Rot3D;
0216 
0217 template <typename T>
0218 inline constexpr Rot3<T> operator*(Rot3<T> const& rh, Rot3<T> const& lh) {
0219   return lh.rotateBack(rh);
0220 }
0221 
0222 template <typename T>
0223 struct Rot2 {
0224   typedef Vec2<T> Vec;
0225   Vec2<T> axis[2];
0226 
0227   constexpr Rot2() : axis{{(Vec){T(1), 0}}, {(Vec){0, T(1)}}} {}
0228 
0229   constexpr Rot2(Vec2<T> ix, Vec2<T> iy) : axis{ix, iy} {}
0230 
0231   constexpr Rot2(T xx, T xy, T yx, T yy) : Rot2((Vec){xx, xy}, (Vec){yx, yy}) {}
0232 
0233   constexpr Rot2 transpose() const { return Rot2(axis[0][0], axis[1][0], axis[0][1], axis[1][1]); }
0234 
0235   constexpr Vec2<T> x() const { return axis[0]; }
0236   constexpr Vec2<T> y() const { return axis[1]; }
0237 
0238   // toLocal...
0239   constexpr Vec2<T> rotate(Vec2<T> v) const { return transpose().rotateBack(v); }
0240 
0241   // toGlobal...
0242   constexpr Vec2<T> rotateBack(Vec2<T> v) const { return v[0] * axis[0] + v[1] * axis[1]; }
0243 
0244   Rot2 rotate(Rot2 const& r) const {
0245     Rot2 tr = transpose();
0246     return Rot2(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]));
0247   }
0248 
0249   constexpr Rot2 rotateBack(Rot2 const& r) const { return Rot2(rotateBack(r.axis[0]), rotateBack(r.axis[1])); }
0250 };
0251 
0252 typedef Rot2<float> Rot2F;
0253 
0254 typedef Rot2<double> Rot2D;
0255 
0256 template <typename T>
0257 inline constexpr Rot2<T> operator*(Rot2<T> const& rh, Rot2<T> const& lh) {
0258   return lh.rotateBack(rh);
0259 }
0260 
0261 #include <iosfwd>
0262 std::ostream& operator<<(std::ostream& out, Vec2D const& v);
0263 std::ostream& operator<<(std::ostream& out, Vec2F const& v);
0264 std::ostream& operator<<(std::ostream& out, Vec4F const& v);
0265 std::ostream& operator<<(std::ostream& out, Vec4D const& v);
0266 
0267 std::ostream& operator<<(std::ostream& out, As3D<Vec4F> const& v);
0268 std::ostream& operator<<(std::ostream& out, As3D<Vec4D> const& v);
0269 
0270 std::ostream& operator<<(std::ostream& out, Rot3F const& v);
0271 std::ostream& operator<<(std::ostream& out, Rot3D const& v);
0272 std::ostream& operator<<(std::ostream& out, Rot2F const& v);
0273 std::ostream& operator<<(std::ostream& out, Rot2D const& v);
0274 
0275 #ifdef USE_INLINE_IO
0276 #include <ostream>
0277 std::ostream& operator<<(std::ostream& out, ::Vec4F const& v) {
0278   return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
0279 }
0280 std::ostream& operator<<(std::ostream& out, ::Vec4D const& v) {
0281   return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
0282 }
0283 std::ostream& operator<<(std::ostream& out, ::Vec2F const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
0284 std::ostream& operator<<(std::ostream& out, ::Vec2D const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
0285 
0286 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4F> const& v) {
0287   return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
0288 }
0289 
0290 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4D> const& v) {
0291   return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
0292 }
0293 
0294 std::ostream& operator<<(std::ostream& out, ::Rot3F const& r) {
0295   return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
0296 }
0297 
0298 std::ostream& operator<<(std::ostream& out, ::Rot3D const& r) {
0299   return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
0300 }
0301 
0302 std::ostream& operator<<(std::ostream& out, ::Rot2F const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
0303 
0304 std::ostream& operator<<(std::ostream& out, ::Rot2D const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
0305 #endif
0306 
0307 #endif