[dpdk-dev,2/2] hash: rte_hash_crc uses 8- and 4-byte CRC32 intrinsics

Message ID 1409724351-23786-3-git-send-email-e_zhumabekov@sts.kz (mailing list archive)
State Superseded, archived
Headers

Commit Message

Yerden Zhumabekov Sept. 3, 2014, 6:05 a.m. UTC
  Calculating hash for data of variable length is more efficient
when that data is sliced into 8-byte pieces. The rest part of data
is hashed using either 8 and 4-byte CRC32 intrinsics.

Signed-off-by: Yerden Zhumabekov <e_zhumabekov@sts.kz>
---
 lib/librte_hash/rte_hash_crc.h |   31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
  

Patch

diff --git a/lib/librte_hash/rte_hash_crc.h b/lib/librte_hash/rte_hash_crc.h
index 102b2a0..d023e5d 100644
--- a/lib/librte_hash/rte_hash_crc.h
+++ b/lib/librte_hash/rte_hash_crc.h
@@ -95,23 +95,38 @@  static inline uint32_t
 rte_hash_crc(const void *data, uint32_t data_len, uint32_t init_val)
 {
 	unsigned i;
-	uint32_t temp = 0;
-	const uint32_t *p32 = (const uint32_t *)data;
+	uint64_t temp = 0;
+	const uint64_t *p64 = (const uint64_t *)data;
 
-	for (i = 0; i < data_len / 4; i++) {
-		init_val = rte_hash_crc_4byte(*p32++, init_val);
+	for (i = 0; i < data_len / 8; i++) {
+		init_val = rte_hash_crc_8byte(*p64++, init_val);
 	}
 
-	switch (3 - (data_len & 0x03)) {
+	switch (7 - (data_len & 0x07)) {
 	case 0:
-		temp |= *((const uint8_t *)p32 + 2) << 16;
+		temp |= (uint64_t) *((const uint8_t *)p64 + 6) << 48;
 		/* Fallthrough */
 	case 1:
-		temp |= *((const uint8_t *)p32 + 1) << 8;
+		temp |= (uint64_t) *((const uint8_t *)p64 + 5) << 40;
 		/* Fallthrough */
 	case 2:
-		temp |= *((const uint8_t *)p32);
+		temp |= (uint64_t) *((const uint8_t *)p64 + 4) << 32;
+		temp |= *((const uint32_t *)p64);
+		init_val = rte_hash_crc_8byte(temp, init_val);
+		break;
+	case 3:
+		init_val = rte_hash_crc_4byte(*(const uint32_t *)p64, init_val);
+		break;
+	case 4:
+		temp |= *((const uint8_t *)p64 + 2) << 16;
+		/* Fallthrough */
+	case 5:
+		temp |= *((const uint8_t *)p64 + 1) << 8;
+		/* Fallthrough */
+	case 6:
+		temp |= *((const uint8_t *)p64);
 		init_val = rte_hash_crc_4byte(temp, init_val);
+		/* Fallthrough */
 	default:
 		break;
 	}