stdbit.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* ISO C23 Standard: 7.18 - Bit and byte utilities <stdbit.h>.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _STDBIT_H
  16. #define _STDBIT_H 1
  17. #include <features.h>
  18. #include <bits/endian.h>
  19. #include <bits/stdint-intn.h>
  20. #include <bits/stdint-uintn.h>
  21. #include <bits/stdint-least.h>
  22. /* In C23, <stdbool.h> defines only an implementation-namespace macro,
  23. so is OK to include here. Before C23, including <stdbool.h> allows
  24. the header to use bool rather than _Bool unconditionally, and so to
  25. compile as C++ (although the type-generic macros are not a good
  26. form of type-generic interface for C++). */
  27. #include <stdbool.h>
  28. #define __need_size_t
  29. #include <stddef.h>
  30. #define __STDC_VERSION_STDBIT_H__ 202311L
  31. #define __STDC_ENDIAN_LITTLE__ __LITTLE_ENDIAN
  32. #define __STDC_ENDIAN_BIG__ __BIG_ENDIAN
  33. #define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER
  34. __BEGIN_DECLS
  35. /* Use __pacify_uint16 (N) instead of (uint16_t) (N) when the cast is helpful
  36. only to pacify older GCC (e.g., GCC 10 -Wconversion) or non-GCC (e.g
  37. clang -Wimplicit-int-conversion). */
  38. #if __GNUC_PREREQ (11, 0)
  39. # define __pacify_uint8(n) (n)
  40. # define __pacify_uint16(n) (n)
  41. #else
  42. # define __pacify_uint8(n) ((uint8_t) (n))
  43. # define __pacify_uint16(n) ((uint16_t) (n))
  44. #endif
  45. /* Count leading zeros. */
  46. extern unsigned int stdc_leading_zeros_uc (unsigned char __x)
  47. __THROW __attribute_const__;
  48. extern unsigned int stdc_leading_zeros_us (unsigned short __x)
  49. __THROW __attribute_const__;
  50. extern unsigned int stdc_leading_zeros_ui (unsigned int __x)
  51. __THROW __attribute_const__;
  52. extern unsigned int stdc_leading_zeros_ul (unsigned long int __x)
  53. __THROW __attribute_const__;
  54. __extension__
  55. extern unsigned int stdc_leading_zeros_ull (unsigned long long int __x)
  56. __THROW __attribute_const__;
  57. #if __glibc_has_builtin (__builtin_stdc_leading_zeros)
  58. # define stdc_leading_zeros(x) (__builtin_stdc_leading_zeros (x))
  59. #else
  60. # define stdc_leading_zeros(x) \
  61. (stdc_leading_zeros_ull (x) \
  62. - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
  63. #endif
  64. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  65. static __always_inline unsigned int
  66. __clz64_inline (uint64_t __x)
  67. {
  68. return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x);
  69. }
  70. static __always_inline unsigned int
  71. __clz32_inline (uint32_t __x)
  72. {
  73. return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x);
  74. }
  75. static __always_inline unsigned int
  76. __clz16_inline (uint16_t __x)
  77. {
  78. return __clz32_inline (__x) - 16;
  79. }
  80. static __always_inline unsigned int
  81. __clz8_inline (uint8_t __x)
  82. {
  83. return __clz32_inline (__x) - 24;
  84. }
  85. # define stdc_leading_zeros_uc(x) (__clz8_inline (x))
  86. # define stdc_leading_zeros_us(x) (__clz16_inline (x))
  87. # define stdc_leading_zeros_ui(x) (__clz32_inline (x))
  88. # if __WORDSIZE == 64
  89. # define stdc_leading_zeros_ul(x) (__clz64_inline (x))
  90. # else
  91. # define stdc_leading_zeros_ul(x) (__clz32_inline (x))
  92. # endif
  93. # define stdc_leading_zeros_ull(x) (__clz64_inline (x))
  94. #endif
  95. /* Count leading ones. */
  96. extern unsigned int stdc_leading_ones_uc (unsigned char __x)
  97. __THROW __attribute_const__;
  98. extern unsigned int stdc_leading_ones_us (unsigned short __x)
  99. __THROW __attribute_const__;
  100. extern unsigned int stdc_leading_ones_ui (unsigned int __x)
  101. __THROW __attribute_const__;
  102. extern unsigned int stdc_leading_ones_ul (unsigned long int __x)
  103. __THROW __attribute_const__;
  104. __extension__
  105. extern unsigned int stdc_leading_ones_ull (unsigned long long int __x)
  106. __THROW __attribute_const__;
  107. #if __glibc_has_builtin (__builtin_stdc_leading_ones)
  108. # define stdc_leading_ones(x) (__builtin_stdc_leading_ones (x))
  109. #else
  110. # define stdc_leading_ones(x) \
  111. (stdc_leading_ones_ull ((unsigned long long int) (x) \
  112. << 8 * (sizeof (0ULL) - sizeof (x))))
  113. #endif
  114. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  115. static __always_inline unsigned int
  116. __clo64_inline (uint64_t __x)
  117. {
  118. return __clz64_inline (~__x);
  119. }
  120. static __always_inline unsigned int
  121. __clo32_inline (uint32_t __x)
  122. {
  123. return __clz32_inline (~__x);
  124. }
  125. static __always_inline unsigned int
  126. __clo16_inline (uint16_t __x)
  127. {
  128. return __clz16_inline (__pacify_uint16 (~__x));
  129. }
  130. static __always_inline unsigned int
  131. __clo8_inline (uint8_t __x)
  132. {
  133. return __clz8_inline (__pacify_uint8 (~__x));
  134. }
  135. # define stdc_leading_ones_uc(x) (__clo8_inline (x))
  136. # define stdc_leading_ones_us(x) (__clo16_inline (x))
  137. # define stdc_leading_ones_ui(x) (__clo32_inline (x))
  138. # if __WORDSIZE == 64
  139. # define stdc_leading_ones_ul(x) (__clo64_inline (x))
  140. # else
  141. # define stdc_leading_ones_ul(x) (__clo32_inline (x))
  142. # endif
  143. # define stdc_leading_ones_ull(x) (__clo64_inline (x))
  144. #endif
  145. /* Count trailing zeros. */
  146. extern unsigned int stdc_trailing_zeros_uc (unsigned char __x)
  147. __THROW __attribute_const__;
  148. extern unsigned int stdc_trailing_zeros_us (unsigned short __x)
  149. __THROW __attribute_const__;
  150. extern unsigned int stdc_trailing_zeros_ui (unsigned int __x)
  151. __THROW __attribute_const__;
  152. extern unsigned int stdc_trailing_zeros_ul (unsigned long int __x)
  153. __THROW __attribute_const__;
  154. __extension__
  155. extern unsigned int stdc_trailing_zeros_ull (unsigned long long int __x)
  156. __THROW __attribute_const__;
  157. #if __glibc_has_builtin (__builtin_stdc_trailing_zeros)
  158. # define stdc_trailing_zeros(x) (__builtin_stdc_trailing_zeros (x))
  159. #else
  160. # define stdc_trailing_zeros(x) \
  161. (sizeof (x) == 8 ? stdc_trailing_zeros_ull (x) \
  162. : sizeof (x) == 4 ? stdc_trailing_zeros_ui (x) \
  163. : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x)) \
  164. : stdc_trailing_zeros_uc (__pacify_uint8 (x)))
  165. #endif
  166. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
  167. static __always_inline unsigned int
  168. __ctz64_inline (uint64_t __x)
  169. {
  170. return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x);
  171. }
  172. static __always_inline unsigned int
  173. __ctz32_inline (uint32_t __x)
  174. {
  175. return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x);
  176. }
  177. static __always_inline unsigned int
  178. __ctz16_inline (uint16_t __x)
  179. {
  180. return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x);
  181. }
  182. static __always_inline unsigned int
  183. __ctz8_inline (uint8_t __x)
  184. {
  185. return __x == 0 ? 8U : (unsigned int) __builtin_ctz (__x);
  186. }
  187. # define stdc_trailing_zeros_uc(x) (__ctz8_inline (x))
  188. # define stdc_trailing_zeros_us(x) (__ctz16_inline (x))
  189. # define stdc_trailing_zeros_ui(x) (__ctz32_inline (x))
  190. # if __WORDSIZE == 64
  191. # define stdc_trailing_zeros_ul(x) (__ctz64_inline (x))
  192. # else
  193. # define stdc_trailing_zeros_ul(x) (__ctz32_inline (x))
  194. # endif
  195. # define stdc_trailing_zeros_ull(x) (__ctz64_inline (x))
  196. #endif
  197. /* Count trailing ones. */
  198. extern unsigned int stdc_trailing_ones_uc (unsigned char __x)
  199. __THROW __attribute_const__;
  200. extern unsigned int stdc_trailing_ones_us (unsigned short __x)
  201. __THROW __attribute_const__;
  202. extern unsigned int stdc_trailing_ones_ui (unsigned int __x)
  203. __THROW __attribute_const__;
  204. extern unsigned int stdc_trailing_ones_ul (unsigned long int __x)
  205. __THROW __attribute_const__;
  206. __extension__
  207. extern unsigned int stdc_trailing_ones_ull (unsigned long long int __x)
  208. __THROW __attribute_const__;
  209. #if __glibc_has_builtin (__builtin_stdc_trailing_ones)
  210. # define stdc_trailing_ones(x) (__builtin_stdc_trailing_ones (x))
  211. #else
  212. # define stdc_trailing_ones(x) (stdc_trailing_ones_ull (x))
  213. #endif
  214. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
  215. static __always_inline unsigned int
  216. __cto64_inline (uint64_t __x)
  217. {
  218. return __ctz64_inline (~__x);
  219. }
  220. static __always_inline unsigned int
  221. __cto32_inline (uint32_t __x)
  222. {
  223. return __ctz32_inline (~__x);
  224. }
  225. static __always_inline unsigned int
  226. __cto16_inline (uint16_t __x)
  227. {
  228. return __ctz16_inline (__pacify_uint16 (~__x));
  229. }
  230. static __always_inline unsigned int
  231. __cto8_inline (uint8_t __x)
  232. {
  233. return __ctz8_inline (__pacify_uint8 (~__x));
  234. }
  235. # define stdc_trailing_ones_uc(x) (__cto8_inline (x))
  236. # define stdc_trailing_ones_us(x) (__cto16_inline (x))
  237. # define stdc_trailing_ones_ui(x) (__cto32_inline (x))
  238. # if __WORDSIZE == 64
  239. # define stdc_trailing_ones_ul(x) (__cto64_inline (x))
  240. # else
  241. # define stdc_trailing_ones_ul(x) (__cto32_inline (x))
  242. # endif
  243. # define stdc_trailing_ones_ull(x) (__cto64_inline (x))
  244. #endif
  245. /* First leading zero. */
  246. extern unsigned int stdc_first_leading_zero_uc (unsigned char __x)
  247. __THROW __attribute_const__;
  248. extern unsigned int stdc_first_leading_zero_us (unsigned short __x)
  249. __THROW __attribute_const__;
  250. extern unsigned int stdc_first_leading_zero_ui (unsigned int __x)
  251. __THROW __attribute_const__;
  252. extern unsigned int stdc_first_leading_zero_ul (unsigned long int __x)
  253. __THROW __attribute_const__;
  254. __extension__
  255. extern unsigned int stdc_first_leading_zero_ull (unsigned long long int __x)
  256. __THROW __attribute_const__;
  257. #if __glibc_has_builtin (__builtin_stdc_first_leading_zero)
  258. # define stdc_first_leading_zero(x) (__builtin_stdc_first_leading_zero (x))
  259. #else
  260. # define stdc_first_leading_zero(x) \
  261. (sizeof (x) == 8 ? stdc_first_leading_zero_ull (x) \
  262. : sizeof (x) == 4 ? stdc_first_leading_zero_ui (x) \
  263. : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x)) \
  264. : stdc_first_leading_zero_uc (__pacify_uint8 (x)))
  265. #endif
  266. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  267. static __always_inline unsigned int
  268. __flz64_inline (uint64_t __x)
  269. {
  270. return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x);
  271. }
  272. static __always_inline unsigned int
  273. __flz32_inline (uint32_t __x)
  274. {
  275. return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x);
  276. }
  277. static __always_inline unsigned int
  278. __flz16_inline (uint16_t __x)
  279. {
  280. return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x);
  281. }
  282. static __always_inline unsigned int
  283. __flz8_inline (uint8_t __x)
  284. {
  285. return __x == (uint8_t) -1 ? 0 : 1 + __clo8_inline (__x);
  286. }
  287. # define stdc_first_leading_zero_uc(x) (__flz8_inline (x))
  288. # define stdc_first_leading_zero_us(x) (__flz16_inline (x))
  289. # define stdc_first_leading_zero_ui(x) (__flz32_inline (x))
  290. # if __WORDSIZE == 64
  291. # define stdc_first_leading_zero_ul(x) (__flz64_inline (x))
  292. # else
  293. # define stdc_first_leading_zero_ul(x) (__flz32_inline (x))
  294. # endif
  295. # define stdc_first_leading_zero_ull(x) (__flz64_inline (x))
  296. #endif
  297. /* First leading one. */
  298. extern unsigned int stdc_first_leading_one_uc (unsigned char __x)
  299. __THROW __attribute_const__;
  300. extern unsigned int stdc_first_leading_one_us (unsigned short __x)
  301. __THROW __attribute_const__;
  302. extern unsigned int stdc_first_leading_one_ui (unsigned int __x)
  303. __THROW __attribute_const__;
  304. extern unsigned int stdc_first_leading_one_ul (unsigned long int __x)
  305. __THROW __attribute_const__;
  306. __extension__
  307. extern unsigned int stdc_first_leading_one_ull (unsigned long long int __x)
  308. __THROW __attribute_const__;
  309. #if __glibc_has_builtin (__builtin_stdc_first_leading_one)
  310. # define stdc_first_leading_one(x) (__builtin_stdc_first_leading_one (x))
  311. #else
  312. # define stdc_first_leading_one(x) \
  313. (sizeof (x) == 8 ? stdc_first_leading_one_ull (x) \
  314. : sizeof (x) == 4 ? stdc_first_leading_one_ui (x) \
  315. : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x)) \
  316. : stdc_first_leading_one_uc (__pacify_uint8 (x)))
  317. #endif
  318. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  319. static __always_inline unsigned int
  320. __flo64_inline (uint64_t __x)
  321. {
  322. return __x == 0 ? 0 : 1 + __clz64_inline (__x);
  323. }
  324. static __always_inline unsigned int
  325. __flo32_inline (uint32_t __x)
  326. {
  327. return __x == 0 ? 0 : 1 + __clz32_inline (__x);
  328. }
  329. static __always_inline unsigned int
  330. __flo16_inline (uint16_t __x)
  331. {
  332. return __x == 0 ? 0 : 1 + __clz16_inline (__x);
  333. }
  334. static __always_inline unsigned int
  335. __flo8_inline (uint8_t __x)
  336. {
  337. return __x == 0 ? 0 : 1 + __clz8_inline (__x);
  338. }
  339. # define stdc_first_leading_one_uc(x) (__flo8_inline (x))
  340. # define stdc_first_leading_one_us(x) (__flo16_inline (x))
  341. # define stdc_first_leading_one_ui(x) (__flo32_inline (x))
  342. # if __WORDSIZE == 64
  343. # define stdc_first_leading_one_ul(x) (__flo64_inline (x))
  344. # else
  345. # define stdc_first_leading_one_ul(x) (__flo32_inline (x))
  346. # endif
  347. # define stdc_first_leading_one_ull(x) (__flo64_inline (x))
  348. #endif
  349. /* First trailing zero. */
  350. extern unsigned int stdc_first_trailing_zero_uc (unsigned char __x)
  351. __THROW __attribute_const__;
  352. extern unsigned int stdc_first_trailing_zero_us (unsigned short __x)
  353. __THROW __attribute_const__;
  354. extern unsigned int stdc_first_trailing_zero_ui (unsigned int __x)
  355. __THROW __attribute_const__;
  356. extern unsigned int stdc_first_trailing_zero_ul (unsigned long int __x)
  357. __THROW __attribute_const__;
  358. __extension__
  359. extern unsigned int stdc_first_trailing_zero_ull (unsigned long long int __x)
  360. __THROW __attribute_const__;
  361. #if __glibc_has_builtin (__builtin_stdc_first_trailing_zero)
  362. # define stdc_first_trailing_zero(x) (__builtin_stdc_first_trailing_zero (x))
  363. #else
  364. # define stdc_first_trailing_zero(x) \
  365. (sizeof (x) == 8 ? stdc_first_trailing_zero_ull (x) \
  366. : sizeof (x) == 4 ? stdc_first_trailing_zero_ui (x) \
  367. : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \
  368. : stdc_first_trailing_zero_uc (__pacify_uint8 (x)))
  369. #endif
  370. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
  371. static __always_inline unsigned int
  372. __ftz64_inline (uint64_t __x)
  373. {
  374. return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x);
  375. }
  376. static __always_inline unsigned int
  377. __ftz32_inline (uint32_t __x)
  378. {
  379. return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x);
  380. }
  381. static __always_inline unsigned int
  382. __ftz16_inline (uint16_t __x)
  383. {
  384. return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x);
  385. }
  386. static __always_inline unsigned int
  387. __ftz8_inline (uint8_t __x)
  388. {
  389. return __x == (uint8_t) -1 ? 0 : 1 + __cto8_inline (__x);
  390. }
  391. # define stdc_first_trailing_zero_uc(x) (__ftz8_inline (x))
  392. # define stdc_first_trailing_zero_us(x) (__ftz16_inline (x))
  393. # define stdc_first_trailing_zero_ui(x) (__ftz32_inline (x))
  394. # if __WORDSIZE == 64
  395. # define stdc_first_trailing_zero_ul(x) (__ftz64_inline (x))
  396. # else
  397. # define stdc_first_trailing_zero_ul(x) (__ftz32_inline (x))
  398. # endif
  399. # define stdc_first_trailing_zero_ull(x) (__ftz64_inline (x))
  400. #endif
  401. /* First trailing one. */
  402. extern unsigned int stdc_first_trailing_one_uc (unsigned char __x)
  403. __THROW __attribute_const__;
  404. extern unsigned int stdc_first_trailing_one_us (unsigned short __x)
  405. __THROW __attribute_const__;
  406. extern unsigned int stdc_first_trailing_one_ui (unsigned int __x)
  407. __THROW __attribute_const__;
  408. extern unsigned int stdc_first_trailing_one_ul (unsigned long int __x)
  409. __THROW __attribute_const__;
  410. __extension__
  411. extern unsigned int stdc_first_trailing_one_ull (unsigned long long int __x)
  412. __THROW __attribute_const__;
  413. #if __glibc_has_builtin (__builtin_stdc_first_trailing_one)
  414. # define stdc_first_trailing_one(x) (__builtin_stdc_first_trailing_one (x))
  415. #else
  416. # define stdc_first_trailing_one(x) \
  417. (sizeof (x) == 8 ? stdc_first_trailing_one_ull (x) \
  418. : sizeof (x) == 4 ? stdc_first_trailing_one_ui (x) \
  419. : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x)) \
  420. : stdc_first_trailing_one_uc (__pacify_uint8 (x)))
  421. #endif
  422. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
  423. static __always_inline unsigned int
  424. __fto64_inline (uint64_t __x)
  425. {
  426. return __x == 0 ? 0 : 1 + __ctz64_inline (__x);
  427. }
  428. static __always_inline unsigned int
  429. __fto32_inline (uint32_t __x)
  430. {
  431. return __x == 0 ? 0 : 1 + __ctz32_inline (__x);
  432. }
  433. static __always_inline unsigned int
  434. __fto16_inline (uint16_t __x)
  435. {
  436. return __x == 0 ? 0 : 1 + __ctz16_inline (__x);
  437. }
  438. static __always_inline unsigned int
  439. __fto8_inline (uint8_t __x)
  440. {
  441. return __x == 0 ? 0 : 1 + __ctz8_inline (__x);
  442. }
  443. # define stdc_first_trailing_one_uc(x) (__fto8_inline (x))
  444. # define stdc_first_trailing_one_us(x) (__fto16_inline (x))
  445. # define stdc_first_trailing_one_ui(x) (__fto32_inline (x))
  446. # if __WORDSIZE == 64
  447. # define stdc_first_trailing_one_ul(x) (__fto64_inline (x))
  448. # else
  449. # define stdc_first_trailing_one_ul(x) (__fto32_inline (x))
  450. # endif
  451. # define stdc_first_trailing_one_ull(x) (__fto64_inline (x))
  452. #endif
  453. /* Count zeros. */
  454. extern unsigned int stdc_count_zeros_uc (unsigned char __x)
  455. __THROW __attribute_const__;
  456. extern unsigned int stdc_count_zeros_us (unsigned short __x)
  457. __THROW __attribute_const__;
  458. extern unsigned int stdc_count_zeros_ui (unsigned int __x)
  459. __THROW __attribute_const__;
  460. extern unsigned int stdc_count_zeros_ul (unsigned long int __x)
  461. __THROW __attribute_const__;
  462. __extension__
  463. extern unsigned int stdc_count_zeros_ull (unsigned long long int __x)
  464. __THROW __attribute_const__;
  465. #if __glibc_has_builtin (__builtin_stdc_count_zeros)
  466. # define stdc_count_zeros(x) (__builtin_stdc_count_zeros (x))
  467. #else
  468. # define stdc_count_zeros(x) \
  469. (stdc_count_zeros_ull (x) \
  470. - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
  471. #endif
  472. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
  473. static __always_inline unsigned int
  474. __cz64_inline (uint64_t __x)
  475. {
  476. return 64U - (unsigned int) __builtin_popcountll (__x);
  477. }
  478. static __always_inline unsigned int
  479. __cz32_inline (uint32_t __x)
  480. {
  481. return 32U - (unsigned int) __builtin_popcount (__x);
  482. }
  483. static __always_inline unsigned int
  484. __cz16_inline (uint16_t __x)
  485. {
  486. return 16U - (unsigned int) __builtin_popcount (__x);
  487. }
  488. static __always_inline unsigned int
  489. __cz8_inline (uint8_t __x)
  490. {
  491. return 8U - (unsigned int) __builtin_popcount (__x);
  492. }
  493. # define stdc_count_zeros_uc(x) (__cz8_inline (x))
  494. # define stdc_count_zeros_us(x) (__cz16_inline (x))
  495. # define stdc_count_zeros_ui(x) (__cz32_inline (x))
  496. # if __WORDSIZE == 64
  497. # define stdc_count_zeros_ul(x) (__cz64_inline (x))
  498. # else
  499. # define stdc_count_zeros_ul(x) (__cz32_inline (x))
  500. # endif
  501. # define stdc_count_zeros_ull(x) (__cz64_inline (x))
  502. #endif
  503. /* Count ones. */
  504. extern unsigned int stdc_count_ones_uc (unsigned char __x)
  505. __THROW __attribute_const__;
  506. extern unsigned int stdc_count_ones_us (unsigned short __x)
  507. __THROW __attribute_const__;
  508. extern unsigned int stdc_count_ones_ui (unsigned int __x)
  509. __THROW __attribute_const__;
  510. extern unsigned int stdc_count_ones_ul (unsigned long int __x)
  511. __THROW __attribute_const__;
  512. __extension__
  513. extern unsigned int stdc_count_ones_ull (unsigned long long int __x)
  514. __THROW __attribute_const__;
  515. #if __glibc_has_builtin (__builtin_stdc_count_ones)
  516. # define stdc_count_ones(x) (__builtin_stdc_count_ones (x))
  517. #else
  518. # define stdc_count_ones(x) (stdc_count_ones_ull (x))
  519. #endif
  520. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
  521. static __always_inline unsigned int
  522. __co64_inline (uint64_t __x)
  523. {
  524. return (unsigned int) __builtin_popcountll (__x);
  525. }
  526. static __always_inline unsigned int
  527. __co32_inline (uint32_t __x)
  528. {
  529. return (unsigned int) __builtin_popcount (__x);
  530. }
  531. static __always_inline unsigned int
  532. __co16_inline (uint16_t __x)
  533. {
  534. return (unsigned int) __builtin_popcount (__x);
  535. }
  536. static __always_inline unsigned int
  537. __co8_inline (uint8_t __x)
  538. {
  539. return (unsigned int) __builtin_popcount (__x);
  540. }
  541. # define stdc_count_ones_uc(x) (__co8_inline (x))
  542. # define stdc_count_ones_us(x) (__co16_inline (x))
  543. # define stdc_count_ones_ui(x) (__co32_inline (x))
  544. # if __WORDSIZE == 64
  545. # define stdc_count_ones_ul(x) (__co64_inline (x))
  546. # else
  547. # define stdc_count_ones_ul(x) (__co32_inline (x))
  548. # endif
  549. # define stdc_count_ones_ull(x) (__co64_inline (x))
  550. #endif
  551. /* Single-bit check. */
  552. extern bool stdc_has_single_bit_uc (unsigned char __x)
  553. __THROW __attribute_const__;
  554. extern bool stdc_has_single_bit_us (unsigned short __x)
  555. __THROW __attribute_const__;
  556. extern bool stdc_has_single_bit_ui (unsigned int __x)
  557. __THROW __attribute_const__;
  558. extern bool stdc_has_single_bit_ul (unsigned long int __x)
  559. __THROW __attribute_const__;
  560. __extension__
  561. extern bool stdc_has_single_bit_ull (unsigned long long int __x)
  562. __THROW __attribute_const__;
  563. #if __glibc_has_builtin (__builtin_stdc_has_single_bit)
  564. # define stdc_has_single_bit(x) (__builtin_stdc_has_single_bit (x))
  565. #else
  566. # define stdc_has_single_bit(x) \
  567. ((bool) (sizeof (x) <= sizeof (unsigned int) \
  568. ? stdc_has_single_bit_ui (x) \
  569. : stdc_has_single_bit_ull (x)))
  570. #endif
  571. static __always_inline bool
  572. __hsb64_inline (uint64_t __x)
  573. {
  574. return (__x ^ (__x - 1)) > __x - 1;
  575. }
  576. static __always_inline bool
  577. __hsb32_inline (uint32_t __x)
  578. {
  579. return (__x ^ (__x - 1)) > __x - 1;
  580. }
  581. static __always_inline bool
  582. __hsb16_inline (uint16_t __x)
  583. {
  584. return (__x ^ (__x - 1)) > __x - 1;
  585. }
  586. static __always_inline bool
  587. __hsb8_inline (uint8_t __x)
  588. {
  589. return (__x ^ (__x - 1)) > __x - 1;
  590. }
  591. #define stdc_has_single_bit_uc(x) (__hsb8_inline (x))
  592. #define stdc_has_single_bit_us(x) (__hsb16_inline (x))
  593. #define stdc_has_single_bit_ui(x) (__hsb32_inline (x))
  594. #if __WORDSIZE == 64
  595. # define stdc_has_single_bit_ul(x) (__hsb64_inline (x))
  596. #else
  597. # define stdc_has_single_bit_ul(x) (__hsb32_inline (x))
  598. #endif
  599. #define stdc_has_single_bit_ull(x) (__hsb64_inline (x))
  600. /* Bit width. */
  601. extern unsigned int stdc_bit_width_uc (unsigned char __x)
  602. __THROW __attribute_const__;
  603. extern unsigned int stdc_bit_width_us (unsigned short __x)
  604. __THROW __attribute_const__;
  605. extern unsigned int stdc_bit_width_ui (unsigned int __x)
  606. __THROW __attribute_const__;
  607. extern unsigned int stdc_bit_width_ul (unsigned long int __x)
  608. __THROW __attribute_const__;
  609. __extension__
  610. extern unsigned int stdc_bit_width_ull (unsigned long long int __x)
  611. __THROW __attribute_const__;
  612. #if __glibc_has_builtin (__builtin_stdc_bit_width)
  613. # define stdc_bit_width(x) (__builtin_stdc_bit_width (x))
  614. #else
  615. # define stdc_bit_width(x) (stdc_bit_width_ull (x))
  616. #endif
  617. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  618. static __always_inline unsigned int
  619. __bw64_inline (uint64_t __x)
  620. {
  621. return 64 - __clz64_inline (__x);
  622. }
  623. static __always_inline unsigned int
  624. __bw32_inline (uint32_t __x)
  625. {
  626. return 32 - __clz32_inline (__x);
  627. }
  628. static __always_inline unsigned int
  629. __bw16_inline (uint16_t __x)
  630. {
  631. return 16 - __clz16_inline (__x);
  632. }
  633. static __always_inline unsigned int
  634. __bw8_inline (uint8_t __x)
  635. {
  636. return 8 - __clz8_inline (__x);
  637. }
  638. # define stdc_bit_width_uc(x) (__bw8_inline (x))
  639. # define stdc_bit_width_us(x) (__bw16_inline (x))
  640. # define stdc_bit_width_ui(x) (__bw32_inline (x))
  641. # if __WORDSIZE == 64
  642. # define stdc_bit_width_ul(x) (__bw64_inline (x))
  643. # else
  644. # define stdc_bit_width_ul(x) (__bw32_inline (x))
  645. # endif
  646. # define stdc_bit_width_ull(x) (__bw64_inline (x))
  647. #endif
  648. /* Bit floor. */
  649. extern unsigned char stdc_bit_floor_uc (unsigned char __x)
  650. __THROW __attribute_const__;
  651. extern unsigned short stdc_bit_floor_us (unsigned short __x)
  652. __THROW __attribute_const__;
  653. extern unsigned int stdc_bit_floor_ui (unsigned int __x)
  654. __THROW __attribute_const__;
  655. extern unsigned long int stdc_bit_floor_ul (unsigned long int __x)
  656. __THROW __attribute_const__;
  657. __extension__
  658. extern unsigned long long int stdc_bit_floor_ull (unsigned long long int __x)
  659. __THROW __attribute_const__;
  660. #if __glibc_has_builtin (__builtin_stdc_bit_floor)
  661. # define stdc_bit_floor(x) (__builtin_stdc_bit_floor (x))
  662. #else
  663. # define stdc_bit_floor(x) ((__typeof (x)) stdc_bit_floor_ull (x))
  664. #endif
  665. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  666. static __always_inline uint64_t
  667. __bf64_inline (uint64_t __x)
  668. {
  669. return __x == 0 ? 0 : ((uint64_t) 1) << (__bw64_inline (__x) - 1);
  670. }
  671. static __always_inline uint32_t
  672. __bf32_inline (uint32_t __x)
  673. {
  674. return __x == 0 ? 0 : ((uint32_t) 1) << (__bw32_inline (__x) - 1);
  675. }
  676. static __always_inline uint16_t
  677. __bf16_inline (uint16_t __x)
  678. {
  679. return __pacify_uint16 (__x == 0
  680. ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1));
  681. }
  682. static __always_inline uint8_t
  683. __bf8_inline (uint8_t __x)
  684. {
  685. return __pacify_uint8 (__x == 0
  686. ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1));
  687. }
  688. # define stdc_bit_floor_uc(x) ((unsigned char) __bf8_inline (x))
  689. # define stdc_bit_floor_us(x) ((unsigned short) __bf16_inline (x))
  690. # define stdc_bit_floor_ui(x) ((unsigned int) __bf32_inline (x))
  691. # if __WORDSIZE == 64
  692. # define stdc_bit_floor_ul(x) ((unsigned long int) __bf64_inline (x))
  693. # else
  694. # define stdc_bit_floor_ul(x) ((unsigned long int) __bf32_inline (x))
  695. # endif
  696. # define stdc_bit_floor_ull(x) ((unsigned long long int) __bf64_inline (x))
  697. #endif
  698. /* Bit ceiling. */
  699. extern unsigned char stdc_bit_ceil_uc (unsigned char __x)
  700. __THROW __attribute_const__;
  701. extern unsigned short stdc_bit_ceil_us (unsigned short __x)
  702. __THROW __attribute_const__;
  703. extern unsigned int stdc_bit_ceil_ui (unsigned int __x)
  704. __THROW __attribute_const__;
  705. extern unsigned long int stdc_bit_ceil_ul (unsigned long int __x)
  706. __THROW __attribute_const__;
  707. __extension__
  708. extern unsigned long long int stdc_bit_ceil_ull (unsigned long long int __x)
  709. __THROW __attribute_const__;
  710. #if __glibc_has_builtin (__builtin_stdc_bit_ceil)
  711. # define stdc_bit_ceil(x) (__builtin_stdc_bit_ceil (x))
  712. #else
  713. # define stdc_bit_ceil(x) ((__typeof (x)) stdc_bit_ceil_ull (x))
  714. #endif
  715. #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
  716. static __always_inline uint64_t
  717. __bc64_inline (uint64_t __x)
  718. {
  719. return __x <= 1 ? 1 : ((uint64_t) 2) << (__bw64_inline (__x - 1) - 1);
  720. }
  721. static __always_inline uint32_t
  722. __bc32_inline (uint32_t __x)
  723. {
  724. return __x <= 1 ? 1 : ((uint32_t) 2) << (__bw32_inline (__x - 1) - 1);
  725. }
  726. static __always_inline uint16_t
  727. __bc16_inline (uint16_t __x)
  728. {
  729. return __pacify_uint16 (__x <= 1
  730. ? 1
  731. : ((uint16_t) 2)
  732. << (__bw16_inline ((uint16_t) (__x - 1)) - 1));
  733. }
  734. static __always_inline uint8_t
  735. __bc8_inline (uint8_t __x)
  736. {
  737. return __pacify_uint8 (__x <= 1
  738. ? 1
  739. : ((uint8_t) 2)
  740. << (__bw8_inline ((uint8_t) (__x - 1)) - 1));
  741. }
  742. # define stdc_bit_ceil_uc(x) ((unsigned char) __bc8_inline (x))
  743. # define stdc_bit_ceil_us(x) ((unsigned short) __bc16_inline (x))
  744. # define stdc_bit_ceil_ui(x) ((unsigned int) __bc32_inline (x))
  745. # if __WORDSIZE == 64
  746. # define stdc_bit_ceil_ul(x) ((unsigned long int) __bc64_inline (x))
  747. # else
  748. # define stdc_bit_ceil_ul(x) ((unsigned long int) __bc32_inline (x))
  749. # endif
  750. # define stdc_bit_ceil_ull(x) ((unsigned long long int) __bc64_inline (x))
  751. #endif
  752. __END_DECLS
  753. #endif /* _STDBIT_H */