1 #ifndef OMEGA_H_SCALAR_HPP
2 #define OMEGA_H_SCALAR_HPP
4 #include <Omega_h_defines.hpp>
5 #include <Omega_h_fail.hpp>
27 using promoted_t =
typename Promoted<T>::type;
34 static OMEGA_H_INLINE
unsigned char max() noexcept {
return UCHAR_MAX; }
35 static OMEGA_H_INLINE
unsigned char min() noexcept {
return 0; }
40 static constexpr OMEGA_H_INLINE
signed char max() noexcept {
43 static constexpr OMEGA_H_INLINE
signed char min() noexcept {
50 static constexpr OMEGA_H_INLINE
unsigned int max() noexcept {
53 static constexpr OMEGA_H_INLINE
unsigned int min() noexcept {
return 0; }
58 static constexpr OMEGA_H_INLINE
int max() noexcept {
return INT_MAX; }
59 static constexpr OMEGA_H_INLINE
int min() noexcept {
return INT_MIN; }
64 static constexpr OMEGA_H_INLINE
unsigned long max() noexcept {
67 static constexpr OMEGA_H_INLINE
unsigned long min() noexcept {
return 0; }
72 static constexpr OMEGA_H_INLINE
signed long max() noexcept {
75 static constexpr OMEGA_H_INLINE
signed long min() noexcept {
82 static constexpr OMEGA_H_INLINE
unsigned long long max() noexcept {
85 static constexpr OMEGA_H_INLINE
unsigned long long min() noexcept {
92 static constexpr OMEGA_H_INLINE
signed long long max() noexcept {
95 static constexpr OMEGA_H_INLINE
signed long long min() noexcept {
102 static constexpr OMEGA_H_INLINE
double max() noexcept {
return DBL_MAX; }
103 static constexpr OMEGA_H_INLINE
double min() noexcept {
return -DBL_MAX; }
106 template <
typename T>
107 constexpr OMEGA_H_INLINE T max2(T a, T b) noexcept {
108 return (a < b) ? (b) : (a);
111 template <
typename T>
112 constexpr OMEGA_H_INLINE T min2(T a, T b) noexcept {
113 return (b < a) ? (b) : (a);
116 template <
typename T>
117 OMEGA_H_INLINE
void swap2(T& a, T& b) noexcept {
123 template <
typename T>
124 constexpr OMEGA_H_INLINE_BIG T factorial(T x) noexcept {
125 return (x > 1) ? (x * factorial(x - 1)) : 1;
128 template <
typename T>
129 constexpr OMEGA_H_INLINE T average(T a, T b) noexcept {
133 template <Int p,
typename T>
135 static_assert(p >= 0,
"negative power not allowed in Raise!");
136 static constexpr OMEGA_H_INLINE T eval(T x) noexcept {
141 template <
typename T>
143 static constexpr OMEGA_H_INLINE T eval(T) noexcept {
return 1; }
146 template <Int p,
typename T>
147 constexpr OMEGA_H_INLINE T
raise(T x) noexcept {
151 template <
typename T>
152 constexpr OMEGA_H_INLINE T square(T x) noexcept {
153 return raise<2, T>(x);
156 template <
typename T>
157 OMEGA_H_INLINE T cube(T x) noexcept {
158 return raise<3, T>(x);
161 OMEGA_H_INLINE Real sign(Real x) noexcept {
return (x < 0.0) ? -1.0 : 1.0; }
163 OMEGA_H_INLINE Real clamp(Real x, Real low, Real high) noexcept {
164 return min2(max2(x, low), high);
172 static OMEGA_H_INLINE Real eval(Real) noexcept {
return 1.0; }
177 static OMEGA_H_INLINE Real eval(Real x) noexcept {
return x; }
182 static OMEGA_H_INLINE Real eval(Real x) noexcept {
return std::sqrt(x); }
187 static OMEGA_H_INLINE Real eval(Real x) noexcept {
return std::cbrt(x); }
191 OMEGA_H_INLINE Real root(Real x) noexcept {
196 constexpr OMEGA_H_INLINE Int gcd(Int a, Int b) noexcept {
197 return (b == 0) ? (a) : (gcd(b, a % b));
206 template <Int np, Int dp, Int cd = gcd(np, dp)>
208 using Power<np / cd, dp / cd>::eval;
209 static_assert(cd != 1,
"reduced case should be specialized");
212 template <Int np, Int dp>
214 static OMEGA_H_INLINE Real eval(Real x) noexcept {
215 return root<dp>(raise<np>(x));
217 static_assert(np != dp,
"equal case should be specialized");
222 static OMEGA_H_INLINE Real eval(Real x) noexcept {
return x; }
225 template <Int np, Int dp>
226 OMEGA_H_INLINE Real power(Real x) noexcept {
230 OMEGA_H_INLINE Real power(Real x, Int np, Int dp) noexcept {
235 return power<1, 1>(x);
237 return power<1, 2>(x);
239 return power<1, 3>(x);
245 return power<2, 1>(x);
247 return power<2, 2>(x);
249 return power<2, 3>(x);
255 return power<3, 1>(x);
257 return power<3, 2>(x);
259 return power<3, 3>(x);
266 OMEGA_H_INLINE Real rel_diff_with_floor(
267 Real a, Real b, Real floor = EPSILON) noexcept {
268 Real am = std::abs(a);
269 Real bm = std::abs(b);
270 if (am <= floor && bm <= floor)
return 0.0;
271 return std::abs(b - a) / max2(am, bm);
274 OMEGA_H_INLINE
bool are_close(
275 Real a, Real b, Real tol = EPSILON, Real floor = EPSILON) noexcept {
276 return rel_diff_with_floor(a, b, floor) <= tol;
279 template <
typename T>
280 T divide_no_remainder(T a, T b) {
281 OMEGA_H_CHECK(b != 0);
282 OMEGA_H_CHECK(a % b == 0);
286 template <
typename T>
288 typedef T first_argument_type;
289 typedef T second_argument_type;
290 typedef T result_type;
291 OMEGA_H_INLINE T operator()(
const T& lhs,
const T& rhs)
const noexcept {
296 template <
typename T>
298 typedef T first_argument_type;
299 typedef T second_argument_type;
300 typedef bool result_type;
301 OMEGA_H_INLINE
bool operator()(
const T& lhs,
const T& rhs)
const noexcept {
306 template <
typename T>
308 typedef T first_argument_type;
309 typedef T second_argument_type;
310 typedef T result_type;
311 OMEGA_H_INLINE T operator()(
const T& lhs,
const T& rhs)
const noexcept {
312 return lhs < rhs ? rhs : lhs;
316 template <
typename T>
318 typedef T first_argument_type;
319 typedef T second_argument_type;
320 typedef T result_type;
321 OMEGA_H_INLINE T operator()(
const T& lhs,
const T& rhs)
const noexcept {
322 return lhs < rhs ? lhs : rhs;
326 template <
typename T>
328 typedef T argument_type;
329 typedef T result_type;
330 OMEGA_H_INLINE
const T& operator()(
const T& x)
const noexcept {
return x; }
333 template <
typename T>
335 typedef T first_argument_type;
336 typedef T second_argument_type;
337 typedef T result_type;
338 OMEGA_H_INLINE T operator()(
const T& lhs,
const T& rhs)
const noexcept {
356 OMEGA_H_INLINE Real sin_x_over_x(Real x) {
357 auto const y = std::abs(x);
358 auto const e2 = std::sqrt(DBL_EPSILON);
359 auto const e4 = std::sqrt(e2);
361 return std::sin(y) / y;
363 return 1.0 - y * y / 6.0;
Definition: amr_mpi_test.cpp:6
Definition: Omega_h_scalar.hpp:30
Definition: Omega_h_scalar.hpp:207
Definition: Omega_h_scalar.hpp:134
Definition: Omega_h_scalar.hpp:168
Definition: Omega_h_scalar.hpp:327
Definition: Omega_h_scalar.hpp:297
Definition: Omega_h_scalar.hpp:307
Definition: Omega_h_scalar.hpp:317
Definition: Omega_h_scalar.hpp:334
Definition: Omega_h_scalar.hpp:287