File: bithelp.h

package info (click to toggle)
libgcrypt20 1.11.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,224 kB
  • sloc: ansic: 181,767; asm: 57,270; sh: 13,057; makefile: 880; sed: 37
file content (146 lines) | stat: -rw-r--r-- 4,186 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* bithelp.h  -  Some bit manipulation helpers
 *	Copyright (C) 1999, 2002 Free Software Foundation, Inc.
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
#ifndef GCRYPT_BITHELP_H
#define GCRYPT_BITHELP_H

#include "config.h"
#include "types.h"


/****************
 * Rotate the 32 bit unsigned integer X by N bits left/right
 */
static inline u32 rol(u32 x, int n)
{
	return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) );
}

static inline u32 ror(u32 x, int n)
{
	return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
}

static inline u64 rol64(u64 x, int n)
{
  return ( (x << (n&(64-1))) | (x >> ((64-n)&(64-1))) );
}

/* Byte swap for 32-bit and 64-bit integers.  If available, use compiler
   provided helpers.  */
#ifdef HAVE_BUILTIN_BSWAP32
# define _gcry_bswap32 __builtin_bswap32
#else
static inline u32
_gcry_bswap32(u32 x)
{
	return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
}
#endif

#ifdef HAVE_BUILTIN_BSWAP64
# define _gcry_bswap64 __builtin_bswap64
#else
static inline u64
_gcry_bswap64(u64 x)
{
	return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
}
#endif

/* Endian dependent byte swap operations.  */
#ifdef WORDS_BIGENDIAN
# define le_bswap32(x) _gcry_bswap32(x)
# define be_bswap32(x) ((u32)(x))
# define le_bswap64(x) _gcry_bswap64(x)
# define be_bswap64(x) ((u64)(x))
#else
# define le_bswap32(x) ((u32)(x))
# define be_bswap32(x) _gcry_bswap32(x)
# define le_bswap64(x) ((u64)(x))
# define be_bswap64(x) _gcry_bswap64(x)
#endif


/* Count trailing zero bits in an unsigend int.  We return an int
   because that is what gcc's builtin does.  X must not be zero. */
static inline int
_gcry_ctz_no_zero (unsigned int x)
{
#if defined(__riscv) && \
    (defined(__riscv_f) && __riscv_f >= 2002000) && \
    (!defined(__riscv_zbb) || __riscv_zbb < 2002000) && \
    defined(HAVE_GCC_ATTRIBUTE_MAY_ALIAS)
  /* Use float cast approach when building for RISC-V without Zbb extension.
   * Without Zbb, GCC gives us slower generic version for __builtin_ctz().
   *
   * See:
   * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast
   */
  float f = (float)(x & -x);
  typedef u32 __attribute__((may_alias)) may_alias_u32;
  return ((*(const may_alias_u32 *)&f) >> 23) - 0x7f;
#elif defined (HAVE_BUILTIN_CTZ)
  return __builtin_ctz (x);
#else
  /* See
   * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
   */
  static const unsigned char mod37[] =
    {
      sizeof (unsigned int)*8,
          0,  1, 26,  2, 23, 27,  0,  3, 16, 24, 30, 28, 11,  0, 13,
      4,  7, 17,  0, 25, 22, 31, 15, 29, 10, 12,  6,  0, 21, 14,  9,
      5, 20,  8, 19, 18
    };
  return (int)mod37[(-x & x) % 37];
#endif
}


/* Count trailing zero bits in an unsigend int.  We return an int
   because that is what gcc's builtin does.  Returns the number of
   bits in X if X is 0. */
static inline int
_gcry_ctz (unsigned int x)
{
  return x ? _gcry_ctz_no_zero (x) : 8 * sizeof (x);
}


/* Count trailing zero bits in an u64.  We return an int because that
   is what gcc's builtin does.  Returns the number of bits in X if X
   is 0.  */
static inline int
_gcry_ctz64(u64 x)
{
#if defined (HAVE_BUILTIN_CTZL) && SIZEOF_UNSIGNED_LONG >= 8
  return x ? __builtin_ctzl (x) : 8 * sizeof (x);
#elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
#warning hello
  return x ? __builtin_ctz (x) : 8 * sizeof (x);
#else
  if ((x & 0xffffffff))
    return _gcry_ctz (x);
  else
    return 32 + _gcry_ctz (x >> 32);
#endif
}


#endif /*GCRYPT_BITHELP_H*/