20 #ifndef LUNCHBOX_BITOPERATION_H
21 #define LUNCHBOX_BITOPERATION_H
23 #include <lunchbox/compiler.h>
25 #include <lunchbox/uuid.h>
28 # pragma warning (push)
29 # pragma warning (disable: 4985) // inconsistent decl of ceil
31 # pragma warning (pop)
33 # include <builtins.h>
34 # include <byteswap.h>
35 #elif defined (LB_GCC_4_3_OR_OLDER) && !defined(__clang__) && !defined( __APPLE__ )
36 # include <byteswap.h>
37 # define LB_GCC_BSWAP_FUNCTION
46 template<
class T >
void byteswap( T& value );
49 template<>
inline int32_t getIndexOfLastBit< uint32_t >( uint32_t value )
52 return ::fls( value ) - 1;
53 #elif defined __GNUC__
54 return value ? (31 - __builtin_clz( value )) : -1;
55 #elif defined _MSC_VER
57 return _BitScanReverse( &i, value ) ? i : -1;
69 template<>
inline int32_t getIndexOfLastBit< uint64_t >( uint64_t value )
72 return value ? (63 - __builtin_clzll( value )) : -1;
75 return _BitScanReverse64( &i, value ) ? i : -1;
87 #if defined(__linux__) && defined(_LP64)
88 template<>
inline int32_t
89 getIndexOfLastBit< unsigned long long >(
unsigned long long value )
95 int32_t getIndexOfLastBit< unsigned long >(
unsigned long value )
99 int32_t getIndexOfLastBit< unsigned long >(
unsigned long value )
104 template<>
inline void byteswap(
void*& ) { }
105 template<>
inline void byteswap(
bool&) { }
106 template<>
inline void byteswap(
char& ) { }
107 template<>
inline void byteswap(
signed char& ) { }
108 template<>
inline void byteswap(
unsigned char& ) { }
110 template<>
inline void byteswap( uint32_t& value )
113 value = _byteswap_ulong( value );
114 #elif defined __xlC__
115 __store4r( value, &value );
116 #elif defined LB_GCC_BSWAP_FUNCTION
117 value = bswap_32( value );
119 value = __builtin_bswap32( value );
123 template<>
inline void byteswap( int32_t& value )
124 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
126 template<>
inline void byteswap(
float& value )
127 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
129 template<>
inline void byteswap( uint16_t& value )
132 value = _byteswap_ushort( value );
133 #elif defined __xlC__
134 __store2r( value, &value );
136 value = (value>>8) | (value<<8);
140 template<>
inline void byteswap( int16_t& value )
141 {
byteswap( reinterpret_cast< uint16_t& >( value )); }
143 template<>
inline void byteswap( uint64_t& value )
146 value = _byteswap_uint64( value );
147 #elif defined __xlC__
148 value = __bswap_constant_64( value );
149 #elif defined LB_GCC_BSWAP_FUNCTION
150 value = bswap_64( value );
152 value = __builtin_bswap64( value );
156 template<>
inline void byteswap( int64_t& value )
157 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
159 template<>
inline void byteswap(
double& value )
160 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
162 template<>
inline void byteswap( uint128_t& value )
168 template<>
inline void byteswap( UUID& value )
169 { byteswap< uint128_t >( value ); }
171 template<
typename T >
172 inline void byteswap(
typename std::vector< T >& value )
174 for(
size_t i = 0; i < value.size(); ++i )
178 #endif //LUNCHBOX_BITOPERATION_H