Newer
Older
/***************************************************************************
-------------------
begin : 2005/07/05
copyright : (C) 2005 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#include <algorithm>
#include <TNL/Devices/CudaCallable.h>
/***
* This function returns minimum of two numbers.
* GPU device code uses the functions defined in the CUDA's math_functions.h,
* MIC uses trivial override and host uses the STL functions.
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__ inline
#if defined(__CUDA_ARCH__)
return ::min( (ResultType) a, (ResultType) b );
#elif defined(__MIC__)
return a < b ? a : b;
#endif
}
/***
* This function returns maximum of two numbers.
* GPU device code uses the functions defined in the CUDA's math_functions.h,
* MIC uses trivial override and host uses the STL functions.
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
#if defined(__CUDA_ARCH__)
return ::max( (ResultType) a, (ResultType) b );
#elif defined(__MIC__)
return a > b ? a : b;
#endif
}
/***
* This function returns absolute value of given number.
*/
template< class T >
__cuda_callable__ inline
Tomáš Oberhuber
committed
{
Tomáš Oberhuber
committed
if( n < ( T ) 0 )
return -n;
return n;
Tomáš Oberhuber
committed
/***
* This function returns argument of minimum of two numbers.
*/
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__ inline
ResultType argMin( const T1& a, const T2& b )
{
return ( a < b ) ? a : b;
}
/***
* This function returns argument of maximum of two numbers.
*/
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__
ResultType argMax( const T1& a, const T2& b )
{
return ( a > b ) ? a : b;
}
/***
* This function returns argument of minimum of absolute values of two numbers.
*/
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__ inline
ResultType argAbsMin( const T1& a, const T2& b )
{
return ( TNL::abs( a ) < TNL::abs( b ) ) ? a : b;
}
/***
* This function returns argument of maximum of absolute values of two numbers.
*/
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__
ResultType argAbsMax( const T1& a, const T2& b )
{
return ( TNL::abs( a ) > TNL::abs( b ) ) ? a : b;
}
template< typename T1, typename T2, typename ResultType = typename std::common_type< T1, T2 >::type >
__cuda_callable__ inline
Tomáš Oberhuber
committed
{
#if defined(__CUDA_ARCH__) || defined(__MIC__)
return ::pow( (ResultType) base, (ResultType) exp );
return std::pow( (ResultType) base, (ResultType) exp );
Tomáš Oberhuber
committed
__cuda_callable__ inline
#if defined(__CUDA_ARCH__) || defined(__MIC__)
return ::sqrt( value );
#endif
}
template< typename Type >
void swap( Type& a, Type& b )
Tomáš Oberhuber
committed
{
Type tmp( a );
a = b;
b = tmp;
Tomáš Oberhuber
committed
Tomáš Oberhuber
committed
{
if( a < ( T ) 0 ) return ( T ) -1;
if( a == ( T ) 0 ) return ( T ) 0;
return ( T ) 1;
bool isSmall( const Real& v,
const Real& tolerance = 1.0e-5 )
{
return ( -tolerance <= v && v <= tolerance );
}
inline int roundUpDivision( const int num, const int div )
{
return num / div + ( num % div != 0 );
}
inline int roundToMultiple( int number, int multiple )
{
return multiple*( number/ multiple + ( number % multiple != 0 ) );
}
__cuda_callable__
inline bool isPow2( int x )
{
}
__cuda_callable__
inline bool isPow2( long int x )
{