| 1 |
|
| /* |
| 2 |
|
| * xxh64.c - XXH64 implementation per the xxHash specification. |
| 3 |
|
| * |
| 4 |
|
| * Big-endian/little-endian agnostic via byte-by-byte loaders. The |
| 5 |
|
| * algorithm itself is little-endian for input lanes; we read each |
| 6 |
|
| * lane bytewise, never type-pun. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #include "xxh64.h" |
| 10 |
|
| #include <string.h> |
| 11 |
|
| |
| 12 |
|
| #define ROL64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) |
| 13 |
|
| |
| 14 |
|
| #define PRIME64_1 0x9E3779B185EBCA87ULL |
| 15 |
|
| #define PRIME64_2 0xC2B2AE3D27D4EB4FULL |
| 16 |
|
| #define PRIME64_3 0x165667B19E3779F9ULL |
| 17 |
|
| #define PRIME64_4 0x85EBCA77C2B2AE63ULL |
| 18 |
|
| #define PRIME64_5 0x27D4EB2F165667C5ULL |
| 19 |
|
| |
| 20 |
|
| static uint64_t |
| 21 |
20008 |
| load64_le(const unsigned char *p) |
| 22 |
|
| { |
| 23 |
20008 |
| return (uint64_t)p[0] |
| 24 |
20008 |
| | ((uint64_t)p[1] << 8) |
| 25 |
20008 |
| | ((uint64_t)p[2] << 16) |
| 26 |
20008 |
| | ((uint64_t)p[3] << 24) |
| 27 |
20008 |
| | ((uint64_t)p[4] << 32) |
| 28 |
20008 |
| | ((uint64_t)p[5] << 40) |
| 29 |
20008 |
| | ((uint64_t)p[6] << 48) |
| 30 |
20008 |
| | ((uint64_t)p[7] << 56); |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
|
| static uint32_t |
| 34 |
2 |
| load32_le(const unsigned char *p) |
| 35 |
|
| { |
| 36 |
2 |
| return (uint32_t)p[0] |
| 37 |
2 |
| | ((uint32_t)p[1] << 8) |
| 38 |
2 |
| | ((uint32_t)p[2] << 16) |
| 39 |
2 |
| | ((uint32_t)p[3] << 24); |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| static uint64_t |
| 43 |
20032 |
| xxh64_round(uint64_t acc, uint64_t input) |
| 44 |
|
| { |
| 45 |
20032 |
| acc += input * PRIME64_2; |
| 46 |
20032 |
| acc = ROL64(acc, 31); |
| 47 |
20032 |
| acc *= PRIME64_1; |
| 48 |
20032 |
| return acc; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| static uint64_t |
| 52 |
24 |
| xxh64_merge_round(uint64_t acc, uint64_t val) |
| 53 |
|
| { |
| 54 |
24 |
| val = xxh64_round(0, val); |
| 55 |
24 |
| acc ^= val; |
| 56 |
24 |
| acc = acc * PRIME64_1 + PRIME64_4; |
| 57 |
24 |
| return acc; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| void |
| 61 |
16 |
| xxh64_init(xxh64_ctx_t *ctx, uint64_t seed) |
| 62 |
|
| { |
| 63 |
16 |
| ctx->v[0] = seed + PRIME64_1 + PRIME64_2; |
| 64 |
16 |
| ctx->v[1] = seed + PRIME64_2; |
| 65 |
16 |
| ctx->v[2] = seed + 0; |
| 66 |
16 |
| ctx->v[3] = seed - PRIME64_1; |
| 67 |
16 |
| ctx->total_len = 0; |
| 68 |
16 |
| ctx->seed = seed; |
| 69 |
16 |
| ctx->large_path_seen = 0; |
| 70 |
16 |
| ctx->buffered = 0; |
| 71 |
16 |
| } |
| 72 |
|
| |
| 73 |
|
| static void |
| 74 |
5000 |
| xxh64_consume_stripe(xxh64_ctx_t *ctx, const unsigned char *p) |
| 75 |
|
| { |
| 76 |
5000 |
| ctx->v[0] = xxh64_round(ctx->v[0], load64_le(p )); |
| 77 |
5000 |
| ctx->v[1] = xxh64_round(ctx->v[1], load64_le(p + 8)); |
| 78 |
5000 |
| ctx->v[2] = xxh64_round(ctx->v[2], load64_le(p + 16)); |
| 79 |
5000 |
| ctx->v[3] = xxh64_round(ctx->v[3], load64_le(p + 24)); |
| 80 |
5000 |
| ctx->large_path_seen = 1; |
| 81 |
5000 |
| } |
| 82 |
|
| |
| 83 |
|
| void |
| 84 |
12 |
| xxh64_update(xxh64_ctx_t *ctx, const void *data, size_t len) |
| 85 |
|
| { |
| 86 |
12 |
| const unsigned char *p = (const unsigned char *)data; |
| 87 |
|
| size_t take; |
| 88 |
|
| |
| 89 |
12 |
| ctx->total_len += len; |
| 90 |
|
| |
| 91 |
|
| /* Drain the partial-stripe buffer if it can complete a stripe. */ |
| 92 |
12 |
▶ | if (ctx->buffered) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered) { |
0 |
12 |
12 |
|
| 93 |
0 |
| take = XXH64_BLOCK_SIZE - ctx->buffered; |
| 94 |
0 |
▶ | if (take > len) take = len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (take > len) take = len; |
0 |
0 |
0 |
|
| 95 |
0 |
| memcpy(ctx->buffer + ctx->buffered, p, take); |
| 96 |
0 |
| ctx->buffered += take; |
| 97 |
0 |
| p += take; |
| 98 |
0 |
| len -= take; |
| 99 |
0 |
▶ | if (ctx->buffered == XXH64_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered == XXH64_BLOCK_SIZE) { |
0 |
0 |
0 |
|
| 100 |
0 |
| xxh64_consume_stripe(ctx, ctx->buffer); |
| 101 |
0 |
| ctx->buffered = 0; |
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
5012 |
▶ | while (len >= XXH64_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len >= XXH64_BLOCK_SIZE) { |
5000 |
12 |
5012 |
|
| 106 |
5000 |
| xxh64_consume_stripe(ctx, p); |
| 107 |
5000 |
| p += XXH64_BLOCK_SIZE; |
| 108 |
5000 |
| len -= XXH64_BLOCK_SIZE; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
12 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
11 |
1 |
12 |
|
| 112 |
11 |
| memcpy(ctx->buffer, p, len); |
| 113 |
11 |
| ctx->buffered = len; |
| 114 |
|
| } |
| 115 |
12 |
| } |
| 116 |
|
| |
| 117 |
|
| void |
| 118 |
16 |
| xxh64_final(xxh64_ctx_t *ctx, unsigned char out[XXH64_DIGEST_SIZE]) |
| 119 |
|
| { |
| 120 |
|
| uint64_t h; |
| 121 |
16 |
| const unsigned char *p = ctx->buffer; |
| 122 |
16 |
| size_t remaining = ctx->buffered; |
| 123 |
|
| int i; |
| 124 |
|
| |
| 125 |
16 |
▶ | if (ctx->large_path_seen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->large_path_seen) { |
6 |
10 |
16 |
|
| 126 |
6 |
| h = ROL64(ctx->v[0], 1) + ROL64(ctx->v[1], 7) |
| 127 |
6 |
| + ROL64(ctx->v[2], 12) + ROL64(ctx->v[3], 18); |
| 128 |
6 |
| h = xxh64_merge_round(h, ctx->v[0]); |
| 129 |
6 |
| h = xxh64_merge_round(h, ctx->v[1]); |
| 130 |
6 |
| h = xxh64_merge_round(h, ctx->v[2]); |
| 131 |
6 |
| h = xxh64_merge_round(h, ctx->v[3]); |
| 132 |
|
| } else { |
| 133 |
10 |
| h = ctx->seed + PRIME64_5; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
16 |
| h += ctx->total_len; |
| 137 |
|
| |
| 138 |
|
| /* Process remaining bytes: 8-byte, then 4-byte, then 1-byte |
| 139 |
|
| * chunks per the spec's mix table. */ |
| 140 |
24 |
▶ | while (remaining >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (remaining >= 8) { |
8 |
16 |
24 |
|
| 141 |
8 |
| uint64_t k = load64_le(p); |
| 142 |
8 |
| k = xxh64_round(0, k); |
| 143 |
8 |
| h ^= k; |
| 144 |
8 |
| h = ROL64(h, 27) * PRIME64_1 + PRIME64_4; |
| 145 |
8 |
| p += 8; |
| 146 |
8 |
| remaining -= 8; |
| 147 |
|
| } |
| 148 |
16 |
▶ | if (remaining >= 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining >= 4) { |
2 |
14 |
16 |
|
| 149 |
2 |
| uint64_t k = (uint64_t)load32_le(p); |
| 150 |
2 |
| h ^= k * PRIME64_1; |
| 151 |
2 |
| h = ROL64(h, 23) * PRIME64_2 + PRIME64_3; |
| 152 |
2 |
| p += 4; |
| 153 |
2 |
| remaining -= 4; |
| 154 |
|
| } |
| 155 |
39 |
▶ | while (remaining > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (remaining > 0) { |
23 |
16 |
39 |
|
| 156 |
23 |
| h ^= (uint64_t)(*p) * PRIME64_5; |
| 157 |
23 |
| h = ROL64(h, 11) * PRIME64_1; |
| 158 |
23 |
| p++; |
| 159 |
23 |
| remaining--; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* Avalanche. */ |
| 163 |
16 |
| h ^= h >> 33; |
| 164 |
16 |
| h *= PRIME64_2; |
| 165 |
16 |
| h ^= h >> 29; |
| 166 |
16 |
| h *= PRIME64_3; |
| 167 |
16 |
| h ^= h >> 32; |
| 168 |
|
| |
| 169 |
|
| /* Big-endian output: matches conventional hex display |
| 170 |
|
| * (xxhsum -H64). */ |
| 171 |
144 |
▶ | for (i = 0; i < 8; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) { |
128 |
16 |
144 |
|
| 172 |
128 |
| out[i] = (unsigned char)((h >> (56 - i * 8)) & 0xFFu); |
| 173 |
|
| } |
| 174 |
16 |
| } |