Add explicit cu32 and cu64 to NumUtil. Duplicate the functionality of xxxx_c64 in xxxx_cu64, in preparation for the addition of xxxx_ci64 (for signed integers) and the eventual removal of xxxx_64.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/3e3c6489 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/3e3c6489 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/3e3c6489 Branch: refs/heads/master Commit: 3e3c64895bc92b19fadebe2a7b3188e8a218597d Parents: 8a48ef5 Author: Marvin Humphrey <[email protected]> Authored: Mon Apr 18 15:07:21 2016 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Mon Apr 18 15:12:10 2016 -0700 ---------------------------------------------------------------------- core/Lucy/Test/Store/TestIOPrimitives.c | 2 +- core/Lucy/Test/Util/TestNumberUtils.c | 42 +++++++-------- core/Lucy/Util/NumberUtils.cfh | 81 ++++++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/3e3c6489/core/Lucy/Test/Store/TestIOPrimitives.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Test/Store/TestIOPrimitives.c b/core/Lucy/Test/Store/TestIOPrimitives.c index 7409d7e..3c09dde 100644 --- a/core/Lucy/Test/Store/TestIOPrimitives.c +++ b/core/Lucy/Test/Store/TestIOPrimitives.c @@ -321,7 +321,7 @@ test_cu64(TestBatchRunner *runner) { char buffer[10]; const char *buf = buffer; size_t size = InStream_Read_Raw_C64(raw_instream, buffer); - uint64_t got = NumUtil_decode_c64(&buf); + uint64_t got = NumUtil_decode_cu64(&buf); UNUSED_VAR(size); if (got != ints[i]) { FAIL(runner, "Read_Raw_C64 failed: %" PRIu64 ", %" PRIu64, http://git-wip-us.apache.org/repos/asf/lucy/blob/3e3c6489/core/Lucy/Test/Util/TestNumberUtils.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Test/Util/TestNumberUtils.c b/core/Lucy/Test/Util/TestNumberUtils.c index 70a63a6..ffd3079 100644 --- a/core/Lucy/Test/Util/TestNumberUtils.c +++ b/core/Lucy/Test/Util/TestNumberUtils.c @@ -94,14 +94,14 @@ test_u4(TestBatchRunner *runner) { } static void -test_c32(TestBatchRunner *runner) { +test_cu32(TestBatchRunner *runner) { uint64_t mins[] = { 0, 0x4000 - 100, (uint32_t)INT32_MAX - 100, UINT32_MAX - 10 }; uint64_t limits[] = { 500, 0x4000 + 100, (uint32_t)INT32_MAX + 100, UINT32_MAX }; uint32_t set_num; uint32_t num_sets = sizeof(mins) / sizeof(uint64_t); size_t count = 64; uint64_t *ints = NULL; - size_t amount = count * C32_MAX_BYTES; + size_t amount = count * CU32_MAX_BYTES; char *encoded = (char*)CALLOCATE(amount, sizeof(char)); char *target = encoded; char *limit = target + amount; @@ -114,13 +114,13 @@ test_c32(TestBatchRunner *runner) { target = encoded; for (size_t i = 0; i < count; i++) { ints[i] = (uint32_t)ints[i]; - NumUtil_encode_c32((uint32_t)ints[i], &target); + NumUtil_encode_cu32((uint32_t)ints[i], &target); } decode = encoded; skip = encoded; for (size_t i = 0; i < count; i++) { - TEST_INT_EQ(runner, NumUtil_decode_c32(&decode), ints[i], - "c32 %lu", (long)ints[i]); + TEST_INT_EQ(runner, NumUtil_decode_cu32(&decode), ints[i], + "cu32 %lu", (long)ints[i]); NumUtil_skip_cint(&skip); if (decode > limit) { THROW(ERR, "overrun"); } } @@ -129,16 +129,16 @@ test_c32(TestBatchRunner *runner) { target = encoded; for (size_t i = 0; i < count; i++) { - NumUtil_encode_padded_c32((uint32_t)ints[i], &target); + NumUtil_encode_padded_cu32((uint32_t)ints[i], &target); } TEST_TRUE(runner, target == limit, - "padded c32 uses 5 bytes (%lu == %lu)", (unsigned long)target, + "padded cu32 uses 5 bytes (%lu == %lu)", (unsigned long)target, (unsigned long)limit); decode = encoded; skip = encoded; for (size_t i = 0; i < count; i++) { - TEST_INT_EQ(runner, NumUtil_decode_c32(&decode), ints[i], - "padded c32 %lu", (long)ints[i]); + TEST_INT_EQ(runner, NumUtil_decode_cu32(&decode), ints[i], + "padded cu32 %lu", (long)ints[i]); NumUtil_skip_cint(&skip); if (decode > limit) { THROW(ERR, "overrun"); } } @@ -147,23 +147,23 @@ test_c32(TestBatchRunner *runner) { } target = encoded; - NumUtil_encode_c32(UINT32_MAX, &target); + NumUtil_encode_cu32(UINT32_MAX, &target); decode = encoded; - TEST_INT_EQ(runner, NumUtil_decode_c32(&decode), UINT32_MAX, "c32 UINT32_MAX"); + TEST_INT_EQ(runner, NumUtil_decode_cu32(&decode), UINT32_MAX, "cu32 UINT32_MAX"); FREEMEM(encoded); FREEMEM(ints); } static void -test_c64(TestBatchRunner *runner) { +test_cu64(TestBatchRunner *runner) { uint64_t mins[] = { 0, 0x4000 - 100, (uint64_t)UINT32_MAX - 100, UINT64_MAX - 10 }; uint64_t limits[] = { 500, 0x4000 + 100, (uint64_t)UINT32_MAX + 1000, UINT64_MAX }; uint32_t set_num; uint32_t num_sets = sizeof(mins) / sizeof(uint64_t); size_t count = 64; uint64_t *ints = NULL; - size_t amount = count * C64_MAX_BYTES; + size_t amount = count * CU64_MAX_BYTES; char *encoded = (char*)CALLOCATE(amount, sizeof(char)); char *target = encoded; char *limit = target + amount; @@ -175,14 +175,14 @@ test_c64(TestBatchRunner *runner) { mins[set_num], limits[set_num]); target = encoded; for (size_t i = 0; i < count; i++) { - NumUtil_encode_c64(ints[i], &target); + NumUtil_encode_cu64(ints[i], &target); } decode = encoded; skip = encoded; for (size_t i = 0; i < count; i++) { - uint64_t got = NumUtil_decode_c64(&decode); + uint64_t got = NumUtil_decode_cu64(&decode); TEST_TRUE(runner, got == ints[i], - "c64 %" PRIu64 " == %" PRIu64, got, ints[i]); + "cu64 %" PRIu64 " == %" PRIu64, got, ints[i]); if (decode > limit) { THROW(ERR, "overrun"); } NumUtil_skip_cint(&skip); } @@ -191,11 +191,11 @@ test_c64(TestBatchRunner *runner) { } target = encoded; - NumUtil_encode_c64(UINT64_MAX, &target); + NumUtil_encode_cu64(UINT64_MAX, &target); decode = encoded; - uint64_t got = NumUtil_decode_c64(&decode); - TEST_TRUE(runner, got == UINT64_MAX, "c64 UINT64_MAX"); + uint64_t got = NumUtil_decode_cu64(&decode); + TEST_TRUE(runner, got == UINT64_MAX, "cu64 UINT64_MAX"); FREEMEM(encoded); FREEMEM(ints); @@ -364,8 +364,8 @@ TestNumUtil_Run_IMP(TestNumberUtils *self, TestBatchRunner *runner) { test_u1(runner); test_u2(runner); test_u4(runner); - test_c32(runner); - test_c64(runner); + test_cu32(runner); + test_cu64(runner); test_bigend_u16(runner); test_bigend_u32(runner); test_bigend_u64(runner); http://git-wip-us.apache.org/repos/asf/lucy/blob/3e3c6489/core/Lucy/Util/NumberUtils.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/NumberUtils.cfh b/core/Lucy/Util/NumberUtils.cfh index b7c08b4..3d9c79d 100644 --- a/core/Lucy/Util/NumberUtils.cfh +++ b/core/Lucy/Util/NumberUtils.cfh @@ -87,6 +87,13 @@ inert class Lucy::Util::NumberUtils nickname NumUtil { inert inline void encode_c32(uint32_t value, char **dest); + /** Encode a compressed 32-bit unsigned integer at the space pointed to by + * `dest`. As a side effect, `dest` will be advanced to immediately after + * the end of the compressed data. + */ + inert inline void + encode_cu32(uint32_t value, char **dest); + /** Encode a C32 at the space pointed to by `dest`, but add * "leading zeroes" so that the space consumed will always be 5 bytes. As * a side effect, `dest` will be advanced to immediately after @@ -95,6 +102,14 @@ inert class Lucy::Util::NumberUtils nickname NumUtil { inert inline void encode_padded_c32(uint32_t value, char **dest); + /** Encode a compressed 32-bit unsigned integer at the space pointed to by + * `dest`, but add "leading zeroes" so that the space consumed will always + * be 5 bytes. As a side effect, `dest` will be advanced to immediately + * after the end of the compressed data. + */ + inert inline void + encode_padded_cu32(uint32_t value, char **dest); + /** Encode a C64 at the space pointed to by `dest`. As a side * effect, `dest` will be advanced to immediately after the end * of the C64. @@ -102,6 +117,13 @@ inert class Lucy::Util::NumberUtils nickname NumUtil { inert inline void encode_c64(uint64_t value, char **dest); + /** Encode a compressed 64-bit unsigned integer at the space pointed to by + * `dest`. As a side effect, `dest` will be advanced to immediately after + * the end of the compressed data. + */ + inert inline void + encode_cu64(uint64_t value, char **dest); + /** Read a C32 from the buffer pointed to by `source`. As a * side effect, advance the pointer, consuming the bytes occupied by the * C32. @@ -109,6 +131,13 @@ inert class Lucy::Util::NumberUtils nickname NumUtil { inert inline uint32_t decode_c32(const char **source); + /** Read a compressed 32-bit unsigned integer from the buffer pointed to + * by `source`. As a side effect, advance the pointer, consuming the + * bytes occupied by the compressed number. + */ + inert inline uint32_t + decode_cu32(const char **source); + /** Read a C64 from the buffer pointed to by `source`. As a * side effect, advance the pointer, consuming the bytes occupied by the * C64. @@ -116,6 +145,13 @@ inert class Lucy::Util::NumberUtils nickname NumUtil { inert inline uint64_t decode_c64(const char **source); + /** Read a compressed 64-bit unsigned integer from the buffer pointed to + * by `source`. As a side effect, advance the pointer, consuming the + * bytes occupied by the compressed number. + */ + inert inline uint64_t + decode_cu64(const char **source); + /** Advance `source` past one encoded C32 or C64. */ inert inline void @@ -296,10 +332,17 @@ lucy_NumUtil_decode_bigend_f64(const void *source) { #define LUCY_NUMUTIL_C32_MAX_BYTES ((sizeof(uint32_t) * 8 / 7) + 1) /* 5 */ #define LUCY_NUMUTIL_C64_MAX_BYTES ((sizeof(uint64_t) * 8 / 7) + 1) /* 10 */ +#define LUCY_NUMUTIL_CU32_MAX_BYTES ((sizeof(uint32_t) * 8 / 7) + 1) /* 5 */ +#define LUCY_NUMUTIL_CU64_MAX_BYTES ((sizeof(uint64_t) * 8 / 7) + 1) /* 10 */ static CFISH_INLINE void lucy_NumUtil_encode_c32(uint32_t value, char **out_buf) { - uint8_t buf[LUCY_NUMUTIL_C32_MAX_BYTES]; + lucy_NumUtil_encode_cu32(value, out_buf); +} + +static CFISH_INLINE void +lucy_NumUtil_encode_cu32(uint32_t value, char **out_buf) { + uint8_t buf[LUCY_NUMUTIL_CU32_MAX_BYTES]; uint8_t *const limit = buf + sizeof(buf); uint8_t *ptr = limit - 1; int num_bytes; @@ -318,7 +361,12 @@ lucy_NumUtil_encode_c32(uint32_t value, char **out_buf) { static CFISH_INLINE void lucy_NumUtil_encode_c64(uint64_t value, char **out_buf) { - uint8_t buf[LUCY_NUMUTIL_C64_MAX_BYTES]; + lucy_NumUtil_encode_cu64(value, out_buf); +} + +static CFISH_INLINE void +lucy_NumUtil_encode_cu64(uint64_t value, char **out_buf) { + uint8_t buf[LUCY_NUMUTIL_CU64_MAX_BYTES]; uint8_t *const limit = buf + sizeof(buf); uint8_t *ptr = limit - 1; int num_bytes; @@ -337,7 +385,12 @@ lucy_NumUtil_encode_c64(uint64_t value, char **out_buf) { static CFISH_INLINE void lucy_NumUtil_encode_padded_c32(uint32_t value, char **out_buf) { - uint8_t buf[LUCY_NUMUTIL_C32_MAX_BYTES] + lucy_NumUtil_encode_padded_cu32(value, out_buf); +} + +static CFISH_INLINE void +lucy_NumUtil_encode_padded_cu32(uint32_t value, char **out_buf) { + uint8_t buf[LUCY_NUMUTIL_CU32_MAX_BYTES] = { 0x80, 0x80, 0x80, 0x80, 0x80 }; uint8_t *const limit = buf + sizeof(buf); uint8_t *ptr = limit - 1; @@ -349,7 +402,7 @@ lucy_NumUtil_encode_padded_c32(uint32_t value, char **out_buf) { *--ptr = ((value & 0x7f) | 0x80); value >>= 7; } - memcpy(*out_buf, buf, LUCY_NUMUTIL_C32_MAX_BYTES); + memcpy(*out_buf, buf, LUCY_NUMUTIL_CU32_MAX_BYTES); *out_buf += sizeof(buf); } @@ -371,6 +424,15 @@ lucy_NumUtil_decode_c32(const char **source_ptr) { return decoded; } +static CFISH_INLINE uint32_t +lucy_NumUtil_decode_cu32(const char **source_ptr) { + const char *source = *source_ptr; + uint32_t decoded; + LUCY_NUMUTIL_DECODE_CINT(decoded, source); + *source_ptr = source; + return decoded; +} + static CFISH_INLINE uint64_t lucy_NumUtil_decode_c64(const char **source_ptr) { const char *source = *source_ptr; @@ -380,6 +442,15 @@ lucy_NumUtil_decode_c64(const char **source_ptr) { return decoded; } +static CFISH_INLINE uint64_t +lucy_NumUtil_decode_cu64(const char **source_ptr) { + const char *source = *source_ptr; + uint64_t decoded; + LUCY_NUMUTIL_DECODE_CINT(decoded, source); + *source_ptr = source; + return decoded; +} + static CFISH_INLINE void lucy_NumUtil_skip_cint(const char **source_ptr) { const uint8_t *ptr = *(const uint8_t**)source_ptr; @@ -459,6 +530,8 @@ lucy_NumUtil_u4set(void *array, uint32_t tick, uint8_t value) { #ifdef LUCY_USE_SHORT_NAMES #define C32_MAX_BYTES LUCY_NUMUTIL_C32_MAX_BYTES #define C64_MAX_BYTES LUCY_NUMUTIL_C64_MAX_BYTES + #define CU32_MAX_BYTES LUCY_NUMUTIL_CU32_MAX_BYTES + #define CU64_MAX_BYTES LUCY_NUMUTIL_CU64_MAX_BYTES #endif __END_C__
