hash: use armv8-a CRC32 instructions
[dpdk.git] / lib / librte_hash / rte_crc_arm64.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Cavium networks. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium networks nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_CRC_ARM64_H_
35 #define _RTE_CRC_ARM64_H_
36
37 /**
38  * @file
39  *
40  * RTE CRC arm64 Hash
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <stdint.h>
48 #include <rte_cpuflags.h>
49 #include <rte_branch_prediction.h>
50 #include <rte_common.h>
51
52 static inline uint32_t
53 crc32c_arm64_u32(uint32_t data, uint32_t init_val)
54 {
55         asm(".arch armv8-a+crc");
56         __asm__ volatile(
57                         "crc32cw %w[crc], %w[crc], %w[value]"
58                         : [crc] "+r" (init_val)
59                         : [value] "r" (data));
60         return init_val;
61 }
62
63 static inline uint32_t
64 crc32c_arm64_u64(uint64_t data, uint32_t init_val)
65 {
66         asm(".arch armv8-a+crc");
67         __asm__ volatile(
68                         "crc32cx %w[crc], %w[crc], %x[value]"
69                         : [crc] "+r" (init_val)
70                         : [value] "r" (data));
71         return init_val;
72 }
73
74 /**
75  * Allow or disallow use of arm64 SIMD instrinsics for CRC32 hash
76  * calculation.
77  *
78  * @param alg
79  *   An OR of following flags:
80  *   - (CRC32_SW) Don't use arm64 crc intrinsics
81  *   - (CRC32_ARM64) Use ARMv8 CRC intrinsic if available
82  *
83  */
84 static inline void
85 rte_hash_crc_set_alg(uint8_t alg)
86 {
87         switch (alg) {
88         case CRC32_ARM64:
89                 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
90                         alg = CRC32_SW;
91         case CRC32_SW:
92                 crc32_alg = alg;
93         default:
94                 break;
95         }
96 }
97
98 /* Setting the best available algorithm */
99 static inline void __attribute__((constructor))
100 rte_hash_crc_init_alg(void)
101 {
102         rte_hash_crc_set_alg(CRC32_ARM64);
103 }
104
105 /**
106  * Use single crc32 instruction to perform a hash on a 4 byte value.
107  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
108  * not supported
109  *
110  * @param data
111  *   Data to perform hash on.
112  * @param init_val
113  *   Value to initialise hash generator.
114  * @return
115  *   32bit calculated hash value.
116  */
117 static inline uint32_t
118 rte_hash_crc_4byte(uint32_t data, uint32_t init_val)
119 {
120         if (likely(crc32_alg & CRC32_ARM64))
121                 return crc32c_arm64_u32(data, init_val);
122
123         return crc32c_1word(data, init_val);
124 }
125
126 /**
127  * Use single crc32 instruction to perform a hash on a 8 byte value.
128  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
129  * not supported
130  *
131  * @param data
132  *   Data to perform hash on.
133  * @param init_val
134  *   Value to initialise hash generator.
135  * @return
136  *   32bit calculated hash value.
137  */
138 static inline uint32_t
139 rte_hash_crc_8byte(uint64_t data, uint32_t init_val)
140 {
141         if (likely(crc32_alg == CRC32_ARM64))
142                 return crc32c_arm64_u64(data, init_val);
143
144         return crc32c_2words(data, init_val);
145 }
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif /* _RTE_CRC_ARM64_H_ */