summaryrefslogtreecommitdiff
path: root/cbits/cryptonite_bitfn.h
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2019-09-28 13:43:29 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:27:53 -0500
commit11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch)
tree5716463275c2d3e902889db619908ded2a73971c /cbits/cryptonite_bitfn.h
parentadd2c76bced51fde5e9917e7449ef52be70faf87 (diff)
Factor out some new libraries
word64-map: Data.Word64Map network-addr: Network.Address tox-crypto: Crypto.Tox lifted-concurrent: Control.Concurrent.Lifted.Instrument Control.Concurrent.Async.Lifted.Instrument psq-wrap: Data.Wrapper.PSQInt Data.Wrapper.PSQ minmax-psq: Data.MinMaxPSQ tasks: Control.Concurrent.Tasks kad: Network.Kademlia Network.Kademlia.Bootstrap Network.Kademlia.Routing Network.Kademlia.CommonAPI Network.Kademlia.Persistence Network.Kademlia.Search
Diffstat (limited to 'cbits/cryptonite_bitfn.h')
-rw-r--r--cbits/cryptonite_bitfn.h253
1 files changed, 0 insertions, 253 deletions
diff --git a/cbits/cryptonite_bitfn.h b/cbits/cryptonite_bitfn.h
deleted file mode 100644
index 3a00dd8a..00000000
--- a/cbits/cryptonite_bitfn.h
+++ /dev/null
@@ -1,253 +0,0 @@
1/*
2 * Copyright (C) 2006-2014 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
33static 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
41static 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
55static inline uint32_t bitfn_swap32(uint32_t a)
56{
57 asm ("bswap %0" : "=r" (a) : "0" (a));
58 return a;
59}
60
61static 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
72static 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
79static 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
86static 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
93static 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
100static 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
107static 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
115static 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
123static 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
131static 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
148static 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_XOR32
155static inline void array_xor32(uint32_t *d, uint32_t *s, uint32_t nb)
156{
157 while (nb--) *d++ ^= *s++;
158}
159#endif
160
161#ifndef ARCH_HAS_ARRAY_COPY64
162static inline void array_copy64(uint64_t *d, uint64_t *s, uint32_t nb)
163{
164 while (nb--) *d++ = *s++;
165}
166#endif
167
168#ifdef __GNUC__
169#define bitfn_ntz(n) __builtin_ctz(n)
170#else
171#error "define ntz for your platform"
172#endif
173
174#ifdef __MINGW32__
175 # define LITTLE_ENDIAN 1234
176 # define BYTE_ORDER LITTLE_ENDIAN
177#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
178 # include <sys/endian.h>
179#elif defined(__OpenBSD__) || defined(__SVR4)
180 # include <sys/types.h>
181#elif defined(__APPLE__)
182 # include <machine/endian.h>
183#elif defined( BSD ) && ( BSD >= 199103 )
184 # include <machine/endian.h>
185#elif defined( __QNXNTO__ ) && defined( __LITTLEENDIAN__ )
186 # define LITTLE_ENDIAN 1234
187 # define BYTE_ORDER LITTLE_ENDIAN
188#elif defined( __QNXNTO__ ) && defined( __BIGENDIAN__ )
189 # define BIG_ENDIAN 1234
190 # define BYTE_ORDER BIG_ENDIAN
191#elif defined( _AIX )
192 # include <sys/machine.h>
193#else
194 # include <endian.h>
195#endif
196/* big endian to cpu */
197#if LITTLE_ENDIAN == BYTE_ORDER
198
199# define be32_to_cpu(a) bitfn_swap32(a)
200# define cpu_to_be32(a) bitfn_swap32(a)
201# define le32_to_cpu(a) (a)
202# define cpu_to_le32(a) (a)
203# define be64_to_cpu(a) bitfn_swap64(a)
204# define cpu_to_be64(a) bitfn_swap64(a)
205# define le64_to_cpu(a) (a)
206# define cpu_to_le64(a) (a)
207
208# define cpu_to_le32_array(d, s, l) array_copy32(d, s, l)
209# define le32_to_cpu_array(d, s, l) array_copy32(d, s, l)
210# define cpu_to_be32_array(d, s, l) array_swap32(d, s, l)
211# define be32_to_cpu_array(d, s, l) array_swap32(d, s, l)
212
213# define cpu_to_le64_array(d, s, l) array_copy64(d, s, l)
214# define le64_to_cpu_array(d, s, l) array_copy64(d, s, l)
215# define cpu_to_be64_array(d, s, l) array_swap64(d, s, l)
216# define be64_to_cpu_array(d, s, l) array_swap64(d, s, l)
217
218# define ror32_be(a, s) rol32(a, s)
219# define rol32_be(a, s) ror32(a, s)
220
221# define ARCH_IS_LITTLE_ENDIAN
222
223#elif BIG_ENDIAN == BYTE_ORDER
224
225# define be32_to_cpu(a) (a)
226# define cpu_to_be32(a) (a)
227# define be64_to_cpu(a) (a)
228# define cpu_to_be64(a) (a)
229# define le64_to_cpu(a) bitfn_swap64(a)
230# define cpu_to_le64(a) bitfn_swap64(a)
231# define le32_to_cpu(a) bitfn_swap32(a)
232# define cpu_to_le32(a) bitfn_swap32(a)
233
234# define cpu_to_le32_array(d, s, l) array_swap32(d, s, l)
235# define le32_to_cpu_array(d, s, l) array_swap32(d, s, l)
236# define cpu_to_be32_array(d, s, l) array_copy32(d, s, l)
237# define be32_to_cpu_array(d, s, l) array_copy32(d, s, l)
238
239# define cpu_to_le64_array(d, s, l) array_swap64(d, s, l)
240# define le64_to_cpu_array(d, s, l) array_swap64(d, s, l)
241# define cpu_to_be64_array(d, s, l) array_copy64(d, s, l)
242# define be64_to_cpu_array(d, s, l) array_copy64(d, s, l)
243
244# define ror32_be(a, s) ror32(a, s)
245# define rol32_be(a, s) rol32(a, s)
246
247# define ARCH_IS_BIG_ENDIAN
248
249#else
250# error "endian not supported"
251#endif
252
253#endif /* !BITFN_H */