Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_MATH_POWER2_H
00002 #define OPENTISSUE_CORE_MATH_MATH_POWER2_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 namespace OpenTissue
00013 {
00014
00015 namespace math
00016 {
00017
00025 template< class T>
00026 inline bool is_power2( T val )
00027 {
00028 T next = 1u;
00029 for ( unsigned int i = 0u; i < 32u; ++i )
00030 {
00031 if ( val == next )
00032 return true;
00033 next = next << 1u;
00034 }
00035 return false;
00036 }
00037
00045 template<class T>
00046 inline T upper_power2( T val )
00047 {
00048 T next = 1u;
00049 for ( unsigned int i = 0u; i < 32u; ++i )
00050 {
00051 if ( next >= val )
00052 return next;
00053 next = next << 1u;
00054 }
00055 return 0u;
00056 }
00057
00065 template<class T>
00066 inline T lower_power2( T val )
00067 {
00068 T next = 1u << 31u;
00069 for ( unsigned int i = 0u; i < 32u; ++i )
00070 {
00071 if ( next <= val )
00072 return next;
00073 next = next >> 1u;
00074 }
00075 return 0u;
00076 }
00077
00078 }
00079
00080 }
00081
00082
00083 #endif