33
44#include "types.h"
55
6- #define ROUNDUP_8 (x ) ((x + 7) & ~7)
7- static inline __attribute__((__always_inline__ ))
8- bool hash_custom_labels (CustomLabelsArray * lbls , int seed , u64 * out ) {
9- // apply murmurhash2 as though this is an array of
10- // the number of labels (8 bytes), followed by all the key/val lengths,
11- // followed by all the keys/vals.
12- const u64 m = 0xc6a4a7935bd1e995LLU ;
13- const int r = 47 ;
14-
15- int len = 8 ;
16- for (int i = 0 ; i < MAX_CUSTOM_LABELS ; ++ i ) {
17- if (i >= lbls -> len )
18- break ;
19- len += 8 ;
20- len += ROUNDUP_8 (lbls -> labels [i ].key_len );
21- len += ROUNDUP_8 (lbls -> labels [i ].val_len );
22- }
23-
24- u64 h = seed ^ (len * m );
25-
26- // hash the number of labels
27- {
28- u64 k = lbls -> len ;
29- k *= m ;
30- k ^= k >> r ;
31- k *= m ;
32-
33- h ^= k ;
34- h *= m ;
35- }
36-
37- // hash each k/v len
38- for (int i = 0 ; i < MAX_CUSTOM_LABELS ; ++ i ) {
39- // force clang not to unroll the loop by hiding the value of i.
40- // Unrolling this loop confuses the verifier.
41- asm volatile ("" : "=r" (i ) : "0" (i ));
42- if (i >= lbls -> len )
43- break ;
44- u64 k = (((u64 )lbls -> labels [i ].key_len ) << 32 ) | ((u64 )lbls -> labels [i ].val_len );
45- k *= m ;
46- k ^= k >> r ;
47- k *= m ;
6+ #define M 0xc6a4a7935bd1e995LLU
487
49- h ^= k ;
50- h *= m ;
8+ static inline __attribute__((__always_inline__ ))
9+ u64 clear_or_hash_custom_labels (CustomLabelsArray * lbls , bool clear ) {
10+ u64 h = lbls -> len * M ;
11+ u64 * bits = (u64 * )lbls ;
12+ #pragma unroll
13+ for (int i = 0 ; i < sizeof (CustomLabelsArray )/8 ; i ++ ) {
14+ if (clear ) {
15+ bits [i ] = 0 ;
16+ } else {
17+ h ^= bits [i ];
18+ h *= M ;
19+ }
5120 }
5221
53- // hash each k/v
54- for (int i = 0 ; i < MAX_CUSTOM_LABELS ; ++ i ) {
55- if (i >= lbls -> len )
56- break ;
57- CustomLabel * lbl = & lbls -> labels [i ];
58- u64 kl = ROUNDUP_8 (lbl -> key_len );
59- for (int j = 0 ; j < CUSTOM_LABEL_MAX_VAL_LEN / 8 ; ++ j ) {
60- if (j >= kl )
61- return false;
62- u64 k = lbl -> key .key_u64 [j ];
63- k *= m ;
64- k ^= k >> r ;
65- k *= m ;
66-
67- h ^= k ;
68- h *= m ;
69- }
70- u64 vl = ROUNDUP_8 (lbl -> val_len );
71- for (int j = 0 ; j < CUSTOM_LABEL_MAX_VAL_LEN / 8 ; ++ j ) {
72- if (j >= vl )
73- return false;
74- u64 k = lbl -> val .val_u64 [j ];
75- k *= m ;
76- k ^= k >> r ;
77- k *= m ;
78-
79- h ^= k ;
80- h *= m ;
81- }
82- }
22+ return h ;
23+ }
8324
84- h ^= h >> r ;
85- h *= m ;
86- h ^= h >> r ;
25+ static inline __attribute__((__always_inline__ ))
26+ void clear_custom_labels (CustomLabelsArray * lbls ) {
27+ clear_or_hash_custom_labels (lbls , true);
28+ }
8729
88- * out = h ;
89- return true;
30+ static inline __attribute__((__always_inline__ ))
31+ u64 hash_custom_labels (CustomLabelsArray * lbls ) {
32+ return clear_or_hash_custom_labels (lbls , false);
9033}
9134
92- #endif
35+ #endif // OPTI_HASH_H
0 commit comments