Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-only */
2#ifndef _LINUX_CRC32_H
3#define _LINUX_CRC32_H
4
5#include <linux/types.h>
6#include <linux/bitrev.h>
7
8u32 crc32_le_arch(u32 crc, const u8 *p, size_t len);
9u32 crc32_le_base(u32 crc, const u8 *p, size_t len);
10u32 crc32_be_arch(u32 crc, const u8 *p, size_t len);
11u32 crc32_be_base(u32 crc, const u8 *p, size_t len);
12u32 crc32c_arch(u32 crc, const u8 *p, size_t len);
13u32 crc32c_base(u32 crc, const u8 *p, size_t len);
14
15static inline u32 crc32_le(u32 crc, const void *p, size_t len)
16{
17 if (IS_ENABLED(CONFIG_CRC32_ARCH))
18 return crc32_le_arch(crc, p, len);
19 return crc32_le_base(crc, p, len);
20}
21
22static inline u32 crc32_be(u32 crc, const void *p, size_t len)
23{
24 if (IS_ENABLED(CONFIG_CRC32_ARCH))
25 return crc32_be_arch(crc, p, len);
26 return crc32_be_base(crc, p, len);
27}
28
29static inline u32 crc32c(u32 crc, const void *p, size_t len)
30{
31 if (IS_ENABLED(CONFIG_CRC32_ARCH))
32 return crc32c_arch(crc, p, len);
33 return crc32c_base(crc, p, len);
34}
35
36/*
37 * crc32_optimizations() returns flags that indicate which CRC32 library
38 * functions are using architecture-specific optimizations. Unlike
39 * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
40 * variants and also whether any needed CPU features are available at runtime.
41 */
42#define CRC32_LE_OPTIMIZATION BIT(0) /* crc32_le() is optimized */
43#define CRC32_BE_OPTIMIZATION BIT(1) /* crc32_be() is optimized */
44#define CRC32C_OPTIMIZATION BIT(2) /* crc32c() is optimized */
45#if IS_ENABLED(CONFIG_CRC32_ARCH)
46u32 crc32_optimizations(void);
47#else
48static inline u32 crc32_optimizations(void) { return 0; }
49#endif
50
51/**
52 * crc32_le_combine - Combine two crc32 check values into one. For two
53 * sequences of bytes, seq1 and seq2 with lengths len1
54 * and len2, crc32_le() check values were calculated
55 * for each, crc1 and crc2.
56 *
57 * @crc1: crc32 of the first block
58 * @crc2: crc32 of the second block
59 * @len2: length of the second block
60 *
61 * Return: The crc32_le() check value of seq1 and seq2 concatenated,
62 * requiring only crc1, crc2, and len2. Note: If seq_full denotes
63 * the concatenated memory area of seq1 with seq2, and crc_full
64 * the crc32_le() value of seq_full, then crc_full ==
65 * crc32_le_combine(crc1, crc2, len2) when crc_full was seeded
66 * with the same initializer as crc1, and crc2 seed was 0. See
67 * also crc32_combine_test().
68 */
69u32 crc32_le_shift(u32 crc, size_t len);
70
71static inline u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
72{
73 return crc32_le_shift(crc1, len2) ^ crc2;
74}
75
76#define crc32(seed, data, length) crc32_le(seed, (unsigned char const *)(data), length)
77
78/*
79 * Helpers for hash table generation of ethernet nics:
80 *
81 * Ethernet sends the least significant bit of a byte first, thus crc32_le
82 * is used. The output of crc32_le is bit reversed [most significant bit
83 * is in bit nr 0], thus it must be reversed before use. Except for
84 * nics that bit swap the result internally...
85 */
86#define ether_crc(length, data) bitrev32(crc32_le(~0, data, length))
87#define ether_crc_le(length, data) crc32_le(~0, data, length)
88
89#endif /* _LINUX_CRC32_H */