GEOSX
math.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "LvArrayConfig.hpp"
17 #include "Macros.hpp"
18 
19 // System includes
20 #include <cmath>
21 #include <type_traits>
22 
23 namespace LvArray
24 {
25 
29 namespace math
30 {
31 
35 
43 template< typename T >
44 LVARRAY_HOST_DEVICE inline constexpr
45 std::enable_if_t< std::is_arithmetic< T >::value, T >
46 max( T const a, T const b )
47 {
48 #if defined(__CUDA_ARCH__)
49  static_assert( std::is_same< decltype( ::max( a, b ) ), T >::value, "Should return a T." );
50  return ::max( a, b );
51 #else
52  return std::max( a, b );
53 #endif
54 }
55 
62 template< typename T >
63 LVARRAY_HOST_DEVICE inline constexpr
64 std::enable_if_t< std::is_arithmetic< T >::value, T >
65 min( T const a, T const b )
66 {
67 #if defined(__CUDA_ARCH__)
68  static_assert( std::is_same< decltype( ::min( a, b ) ), T >::value, "Should return a T." );
69  return ::min( a, b );
70 #else
71  return std::min( a, b );
72 #endif
73 }
74 
81 float abs( float const x )
82 { return ::fabsf( x ); }
83 
86 double abs( double const x )
87 { return ::fabs( x ); }
88 
93 template< typename T >
95 std::enable_if_t< std::is_integral< T >::value, T >
96 abs( T const x )
97 {
98 #if defined(__CUDA_ARCH__)
99  static_assert( std::is_same< decltype( ::abs( x ) ), T >::value, "Should return a T." );
100  return ::abs( x );
101 #else
102  static_assert( std::is_same< decltype( std::abs( x ) ), T >::value, "Should return a T." );
103  return std::abs( x );
104 #endif
105 }
106 
108 
112 
120 LVARRAY_HOST_DEVICE inline
121 float sqrt( float const x )
122 { return ::sqrtf( x ); }
123 
128 template< typename T >
129 LVARRAY_HOST_DEVICE inline
130 std::enable_if_t< std::is_arithmetic< T >::value, double >
131 sqrt( T const x )
132 {
133 #if defined(__CUDA_ARCH__)
134  return ::sqrt( double( x ) );
135 #else
136  return std::sqrt( x );
137 #endif
138 }
139 
146 LVARRAY_HOST_DEVICE inline
147 float invSqrt( float const x )
148 {
149 #if defined(__CUDA_ARCH__)
150  return ::rsqrtf( x );
151 #else
152  return 1 / std::sqrt( x );
153 #endif
154 }
155 
160 template< typename T >
161 LVARRAY_HOST_DEVICE inline
162 std::enable_if_t< std::is_arithmetic< T >::value, double >
163 invSqrt( T const x )
164 {
165 #if defined(__CUDACC__)
166  return ::rsqrt( double( x ) );
167 #else
168  return 1 / std::sqrt( x );
169 #endif
170 }
171 
173 
177 
183 LVARRAY_HOST_DEVICE inline
184 float sin( float const theta )
185 { return ::sinf( theta ); }
186 
188 LVARRAY_HOST_DEVICE inline
189 double sin( double const theta )
190 { return ::sin( theta ); }
191 
196 LVARRAY_HOST_DEVICE inline
197 float cos( float const theta )
198 { return ::cosf( theta ); }
199 
201 LVARRAY_HOST_DEVICE inline
202 double cos( double const theta )
203 { return ::cos( theta ); }
204 
209 LVARRAY_HOST_DEVICE inline
210 float tan( float const theta )
211 { return ::tanf( theta ); }
212 
214 LVARRAY_HOST_DEVICE inline
215 double tan( double const theta )
216 { return ::tan( theta ); }
217 
224 LVARRAY_HOST_DEVICE inline
225 void sincos( float const theta, float & sinTheta, float & cosTheta )
226 {
227 #if defined(__CUDACC__)
228  ::sincosf( theta, &sinTheta, &cosTheta );
229 #else
230  sinTheta = ::sin( theta );
231  cosTheta = ::cos( theta );
232 #endif
233 }
234 
236 LVARRAY_HOST_DEVICE inline
237 void sincos( double const theta, double & sinTheta, double & cosTheta )
238 {
239 #if defined(__CUDACC__)
240  ::sincos( theta, &sinTheta, &cosTheta );
241 #else
242  sinTheta = ::sin( theta );
243  cosTheta = ::cos( theta );
244 #endif
245 }
246 
248 
252 
258 LVARRAY_HOST_DEVICE inline
259 float asin( float const x )
260 { return ::asinf( x ); }
261 
263 LVARRAY_HOST_DEVICE inline
264 double asin( double const x )
265 { return ::asin( x ); }
266 
271 LVARRAY_HOST_DEVICE inline
272 float acos( float const x )
273 { return ::acosf( x ); }
274 
276 LVARRAY_HOST_DEVICE inline
277 double acos( double const x )
278 { return ::acos( x ); }
279 
285 LVARRAY_HOST_DEVICE inline
286 float atan2( float const y, float const x )
287 { return ::atan2f( y, x ); }
288 
290 LVARRAY_HOST_DEVICE inline
291 double atan2( double const y, double const x )
292 { return ::atan2( y, x ); }
293 
295 
296 } // namespace math
297 } // namespace LvArray
float sin(float const theta)
Definition: math.hpp:184
double asin(double const x)
Definition: math.hpp:264
double atan2(double const y, double const x)
Definition: math.hpp:291
std::enable_if_t< std::is_integral< T >::value, T > abs(T const x)
Definition: math.hpp:96
double sin(double const theta)
Definition: math.hpp:189
float sqrt(float const x)
Definition: math.hpp:121
float tan(float const theta)
Definition: math.hpp:210
double tan(double const theta)
Definition: math.hpp:215
double cos(double const theta)
Definition: math.hpp:202
float acos(float const x)
Definition: math.hpp:272
float invSqrt(float const x)
Definition: math.hpp:147
void sincos(float const theta, float &sinTheta, float &cosTheta)
Compute the sine and cosine of theta.
Definition: math.hpp:225
float abs(float const x)
Definition: math.hpp:81
std::enable_if_t< std::is_arithmetic< T >::value, double > sqrt(T const x)
Definition: math.hpp:131
float asin(float const x)
Definition: math.hpp:259
The top level namespace.
Definition: Array.hpp:24
Contains a bunch of macro definitions.
double acos(double const x)
Definition: math.hpp:277
constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > min(T const a, T const b)
Definition: math.hpp:65
constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > max(T const a, T const b)
Definition: math.hpp:46
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389
float atan2(float const y, float const x)
Definition: math.hpp:286
float cos(float const theta)
Definition: math.hpp:197