root/lib/count-one-bits.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. count_one_bits_32
  2. __popcnt64
  3. popcount_supported
  4. count_one_bits
  5. count_one_bits_l
  6. count_one_bits_ll

     1 /* count-one-bits.h -- counts the number of 1-bits in a word.
     2    Copyright (C) 2007-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 Ben Pfaff.  */
    18 
    19 #ifndef COUNT_ONE_BITS_H
    20 #define COUNT_ONE_BITS_H 1
    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 <stdlib.h>
    29 
    30 _GL_INLINE_HEADER_BEGIN
    31 #ifndef COUNT_ONE_BITS_INLINE
    32 # define COUNT_ONE_BITS_INLINE _GL_INLINE
    33 #endif
    34 
    35 #ifdef __cplusplus
    36 extern "C" {
    37 #endif
    38 
    39 /* Assuming the GCC builtin is GCC_BUILTIN and the MSC builtin is MSC_BUILTIN,
    40    expand to code that computes the number of 1-bits of the local
    41    variable 'x' of type TYPE (an unsigned integer type) and return it
    42    from the current function.  */
    43 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \
    44     || (__clang_major__ >= 4)
    45 # define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
    46     return GCC_BUILTIN (x)
    47 #else
    48 
    49 /* Compute and return the number of 1-bits set in the least
    50    significant 32 bits of X. */
    51 COUNT_ONE_BITS_INLINE int
    52 count_one_bits_32 (unsigned int x)
    53 {
    54   x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U);
    55   x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U);
    56   x = (x >> 16) + (x & 0xffff);
    57   x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f);
    58   return (x >> 8) + (x & 0x00ff);
    59 }
    60 
    61 /* Expand to code that computes the number of 1-bits of the local
    62    variable 'x' of type TYPE (an unsigned integer type) and return it
    63    from the current function.  */
    64 # define COUNT_ONE_BITS_GENERIC(TYPE)                                   \
    65     do                                                                  \
    66       {                                                                 \
    67         int count = 0;                                                  \
    68         int bits;                                                       \
    69         for (bits = 0; bits < sizeof (TYPE) * CHAR_BIT; bits += 32)     \
    70           {                                                             \
    71             count += count_one_bits_32 (x);                             \
    72             x = x >> 31 >> 1;                                           \
    73           }                                                             \
    74         return count;                                                   \
    75       }                                                                 \
    76     while (0)
    77 
    78 # if 1500 <= _MSC_VER && (defined _M_IX86 || defined _M_X64)
    79 
    80 /* While gcc falls back to its own generic code if the machine
    81    on which it's running doesn't support popcount, with Microsoft's
    82    compiler we need to detect and fallback ourselves.  */
    83 
    84 #  if 0
    85 #   include <intrin.h>
    86 #  else
    87     /* Don't pollute the namespace with too many MSVC intrinsics.  */
    88 #   pragma intrinsic (__cpuid)
    89 #   pragma intrinsic (__popcnt)
    90 #   if defined _M_X64
    91 #    pragma intrinsic (__popcnt64)
    92 #   endif
    93 #  endif
    94 
    95 #  if !defined _M_X64
    96 static inline __popcnt64 (unsigned long long x)
    97 {
    98   return __popcnt ((unsigned int) (x >> 32)) + __popcnt ((unsigned int) x);
    99 }
   100 #  endif
   101 
   102 /* Return nonzero if popcount is supported.  */
   103 
   104 /* 1 if supported, 0 if not supported, -1 if unknown.  */
   105 extern int popcount_support;
   106 
   107 COUNT_ONE_BITS_INLINE int
   108 popcount_supported (void)
   109 {
   110   if (popcount_support < 0)
   111     {
   112       /* Do as described in
   113          <https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64> */
   114       int cpu_info[4];
   115       __cpuid (cpu_info, 1);
   116       popcount_support = (cpu_info[2] >> 23) & 1;
   117     }
   118   return popcount_support;
   119 }
   120 
   121 #  define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
   122      do                                                  \
   123        {                                                 \
   124          if (popcount_supported ())                      \
   125            return MSC_BUILTIN (x);                       \
   126          else                                            \
   127            COUNT_ONE_BITS_GENERIC (TYPE);                \
   128        }                                                 \
   129      while (0)
   130 
   131 # else
   132 
   133 #  define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
   134      COUNT_ONE_BITS_GENERIC (TYPE)
   135 
   136 # endif
   137 #endif
   138 
   139 /* Compute and return the number of 1-bits set in X. */
   140 COUNT_ONE_BITS_INLINE int
   141 count_one_bits (unsigned int x)
   142 {
   143   COUNT_ONE_BITS (__builtin_popcount, __popcnt, unsigned int);
   144 }
   145 
   146 /* Compute and return the number of 1-bits set in X. */
   147 COUNT_ONE_BITS_INLINE int
   148 count_one_bits_l (unsigned long int x)
   149 {
   150   COUNT_ONE_BITS (__builtin_popcountl, __popcnt, unsigned long int);
   151 }
   152 
   153 /* Compute and return the number of 1-bits set in X. */
   154 COUNT_ONE_BITS_INLINE int
   155 count_one_bits_ll (unsigned long long int x)
   156 {
   157   COUNT_ONE_BITS (__builtin_popcountll, __popcnt64, unsigned long long int);
   158 }
   159 
   160 #ifdef __cplusplus
   161 }
   162 #endif
   163 
   164 _GL_INLINE_HEADER_END
   165 
   166 #endif /* COUNT_ONE_BITS_H */

/* [<][>][^][v][top][bottom][index][help] */