LCOV - code coverage report
Current view: top level - cipher - bithelp.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 10 90.0 %
Date: 2017-03-02 16:44:37 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* bithelp.h  -  Some bit manipulation helpers
       2             :  *      Copyright (C) 1999, 2002 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       6             :  * Libgcrypt is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * Libgcrypt is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : #ifndef GCRYPT_BITHELP_H
      20             : #define GCRYPT_BITHELP_H
      21             : 
      22             : #include "types.h"
      23             : 
      24             : 
      25             : /****************
      26             :  * Rotate the 32 bit unsigned integer X by N bits left/right
      27             :  */
      28  1139338356 : static inline u32 rol(u32 x, int n)
      29             : {
      30  1139338356 :         return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) );
      31             : }
      32             : 
      33   462999992 : static inline u32 ror(u32 x, int n)
      34             : {
      35   462999992 :         return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
      36             : }
      37             : 
      38             : /* Byte swap for 32-bit and 64-bit integers.  If available, use compiler
      39             :    provided helpers.  */
      40             : #ifdef HAVE_BUILTIN_BSWAP32
      41             : # define _gcry_bswap32 __builtin_bswap32
      42             : #else
      43             : static inline u32
      44             : _gcry_bswap32(u32 x)
      45             : {
      46             :         return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
      47             : }
      48             : #endif
      49             : 
      50             : #ifdef HAVE_BUILTIN_BSWAP64
      51             : # define _gcry_bswap64 __builtin_bswap64
      52             : #else
      53             : static inline u64
      54             : _gcry_bswap64(u64 x)
      55             : {
      56             :         return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
      57             : }
      58             : #endif
      59             : 
      60             : /* Endian dependent byte swap operations.  */
      61             : #ifdef WORDS_BIGENDIAN
      62             : # define le_bswap32(x) _gcry_bswap32(x)
      63             : # define be_bswap32(x) ((u32)(x))
      64             : # define le_bswap64(x) _gcry_bswap64(x)
      65             : # define be_bswap64(x) ((u64)(x))
      66             : #else
      67             : # define le_bswap32(x) ((u32)(x))
      68             : # define be_bswap32(x) _gcry_bswap32(x)
      69             : # define le_bswap64(x) ((u64)(x))
      70             : # define be_bswap64(x) _gcry_bswap64(x)
      71             : #endif
      72             : 
      73             : 
      74             : /* Count trailing zero bits in an unsigend int.  We return an int
      75             :    because that is what gcc's builtin does.  Returns the number of
      76             :    bits in X if X is 0. */
      77             : static inline int
      78         960 : _gcry_ctz (unsigned int x)
      79             : {
      80             : #if defined (HAVE_BUILTIN_CTZ)
      81         960 :   return x? __builtin_ctz (x) : 8 * sizeof (x);
      82             : #else
      83             :   /* See
      84             :    * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
      85             :    */
      86             :   static const unsigned char mod37[] =
      87             :     {
      88             :       sizeof (unsigned int)*8,
      89             :           0,  1, 26,  2, 23, 27,  0,  3, 16, 24, 30, 28, 11,  0, 13,
      90             :       4,  7, 17,  0, 25, 22, 31, 15, 29, 10, 12,  6,  0, 21, 14,  9,
      91             :       5, 20,  8, 19, 18
      92             :     };
      93             :   return (int)mod37[(-x & x) % 37];
      94             : #endif
      95             : }
      96             : 
      97             : 
      98             : /* Count trailing zero bits in an u64.  We return an int because that
      99             :    is what gcc's builtin does.  Returns the number of bits in X if X
     100             :    is 0.  */
     101             : static inline int
     102         960 : _gcry_ctz64(u64 x)
     103             : {
     104             : #if defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
     105             : #warning hello
     106             :   return x? __builtin_ctz (x) : 8 * sizeof (x);
     107             : #else
     108         960 :   if ((x & 0xffffffff))
     109         960 :     return _gcry_ctz (x);
     110             :   else
     111           0 :     return 32 + _gcry_ctz (x >> 32);
     112             : #endif
     113             : }
     114             : 
     115             : 
     116             : #endif /*GCRYPT_BITHELP_H*/

Generated by: LCOV version 1.13