(root)/
tar-1.35/
gnu/
bitrotate.h
       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  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
      23  #if !_GL_CONFIG_H_INCLUDED
      24   #error "Please include config.h first."
      25  #endif
      26  
      27  #include <limits.h>
      28  #include <stdint.h>
      29  #include <sys/types.h>
      30  
      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 (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
     101           & UINT16_MAX;
     102  }
     103  
     104  /* Given an unsigned 16-bit argument X, return the value corresponding
     105     to rotating the bits N steps to the right.  N must be in 1 to 15
     106     inclusive, but on most relevant targets N can also be 0 and 16
     107     because 'int' is at least 32 bits and the arguments must widen
     108     before shifting. */
     109  BITROTATE_INLINE uint16_t
     110  rotr16 (uint16_t x, int n)
     111  {
     112    return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
     113           & UINT16_MAX;
     114  }
     115  
     116  /* Given an unsigned 8-bit argument X, return the value corresponding
     117     to rotating the bits N steps to the left.  N must be between 1 to 7
     118     inclusive, but on most relevant targets N can also be 0 and 8
     119     because 'int' is at least 32 bits and the arguments must widen
     120     before shifting. */
     121  BITROTATE_INLINE uint8_t
     122  rotl8 (uint8_t x, int n)
     123  {
     124    return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
     125  }
     126  
     127  /* Given an unsigned 8-bit argument X, return the value corresponding
     128     to rotating the bits N steps to the right.  N must be in 1 to 7
     129     inclusive, but on most relevant targets N can also be 0 and 8
     130     because 'int' is at least 32 bits and the arguments must widen
     131     before shifting. */
     132  BITROTATE_INLINE uint8_t
     133  rotr8 (uint8_t x, int n)
     134  {
     135    return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
     136  }
     137  
     138  _GL_INLINE_HEADER_END
     139  
     140  #endif /* _GL_BITROTATE_H */