| 1 | // { dg-do run { target c++26 } } |
| 2 | |
| 3 | #include <stdbit.h> |
| 4 | |
| 5 | #if __STDC_VERSION_STDBIT_H__ != 202311L |
| 6 | # error "__STDC_VERSION_STDBIT_H__ not defined correctly in <stdbit.h>" |
| 7 | #endif |
| 8 | #ifndef __STDC_ENDIAN_BIG__ |
| 9 | # error "__STDC_ENDIAN_BIG__ is not defined in <stdbit.h>" |
| 10 | #endif |
| 11 | #ifndef __STDC_ENDIAN_LITTLE__ |
| 12 | # error "__STDC_ENDIAN_LITTLE__ is not defined in <stdbit.h>" |
| 13 | #endif |
| 14 | #ifndef __STDC_ENDIAN_NATIVE__ |
| 15 | # error "__STDC_ENDIAN_NATIVE__ is not defined in <stdbit.h>" |
| 16 | #endif |
| 17 | |
| 18 | #include <testsuite_hooks.h> |
| 19 | #include <climits> |
| 20 | |
| 21 | void |
| 22 | test_leading_zeros() |
| 23 | { |
| 24 | VERIFY( stdc_leading_zeros_uc(0) == CHAR_BIT ); |
| 25 | VERIFY( stdc_leading_zeros_uc(UCHAR_MAX) == 0 ); |
| 26 | VERIFY( stdc_leading_zeros_uc(CHAR_MAX) == (CHAR_MIN ? 1 : 0) ); |
| 27 | VERIFY( stdc_leading_zeros_uc(UCHAR_MAX >> 3) == 3 ); |
| 28 | VERIFY( stdc_leading_zeros_uc((unsigned char)(UCHAR_MAX << 3)) == 0 ); |
| 29 | |
| 30 | // TODO: replace __X_WIDTH__ with the standard macros once they're in C++26 |
| 31 | VERIFY( stdc_leading_zeros_us(0) == __SHRT_WIDTH__ ); |
| 32 | VERIFY( stdc_leading_zeros_us(USHRT_MAX) == 0 ); |
| 33 | VERIFY( stdc_leading_zeros_us(USHRT_MAX >> 11) == 11 ); |
| 34 | |
| 35 | VERIFY( stdc_leading_zeros_ui(0) == __INT_WIDTH__ ); |
| 36 | VERIFY( stdc_leading_zeros_ui(UINT_MAX) == 0 ); |
| 37 | VERIFY( stdc_leading_zeros_ui(UINT_MAX >> 11) == 11 ); |
| 38 | |
| 39 | VERIFY( stdc_leading_zeros_ul(0) == __LONG_WIDTH__ ); |
| 40 | VERIFY( stdc_leading_zeros_ul(ULONG_MAX) == 0 ); |
| 41 | VERIFY( stdc_leading_zeros_ul(ULONG_MAX >> 19) == 19 ); |
| 42 | |
| 43 | VERIFY( stdc_leading_zeros_ull(0) == __LONG_LONG_WIDTH__ ); |
| 44 | VERIFY( stdc_leading_zeros_ull(ULLONG_MAX) == 0 ); |
| 45 | VERIFY( stdc_leading_zeros_ull(ULLONG_MAX >> 33) == 33 ); |
| 46 | VERIFY( stdc_leading_zeros_ull(7) == (__LONG_LONG_WIDTH__ - 3) ); |
| 47 | |
| 48 | VERIFY( stdc_leading_zeros(7U) == (__INT_WIDTH__ - 3) ); |
| 49 | VERIFY( stdc_leading_zeros(7UL) == (__LONG_WIDTH__ - 3) ); |
| 50 | VERIFY( stdc_leading_zeros(7ULL) == (__LONG_LONG_WIDTH__ - 3) ); |
| 51 | |
| 52 | static_assert( std::is_same_v<decltype(stdc_leading_zeros(0UL)), unsigned> ); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | test_leading_ones() |
| 57 | { |
| 58 | VERIFY( stdc_leading_ones_uc(0) == 0 ); |
| 59 | VERIFY( stdc_leading_ones_uc(UCHAR_MAX) == CHAR_BIT ); |
| 60 | VERIFY( stdc_leading_ones_uc(CHAR_MAX) == (CHAR_MIN ? 0 : CHAR_BIT) ); |
| 61 | VERIFY( stdc_leading_ones_uc(UCHAR_MAX >> 3) == 0 ); |
| 62 | VERIFY( stdc_leading_ones_uc((unsigned char)(UCHAR_MAX << 3)) == (CHAR_BIT - 3) ); |
| 63 | |
| 64 | VERIFY( stdc_leading_ones_us(0) == 0 ); |
| 65 | VERIFY( stdc_leading_ones_us(USHRT_MAX) == __SHRT_WIDTH__ ); |
| 66 | VERIFY( stdc_leading_ones_us((unsigned short)(USHRT_MAX << 11)) == (__SHRT_WIDTH__ - 11) ); |
| 67 | VERIFY( stdc_leading_ones_us(USHRT_MAX >> 11) == 0 ); |
| 68 | |
| 69 | VERIFY( stdc_leading_ones_ui(0) == 0 ); |
| 70 | VERIFY( stdc_leading_ones_ui(UINT_MAX) == __INT_WIDTH__ ); |
| 71 | VERIFY( stdc_leading_ones_ui(UINT_MAX << 11) == (__INT_WIDTH__ - 11) ); |
| 72 | |
| 73 | VERIFY( stdc_leading_ones_ul(0) == 0 ); |
| 74 | VERIFY( stdc_leading_ones_ul(ULONG_MAX) == __LONG_WIDTH__ ); |
| 75 | VERIFY( stdc_leading_ones_ul(ULONG_MAX << 19) == (__LONG_WIDTH__ - 19) ); |
| 76 | |
| 77 | VERIFY( stdc_leading_ones_ull(0) == 0 ); |
| 78 | VERIFY( stdc_leading_ones_ull(ULLONG_MAX) == __LONG_LONG_WIDTH__ ); |
| 79 | VERIFY( stdc_leading_ones_ull(ULLONG_MAX << 33) == (__LONG_LONG_WIDTH__ - 33) ); |
| 80 | |
| 81 | VERIFY( stdc_leading_ones(-1U << 2) == (__INT_WIDTH__ - 2) ); |
| 82 | VERIFY( stdc_leading_ones(-1UL << 3) == (__LONG_WIDTH__ - 3) ); |
| 83 | VERIFY( stdc_leading_ones(-1ULL << 4) == (__LONG_LONG_WIDTH__ - 4) ); |
| 84 | |
| 85 | static_assert( std::is_same_v<decltype(stdc_leading_ones(0UL)), unsigned> ); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | test_trailing_zeros() |
| 90 | { |
| 91 | VERIFY( stdc_trailing_zeros_uc((unsigned char)(UCHAR_MAX << 5)) == 5 ); |
| 92 | VERIFY( stdc_trailing_zeros_us((unsigned short)(USHRT_MAX << 5)) == 5 ); |
| 93 | VERIFY( stdc_trailing_zeros_ui(UINT_MAX << 9) == 9 ); |
| 94 | VERIFY( stdc_trailing_zeros_ul(ULONG_MAX << 19) == 19 ); |
| 95 | VERIFY( stdc_trailing_zeros_ull(ULLONG_MAX << 39) == 39 ); |
| 96 | VERIFY( stdc_trailing_zeros(8U) == 3 ); |
| 97 | |
| 98 | static_assert( std::is_same_v<decltype(stdc_trailing_zeros(0UL)), unsigned> ); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | test_trailing_ones() |
| 103 | { |
| 104 | VERIFY( stdc_trailing_ones_uc((unsigned char)3) == 2 ); |
| 105 | VERIFY( stdc_trailing_ones_uc((unsigned char)135) == 3 ); |
| 106 | VERIFY( stdc_trailing_ones_us((unsigned short)7) == 3 ); |
| 107 | VERIFY( stdc_trailing_ones_us((unsigned short)23) == 3 ); |
| 108 | VERIFY( stdc_trailing_ones_us((unsigned short)235) == 2 ); |
| 109 | VERIFY( stdc_trailing_ones_ui(11U) == 2 ); |
| 110 | VERIFY( stdc_trailing_ones_ui(15U) == 4 ); |
| 111 | VERIFY( stdc_trailing_ones_ul(ULONG_MAX) == __LONG_WIDTH__ ); |
| 112 | VERIFY( stdc_trailing_ones_ull(ULLONG_MAX) == __LONG_LONG_WIDTH__ ); |
| 113 | VERIFY( stdc_trailing_ones(7U) == 3 ); |
| 114 | |
| 115 | static_assert( std::is_same_v<decltype(stdc_trailing_ones(0UL)), unsigned> ); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | test_first_leading_zero() |
| 120 | { |
| 121 | VERIFY( stdc_first_leading_zero_uc(0) == 1 ); |
| 122 | VERIFY( stdc_first_leading_zero_uc(UCHAR_MAX) == 0 ); |
| 123 | VERIFY( stdc_first_leading_zero_uc(UCHAR_MAX ^ 0b111) == (CHAR_BIT - 2) ); |
| 124 | VERIFY( stdc_first_leading_zero_us(USHRT_MAX) == 0 ); |
| 125 | VERIFY( stdc_first_leading_zero_us(USHRT_MAX ^ 0b111) == (__SHRT_WIDTH__ - 2) ); |
| 126 | VERIFY( stdc_first_leading_zero_ui(UINT_MAX ^ 0b10111) == (__INT_WIDTH__ - 4) ); |
| 127 | VERIFY( stdc_first_leading_zero_ul(ULONG_MAX ^ 0b10111) == (__LONG_WIDTH__ - 4) ); |
| 128 | VERIFY( stdc_first_leading_zero_ull(ULLONG_MAX ^ 0b10111) == (__LONG_LONG_WIDTH__ - 4) ); |
| 129 | |
| 130 | VERIFY( stdc_first_leading_zero(0U) == 1 ); |
| 131 | VERIFY( stdc_first_leading_zero(-1U ^ 0b1111) == (__INT_WIDTH__ - 3) ); |
| 132 | |
| 133 | static_assert( std::is_same_v<decltype(stdc_first_leading_zero(0UL)), unsigned> ); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | test_first_leading_one() |
| 138 | { |
| 139 | VERIFY( stdc_first_leading_one_uc(0) == 0 ); |
| 140 | VERIFY( stdc_first_leading_one_uc(0b00100) == (CHAR_BIT - 2) ); |
| 141 | VERIFY( stdc_first_leading_one_ui(0b001100) == (__INT_WIDTH__ - 3) ); |
| 142 | VERIFY( stdc_first_leading_one_ul(0b101100) == (__LONG_WIDTH__ - 5) ); |
| 143 | VERIFY( stdc_first_leading_one_ull(0b1110000) == (__LONG_LONG_WIDTH__ - 6) ); |
| 144 | |
| 145 | VERIFY( stdc_first_leading_one(0U) == 0 ); |
| 146 | VERIFY( stdc_first_leading_one(-1U >> 4) == 5 ); |
| 147 | VERIFY( stdc_first_leading_one(-1ULL >> 43) == 44 ); |
| 148 | |
| 149 | static_assert( std::is_same_v<decltype(stdc_first_leading_one(0UL)), unsigned> ); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | test_first_trailing_zero() |
| 154 | { |
| 155 | VERIFY( stdc_first_trailing_zero_uc(0) == 1 ); |
| 156 | VERIFY( stdc_first_trailing_zero_uc(1) == 2 ); |
| 157 | VERIFY( stdc_first_trailing_zero_uc(7) == 4 ); |
| 158 | VERIFY( stdc_first_trailing_zero_us(15) == 5 ); |
| 159 | VERIFY( stdc_first_trailing_zero_ui(15) == 5 ); |
| 160 | VERIFY( stdc_first_trailing_zero_ul(15) == 5 ); |
| 161 | VERIFY( stdc_first_trailing_zero_ull(15) == 5 ); |
| 162 | |
| 163 | VERIFY( stdc_first_trailing_zero(15U) == 5 ); |
| 164 | |
| 165 | static_assert( std::is_same_v<decltype(stdc_first_trailing_zero(0UL)), unsigned> ); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | test_first_trailing_one() |
| 170 | { |
| 171 | VERIFY( stdc_first_trailing_one_uc(0) == 0 ); |
| 172 | VERIFY( stdc_first_trailing_one_uc(1) == 1 ); |
| 173 | VERIFY( stdc_first_trailing_one_uc(7) == 1 ); |
| 174 | VERIFY( stdc_first_trailing_one_us(16) == 5 ); |
| 175 | VERIFY( stdc_first_trailing_one_ui(16) == 5 ); |
| 176 | VERIFY( stdc_first_trailing_one_ul(16) == 5 ); |
| 177 | VERIFY( stdc_first_trailing_one_ull(16) == 5 ); |
| 178 | |
| 179 | VERIFY( stdc_first_trailing_one(16U) == 5 ); |
| 180 | VERIFY( stdc_first_trailing_one(-1ULL << 17) == 18 ); |
| 181 | |
| 182 | static_assert( std::is_same_v<decltype(stdc_first_trailing_one(0UL)), unsigned> ); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | test_count_zeros() |
| 187 | { |
| 188 | VERIFY( stdc_count_zeros_uc(0b101010) == (CHAR_BIT - 3) ); |
| 189 | VERIFY( stdc_count_zeros_us(0b1010101) == (__SHRT_WIDTH__ - 4) ); |
| 190 | VERIFY( stdc_count_zeros_ui(0b1010101111) == (__INT_WIDTH__ - 7) ); |
| 191 | VERIFY( stdc_count_zeros_ul(0b10101011110101) == (__LONG_WIDTH__ - 9) ); |
| 192 | VERIFY( stdc_count_zeros_ull(0b10101011110101) == (__LONG_LONG_WIDTH__ - 9) ); |
| 193 | |
| 194 | VERIFY( stdc_count_zeros(0b111UL) == (__LONG_WIDTH__ - 3) ); |
| 195 | VERIFY( stdc_count_zeros(0U) == __INT_WIDTH__ ); |
| 196 | |
| 197 | // std::popcount returns signed int, stdc_count_zeros_uc returns unsigned int. |
| 198 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0)), unsigned> ); |
| 199 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0)), unsigned> ); |
| 200 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0)), unsigned> ); |
| 201 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0)), unsigned> ); |
| 202 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0)), unsigned> ); |
| 203 | static_assert( std::is_same_v<decltype(stdc_count_zeros_uc(0u)), unsigned> ); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | test_count_ones() |
| 208 | { |
| 209 | VERIFY( stdc_count_ones_uc(0b101010) == 3 ); |
| 210 | VERIFY( stdc_count_ones_us(0b1010101) == 4 ); |
| 211 | VERIFY( stdc_count_ones_ui(0b1010101111) == 7 ); |
| 212 | VERIFY( stdc_count_ones_ul(0b10101011110101) == 9 ); |
| 213 | VERIFY( stdc_count_ones_ull(0b10101011110101) == 9 ); |
| 214 | |
| 215 | VERIFY( stdc_count_ones(0b10101011110101U) == 9 ); |
| 216 | VERIFY( stdc_count_ones(0U) == 0 ); |
| 217 | |
| 218 | // std::popcount returns signed int, stdc_count_ones_uc returns unsigned int. |
| 219 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0)), unsigned> ); |
| 220 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0)), unsigned> ); |
| 221 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0)), unsigned> ); |
| 222 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0)), unsigned> ); |
| 223 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0)), unsigned> ); |
| 224 | static_assert( std::is_same_v<decltype(stdc_count_ones_uc(0u)), unsigned> ); |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | test_has_single_bit() |
| 229 | { |
| 230 | VERIFY ( ! stdc_has_single_bit_uc(0) ); |
| 231 | VERIFY ( ! stdc_has_single_bit_uc(41) ); |
| 232 | VERIFY ( stdc_has_single_bit_uc(1) ); |
| 233 | VERIFY ( stdc_has_single_bit_uc(64) ); |
| 234 | VERIFY ( stdc_has_single_bit_us(1 << 7) ); |
| 235 | VERIFY ( stdc_has_single_bit_ui(1 << 11) ); |
| 236 | VERIFY ( stdc_has_single_bit_ul(1 << 14) ); |
| 237 | VERIFY ( stdc_has_single_bit_ull(1 << 24) ); |
| 238 | |
| 239 | VERIFY ( stdc_has_single_bit(64U) ); |
| 240 | VERIFY ( stdc_has_single_bit(128UL) ); |
| 241 | VERIFY ( ! stdc_has_single_bit(129UL) ); |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | test_bit_width() |
| 246 | { |
| 247 | VERIFY( stdc_bit_width_uc(0) == 0 ); |
| 248 | VERIFY( stdc_bit_width_uc(7) == 3 ); |
| 249 | VERIFY( stdc_bit_width_uc(0b10101) == 5 ); |
| 250 | VERIFY( stdc_bit_width_us(0b101010) == 6 ); |
| 251 | VERIFY( stdc_bit_width_ui(0b1010101) == 7 ); |
| 252 | VERIFY( stdc_bit_width_ul(ULONG_MAX) == __LONG_WIDTH__ ); |
| 253 | VERIFY( stdc_bit_width_ull(ULLONG_MAX >> 2) == (__LONG_LONG_WIDTH__ - 2) ); |
| 254 | |
| 255 | VERIFY( stdc_bit_width(0b1010101U) == 7U ); |
| 256 | |
| 257 | // std::bit_width returns signed int, stdc_bit_width returns unsigned int. |
| 258 | static_assert( std::is_same_v<decltype(stdc_bit_width_uc(0)), unsigned> ); |
| 259 | static_assert( std::is_same_v<decltype(stdc_bit_width_us(0)), unsigned> ); |
| 260 | static_assert( std::is_same_v<decltype(stdc_bit_width_ui(0)), unsigned> ); |
| 261 | static_assert( std::is_same_v<decltype(stdc_bit_width_ul(0)), unsigned> ); |
| 262 | static_assert( std::is_same_v<decltype(stdc_bit_width_ul(0)), unsigned> ); |
| 263 | static_assert( std::is_same_v<decltype(stdc_bit_width(0u)), unsigned> ); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | test_bit_floor() |
| 268 | { |
| 269 | VERIFY( stdc_bit_floor_uc(0) == 0 ); |
| 270 | VERIFY( stdc_bit_floor_uc(7) == 4 ); |
| 271 | VERIFY( stdc_bit_floor_uc(0b10101) == 0b10000 ); |
| 272 | VERIFY( stdc_bit_floor_us(0b101010) == 0b100000 ); |
| 273 | VERIFY( stdc_bit_floor_ui(0b1010101) == 0b1000000 ); |
| 274 | VERIFY( stdc_bit_floor_ul(ULONG_MAX) == (1ul << (__LONG_WIDTH__ - 1)) ); |
| 275 | VERIFY( stdc_bit_floor_ull(1000000) == (1ULL << 19) ); |
| 276 | |
| 277 | VERIFY( stdc_bit_floor(0b1010101U) == 0b1000000 ); |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | test_bit_ceil() |
| 282 | { |
| 283 | VERIFY( stdc_bit_ceil_uc(0) == 1 ); |
| 284 | VERIFY( stdc_bit_ceil_uc(1) == 1 ); |
| 285 | VERIFY( stdc_bit_ceil_uc(2) == 2 ); |
| 286 | VERIFY( stdc_bit_ceil_uc(3) == 4 ); |
| 287 | VERIFY( stdc_bit_ceil_us(11) == 16 ); |
| 288 | VERIFY( stdc_bit_ceil_ui(257) == 512 ); |
| 289 | VERIFY( stdc_bit_ceil_ul(1048) == 2048 ); |
| 290 | VERIFY( stdc_bit_ceil_ull(1000000) == (1ULL << 20) ); |
| 291 | |
| 292 | VERIFY( stdc_bit_ceil(0b1010101U) == 0b10000000 ); |
| 293 | } |
| 294 | |
| 295 | int main() |
| 296 | { |
| 297 | test_leading_zeros(); |
| 298 | test_leading_ones(); |
| 299 | test_trailing_zeros(); |
| 300 | test_trailing_ones(); |
| 301 | test_first_leading_zero(); |
| 302 | test_first_leading_one(); |
| 303 | test_first_trailing_zero(); |
| 304 | test_first_trailing_one(); |
| 305 | test_count_zeros(); |
| 306 | test_count_ones(); |
| 307 | test_has_single_bit(); |
| 308 | test_bit_width(); |
| 309 | test_bit_floor(); |
| 310 | test_bit_ceil(); |
| 311 | } |
| 312 | |
| 313 | #include <bit> |
| 314 | // The standard doesn't require these values to match, |
| 315 | // so this is specific to libstdc++. |
| 316 | #ifdef _GLIBCXX_RELEASE |
| 317 | static_assert(__STDC_ENDIAN_BIG__ == (int)std::endian::big); |
| 318 | static_assert(__STDC_ENDIAN_LITTLE__ == (int)std::endian::little); |
| 319 | static_assert(__STDC_ENDIAN_NATIVE__ == (int)std::endian::native); |
| 320 | #endif |