20 #ifndef LUNCHBOX_BITOPERATION_H
21 #define LUNCHBOX_BITOPERATION_H
23 #include <lunchbox/compiler.h>
25 #include <lunchbox/uint128_t.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
55 template<
class T >
void byteswap( T& value );
60 template<>
inline int32_t getIndexOfLastBit< uint32_t >( uint32_t value )
63 return ::fls( value ) - 1;
64 #elif defined __GNUC__
65 return value ? (31 - __builtin_clz( value )) : -1;
66 #elif defined _MSC_VER
68 return _BitScanReverse( &i, value ) ? i : -1;
80 template<>
inline int32_t getIndexOfLastBit< uint64_t >( uint64_t value )
83 return value ? (63 - __builtin_clzll( value )) : -1;
86 return _BitScanReverse64( &i, value ) ? i : -1;
98 #if defined(__linux__) && defined(_LP64)
99 template<>
inline int32_t
100 getIndexOfLastBit< unsigned long long >(
unsigned long long value )
106 int32_t getIndexOfLastBit< unsigned long >(
unsigned long value )
110 int32_t getIndexOfLastBit< unsigned long >(
unsigned long value )
115 template<>
inline void byteswap(
void*& ) { }
116 template<>
inline void byteswap(
bool&) { }
117 template<>
inline void byteswap(
char& ) { }
118 template<>
inline void byteswap(
signed char& ) { }
119 template<>
inline void byteswap(
unsigned char& ) { }
121 template<>
inline void byteswap( uint32_t& value )
124 value = _byteswap_ulong( value );
125 #elif defined __xlC__
126 __store4r( value, &value );
127 #elif defined LB_GCC_BSWAP_FUNCTION
128 value = bswap_32( value );
130 value = __builtin_bswap32( value );
134 template<>
inline void byteswap( int32_t& value )
135 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
137 template<>
inline void byteswap(
float& value )
138 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
140 template<>
inline void byteswap( uint16_t& value )
143 value = _byteswap_ushort( value );
144 #elif defined __xlC__
145 __store2r( value, &value );
147 value = (value>>8) | (value<<8);
151 template<>
inline void byteswap( int16_t& value )
152 {
byteswap( reinterpret_cast< uint16_t& >( value )); }
155 template<>
inline void byteswap(
unsigned long& value )
156 {
byteswap( reinterpret_cast< unsigned long& >( value )); }
159 template<>
inline void byteswap( uint64_t& value )
162 value = _byteswap_uint64( value );
163 #elif defined __xlC__
164 value = __bswap_constant_64( value );
165 #elif defined LB_GCC_BSWAP_FUNCTION
166 value = bswap_64( value );
168 value = __builtin_bswap64( value );
172 template<>
inline void byteswap( int64_t& value )
173 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
175 template<>
inline void byteswap(
double& value )
176 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
178 template<>
inline void byteswap( uint128_t& value )
184 template<
typename T >
185 inline void byteswap(
typename std::vector< T >& value )
187 for(
size_t i = 0; i < value.size(); ++i )
193 #endif //LUNCHBOX_BITOPERATION_H
Basic type definitions not provided by the operating system.
void byteswap(T &value)
Swap the byte order of the given value.
int32_t getIndexOfLastBit(T value)