Minor changes for more efficient endian switching

This commit is contained in:
Campbell Barton 2015-07-12 05:33:04 +10:00
parent 17ebbc06e2
commit 968351d916
2 changed files with 20 additions and 16 deletions

@ -42,9 +42,13 @@ BLI_INLINE void BLI_endian_switch_int16(short *val)
} }
BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val) BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
{ {
#ifdef __GNUC__
*val = __builtin_bswap16(*val);
#else
unsigned short tval = *val; unsigned short tval = *val;
*val = (tval >> 8) | *val = (tval >> 8) |
(tval << 8); (tval << 8);
#endif
} }
@ -55,11 +59,15 @@ BLI_INLINE void BLI_endian_switch_int32(int *val)
} }
BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val) BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
{ {
#ifdef __GNUC__
*val = __builtin_bswap32(*val);
#else
unsigned int tval = *val; unsigned int tval = *val;
*val = ((tval >> 24)) | *val = ((tval >> 24)) |
((tval << 8) & 0x00ff0000) | ((tval << 8) & 0x00ff0000) |
((tval >> 8) & 0x0000ff00) | ((tval >> 8) & 0x0000ff00) |
((tval << 24)); ((tval << 24));
#endif
} }
BLI_INLINE void BLI_endian_switch_float(float *val) BLI_INLINE void BLI_endian_switch_float(float *val)
{ {
@ -74,6 +82,9 @@ BLI_INLINE void BLI_endian_switch_int64(int64_t *val)
} }
BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val) BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
{ {
#ifdef __GNUC__
*val = __builtin_bswap16(*val);
#else
uint64_t tval = *val; uint64_t tval = *val;
*val = ((tval >> 56)) | *val = ((tval >> 56)) |
((tval << 40) & 0x00ff000000000000ll) | ((tval << 40) & 0x00ff000000000000ll) |
@ -83,6 +94,7 @@ BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
((tval >> 24) & 0x0000000000ff0000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
((tval >> 40) & 0x000000000000ff00ll) | ((tval >> 40) & 0x000000000000ff00ll) |
((tval << 56)); ((tval << 56));
#endif
} }
BLI_INLINE void BLI_endian_switch_double(double *val) BLI_INLINE void BLI_endian_switch_double(double *val)
{ {

@ -32,9 +32,8 @@ void BLI_endian_switch_int16_array(short *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_int16(val--); BLI_endian_switch_int16(val++);
} }
} }
} }
@ -43,9 +42,8 @@ void BLI_endian_switch_uint16_array(unsigned short *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_uint16(val--); BLI_endian_switch_uint16(val++);
} }
} }
} }
@ -54,9 +52,8 @@ void BLI_endian_switch_int32_array(int *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_int32(val--); BLI_endian_switch_int32(val++);
} }
} }
} }
@ -65,9 +62,8 @@ void BLI_endian_switch_uint32_array(unsigned int *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_uint32(val--); BLI_endian_switch_uint32(val++);
} }
} }
} }
@ -76,9 +72,8 @@ void BLI_endian_switch_float_array(float *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_float(val--); BLI_endian_switch_float(val++);
} }
} }
} }
@ -87,9 +82,8 @@ void BLI_endian_switch_int64_array(int64_t *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_int64(val--); BLI_endian_switch_int64(val++);
} }
} }
} }
@ -98,9 +92,8 @@ void BLI_endian_switch_uint64_array(uint64_t *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_uint64(val--); BLI_endian_switch_uint64(val++);
} }
} }
} }
@ -110,9 +103,8 @@ void BLI_endian_switch_double_array(double *val, const int size)
{ {
if (size > 0) { if (size > 0) {
int i = size; int i = size;
val = val + (size - 1);
while (i--) { while (i--) {
BLI_endian_switch_double(val--); BLI_endian_switch_double(val++);
} }
} }
} }