Add crc32c inline function, allows compilation on 32-bit systems 52/6452/6
authorDamjan Marion <damarion@cisco.com>
Wed, 26 Apr 2017 11:05:05 +0000 (13:05 +0200)
committerDave Barach <openvpp@barachs.net>
Wed, 10 May 2017 16:47:10 +0000 (16:47 +0000)
32-bit code still can use crc32c instructions, but it operates
on 32 registers

Change-Id: I9bb6b0b59635d6ea6a753584676ebcf59c8f6584
Signed-off-by: Damjan Marion <damarion@cisco.com>
src/plugins/acl/bihash_40_8.h
src/plugins/lb/lbhash.h
src/vnet/bfd/bfd_main.c
src/vppinfra.am
src/vppinfra/bihash_24_8.h
src/vppinfra/bihash_8_8.h
src/vppinfra/crc32.h [new file with mode: 0644]

index ba3dfbe..1dfb6a1 100644 (file)
@@ -24,6 +24,7 @@
 #include <vppinfra/format.h>
 #include <vppinfra/pool.h>
 #include <vppinfra/xxhash.h>
+#include <vppinfra/crc32.h>
 
 typedef struct
 {
@@ -44,13 +45,7 @@ static inline u64
 clib_bihash_hash_40_8 (const clib_bihash_kv_40_8_t * v)
 {
 #if __SSE4_2__
-  u32 value = 0;
-  value = _mm_crc32_u64 (value, v->key[0]);
-  value = _mm_crc32_u64 (value, v->key[1]);
-  value = _mm_crc32_u64 (value, v->key[2]);
-  value = _mm_crc32_u64 (value, v->key[3]);
-  value = _mm_crc32_u64 (value, v->key[4]);
-  return value;
+  return clib_crc32c ((u8 *) v->key, 40);
 #else
   u64 tmp = v->key[0] ^ v->key[1] ^ v->key[2] ^ v->key[3] ^ v->key[4];
   return clib_xxhash (tmp);
index ca3cc14..c514fb5 100644 (file)
@@ -101,7 +101,7 @@ void lb_hash_free(lb_hash_t *h)
   vec_free(mem);
 }
 
-#if __SSE4_2__
+#if __SSE4_2__ && !defined (__i386__)
 static_always_inline
 u32 lb_hash_hash(u64 k0, u64 k1, u64 k2, u64 k3, u64 k4)
 {
index 66b31ce..b58a513 100644 (file)
@@ -39,7 +39,7 @@ static u64
 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
 {
   u64 checksum = 0;
-#if __SSE4_2__
+#if __SSE4_2__ && !defined (__i386__)
   checksum = _mm_crc32_u64 (0, discriminator);
   checksum = _mm_crc32_u64 (checksum, expire_time);
   checksum = _mm_crc32_u64 (checksum, secret);
index fed1981..3939d3c 100644 (file)
@@ -168,6 +168,7 @@ nobase_include_HEADERS = \
   vppinfra/cache.h \
   vppinfra/clib.h \
   vppinfra/cpu.h \
+  vppinfra/crc32.h \
   vppinfra/dlist.h \
   vppinfra/elf.h \
   vppinfra/elf_clib.h \
index 353f06b..655dab8 100644 (file)
@@ -24,6 +24,7 @@
 #include <vppinfra/format.h>
 #include <vppinfra/pool.h>
 #include <vppinfra/xxhash.h>
+#include <vppinfra/crc32.h>
 
 typedef struct
 {
@@ -44,11 +45,7 @@ static inline u64
 clib_bihash_hash_24_8 (const clib_bihash_kv_24_8_t * v)
 {
 #if __SSE4_2__
-  u32 value = 0;
-  value = _mm_crc32_u64 (value, v->key[0]);
-  value = _mm_crc32_u64 (value, v->key[1]);
-  value = _mm_crc32_u64 (value, v->key[2]);
-  return value;
+  return clib_crc32c ((u8 *) v->key, 24);
 #else
   u64 tmp = v->key[0] ^ v->key[1] ^ v->key[2];
   return clib_xxhash (tmp);
index d70da59..b5c1746 100644 (file)
 #include <vppinfra/format.h>
 #include <vppinfra/pool.h>
 #include <vppinfra/xxhash.h>
-
-#if __SSE4_2__
-#include <x86intrin.h>
-#endif
+#include <vppinfra/crc32.h>
 
 /** 8 octet key, 8 octet key value pair */
 typedef struct
@@ -55,7 +52,7 @@ clib_bihash_hash_8_8 (clib_bihash_kv_8_8_t * v)
 {
   /* Note: to torture-test linear scan, make this fn return a constant */
 #if __SSE4_2__
-  return _mm_crc32_u64 (0, v->key);
+  return clib_crc32c ((u8 *) & v->key, 8);
 #else
   return clib_xxhash (v->key);
 #endif
diff --git a/src/vppinfra/crc32.h b/src/vppinfra/crc32.h
new file mode 100644 (file)
index 0000000..abb2953
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __included_crc32_h__
+#define __included_crc32_h__
+
+#if __SSE4_2__
+#include <x86intrin.h>
+
+static_always_inline u32
+clib_crc32c (u8 * s, int len)
+{
+  u32 v = 0;
+
+#if __x86_64__
+  for (; len >= 8; len -= 8, s += 8)
+    v = _mm_crc32_u64 (v, *((u64 *) s));
+#else
+  /* workaround weird GCC bug when using _mm_crc32_u32
+     which happens with -O2 optimization */
+  volatile ("":::"memory");
+#endif
+
+  for (; len >= 4; len -= 4, s += 4)
+    v = _mm_crc32_u32 (v, *((u32 *) s));
+
+  for (; len >= 2; len -= 2, s += 2)
+    v = _mm_crc32_u16 (v, *((u16 *) s));
+
+  for (; len >= 1; len -= 1, s += 1)
+    v = _mm_crc32_u8 (v, *((u16 *) s));
+
+  return v;
+}
+
+#endif
+
+#endif /* __included_crc32_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */