1 /* bitrotate.h - Rotate bits in integers 2 Copyright (C) 2008-2011 Free Software Foundation, Inc. 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 3 of the License, or 7 (at your option) any later version. 8 9 This program 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 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <http://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 #ifdef UINT64_MAX 27 /* Given an unsigned 64-bit argument X, return the value corresponding 28 to rotating the bits N steps to the left. N must be between 1 and 29 63 inclusive. */ 30 static inline uint64_t 31 rotl64 (uint64_t x, int n) 32 { 33 return ((x << n) | (x >> (64 - n))) & UINT64_MAX; 34 } 35 36 /* Given an unsigned 64-bit argument X, return the value corresponding 37 to rotating the bits N steps to the right. N must be between 1 to 38 63 inclusive.*/ 39 static inline uint64_t 40 rotr64 (uint64_t x, int n) 41 { 42 return ((x >> n) | (x << (64 - n))) & UINT64_MAX; 43 } 44 #endif 45 46 /* Given an unsigned 32-bit argument X, return the value corresponding 47 to rotating the bits N steps to the left. N must be between 1 and 48 31 inclusive. */ 49 static inline uint32_t 50 rotl32 (uint32_t x, int n) 51 { 52 return ((x << n) | (x >> (32 - n))) & UINT32_MAX; 53 } 54 55 /* Given an unsigned 32-bit argument X, return the value corresponding 56 to rotating the bits N steps to the right. N must be between 1 to 57 31 inclusive.*/ 58 static inline uint32_t 59 rotr32 (uint32_t x, int n) 60 { 61 return ((x >> n) | (x << (32 - n))) & UINT32_MAX; 62 } 63 64 /* Given a size_t argument X, return the value corresponding 65 to rotating the bits N steps to the left. N must be between 1 and 66 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */ 67 static inline size_t 68 rotl_sz (size_t x, int n) 69 { 70 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX; 71 } 72 73 /* Given a size_t argument X, return the value corresponding 74 to rotating the bits N steps to the right. N must be between 1 to 75 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */ 76 static inline size_t 77 rotr_sz (size_t x, int n) 78 { 79 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX; 80 } 81 82 /* Given an unsigned 16-bit argument X, return the value corresponding 83 to rotating the bits N steps to the left. N must be between 1 to 84 15 inclusive, but on most relevant targets N can also be 0 and 16 85 because 'int' is at least 32 bits and the arguments must widen 86 before shifting. */ 87 static inline uint16_t 88 rotl16 (uint16_t x, int n) 89 { 90 return ((x << n) | (x >> (16 - n))) & UINT16_MAX; 91 } 92 93 /* Given an unsigned 16-bit argument X, return the value corresponding 94 to rotating the bits N steps to the right. N must be in 1 to 15 95 inclusive, but on most relevant targets N can also be 0 and 16 96 because 'int' is at least 32 bits and the arguments must widen 97 before shifting. */ 98 static inline uint16_t 99 rotr16 (uint16_t x, int n) 100 { 101 return ((x >> n) | (x << (16 - n))) & UINT16_MAX; 102 } 103 104 /* Given an unsigned 8-bit argument X, return the value corresponding 105 to rotating the bits N steps to the left. N must be between 1 to 7 106 inclusive, but on most relevant targets N can also be 0 and 8 107 because 'int' is at least 32 bits and the arguments must widen 108 before shifting. */ 109 static inline uint8_t 110 rotl8 (uint8_t x, int n) 111 { 112 return ((x << n) | (x >> (8 - n))) & UINT8_MAX; 113 } 114 115 /* Given an unsigned 8-bit argument X, return the value corresponding 116 to rotating the bits N steps to the right. N must be in 1 to 7 117 inclusive, but on most relevant targets N can also be 0 and 8 118 because 'int' is at least 32 bits and the arguments must widen 119 before shifting. */ 120 static inline uint8_t 121 rotr8 (uint8_t x, int n) 122 { 123 return ((x >> n) | (x << (8 - n))) & UINT8_MAX; 124 } 125 126 #endif /* _GL_BITROTATE_H */ 127