F
forums_mp
Consider:
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
// assert macro
union {
T value;
char bytes[ size ];
} input, output;
input.value = value;
for ( unsigned int idx = 0; idx < size / 2; ++idx )
{
output.bytes[ idx ] = input.bytes[size - 1 - idx ];
output.bytes[size - 1 - idx ] = input.bytes[ idx ];
}
return output.value;
}
Interested in perhaps a more efficient means to achieve the same
objective? Benchmarks over a million interations shows the funciton
above as approximately ~55% of my total number. std::swap if memory
serves is linear time so .. i'm wondering (laptop got fried last night
so I'm unable to check) if a standard swap approach would eb more
prudent
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
// assert macro
union {
T value;
char bytes[ size ];
} input, output;
input.value = value;
for ( unsigned int idx = 0; idx < size / 2; ++idx )
{
output.bytes[ idx ] = input.bytes[size - 1 - idx ];
output.bytes[size - 1 - idx ] = input.bytes[ idx ];
}
return output.value;
}
Interested in perhaps a more efficient means to achieve the same
objective? Benchmarks over a million interations shows the funciton
above as approximately ~55% of my total number. std::swap if memory
serves is linear time so .. i'm wondering (laptop got fried last night
so I'm unable to check) if a standard swap approach would eb more
prudent