|
| 1 | +#ifndef _STDBIT_H |
| 2 | +#define _STDBIT_H |
| 3 | + |
| 4 | +#include <cdefs.h> |
| 5 | +#include <stdbool.h> |
| 6 | + |
| 7 | +#ifdef __cplusplus |
| 8 | +extern "C" { |
| 9 | +#endif |
| 10 | + |
| 11 | +// #define __STDC_VERSION_STDBIT_H__ 202311L |
| 12 | + |
| 13 | +#define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__ |
| 14 | +#define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__ |
| 15 | +#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__ |
| 16 | + |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +// stdc_leading_zeros |
| 19 | +//------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +// #define stdc_leading_zeros_uc(x) |
| 22 | + |
| 23 | +#define stdc_leading_zeros_us(x) \ |
| 24 | +__builtin_clzs(x) |
| 25 | + |
| 26 | +#define stdc_leading_zeros_ui(x) \ |
| 27 | +__builtin_clz(x) |
| 28 | + |
| 29 | +#define stdc_leading_zeros_ul(x) \ |
| 30 | +__builtin_clzl(x) |
| 31 | + |
| 32 | +// #define stdc_leading_zeros_ui48(x) |
| 33 | + |
| 34 | +#define stdc_leading_zeros_ull(x) \ |
| 35 | +__builtin_clzll(x) |
| 36 | + |
| 37 | +#define stdc_leading_zeros(x) _Generic((x), \ |
| 38 | + /* unsigned char: stdc_leading_zeros_uc, */ \ |
| 39 | + unsigned short: stdc_leading_zeros_us, \ |
| 40 | + unsigned int: stdc_leading_zeros_ui, \ |
| 41 | + unsigned long: stdc_leading_zeros_ul, \ |
| 42 | + /* unsigned __int48: stdc_leading_zeros_ui48, */ \ |
| 43 | + unsigned long long: stdc_leading_zeros_ull \ |
| 44 | +)(x) |
| 45 | + |
| 46 | +//------------------------------------------------------------------------------ |
| 47 | +// stdc_leading_ones |
| 48 | +//------------------------------------------------------------------------------ |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +//------------------------------------------------------------------------------ |
| 53 | +// stdc_trailing_zeros |
| 54 | +//------------------------------------------------------------------------------ |
| 55 | + |
| 56 | +// #define stdc_trailing_zeros_uc(x) |
| 57 | + |
| 58 | +#define stdc_trailing_zeros_us(x) \ |
| 59 | +__builtin_ctzs(x) |
| 60 | + |
| 61 | +#define stdc_trailing_zeros_ui(x) \ |
| 62 | +__builtin_ctz(x) |
| 63 | + |
| 64 | +#define stdc_trailing_zeros_ul(x) \ |
| 65 | +__builtin_ctzl(x) |
| 66 | + |
| 67 | +// #define stdc_trailing_zeros_ui48(x) |
| 68 | + |
| 69 | +#define stdc_trailing_zeros_ull(x) \ |
| 70 | +__builtin_ctzll(x) |
| 71 | + |
| 72 | +#define stdc_trailing_zeros(x) _Generic((x), \ |
| 73 | + /* unsigned char: stdc_trailing_zeros_uc, */ \ |
| 74 | + unsigned short: stdc_trailing_zeros_us, \ |
| 75 | + unsigned int: stdc_trailing_zeros_ui, \ |
| 76 | + unsigned long: stdc_trailing_zeros_ul, \ |
| 77 | + /* unsigned __int48: stdc_trailing_zeros_ui48, */ \ |
| 78 | + unsigned long long: stdc_trailing_zeros_ull \ |
| 79 | +)(x) |
| 80 | + |
| 81 | +//------------------------------------------------------------------------------ |
| 82 | +// stdc_trailing_ones |
| 83 | +//------------------------------------------------------------------------------ |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +//------------------------------------------------------------------------------ |
| 88 | +// stdc_first_leading_zero |
| 89 | +//------------------------------------------------------------------------------ |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +//------------------------------------------------------------------------------ |
| 94 | +// stdc_first_trailing_one |
| 95 | +//------------------------------------------------------------------------------ |
| 96 | + |
| 97 | +#define stdc_first_trailing_one_uc(x) \ |
| 98 | +__builtin_ffs((unsigned int)x) /** @todo fix this inefficiency */ |
| 99 | + |
| 100 | +#define stdc_first_trailing_one_us(x) \ |
| 101 | +__builtin_ffs((unsigned int)x) /** @todo fix this inefficiency */ |
| 102 | + |
| 103 | +#define stdc_first_trailing_one_ui(x) \ |
| 104 | +__builtin_ffs(x) |
| 105 | + |
| 106 | +#define stdc_first_trailing_one_ul(x) \ |
| 107 | +__builtin_ffsl(x) |
| 108 | + |
| 109 | +#define stdc_first_trailing_one_ui48(x) \ |
| 110 | +__builtin_ffsll((unsigned long long)x) /** @todo fix this inefficiency */ |
| 111 | + |
| 112 | +#define stdc_first_trailing_one_ull(x) \ |
| 113 | +__builtin_ffsll(x) |
| 114 | + |
| 115 | +#define stdc_first_trailing_one(x) _Generic((x), \ |
| 116 | + unsigned char: stdc_first_trailing_one_uc, \ |
| 117 | + unsigned short: stdc_first_trailing_one_us, \ |
| 118 | + unsigned int: stdc_first_trailing_one_ui, \ |
| 119 | + unsigned long: stdc_first_trailing_one_ul, \ |
| 120 | + unsigned __int48: stdc_first_trailing_one_ui48, \ |
| 121 | + unsigned long long: stdc_first_trailing_one_ull \ |
| 122 | +)(x) |
| 123 | + |
| 124 | +//------------------------------------------------------------------------------ |
| 125 | +// stdc_first_trailing_zero |
| 126 | +//------------------------------------------------------------------------------ |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +//------------------------------------------------------------------------------ |
| 131 | +// stdc_count_ones |
| 132 | +//------------------------------------------------------------------------------ |
| 133 | + |
| 134 | +#define stdc_count_ones_uc(x) \ |
| 135 | +__builtin_popcount((unsigned int)x) /** @todo fix this inefficiency */ |
| 136 | +#define stdc_count_ones_us(x) \ |
| 137 | +__builtin_popcount((unsigned int)x) /** @todo fix this inefficiency */ |
| 138 | +#define stdc_count_ones_ui(x) \ |
| 139 | +__builtin_popcount(x) |
| 140 | +#define stdc_count_ones_ul(x) \ |
| 141 | +__builtin_popcountl(x) |
| 142 | +#define stdc_count_ones_ui48(x) \ |
| 143 | +__builtin_popcountll((unsigned long long)x) /** @todo fix this inefficiency */ |
| 144 | +#define stdc_count_ones_ull(x) \ |
| 145 | +__builtin_popcountll(x) |
| 146 | + |
| 147 | +#define stdc_count_ones(x) _Generic((x), \ |
| 148 | + unsigned char: stdc_count_ones_uc, \ |
| 149 | + unsigned short: stdc_count_ones_us, \ |
| 150 | + unsigned int: stdc_count_ones_ui, \ |
| 151 | + unsigned long: stdc_count_ones_ul, \ |
| 152 | + unsigned __int48: stdc_count_ones_ui48, \ |
| 153 | + unsigned long long: stdc_count_ones_ull \ |
| 154 | +)(x) |
| 155 | + |
| 156 | +//------------------------------------------------------------------------------ |
| 157 | +// stdc_count_zeros |
| 158 | +//------------------------------------------------------------------------------ |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +//------------------------------------------------------------------------------ |
| 163 | +// stdc_has_single_bit |
| 164 | +//------------------------------------------------------------------------------ |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +//------------------------------------------------------------------------------ |
| 169 | +// stdc_bit_width |
| 170 | +//------------------------------------------------------------------------------ |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +//------------------------------------------------------------------------------ |
| 175 | +// stdc_bit_floor |
| 176 | +//------------------------------------------------------------------------------ |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +//------------------------------------------------------------------------------ |
| 181 | +// stdc_rotate_left |
| 182 | +//------------------------------------------------------------------------------ |
| 183 | + |
| 184 | +#define stdc_rotate_left_uc(x, s) \ |
| 185 | +__builtin_rotateleft8(x, s) |
| 186 | + |
| 187 | +#define stdc_rotate_left_us(x, s) \ |
| 188 | +__builtin_rotateleft16(x, s) |
| 189 | + |
| 190 | +// #define stdc_rotate_left_ui(x, s) |
| 191 | + |
| 192 | +#define stdc_rotate_left_ul(x, s) \ |
| 193 | +__builtin_rotateleft32(x, s) |
| 194 | + |
| 195 | +// #define stdc_rotate_left_ui48(x, s) |
| 196 | + |
| 197 | +#define stdc_rotate_left_ull(x, s) \ |
| 198 | +__builtin_rotateleft64(x, s) |
| 199 | + |
| 200 | +#define stdc_rotate_left(x, s) _Generic((x), \ |
| 201 | + unsigned char: stdc_rotate_left_uc, \ |
| 202 | + unsigned short: stdc_rotate_left_us, \ |
| 203 | + /* unsigned int: stdc_rotate_left_ui, */ \ |
| 204 | + unsigned long: stdc_rotate_left_ul, \ |
| 205 | + /* unsigned __int48: stdc_rotate_left_ui48, */ \ |
| 206 | + unsigned long long: stdc_rotate_left_ull \ |
| 207 | +)(x, s) |
| 208 | + |
| 209 | +//------------------------------------------------------------------------------ |
| 210 | +// stdc_rotate_right |
| 211 | +//------------------------------------------------------------------------------ |
| 212 | + |
| 213 | +#define stdc_rotate_right_uc(x, s) \ |
| 214 | +__builtin_rotateright8(x, s) |
| 215 | + |
| 216 | +#define stdc_rotate_right_us(x, s) \ |
| 217 | +__builtin_rotateright16(x, s) |
| 218 | + |
| 219 | +// #define stdc_rotate_right_ui(x, s) |
| 220 | + |
| 221 | +#define stdc_rotate_right_ul(x, s) \ |
| 222 | +__builtin_rotateright32(x, s) |
| 223 | + |
| 224 | +// #define stdc_rotate_right_ui48(x, s) |
| 225 | + |
| 226 | +#define stdc_rotate_right_ull(x, s) \ |
| 227 | +__builtin_rotateright64(x, s) |
| 228 | + |
| 229 | +#define stdc_rotate_right(x, s) _Generic((x), \ |
| 230 | + unsigned char: stdc_rotate_right_uc, \ |
| 231 | + unsigned short: stdc_rotate_right_us, \ |
| 232 | + /* unsigned int: stdc_rotate_right_ui, */ \ |
| 233 | + unsigned long: stdc_rotate_right_ul, \ |
| 234 | + /* unsigned __int48: stdc_rotate_right_ui48, */ \ |
| 235 | + unsigned long long: stdc_rotate_right_ull \ |
| 236 | +)(x, s) |
| 237 | + |
| 238 | +#ifdef __cplusplus |
| 239 | +} |
| 240 | +#endif |
| 241 | + |
| 242 | +#endif /* _STDBIT_H */ |
0 commit comments