pow.h
Go to the documentation of this file.
1 #ifndef cetlib_pow_h
2 #define cetlib_pow_h
3 
4 // ======================================================================
5 //
6 // pow<>(): Minimal-multiply algorithm for integral non-negative powers
7 //
8 // ======================================================================
9 
10 #include "cetlib_except/exception.h"
11 
12 #include <type_traits>
13 
14 namespace cet {
15 
16  template <unsigned N, class T>
17  constexpr T pow(T x);
18 
19  template <class T>
20  constexpr T
22  {
23  return pow<2u>(x);
24  }
25  template <class T>
26  constexpr T
27  cube(T x)
28  {
29  return pow<3u>(x);
30  }
31  template <class T>
32  constexpr T
34  {
35  return pow<4u>(x);
36  }
37 
38  template <class T>
39  constexpr T diff_of_squares(T x, T y);
40 
41  template <class T>
42  constexpr T sum_of_squares(T x, T y);
43 
44  template <class T>
45  constexpr T sum_of_squares(T x, T y, T z);
46 }
47 
48 namespace cet::detail {
49  template <unsigned N, class T, unsigned = N % 2u>
50  struct pow;
51 
52  template <unsigned N, class T>
53  struct pow<N, T, 0u>;
54 
55  template <unsigned N, class T>
56  struct pow<N, T, 1u>;
57 
58  template <class T>
59  struct pow<0u, T, 0u>;
60 
61  template <class T>
62  struct pow<1u, T, 1u>;
63 
64  template <class T>
65  struct pow<2u, T, 0u>;
66 } // cet::detail
67 
68 // ----------------------------------------------------------------------
69 
70 template <unsigned N, class T>
71 constexpr T
73 {
74  return detail::pow<N, T>()(x);
75 }
76 
77 // ----------------------------------------------------------------------
78 
79 template <unsigned N, class T>
80 struct cet::detail::pow<N, T, 0u> {
81  pow<N / 2u, T> pow_half;
82  constexpr T
84  {
85  return pow_half(x * x);
86  }
87 }; // pow<N,T,0>
88 
89 template <unsigned N, class T>
90 struct cet::detail::pow<N, T, 1u> {
91  pow<N / 2u, T> pow_half;
92  constexpr T
94  {
95  return x * pow_half(x * x);
96  }
97 }; // pow<N,T,1>
98 
99 template <class T>
100 struct cet::detail::pow<0u, T, 0u> {
101  constexpr T
103  {
104  return (x != 0) ?
105  1 :
106  throw cet::exception("cet::pow") << "pow<0>(0) is indeterminate!";
107  }
108 }; // pow<0,T,0>
109 
110 template <class T>
111 struct cet::detail::pow<1u, T, 1u> {
112  constexpr T
114  {
115  return x;
116  }
117 }; // pow<1,T,1>
118 
119 template <class T>
120 struct cet::detail::pow<2u, T, 0u> {
121  constexpr T
123  {
124  return x * x;
125  }
126 }; // pow<2,T,0>
127 
128 // ----------------------------------------------------------------------
129 
130 template <class T>
131 constexpr T
133 {
134  return (x + y) * (x - y);
135 }
136 
137 template <class T>
138 constexpr T
140 {
141  return square(x) + square(y);
142 }
143 
144 template <class T>
145 constexpr T
147 {
148  return square(x) + square(y) + square(z);
149 }
150 
151 // ======================================================================
152 
153 #endif /* cetlib_pow_h */
154 
155 // Local Variables:
156 // mode: c++
157 // End:
constexpr T operator()(T x)
Definition: pow.h:93
pow< N/2u, T > pow_half
Definition: pow.h:81
constexpr T fourth(T x)
Definition: pow.h:33
constexpr T diff_of_squares(T x, T y)
Definition: pow.h:132
constexpr T sum_of_squares(T x, T y)
Definition: pow.h:139
constexpr T pow(T x)
Definition: pow.h:72
constexpr T operator()(T x)
Definition: pow.h:113
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
constexpr T square(T x)
Definition: pow.h:21
constexpr T operator()(T x)
Definition: pow.h:102
constexpr T cube(T x)
Definition: pow.h:27
constexpr T operator()(T x)
Definition: pow.h:83
pow< N/2u, T > pow_half
Definition: pow.h:91
z
Definition: test.py:28
double T
Definition: Xdiff_gwt.C:5
constexpr T operator()(T x)
Definition: pow.h:122