Blame SOURCES/0165-trie-use-BIT-and-MASK-macros.patch

da51ba
From 8ef5456338a947944cc03b95c22c837af5884ddc Mon Sep 17 00:00:00 2001
da51ba
From: Eugene Syromyatnikov <evgsyr@gmail.com>
da51ba
Date: Wed, 18 Aug 2021 21:51:22 +0200
da51ba
Subject: [PATCH 149/150] trie: use BIT* and MASK* macros
da51ba
da51ba
This makes reading the code a bit easier.  It also solves some issues
da51ba
where there is a hypothertical possibility of having bit shifts of size
da51ba
64, by virtue of using the *_SAFE macros (that should silence some
da51ba
reported "left shifting by more than 63 bits has undefined behavior"
da51ba
covscan issues).
da51ba
da51ba
* src/trie.c (trie_create): Use BIT32, MASK64.
da51ba
(trie_create_data_block): Use BIT32, change iterator variable type
da51ba
to size_t.
da51ba
(trie_get_node): Use BIT64, MASK64.
da51ba
(trie_data_block_calc_pos): Use BIT32, MASK64, MASK64_SAFE.
da51ba
(trie_iterate_keys_node): Use BIT64, MASK64, MASK64_SAFE.
da51ba
(trie_free_node): Use BIT64.
da51ba
---
da51ba
 src/trie.c | 34 +++++++++++++++++-----------------
da51ba
 1 file changed, 17 insertions(+), 17 deletions(-)
da51ba
da51ba
diff --git a/src/trie.c b/src/trie.c
da51ba
index 586ff25..0a231e4 100644
da51ba
--- a/src/trie.c
da51ba
+++ b/src/trie.c
da51ba
@@ -15,6 +15,7 @@
da51ba
 #include <stdio.h>
da51ba
 
da51ba
 #include "trie.h"
da51ba
+#include "macros.h"
da51ba
 #include "xmalloc.h"
da51ba
 
da51ba
 static const uint8_t ptr_sz_lg = (sizeof(void *) == 8 ? 6 : 5);
da51ba
@@ -87,7 +88,7 @@ trie_create(uint8_t key_size, uint8_t item_size_lg, uint8_t node_key_bits,
da51ba
 		/ t->node_key_bits;
da51ba
 
da51ba
 	if (item_size_lg != 6)
da51ba
-		t->empty_value &= (((uint64_t) 1 << (1 << t->item_size_lg)) - 1);
da51ba
+		t->empty_value &= MASK64(BIT32(t->item_size_lg));
da51ba
 
da51ba
 	return t;
da51ba
 }
da51ba
@@ -96,8 +97,8 @@ static void *
da51ba
 trie_create_data_block(struct trie *t)
da51ba
 {
da51ba
 	uint64_t fill_value = t->empty_value;
da51ba
-	for (int i = 1; i < 1 << (6 - t->item_size_lg); i++) {
da51ba
-		fill_value <<= (1 << t->item_size_lg);
da51ba
+	for (size_t i = 1; i < BIT32(6 - t->item_size_lg); i++) {
da51ba
+		fill_value <<= BIT32(t->item_size_lg);
da51ba
 		fill_value |= t->empty_value;
da51ba
 	}
da51ba
 
da51ba
@@ -105,7 +106,7 @@ trie_create_data_block(struct trie *t)
da51ba
 	if (sz < 6)
da51ba
 		sz = 6;
da51ba
 
da51ba
-	size_t count = 1 << (sz - 6);
da51ba
+	size_t count = BIT32(sz - 6);
da51ba
 	uint64_t *data_block = xcalloc(count, 8);
da51ba
 
da51ba
 	for (size_t i = 0; i < count; i++)
da51ba
@@ -119,7 +120,7 @@ trie_get_node(struct trie *t, uint64_t key, bool auto_create)
da51ba
 {
da51ba
 	void **cur_node = &(t->data);
da51ba
 
da51ba
-	if (t->key_size < 64 && key > (uint64_t) 1 << t->key_size)
da51ba
+	if (t->key_size < 64 && key > MASK64(t->key_size))
da51ba
 		return NULL;
da51ba
 
da51ba
 	for (uint8_t cur_depth = 0; cur_depth <= t->max_depth; cur_depth++) {
da51ba
@@ -133,13 +134,13 @@ trie_get_node(struct trie *t, uint64_t key, bool auto_create)
da51ba
 			if (cur_depth == t->max_depth)
da51ba
 				*cur_node = trie_create_data_block(t);
da51ba
 			else
da51ba
-				*cur_node = xcalloc(1 << sz, 1);
da51ba
+				*cur_node = xcalloc(BIT64(sz), 1);
da51ba
 		}
da51ba
 
da51ba
 		if (cur_depth == t->max_depth)
da51ba
 			break;
da51ba
 
da51ba
-		size_t pos = (key >> offs) & ((1 << (sz - ptr_sz_lg)) - 1);
da51ba
+		size_t pos = (key >> offs) & MASK64(sz - ptr_sz_lg);
da51ba
 		cur_node = (((void **) (*cur_node)) + pos);
da51ba
 	}
da51ba
 
da51ba
@@ -152,7 +153,7 @@ trie_data_block_calc_pos(struct trie *t, uint64_t key,
da51ba
 {
da51ba
 	uint64_t key_mask;
da51ba
 
da51ba
-	key_mask = (1 << t->data_block_key_bits) - 1;
da51ba
+	key_mask = MASK64(t->data_block_key_bits);
da51ba
 	*pos = (key & key_mask) >> (6 - t->item_size_lg);
da51ba
 
da51ba
 	if (t->item_size_lg == 6) {
da51ba
@@ -161,10 +162,10 @@ trie_data_block_calc_pos(struct trie *t, uint64_t key,
da51ba
 		return;
da51ba
 	}
da51ba
 
da51ba
-	key_mask = (1 << (6 - t->item_size_lg)) - 1;
da51ba
-	*offs = (key & key_mask) * (1 << t->item_size_lg);
da51ba
+	key_mask = MASK64(6 - t->item_size_lg);
da51ba
+	*offs = (key & key_mask) << t->item_size_lg;
da51ba
 
da51ba
-	*mask = (((uint64_t) 1 << (1 << t->item_size_lg)) - 1) << *offs;
da51ba
+	*mask = MASK64_SAFE(BIT32(t->item_size_lg)) << *offs;
da51ba
 }
da51ba
 
da51ba
 bool
da51ba
@@ -211,7 +212,7 @@ trie_iterate_keys_node(struct trie *t,
da51ba
 		return 0;
da51ba
 
da51ba
 	if (t->key_size < 64) {
da51ba
-		uint64_t key_max = ((uint64_t) 1 << t->key_size) - 1;
da51ba
+		uint64_t key_max = MASK64(t->key_size);
da51ba
 		if (end > key_max)
da51ba
 			end = key_max;
da51ba
 	}
da51ba
@@ -228,15 +229,14 @@ trie_iterate_keys_node(struct trie *t,
da51ba
 		t->key_size :
da51ba
 		trie_get_node_bit_offs(t, depth - 1);
da51ba
 
da51ba
-	uint64_t first_key_in_node = start &
da51ba
-		(uint64_t) -1 << parent_node_bit_off;
da51ba
+	uint64_t first_key_in_node = start & ~MASK64_SAFE(parent_node_bit_off);
da51ba
 
da51ba
 	uint8_t node_bit_off = trie_get_node_bit_offs(t, depth);
da51ba
 	uint8_t node_key_bits = parent_node_bit_off - node_bit_off;
da51ba
-	uint64_t mask = ((uint64_t) 1 << (node_key_bits)) - 1;
da51ba
+	uint64_t mask = MASK64_SAFE(node_key_bits);
da51ba
 	uint64_t start_index = (start >> node_bit_off) & mask;
da51ba
 	uint64_t end_index = (end >> node_bit_off) & mask;
da51ba
-	uint64_t child_key_count = (uint64_t) 1 << node_bit_off;
da51ba
+	uint64_t child_key_count = BIT64(node_bit_off);
da51ba
 
da51ba
 	uint64_t count = 0;
da51ba
 
da51ba
@@ -274,7 +274,7 @@ trie_free_node(struct trie *t, void *node, uint8_t depth)
da51ba
 	if (depth >= t->max_depth)
da51ba
 		goto free_node;
da51ba
 
da51ba
-	size_t sz = 1 << (trie_get_node_size(t, depth) - ptr_sz_lg);
da51ba
+	size_t sz = BIT64(trie_get_node_size(t, depth) - ptr_sz_lg);
da51ba
 	for (size_t i = 0; i < sz; i++)
da51ba
 		trie_free_node(t, ((void **) node)[i], depth + 1);
da51ba
 
da51ba
-- 
da51ba
2.1.4
da51ba