bitrotate.h 4.3 KB

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