Debian Codegraph @

cipher/bithelp.h

raw
1 /* bithelp.h - Some bit manipulation helpers
2 * Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GCRYPT_BITHELP_H
20 #define GCRYPT_BITHELP_H
21
22 #include "config.h"
23 #include "types.h"
24
25
26 /****************
27 * Rotate the 32 bit unsigned integer X by N bits left/right
28 */
29 static inline u32 rol(u32 x, int n)
30 {
31 return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) );
32 }
33
34 static inline u32 ror(u32 x, int n)
35 {
36 return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
37 }
38
39 static inline u64 rol64(u64 x, int n)
40 {
41 return ( (x << (n&(64-1))) | (x >> ((64-n)&(64-1))) );
42 }
43
44 /* Byte swap for 32-bit and 64-bit integers. If available, use compiler
45 provided helpers. */
46 #ifdef HAVE_BUILTIN_BSWAP32
47 # define _gcry_bswap32 __builtin_bswap32
48 #else
49 static inline u32
50 _gcry_bswap32(u32 x)
51 {
52 return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
53 }
54 #endif
55
56 #ifdef HAVE_BUILTIN_BSWAP64
57 # define _gcry_bswap64 __builtin_bswap64
58 #else
59 static inline u64
60 _gcry_bswap64(u64 x)
61 {
62 return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
63 }
64 #endif
65
66 /* Endian dependent byte swap operations. */
67 #ifdef WORDS_BIGENDIAN
68 # define le_bswap32(x) _gcry_bswap32(x)
69 # define be_bswap32(x) ((u32)(x))
70 # define le_bswap64(x) _gcry_bswap64(x)
71 # define be_bswap64(x) ((u64)(x))
72 #else
73 # define le_bswap32(x) ((u32)(x))
74 # define be_bswap32(x) _gcry_bswap32(x)
75 # define le_bswap64(x) ((u64)(x))
76 # define be_bswap64(x) _gcry_bswap64(x)
77 #endif
78
79
80 /* Count trailing zero bits in an unsigend int. We return an int
81 because that is what gcc's builtin does. X must not be zero. */
82 static inline int
83 _gcry_ctz_no_zero (unsigned int x)
84 {
85 #if defined(__riscv) && \
86 (defined(__riscv_f) && __riscv_f >= 2002000) && \
87 (!defined(__riscv_zbb) || __riscv_zbb < 1000000) && \
88 defined(HAVE_GCC_ATTRIBUTE_MAY_ALIAS)
89 /* Use float cast approach when building for RISC-V without Zbb extension.
90 * Without Zbb, GCC gives us slower generic version for __builtin_ctz().
91 *
92 * See:
93 * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast
94 */
95 float f = (float)(x & -x);
96 typedef u32 __attribute__((may_alias)) may_alias_u32;
97 return ((*(const may_alias_u32 *)&f) >> 23) - 0x7f;
98 #elif defined (HAVE_BUILTIN_CTZ)
99 return __builtin_ctz (x);
100 #else
101 /* See
102 * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
103 */
104 static const unsigned char mod37[] =
105 {
106 sizeof (unsigned int)*8,
107 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
108 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
109 5, 20, 8, 19, 18
110 };
111 return (int)mod37[(-x & x) % 37];
112 #endif
113 }
114
115
116 /* Count trailing zero bits in an unsigend int. We return an int
117 because that is what gcc's builtin does. Returns the number of
118 bits in X if X is 0. */
119 static inline int
120 _gcry_ctz (unsigned int x)
121 {
122 return x ? _gcry_ctz_no_zero (x) : 8 * sizeof (x);
123 }
124
125
126 /* Count trailing zero bits in an u64. We return an int because that
127 is what gcc's builtin does. Returns the number of bits in X if X
128 is 0. */
129 static inline int
130 _gcry_ctz64(u64 x)
131 {
132 #if defined (HAVE_BUILTIN_CTZL) && SIZEOF_UNSIGNED_LONG >= 8
133 return x ? __builtin_ctzl (x) : 8 * sizeof (x);
134 #elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
135 #warning hello
136 return x ? __builtin_ctz (x) : 8 * sizeof (x);
137 #else
138 if ((x & 0xffffffff))
139 return _gcry_ctz (x);
140 else
141 return 32 + _gcry_ctz (x >> 32);
142 #endif
143 }
144
145
146 #endif /*GCRYPT_BITHELP_H*/