20 #ifndef LUNCHBOX_BITOPERATION_H
21 #define LUNCHBOX_BITOPERATION_H
23 #include <lunchbox/compiler.h>
25 #include <servus/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& ) { }
120 template<>
inline void byteswap( std::string& ) { }
122 template<>
inline void byteswap( uint32_t& value )
125 value = _byteswap_ulong( value );
126 #elif defined __xlC__
127 __store4r( value, &value );
128 #elif defined LB_GCC_BSWAP_FUNCTION
129 value = bswap_32( value );
131 value = __builtin_bswap32( value );
135 template<>
inline void byteswap( int32_t& value )
136 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
138 template<>
inline void byteswap(
float& value )
139 {
byteswap( reinterpret_cast< uint32_t& >( value )); }
141 template<>
inline void byteswap( uint16_t& value )
144 value = _byteswap_ushort( value );
145 #elif defined __xlC__
146 __store2r( value, &value );
148 value = (uint16_t)(value>>8) | (uint16_t)(value<<8);
152 template<>
inline void byteswap( int16_t& value )
153 {
byteswap( reinterpret_cast< uint16_t& >( value )); }
156 template<>
inline void byteswap(
unsigned long& value )
157 {
byteswap( reinterpret_cast< unsigned long& >( value )); }
160 template<>
inline void byteswap( uint64_t& value )
163 value = _byteswap_uint64( value );
164 #elif defined __xlC__
165 value = __bswap_constant_64( value );
166 #elif defined LB_GCC_BSWAP_FUNCTION
167 value = bswap_64( value );
169 value = __builtin_bswap64( value );
173 template<>
inline void byteswap( int64_t& value )
174 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
176 template<>
inline void byteswap(
double& value )
177 {
byteswap( reinterpret_cast< uint64_t& >( value )); }
179 template<>
inline void byteswap( servus::uint128_t& value )
185 template<
typename T >
186 inline void byteswap(
typename std::vector< T >& value )
188 for(
size_t i = 0; i < value.size(); ++i )
194 #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)
Abstraction layer and common utilities for multi-threaded programming.