/src/gnutls/gl/bitrotate.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* bitrotate.h - Rotate bits in integers |
2 | | Copyright (C) 2008-2023 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 | | #include <limits.h> |
23 | | #include <stdint.h> |
24 | | #include <sys/types.h> |
25 | | |
26 | | #ifndef _GL_INLINE_HEADER_BEGIN |
27 | | #error "Please include config.h first." |
28 | | #endif |
29 | | _GL_INLINE_HEADER_BEGIN |
30 | | #ifndef BITROTATE_INLINE |
31 | | # define BITROTATE_INLINE _GL_INLINE |
32 | | #endif |
33 | | |
34 | | #ifdef UINT64_MAX |
35 | | /* Given an unsigned 64-bit argument X, return the value corresponding |
36 | | to rotating the bits N steps to the left. N must be between 1 and |
37 | | 63 inclusive. */ |
38 | | BITROTATE_INLINE uint64_t |
39 | | rotl64 (uint64_t x, int n) |
40 | 0 | { |
41 | 0 | return ((x << n) | (x >> (64 - n))) & UINT64_MAX; |
42 | 0 | } |
43 | | |
44 | | /* Given an unsigned 64-bit argument X, return the value corresponding |
45 | | to rotating the bits N steps to the right. N must be between 1 to |
46 | | 63 inclusive.*/ |
47 | | BITROTATE_INLINE uint64_t |
48 | | rotr64 (uint64_t x, int n) |
49 | 0 | { |
50 | 0 | return ((x >> n) | (x << (64 - n))) & UINT64_MAX; |
51 | 0 | } |
52 | | #endif |
53 | | |
54 | | /* Given an unsigned 32-bit argument X, return the value corresponding |
55 | | to rotating the bits N steps to the left. N must be between 1 and |
56 | | 31 inclusive. */ |
57 | | BITROTATE_INLINE uint32_t |
58 | | rotl32 (uint32_t x, int n) |
59 | 0 | { |
60 | 0 | return ((x << n) | (x >> (32 - n))) & UINT32_MAX; |
61 | 0 | } |
62 | | |
63 | | /* Given an unsigned 32-bit argument X, return the value corresponding |
64 | | to rotating the bits N steps to the right. N must be between 1 to |
65 | | 31 inclusive.*/ |
66 | | BITROTATE_INLINE uint32_t |
67 | | rotr32 (uint32_t x, int n) |
68 | 0 | { |
69 | 0 | return ((x >> n) | (x << (32 - n))) & UINT32_MAX; |
70 | 0 | } |
71 | | |
72 | | /* Given a size_t argument X, return the value corresponding |
73 | | to rotating the bits N steps to the left. N must be between 1 and |
74 | | (CHAR_BIT * sizeof (size_t) - 1) inclusive. */ |
75 | | BITROTATE_INLINE size_t |
76 | | rotl_sz (size_t x, int n) |
77 | 0 | { |
78 | 0 | return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX; |
79 | 0 | } |
80 | | |
81 | | /* Given a size_t argument X, return the value corresponding |
82 | | to rotating the bits N steps to the right. N must be between 1 to |
83 | | (CHAR_BIT * sizeof (size_t) - 1) inclusive. */ |
84 | | BITROTATE_INLINE size_t |
85 | | rotr_sz (size_t x, int n) |
86 | 0 | { |
87 | 0 | return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX; |
88 | 0 | } |
89 | | |
90 | | /* Given an unsigned 16-bit argument X, return the value corresponding |
91 | | to rotating the bits N steps to the left. N must be between 1 to |
92 | | 15 inclusive, but on most relevant targets N can also be 0 and 16 |
93 | | because 'int' is at least 32 bits and the arguments must widen |
94 | | before shifting. */ |
95 | | BITROTATE_INLINE uint16_t |
96 | | rotl16 (uint16_t x, int n) |
97 | 0 | { |
98 | 0 | return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n))) |
99 | 0 | & UINT16_MAX; |
100 | 0 | } |
101 | | |
102 | | /* Given an unsigned 16-bit argument X, return the value corresponding |
103 | | to rotating the bits N steps to the right. N must be in 1 to 15 |
104 | | inclusive, but on most relevant targets N can also be 0 and 16 |
105 | | because 'int' is at least 32 bits and the arguments must widen |
106 | | before shifting. */ |
107 | | BITROTATE_INLINE uint16_t |
108 | | rotr16 (uint16_t x, int n) |
109 | 0 | { |
110 | 0 | return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n))) |
111 | 0 | & UINT16_MAX; |
112 | 0 | } |
113 | | |
114 | | /* Given an unsigned 8-bit argument X, return the value corresponding |
115 | | to rotating the bits N steps to the left. N must be between 1 to 7 |
116 | | inclusive, but on most relevant targets N can also be 0 and 8 |
117 | | because 'int' is at least 32 bits and the arguments must widen |
118 | | before shifting. */ |
119 | | BITROTATE_INLINE uint8_t |
120 | | rotl8 (uint8_t x, int n) |
121 | 0 | { |
122 | 0 | return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX; |
123 | 0 | } |
124 | | |
125 | | /* Given an unsigned 8-bit argument X, return the value corresponding |
126 | | to rotating the bits N steps to the right. N must be in 1 to 7 |
127 | | inclusive, but on most relevant targets N can also be 0 and 8 |
128 | | because 'int' is at least 32 bits and the arguments must widen |
129 | | before shifting. */ |
130 | | BITROTATE_INLINE uint8_t |
131 | | rotr8 (uint8_t x, int n) |
132 | 0 | { |
133 | 0 | return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX; |
134 | 0 | } |
135 | | |
136 | | _GL_INLINE_HEADER_END |
137 | | |
138 | | #endif /* _GL_BITROTATE_H */ |