Import upstream version 1.28
[debian/tar] / gnu / bitrotate.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* bitrotate.h - Rotate bits in integers
4    Copyright (C) 2008-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program 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 General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
20
21 #ifndef _GL_BITROTATE_H
22 #define _GL_BITROTATE_H
23
24 #include <limits.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27
28 #ifndef _GL_INLINE_HEADER_BEGIN
29  #error "Please include config.h first."
30 #endif
31 _GL_INLINE_HEADER_BEGIN
32 #ifndef BITROTATE_INLINE
33 # define BITROTATE_INLINE _GL_INLINE
34 #endif
35
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)
42 {
43   return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
44 }
45
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)
51 {
52   return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
53 }
54 #endif
55
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)
61 {
62   return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
63 }
64
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)
70 {
71   return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
72 }
73
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)
79 {
80   return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
81 }
82
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)
88 {
89   return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
90 }
91
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)
99 {
100   return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
101 }
102
103 /* Given an unsigned 16-bit argument X, return the value corresponding
104    to rotating the bits N steps to the right.  N must be in 1 to 15
105    inclusive, but on most relevant targets N can also be 0 and 16
106    because 'int' is at least 32 bits and the arguments must widen
107    before shifting. */
108 BITROTATE_INLINE uint16_t
109 rotr16 (uint16_t x, int n)
110 {
111   return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
112 }
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 {
122   return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
123 }
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 {
133   return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
134 }
135
136 _GL_INLINE_HEADER_END
137
138 #endif /* _GL_BITROTATE_H */