Optimize NumUtil_encode_bigend_* functions
Make GCC generate bswap instructions. For details see:
@@ -217,48 +217,40 @@ lucy_NumUtil_is_bigend() {
static CFISH_INLINE void
lucy_NumUtil_encode_bigend_u16(uint16_t value, void *dest_ptr) {
uint8_t *dest = *(uint8_t**)dest_ptr;
- if (lucy_NumUtil_is_bigend()) {
- memcpy(dest, &value, sizeof(uint16_t));
- }
- else {
- uint8_t *source = (uint8_t*)&value;
- dest[0] = source[1];
- dest[1] = source[0];
+ if (!lucy_NumUtil_is_bigend()) {
+ value = ((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8);
}
+ memcpy(dest, &value, sizeof(uint16_t));
}
static CFISH_INLINE void
lucy_NumUtil_encode_bigend_u32(uint32_t value, void *dest_ptr) {
uint8_t *dest = *(uint8_t**)dest_ptr;
- if (lucy_NumUtil_is_bigend()) {
- memcpy(dest, &value, sizeof(uint32_t));
- }
- else {
- uint8_t *source = (uint8_t*)&value;
- dest[0] = source[3];
- dest[1] = source[2];
- dest[2] = source[1];
- dest[3] = source[0];
+ if (!lucy_NumUtil_is_bigend()) {
+ value =
+ ((value & 0xFF000000) >> 24) |
+ ((value & 0x00FF0000) >> 8) |
+ ((value & 0x0000FF00) << 8) |
+ ((value & 0x000000FF) << 24);
}
+ memcpy(dest, &value, sizeof(uint32_t));
}
static CFISH_INLINE void
lucy_NumUtil_encode_bigend_u64(uint64_t value, void *dest_ptr) {
uint8_t *dest = *(uint8_t**)dest_ptr;
- if (lucy_NumUtil_is_bigend()) {
- memcpy(dest, &value, sizeof(uint64_t));
- }
- else {
- uint8_t *source = (uint8_t*)&value;
- dest[0] = source[7];
- dest[1] = source[6];
- dest[2] = source[5];
- dest[3] = source[4];
- dest[4] = source[3];
- dest[5] = source[2];
- dest[6] = source[1];
- dest[7] = source[0];
+ if (!lucy_NumUtil_is_bigend()) {
+ value =
+ ((value & 0xFF00000000000000) >> 56) |
+ ((value & 0x00FF000000000000) >> 40) |
+ ((value & 0x0000FF0000000000) >> 24) |
+ ((value & 0x000000FF00000000) >> 8) |
+ ((value & 0x00000000FF000000) << 8) |
+ ((value & 0x0000000000FF0000) << 24) |
+ ((value & 0x000000000000FF00) << 40) |
+ ((value & 0x00000000000000FF) << 56);
}
+ memcpy(dest, &value, sizeof(uint64_t));
}
static CFISH_INLINE uint16_t
