1 /*
2 * Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #ifndef BITFN_H
26 #define BITFN_H
27 #include <stdint.h>
28
29 #ifndef NO_INLINE_ASM
30 /**********************************************************/
31 # if (defined(__i386__))
32 # define ARCH_HAS_SWAP32
bitfn_swap32(uint32_t a)33 static inline uint32_t bitfn_swap32(uint32_t a)
34 {
35 asm ("bswap %0" : "=r" (a) : "0" (a));
36 return a;
37 }
38 /**********************************************************/
39 # elif (defined(__arm__))
40 # define ARCH_HAS_SWAP32
bitfn_swap32(uint32_t a)41 static inline uint32_t bitfn_swap32(uint32_t a)
42 {
43 uint32_t tmp = a;
44 asm volatile ("eor %1, %0, %0, ror #16\n"
45 "bic %1, %1, #0xff0000\n"
46 "mov %0, %0, ror #8\n"
47 "eor %0, %0, %1, lsr #8\n"
48 : "=r" (a), "=r" (tmp) : "0" (a), "1" (tmp));
49 return a;
50 }
51 /**********************************************************/
52 # elif defined(__x86_64__)
53 # define ARCH_HAS_SWAP32
54 # define ARCH_HAS_SWAP64
bitfn_swap32(uint32_t a)55 static inline uint32_t bitfn_swap32(uint32_t a)
56 {
57 asm ("bswap %0" : "=r" (a) : "0" (a));
58 return a;
59 }
60
bitfn_swap64(uint64_t a)61 static inline uint64_t bitfn_swap64(uint64_t a)
62 {
63 asm ("bswap %0" : "=r" (a) : "0" (a));
64 return a;
65 }
66
67 # endif
68 #endif /* NO_INLINE_ASM */
69 /**********************************************************/
70
71 #ifndef ARCH_HAS_ROL32
rol32(uint32_t word,uint32_t shift)72 static inline uint32_t rol32(uint32_t word, uint32_t shift)
73 {
74 return (word << shift) | (word >> (32 - shift));
75 }
76 #endif
77
78 #ifndef ARCH_HAS_ROR32
ror32(uint32_t word,uint32_t shift)79 static inline uint32_t ror32(uint32_t word, uint32_t shift)
80 {
81 return (word >> shift) | (word << (32 - shift));
82 }
83 #endif
84
85 #ifndef ARCH_HAS_ROL64
rol64(uint64_t word,uint32_t shift)86 static inline uint64_t rol64(uint64_t word, uint32_t shift)
87 {
88 return (word << shift) | (word >> (64 - shift));
89 }
90 #endif
91
92 #ifndef ARCH_HAS_ROR64
ror64(uint64_t word,uint32_t shift)93 static inline uint64_t ror64(uint64_t word, uint32_t shift)
94 {
95 return (word >> shift) | (word << (64 - shift));
96 }
97 #endif
98
99 #ifndef ARCH_HAS_SWAP32
bitfn_swap32(uint32_t a)100 static inline uint32_t bitfn_swap32(uint32_t a)
101 {
102 return (a << 24) | ((a & 0xff00) << 8) | ((a >> 8) & 0xff00) | (a >> 24);
103 }
104 #endif
105
106 #ifndef ARCH_HAS_ARRAY_SWAP32
array_swap32(uint32_t * d,uint32_t * s,uint32_t nb)107 static inline void array_swap32(uint32_t *d, uint32_t *s, uint32_t nb)
108 {
109 while (nb--)
110 *d++ = bitfn_swap32(*s++);
111 }
112 #endif
113
114 #ifndef ARCH_HAS_SWAP64
bitfn_swap64(uint64_t a)115 static inline uint64_t bitfn_swap64(uint64_t a)
116 {
117 return ((uint64_t) bitfn_swap32((uint32_t) (a >> 32))) |
118 (((uint64_t) bitfn_swap32((uint32_t) a)) << 32);
119 }
120 #endif
121
122 #ifndef ARCH_HAS_ARRAY_SWAP64
array_swap64(uint64_t * d,uint64_t * s,uint32_t nb)123 static inline void array_swap64(uint64_t *d, uint64_t *s, uint32_t nb)
124 {
125 while (nb--)
126 *d++ = bitfn_swap64(*s++);
127 }
128 #endif
129
130 #ifndef ARCH_HAS_MEMORY_ZERO
memory_zero(void * ptr,uint32_t len)131 static inline void memory_zero(void *ptr, uint32_t len)
132 {
133 uint32_t *ptr32 = ptr;
134 uint8_t *ptr8;
135 int i;
136
137 for (i = 0; i < len / 4; i++)
138 *ptr32++ = 0;
139 if (len % 4) {
140 ptr8 = (uint8_t *) ptr32;
141 for (i = len % 4; i >= 0; i--)
142 ptr8[i] = 0;
143 }
144 }
145 #endif
146
147 #ifndef ARCH_HAS_ARRAY_COPY32
array_copy32(uint32_t * d,uint32_t * s,uint32_t nb)148 static inline void array_copy32(uint32_t *d, uint32_t *s, uint32_t nb)
149 {
150 while (nb--) *d++ = *s++;
151 }
152 #endif
153
154 #ifndef ARCH_HAS_ARRAY_COPY64
array_copy64(uint64_t * d,uint64_t * s,uint32_t nb)155 static inline void array_copy64(uint64_t *d, uint64_t *s, uint32_t nb)
156 {
157 while (nb--) *d++ = *s++;
158 }
159 #endif
160
161 #ifdef __GNUC__
162 #define bitfn_ntz(n) __builtin_ctz(n)
163 #else
164 #error "define ntz for your platform"
165 #endif
166
167 #ifdef __MINGW32__
168 # define LITTLE_ENDIAN 1234
169 # define BYTE_ORDER LITTLE_ENDIAN
170 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
171 # include <sys/endian.h>
172 #elif defined(__OpenBSD__) || defined(__SVR4)
173 # include <sys/types.h>
174 #elif defined(__APPLE__)
175 # include <machine/endian.h>
176 #elif defined( BSD ) && ( BSD >= 199103 )
177 # include <machine/endian.h>
178 #elif defined( __QNXNTO__ ) && defined( __LITTLEENDIAN__ )
179 # define LITTLE_ENDIAN 1234
180 # define BYTE_ORDER LITTLE_ENDIAN
181 #elif defined( __QNXNTO__ ) && defined( __BIGENDIAN__ )
182 # define BIG_ENDIAN 1234
183 # define BYTE_ORDER BIG_ENDIAN
184 #else
185 # include <endian.h>
186 #endif
187 /* big endian to cpu */
188 #if LITTLE_ENDIAN == BYTE_ORDER
189
190 # define be32_to_cpu(a) bitfn_swap32(a)
191 # define cpu_to_be32(a) bitfn_swap32(a)
192 # define le32_to_cpu(a) (a)
193 # define cpu_to_le32(a) (a)
194 # define be64_to_cpu(a) bitfn_swap64(a)
195 # define cpu_to_be64(a) bitfn_swap64(a)
196 # define le64_to_cpu(a) (a)
197 # define cpu_to_le64(a) (a)
198
199 # define cpu_to_le32_array(d, s, l) array_copy32(d, s, l)
200 # define le32_to_cpu_array(d, s, l) array_copy32(d, s, l)
201 # define cpu_to_be32_array(d, s, l) array_swap32(d, s, l)
202 # define be32_to_cpu_array(d, s, l) array_swap32(d, s, l)
203
204 # define cpu_to_le64_array(d, s, l) array_copy64(d, s, l)
205 # define le64_to_cpu_array(d, s, l) array_copy64(d, s, l)
206 # define cpu_to_be64_array(d, s, l) array_swap64(d, s, l)
207 # define be64_to_cpu_array(d, s, l) array_swap64(d, s, l)
208
209 # define ror32_be(a, s) rol32(a, s)
210 # define rol32_be(a, s) ror32(a, s)
211
212 # define ARCH_IS_LITTLE_ENDIAN
213
214 #elif BIG_ENDIAN == BYTE_ORDER
215
216 # define be32_to_cpu(a) (a)
217 # define cpu_to_be32(a) (a)
218 # define be64_to_cpu(a) (a)
219 # define cpu_to_be64(a) (a)
220 # define le64_to_cpu(a) bitfn_swap64(a)
221 # define cpu_to_le64(a) bitfn_swap64(a)
222 # define le32_to_cpu(a) bitfn_swap32(a)
223 # define cpu_to_le32(a) bitfn_swap32(a)
224
225 # define cpu_to_le32_array(d, s, l) array_swap32(d, s, l)
226 # define le32_to_cpu_array(d, s, l) array_swap32(d, s, l)
227 # define cpu_to_be32_array(d, s, l) array_copy32(d, s, l)
228 # define be32_to_cpu_array(d, s, l) array_copy32(d, s, l)
229
230 # define cpu_to_le64_array(d, s, l) array_swap64(d, s, l)
231 # define le64_to_cpu_array(d, s, l) array_swap64(d, s, l)
232 # define cpu_to_be64_array(d, s, l) array_copy64(d, s, l)
233 # define be64_to_cpu_array(d, s, l) array_copy64(d, s, l)
234
235 # define ror32_be(a, s) ror32(a, s)
236 # define rol32_be(a, s) rol32(a, s)
237
238 # define ARCH_IS_BIG_ENDIAN
239
240 #else
241 # error "endian not supported"
242 #endif
243
244 #endif /* !BITFN_H */
245