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