Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:07

0001 #include <cmath>
0002 namespace {
0003   // not a generic solution (wrong for N negative for instance)
0004   template <int N>
0005   struct PowN {
0006     template <typename T>
0007     static T op(T t) {
0008       return PowN<N / 2>::op(t) * PowN<(N + 1) / 2>::op(t);
0009     }
0010   };
0011   template <>
0012   struct PowN<0> {
0013     template <typename T>
0014     static T op(T t) {
0015       return T(1);
0016     }
0017   };
0018   template <>
0019   struct PowN<1> {
0020     template <typename T>
0021     static T op(T t) {
0022       return t;
0023     }
0024   };
0025   template <>
0026   struct PowN<2> {
0027     template <typename T>
0028     static T op(T t) {
0029       return t * t;
0030     }
0031   };
0032 
0033   template <typename T>
0034   T powN(T t, int n) {
0035     switch (n) {
0036       case 4:
0037         return PowN<4>::op(t);  // the only one that matters
0038       case 3:
0039         return PowN<3>::op(t);  // and this
0040       case 8:
0041         return PowN<8>::op(t);  // used in conversion????
0042       case 2:
0043         return PowN<2>::op(t);
0044       case 5:
0045         return PowN<5>::op(t);
0046       case 6:
0047         return PowN<6>::op(t);
0048       case 7:
0049         return PowN<7>::op(t);
0050       case 0:
0051         return PowN<0>::op(t);
0052       case 1:
0053         return PowN<1>::op(t);
0054       default:
0055         return std::pow(t, T(n));
0056     }
0057   }
0058 }  // namespace