exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / bitrotate.h
blob91744cc5f98dc314fd6f273676b4fd8aa37c16d6
1 /* bitrotate.h - Rotate bits in integers
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
19 #ifndef _GL_BITROTATE_H
20 #define _GL_BITROTATE_H
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
27 #include <limits.h>
28 #include <stdint.h>
29 #include <sys/types.h>
31 _GL_INLINE_HEADER_BEGIN
32 #ifndef BITROTATE_INLINE
33 # define BITROTATE_INLINE _GL_INLINE
34 #endif
36 #ifdef UINT64_MAX
37 /* Given an unsigned 64-bit argument X, return the value corresponding
38 to rotating the bits N steps to the left. N must be between 1 and
39 63 inclusive. */
40 BITROTATE_INLINE uint64_t
41 rotl64 (uint64_t x, int n)
43 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
46 /* Given an unsigned 64-bit argument X, return the value corresponding
47 to rotating the bits N steps to the right. N must be between 1 to
48 63 inclusive.*/
49 BITROTATE_INLINE uint64_t
50 rotr64 (uint64_t x, int n)
52 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
54 #endif
56 /* Given an unsigned 32-bit argument X, return the value corresponding
57 to rotating the bits N steps to the left. N must be between 1 and
58 31 inclusive. */
59 BITROTATE_INLINE uint32_t
60 rotl32 (uint32_t x, int n)
62 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
65 /* Given an unsigned 32-bit argument X, return the value corresponding
66 to rotating the bits N steps to the right. N must be between 1 to
67 31 inclusive.*/
68 BITROTATE_INLINE uint32_t
69 rotr32 (uint32_t x, int n)
71 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
74 /* Given a size_t argument X, return the value corresponding
75 to rotating the bits N steps to the left. N must be between 1 and
76 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
77 BITROTATE_INLINE size_t
78 rotl_sz (size_t x, int n)
80 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
83 /* Given a size_t argument X, return the value corresponding
84 to rotating the bits N steps to the right. N must be between 1 to
85 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
86 BITROTATE_INLINE size_t
87 rotr_sz (size_t x, int n)
89 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
92 /* Given an unsigned 16-bit argument X, return the value corresponding
93 to rotating the bits N steps to the left. N must be between 1 to
94 15 inclusive, but on most relevant targets N can also be 0 and 16
95 because 'int' is at least 32 bits and the arguments must widen
96 before shifting. */
97 BITROTATE_INLINE uint16_t
98 rotl16 (uint16_t x, int n)
100 return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
101 & UINT16_MAX;
104 /* Given an unsigned 16-bit argument X, return the value corresponding
105 to rotating the bits N steps to the right. N must be in 1 to 15
106 inclusive, but on most relevant targets N can also be 0 and 16
107 because 'int' is at least 32 bits and the arguments must widen
108 before shifting. */
109 BITROTATE_INLINE uint16_t
110 rotr16 (uint16_t x, int n)
112 return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
113 & UINT16_MAX;
116 /* Given an unsigned 8-bit argument X, return the value corresponding
117 to rotating the bits N steps to the left. N must be between 1 to 7
118 inclusive, but on most relevant targets N can also be 0 and 8
119 because 'int' is at least 32 bits and the arguments must widen
120 before shifting. */
121 BITROTATE_INLINE uint8_t
122 rotl8 (uint8_t x, int n)
124 return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
127 /* Given an unsigned 8-bit argument X, return the value corresponding
128 to rotating the bits N steps to the right. N must be in 1 to 7
129 inclusive, but on most relevant targets N can also be 0 and 8
130 because 'int' is at least 32 bits and the arguments must widen
131 before shifting. */
132 BITROTATE_INLINE uint8_t
133 rotr8 (uint8_t x, int n)
135 return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
138 _GL_INLINE_HEADER_END
140 #endif /* _GL_BITROTATE_H */