Blame gl/bitrotate.h

Packit Service 4684c1
/* bitrotate.h - Rotate bits in integers
Packit Service 4684c1
   Copyright (C) 2008-2020 Free Software Foundation, Inc.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software: you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU Lesser General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 2.1 of the License, or
Packit Service 4684c1
   (at your option) any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU Lesser General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
/* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
Packit Service 4684c1
Packit Service 4684c1
#ifndef _GL_BITROTATE_H
Packit Service 4684c1
#define _GL_BITROTATE_H
Packit Service 4684c1
Packit Service 4684c1
#include <limits.h>
Packit Service 4684c1
#include <stdint.h>
Packit Service 4684c1
#include <sys/types.h>
Packit Service 4684c1
Packit Service 4684c1
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service 4684c1
 #error "Please include config.h first."
Packit Service 4684c1
#endif
Packit Service 4684c1
_GL_INLINE_HEADER_BEGIN
Packit Service 4684c1
#ifndef BITROTATE_INLINE
Packit Service 4684c1
# define BITROTATE_INLINE _GL_INLINE
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#ifdef UINT64_MAX
Packit Service 4684c1
/* Given an unsigned 64-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the left.  N must be between 1 and
Packit Service 4684c1
   63 inclusive. */
Packit Service 4684c1
BITROTATE_INLINE uint64_t
Packit Service 4684c1
rotl64 (uint64_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 64-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the right.  N must be between 1 to
Packit Service 4684c1
   63 inclusive.*/
Packit Service 4684c1
BITROTATE_INLINE uint64_t
Packit Service 4684c1
rotr64 (uint64_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 32-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the left.  N must be between 1 and
Packit Service 4684c1
   31 inclusive. */
Packit Service 4684c1
BITROTATE_INLINE uint32_t
Packit Service 4684c1
rotl32 (uint32_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 32-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the right.  N must be between 1 to
Packit Service 4684c1
   31 inclusive.*/
Packit Service 4684c1
BITROTATE_INLINE uint32_t
Packit Service 4684c1
rotr32 (uint32_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given a size_t argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the left.  N must be between 1 and
Packit Service 4684c1
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
Packit Service 4684c1
BITROTATE_INLINE size_t
Packit Service 4684c1
rotl_sz (size_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given a size_t argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the right.  N must be between 1 to
Packit Service 4684c1
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
Packit Service 4684c1
BITROTATE_INLINE size_t
Packit Service 4684c1
rotr_sz (size_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 16-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the left.  N must be between 1 to
Packit Service 4684c1
   15 inclusive, but on most relevant targets N can also be 0 and 16
Packit Service 4684c1
   because 'int' is at least 32 bits and the arguments must widen
Packit Service 4684c1
   before shifting. */
Packit Service 4684c1
BITROTATE_INLINE uint16_t
Packit Service 4684c1
rotl16 (uint16_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
Packit Service 4684c1
         & UINT16_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 16-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the right.  N must be in 1 to 15
Packit Service 4684c1
   inclusive, but on most relevant targets N can also be 0 and 16
Packit Service 4684c1
   because 'int' is at least 32 bits and the arguments must widen
Packit Service 4684c1
   before shifting. */
Packit Service 4684c1
BITROTATE_INLINE uint16_t
Packit Service 4684c1
rotr16 (uint16_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
Packit Service 4684c1
         & UINT16_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 8-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the left.  N must be between 1 to 7
Packit Service 4684c1
   inclusive, but on most relevant targets N can also be 0 and 8
Packit Service 4684c1
   because 'int' is at least 32 bits and the arguments must widen
Packit Service 4684c1
   before shifting. */
Packit Service 4684c1
BITROTATE_INLINE uint8_t
Packit Service 4684c1
rotl8 (uint8_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* Given an unsigned 8-bit argument X, return the value corresponding
Packit Service 4684c1
   to rotating the bits N steps to the right.  N must be in 1 to 7
Packit Service 4684c1
   inclusive, but on most relevant targets N can also be 0 and 8
Packit Service 4684c1
   because 'int' is at least 32 bits and the arguments must widen
Packit Service 4684c1
   before shifting. */
Packit Service 4684c1
BITROTATE_INLINE uint8_t
Packit Service 4684c1
rotr8 (uint8_t x, int n)
Packit Service 4684c1
{
Packit Service 4684c1
  return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
_GL_INLINE_HEADER_END
Packit Service 4684c1
Packit Service 4684c1
#endif /* _GL_BITROTATE_H */