From 9f80d0d60e1272717fbb90289b37646de5346111 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 20 Jun 2009 22:45:24 -0700 Subject: Do unaligned reads if UPB_UNALIGNED_READS_OK is defined. Also removed commented-out alternate implementations for varint-reading code (they are still in the history). --- upb_parse.c | 99 ++++++++++--------------------------------------------------- 1 file changed, 15 insertions(+), 84 deletions(-) (limited to 'upb_parse.c') diff --git a/upb_parse.c b/upb_parse.c index 33ccb42..b13ca24 100644 --- a/upb_parse.c +++ b/upb_parse.c @@ -54,80 +54,6 @@ done: return UPB_STATUS_OK; } -/* Alternate implementations of get_v_uint64_t -- one performs no branching, - * another performs only a single branch. TODO: test performance. */ - -#if 0 -/* The no-branching version. */ -static upb_status_t get_v_uint64_t(void *restrict *buf, - uint64_t *restrict val) -{ - uint8_t *b = *buf; - /* Endian-specific! */ - uint64_t b0 = *(uint64_t*)b; - uint16_t b10 = *(uint16_t*)(b+8); - - /* Put the 10 continuation bits in the bottom 10 bits of cont. */ - uint32_t cont = (b0 & 0x8080808080808080ULL) * 0x2040810204081ULL >> 56; - cont |= (*(b+8) & 0x80) << 1 | (*(b+9) & 0x80) << 2; - if(cont == 0x3FF) return UPB_ERROR_UNTERMINATED_VARINT; - - int num_bytes = __builtin_ctzll(~cont) + 1; - *buf += num_bytes; - - uint64_t more_than_8_mask = -(num_bytes > 8); - uint64_t low10_mask = (0x7F7F7F7F7F7F7F7FULL >> (8-num_bytes)*8); - low10_mask ^= (low10_mask ^ 0x7F7F7F7F7F7F7F7FULL) & more_than_8_mask; - uint64_t valb0 = b0 & low10_mask; - uint16_t valb10 = b10 & (0x7F7FU >> (10-num_bytes)*8) & more_than_8_mask; - - *val = ((valb0 & 0xFF00000000000000) >> 7) | - ((valb0 & 0x00FF000000000000) >> 6) | - ((valb0 & 0x0000FF0000000000) >> 5) | - ((valb0 & 0x000000FF00000000) >> 4) | - ((valb0 & 0x00000000FF000000) >> 3) | - ((valb0 & 0x0000000000FF0000) >> 2) | - ((valb0 & 0x000000000000FF00) >> 1) | - ((valb0 & 0x00000000000000FF)) | - (((uint64_t)valb10 & 0xFF00) << 55) | - (((uint64_t)valb10 & 0xFF) << 56); - return UPB_STATUS_OK; -} - -/* The single-branch version. */ -static upb_status_t get_v_uint64_t(void *restrict *buf, - uint64_t *restrict val) -{ - uint8_t *b = *buf; - /* Endian-specific! */ - uint64_t b0 = *(uint64_t*)b; - - /* Put the 10 continuation bits in the bottom 10 bits of cont. */ - uint32_t cont = (b0 & 0x8080808080808080ULL) * 0x2040810204081ULL >> 56; - cont |= (*(b+8) & 0x80) << 1 | (*(b+9) & 0x80) << 2; - - int num_bytes = __builtin_ffs(~cont); - uint32_t part0 = 0, part1 = 0, part2 = 0; - - switch(num_bytes) { - default: return UPB_ERROR_UNTERMINATED_VARINT; - case 10: part2 |= (b[9] & 0x7F) << 7; - case 9: part2 |= (b[8] & 0x7F) ; - case 8: part1 |= (b[7] & 0x7F) << 21; - case 7: part1 |= (b[6] & 0x7F) << 14; - case 6: part1 |= (b[5] & 0x7F) << 7; - case 5: part1 |= (b[4] & 0x7F) ; - case 4: part0 |= (b[3] & 0x7F) << 21; - case 3: part0 |= (b[2] & 0x7F) << 14; - case 2: part0 |= (b[1] & 0x7F) << 7; - case 1: part0 |= (b[0] & 0x7F) ; - } - *buf = b + num_bytes; - *val = (uint64_t)part0 | ((uint64_t)part1 << 28) | ((uint64_t)part2 << 56); - return UPB_STATUS_OK; -} -#endif - static upb_status_t skip_v_uint64_t(void **buf) { uint8_t *ptr = *buf, b; @@ -168,17 +94,21 @@ done: return UPB_STATUS_OK; } +#define SHL(val, bits) ((uint32_t)val << bits) static upb_status_t get_f_uint32_t(void *restrict *buf, uint32_t *restrict val) { uint8_t *b = *buf; -#define SHL(val, bits) ((uint32_t)val << bits) +#if UPB_UNALIGNED_READS_OK + *val = *(uint32_t*)b; +#else *val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24); -#undef SHL +#endif b += sizeof(uint32_t); *buf = b; return UPB_STATUS_OK; } +#undef SHL static upb_status_t skip_f_uint32_t(void **buf) { @@ -189,14 +119,15 @@ static upb_status_t skip_f_uint32_t(void **buf) static upb_status_t get_f_uint64_t(void *restrict *buf, uint64_t *restrict val) { - uint8_t *b = *buf; - /* TODO: is this worth 32/64 specializing? */ -#define SHL(val, bits) ((uint64_t)val << bits) - *val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24) | - SHL(b[4], 32) | SHL(b[5], 40) | SHL(b[6], 48) | SHL(b[7], 56); -#undef SHL - b += sizeof(uint64_t); - *buf = b; +#if UPB_UNALIGNED_READS_OK + *val = *(uint64_t*)*buf; + *buf = (char*)*buf + sizeof(uint64_t); +#else + uint32_t lo32, hi32; + get_f_uint32_t(buf, &lo32); + get_f_uint32_t(buf, &hi32); + *val = lo32 | ((uint64_t)hi32 << 32); +#endif return UPB_STATUS_OK; } -- cgit v1.2.3