1 /*
2 Copyright (c) 2018, MIPI Alliance, Inc.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12 * Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in
14   the documentation and/or other materials provided with the
15   distribution.
16 
17 * Neither the name of the copyright holder 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 /*
35  * Contributors:
36  * Norbert Schulz (Intel Corporation) - Initial API and implementation
37  */
38 
39 /* Defines for CRC32C computation */
40 
41 #ifndef MIPI_SYST_CRC32_INCLUDED
42 #define MIPI_SYST_CRC32_INCLUDED
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
49 #if !defined(MIPI_SYST_PCFG_ENABLE_CHECKSUM)
50 
51 #define MIPI_SYST_CRC32_INIT(v)
52 #define MIPI_SYST_CRC32_GET(v)
53 
54 #define MIPI_SYST_CRC32_U8(crc, v)
55 #define MIPI_SYST_CRC32_U16(crc, v)
56 #define MIPI_SYST_CRC32_U32(crc, v)
57 #define MIPI_SYST_CRC32_U64(crc, v)
58 
59 #else
60 
61 extern const mipi_syst_u32 mipi_syst_crc_table[256];
62 
63 #define MIPI_SYST_CRC32_INIT(v) ((v) ^ 0xFFFFFFFF)
64 #define MIPI_SYST_CRC32_GET(v)  ((v) ^ 0xFFFFFFFF)
65 
66 #define MIPI_SYST_CRC32_U8(crc, v)   { crc = mipi_syst_crc32_8((crc), (v));  }
67 #define MIPI_SYST_CRC32_U16(crc, v)  { crc = mipi_syst_crc32_16((crc), (v)); }
68 #define MIPI_SYST_CRC32_U32(crc, v)  { crc = mipi_syst_crc32_32((crc), (v)); }
69 #define MIPI_SYST_CRC32_U64(crc, v)  { crc = mipi_syst_crc32_64((crc), (v)); }
70 
71 #if !defined(MIPI_SYST_PCFG_ENABLE_INLINE)
72 
73 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_8(mipi_syst_u32 crc, mipi_syst_u8 b);
74 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_16(mipi_syst_u32 crc, mipi_syst_u16 hw);
75 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_32(mipi_syst_u32 crc, mipi_syst_u32 hw);
76 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_64(mipi_syst_u32 crc, mipi_syst_u64 hw);
77 
78 #else
79 
80 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_8(mipi_syst_u32 crc, mipi_syst_u8 b)
81 {
82 #if (defined(MIPI_SYST_CRC_INTRINSIC))
83 	return _MIPI_SYST_CPU_CRC8(crc, b);
84 #else
85 	return mipi_syst_crc_table[((int) crc ^ b) & 0xff] ^ (crc >> 8);
86 #endif
87 }
88 
89 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_16(mipi_syst_u32 crc, mipi_syst_u16 s)
90 {
91 #if (defined(MIPI_SYST_CRC_INTRINSIC))
92 	return _MIPI_SYST_CPU_CRC16(crc, s);
93 #else
94 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) s);
95 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (s >> 8));
96 	return crc;
97 #endif
98 }
99 
100 
101 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_32(mipi_syst_u32 crc, mipi_syst_u32 w)
102 {
103 #if (defined(MIPI_SYST_CRC_INTRINSIC))
104 	return _MIPI_SYST_CPU_CRC32(crc, w);
105 #else
106 
107 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) w);
108 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (w >> 8));
109 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (w >> 16));
110 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (w >> 24));
111 	return crc;
112 #endif
113 }
114 
115 MIPI_SYST_INLINE mipi_syst_u32 mipi_syst_crc32_64(mipi_syst_u32 crc, mipi_syst_u64 l)
116 {
117 #if (defined(MIPI_SYST_CRC_INTRINSIC))
118 	return _MIPI_SYST_CPU_CRC64(crc, l);
119 #else
120 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) l);
121 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 8));
122 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 16));
123 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 24));
124 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 32));
125 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 40));
126 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 48));
127 	crc = mipi_syst_crc32_8(crc, (mipi_syst_u8) (l >> 56));
128 	return crc;
129 #endif
130 }
131 #endif
132 #endif		/* defined(MIPI_SYST_PCFG_ENABLE_CHECKSUM) */
133 
134 #ifdef __cplusplus
135 }		/* extern C */
136 #endif
137 
138 #endif
139