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