Minor changes for more efficient endian switching
authorCampbell Barton <ideasman42@gmail.com>
Sat, 11 Jul 2015 19:33:04 +0000 (05:33 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 11 Jul 2015 19:33:04 +0000 (05:33 +1000)
source/blender/blenlib/BLI_endian_switch_inline.h
source/blender/blenlib/intern/endian_switch.c

index 358206f1177e01ad573e58955056e4ba8ee61563..f5f0e59cfa8227620865b8fb43d16ec6a008a271 100644 (file)
@@ -42,9 +42,13 @@ BLI_INLINE void BLI_endian_switch_int16(short *val)
 }
 BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
 {
+#ifdef __GNUC__
+       *val = __builtin_bswap16(*val);
+#else
        unsigned short tval = *val;
        *val = (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)
 {
+#ifdef __GNUC__
+       *val = __builtin_bswap32(*val);
+#else
        unsigned int tval = *val;
        *val = ((tval >> 24))             |
               ((tval << 8) & 0x00ff0000) |
               ((tval >> 8) & 0x0000ff00) |
               ((tval << 24));
+#endif
 }
 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)
 {
+#ifdef __GNUC__
+       *val = __builtin_bswap16(*val);
+#else
        uint64_t tval = *val;
        *val = ((tval >> 56)) |
               ((tval << 40) & 0x00ff000000000000ll) |
@@ -83,6 +94,7 @@ BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
               ((tval >> 24) & 0x0000000000ff0000ll) |
               ((tval >> 40) & 0x000000000000ff00ll) |
               ((tval << 56));
+#endif
 }
 BLI_INLINE void BLI_endian_switch_double(double *val)
 {
index 892d739601193ef3e7e8dcfd22dd897a7e408537..b1dcdffba01430190a61b076fa737e7188df7e7a 100644 (file)
@@ -32,9 +32,8 @@ void BLI_endian_switch_int16_array(short *val, const int size)
 {
        if (size > 0) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                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) {
                int i = size;
-               val = val + (size - 1);
                while (i--) {
-                       BLI_endian_switch_double(val--);
+                       BLI_endian_switch_double(val++);
                }
        }
 }