/src/libgcrypt/cipher/bithelp.h
Line | Count | Source (jump to first uncovered line) |
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 "types.h" |
23 | | |
24 | | |
25 | | /**************** |
26 | | * Rotate the 32 bit unsigned integer X by N bits left/right |
27 | | */ |
28 | | static inline u32 rol(u32 x, int n) |
29 | 0 | { |
30 | 0 | return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) ); |
31 | 0 | } Unexecuted instantiation: random-drbg.c:rol Unexecuted instantiation: rndjent.c:rol |
32 | | |
33 | | static inline u32 ror(u32 x, int n) |
34 | 0 | { |
35 | 0 | return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) ); |
36 | 0 | } Unexecuted instantiation: random-drbg.c:ror Unexecuted instantiation: rndjent.c:ror |
37 | | |
38 | | static inline u64 rol64(u64 x, int n) |
39 | 0 | { |
40 | 0 | return ( (x << (n&(64-1))) | (x >> ((64-n)&(64-1))) ); |
41 | 0 | } Unexecuted instantiation: random-drbg.c:rol64 Unexecuted instantiation: rndjent.c:rol64 |
42 | | |
43 | | /* Byte swap for 32-bit and 64-bit integers. If available, use compiler |
44 | | provided helpers. */ |
45 | | #ifdef HAVE_BUILTIN_BSWAP32 |
46 | 0 | # define _gcry_bswap32 __builtin_bswap32 |
47 | | #else |
48 | | static inline u32 |
49 | | _gcry_bswap32(u32 x) |
50 | | { |
51 | | return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L)); |
52 | | } |
53 | | #endif |
54 | | |
55 | | #ifdef HAVE_BUILTIN_BSWAP64 |
56 | 0 | # define _gcry_bswap64 __builtin_bswap64 |
57 | | #else |
58 | | static inline u64 |
59 | | _gcry_bswap64(u64 x) |
60 | | { |
61 | | return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32)); |
62 | | } |
63 | | #endif |
64 | | |
65 | | /* Endian dependent byte swap operations. */ |
66 | | #ifdef WORDS_BIGENDIAN |
67 | | # define le_bswap32(x) _gcry_bswap32(x) |
68 | | # define be_bswap32(x) ((u32)(x)) |
69 | | # define le_bswap64(x) _gcry_bswap64(x) |
70 | | # define be_bswap64(x) ((u64)(x)) |
71 | | #else |
72 | | # define le_bswap32(x) ((u32)(x)) |
73 | 0 | # define be_bswap32(x) _gcry_bswap32(x) |
74 | | # define le_bswap64(x) ((u64)(x)) |
75 | 0 | # define be_bswap64(x) _gcry_bswap64(x) |
76 | | #endif |
77 | | |
78 | | |
79 | | /* Count trailing zero bits in an unsigend int. We return an int |
80 | | because that is what gcc's builtin does. Returns the number of |
81 | | bits in X if X is 0. */ |
82 | | static inline int |
83 | | _gcry_ctz (unsigned int x) |
84 | 0 | { |
85 | 0 | #if defined (HAVE_BUILTIN_CTZ) |
86 | 0 | return x ? __builtin_ctz (x) : 8 * sizeof (x); |
87 | 0 | #else |
88 | 0 | /* See |
89 | 0 | * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup |
90 | 0 | */ |
91 | 0 | static const unsigned char mod37[] = |
92 | 0 | { |
93 | 0 | sizeof (unsigned int)*8, |
94 | 0 | 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, |
95 | 0 | 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, |
96 | 0 | 5, 20, 8, 19, 18 |
97 | 0 | }; |
98 | 0 | return (int)mod37[(-x & x) % 37]; |
99 | 0 | #endif |
100 | 0 | } Unexecuted instantiation: random-drbg.c:_gcry_ctz Unexecuted instantiation: rndjent.c:_gcry_ctz |
101 | | |
102 | | |
103 | | /* Count trailing zero bits in an u64. We return an int because that |
104 | | is what gcc's builtin does. Returns the number of bits in X if X |
105 | | is 0. */ |
106 | | static inline int |
107 | | _gcry_ctz64(u64 x) |
108 | 0 | { |
109 | 0 | #if defined (HAVE_BUILTIN_CTZL) && SIZEOF_UNSIGNED_LONG >= 8 |
110 | 0 | return x ? __builtin_ctzl (x) : 8 * sizeof (x); |
111 | 0 | #elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8 |
112 | 0 | #warning hello |
113 | 0 | return x ? __builtin_ctz (x) : 8 * sizeof (x); |
114 | 0 | #else |
115 | 0 | if ((x & 0xffffffff)) |
116 | 0 | return _gcry_ctz (x); |
117 | 0 | else |
118 | 0 | return 32 + _gcry_ctz (x >> 32); |
119 | 0 | #endif |
120 | 0 | } Unexecuted instantiation: random-drbg.c:_gcry_ctz64 Unexecuted instantiation: rndjent.c:_gcry_ctz64 |
121 | | |
122 | | |
123 | | #endif /*GCRYPT_BITHELP_H*/ |