Coverage Report

Created: 2025-03-18 06:55

/src/gnutls/gl/bitrotate.h
Line
Count
Source (jump to first uncovered line)
1
/* bitrotate.h - Rotate bits in integers
2
   Copyright (C) 2008-2025 Free Software Foundation, Inc.
3
4
   This file is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU Lesser General Public License as
6
   published by the Free Software Foundation; either version 2.1 of the
7
   License, or (at your option) any later version.
8
9
   This file is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU Lesser General Public License for more details.
13
14
   You should have received a copy of the GNU Lesser General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
18
19
#ifndef _GL_BITROTATE_H
20
#define _GL_BITROTATE_H
21
22
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
23
#if !_GL_CONFIG_H_INCLUDED
24
 #error "Please include config.h first."
25
#endif
26
27
#include <limits.h>
28
#include <stdint.h>
29
#include <sys/types.h>
30
31
_GL_INLINE_HEADER_BEGIN
32
#ifndef BITROTATE_INLINE
33
# define BITROTATE_INLINE _GL_INLINE
34
#endif
35
36
#ifdef __cplusplus
37
extern "C" {
38
#endif
39
40
41
#ifdef UINT64_MAX
42
/* Given an unsigned 64-bit argument X, return the value corresponding
43
   to rotating the bits N steps to the left.  N must be between 1 and
44
   63 inclusive. */
45
BITROTATE_INLINE uint64_t
46
rotl64 (uint64_t x, int n)
47
0
{
48
0
  return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
49
0
}
50
51
/* Given an unsigned 64-bit argument X, return the value corresponding
52
   to rotating the bits N steps to the right.  N must be between 1 to
53
   63 inclusive.*/
54
BITROTATE_INLINE uint64_t
55
rotr64 (uint64_t x, int n)
56
0
{
57
0
  return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
58
0
}
59
#endif
60
61
/* Given an unsigned 32-bit argument X, return the value corresponding
62
   to rotating the bits N steps to the left.  N must be between 1 and
63
   31 inclusive. */
64
BITROTATE_INLINE uint32_t
65
rotl32 (uint32_t x, int n)
66
0
{
67
0
  return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
68
0
}
69
70
/* Given an unsigned 32-bit argument X, return the value corresponding
71
   to rotating the bits N steps to the right.  N must be between 1 to
72
   31 inclusive.*/
73
BITROTATE_INLINE uint32_t
74
rotr32 (uint32_t x, int n)
75
0
{
76
0
  return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
77
0
}
78
79
/* Given a size_t argument X, return the value corresponding
80
   to rotating the bits N steps to the left.  N must be between 1 and
81
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
82
BITROTATE_INLINE size_t
83
rotl_sz (size_t x, int n)
84
0
{
85
0
  return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
86
0
}
87
88
/* Given a size_t argument X, return the value corresponding
89
   to rotating the bits N steps to the right.  N must be between 1 to
90
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
91
BITROTATE_INLINE size_t
92
rotr_sz (size_t x, int n)
93
0
{
94
0
  return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
95
0
}
96
97
/* Given an unsigned 16-bit argument X, return the value corresponding
98
   to rotating the bits N steps to the left.  N must be between 1 to
99
   15 inclusive, but on most relevant targets N can also be 0 and 16
100
   because 'int' is at least 32 bits and the arguments must widen
101
   before shifting. */
102
BITROTATE_INLINE uint16_t
103
rotl16 (uint16_t x, int n)
104
0
{
105
0
  return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
106
0
         & UINT16_MAX;
107
0
}
108
109
/* Given an unsigned 16-bit argument X, return the value corresponding
110
   to rotating the bits N steps to the right.  N must be in 1 to 15
111
   inclusive, but on most relevant targets N can also be 0 and 16
112
   because 'int' is at least 32 bits and the arguments must widen
113
   before shifting. */
114
BITROTATE_INLINE uint16_t
115
rotr16 (uint16_t x, int n)
116
0
{
117
0
  return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
118
0
         & UINT16_MAX;
119
0
}
120
121
/* Given an unsigned 8-bit argument X, return the value corresponding
122
   to rotating the bits N steps to the left.  N must be between 1 to 7
123
   inclusive, but on most relevant targets N can also be 0 and 8
124
   because 'int' is at least 32 bits and the arguments must widen
125
   before shifting. */
126
BITROTATE_INLINE uint8_t
127
rotl8 (uint8_t x, int n)
128
0
{
129
0
  return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
130
0
}
131
132
/* Given an unsigned 8-bit argument X, return the value corresponding
133
   to rotating the bits N steps to the right.  N must be in 1 to 7
134
   inclusive, but on most relevant targets N can also be 0 and 8
135
   because 'int' is at least 32 bits and the arguments must widen
136
   before shifting. */
137
BITROTATE_INLINE uint8_t
138
rotr8 (uint8_t x, int n)
139
0
{
140
0
  return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
141
0
}
142
143
144
#ifdef __cplusplus
145
}
146
#endif
147
148
_GL_INLINE_HEADER_END
149
150
#endif /* _GL_BITROTATE_H */