Вы можете сгенерировать их во время сборки, посчитав биты в соответствующем максимуме без знака.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int popcnt(unsigned long long X) { int pc=0;for(;(X&1);pc++,X>>=1){;} return pc; }
int main()
{
if(0>printf("#define CHAR_BIT %d\n", popcnt(UCHAR_MAX))) return EXIT_FAILURE;
if(0>printf("#define UCHAR_BIT %d\n", popcnt(UCHAR_MAX))) return EXIT_FAILURE;
if(0>printf("#define SCHAR_BIT %d\n", popcnt(UCHAR_MAX))) return EXIT_FAILURE;
if(0>printf("#define SHRT_BIT %d\n", popcnt(USHRT_MAX))) return EXIT_FAILURE;
if(0>printf("#define USHRT_BIT %d\n", popcnt(USHRT_MAX))) return EXIT_FAILURE;
if(0>printf("#define INT_BIT %d\n", popcnt(UINT_MAX))) return EXIT_FAILURE;
if(0>printf("#define UINT_BIT %d\n", popcnt(UINT_MAX))) return EXIT_FAILURE;
if(0>printf("#define LONG_BIT %d\n", popcnt(ULONG_MAX))) return EXIT_FAILURE;
if(0>printf("#define ULONG_BIT %d\n", popcnt(ULONG_MAX))) return EXIT_FAILURE;
if(0>printf("#define LLONG_BIT %d\n", popcnt(ULLONG_MAX))) return EXIT_FAILURE;
if(0>printf("#define ULLONG_BIT %d\n", popcnt(ULLONG_MAX))) return EXIT_FAILURE;
if(0>fclose(stdout)) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
На моей платформе вышесказанное дает мне:
#define CHAR_BIT 8
#define UCHAR_BIT 8
#define SCHAR_BIT 8
#define SHRT_BIT 16
#define USHRT_BIT 16
#define INT_BIT 32
#define UINT_BIT 32
#define LONG_BIT 64
#define ULONG_BIT 64
#define LLONG_BIT 64
#define ULLONG_BIT 64
(Обратите внимание, чтотехнически они не гарантируются равными sizeof(the_type)*CHAR_BIT
, так как стандарт C допускает заполнение битов в нативных типах.)
Если выполнение этого фрагмента кода перед сборкой на любой платформе, которую вы хотите поддерживать, раздражает вас, Я вас понял.
Используя этот небольшой сценарий оболочки, вы можете сгенерировать (один раз для каждой платформы) совершенно не вызывающий UB-вызов, сохраняющий выражение (надеюсь) popcnt
cpp-macro длядо N-битных целых чисел:
#!/bin/sh -eu
gen()
{
Max=$1
local i=0;
prev='(X)'
#continuous popcount at compile time
printf '#define contpopcnt(X) %s\n' "(($prev&1)\\"
i=1; while [ $i -le $Max ]; do
#look 1 bit ahead the left shift never reaches or exceeds
#the width of whatever X's type might be (would be UB)
prev="(($prev&2)?((X)>>$i):0)"
printf '+(%s&1)\\\n' "$prev"
i=$((i+1));done
printf ')\n'
}
gen 256
cat<<EOF
//#define CHAR_BIT contpopcnt(UCHAR_MAX)
#define UCHAR_BIT contpopcnt(UCHAR_MAX)
#define SCHAR_BIT contpopcnt(UCHAR_MAX)
#define SHRT_BIT contpopcnt(USHRT_MAX)
#define USHRT_BIT contpopcnt(USHRT_MAX)
#define INT_BIT contpopcnt(UINT_MAX)
#define UINT_BIT contpopcnt(UINT_MAX)
#define LONG_BIT contpopcnt(ULONG_MAX)
#define ULONG_BIT contpopcnt(ULONG_MAX)
#define LLONG_BIT contpopcnt(ULLONG_MAX)
#define ULLONG_BIT contpopcnt(ULLONG_MAX)
//a little sanity check
#include <limits.h>
#include <stdint.h>
#if CHAR_BIT!=contpopcnt(UCHAR_MAX)
#error "oops"
#endif
EOF
И 256-битный вариант макроса имеет размер только (!) 605 КБ.Таким образом, вам не нужны эти *_BIT
макросы.
Редактировать:
Здесь гораздо более компактный способ: https://stackoverflow.com/a/4589384/1084774.