2
0

stdbit.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (C) 2023 Rémi Denis-Courmont
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #ifndef __STDC_VERSION_STDBIT_H__
  19. #define __STDC_VERSION_STDBIT_H__ 202311L
  20. #include <stdbool.h>
  21. #include <limits.h> /* CHAR_BIT */
  22. #define __STDC_ENDIAN_LITTLE__ 1234
  23. #define __STDC_ENDIAN_BIG__ 4321
  24. #ifdef __BYTE_ORDER__
  25. # if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  26. # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
  27. # elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  28. # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__
  29. # else
  30. # define __STDC_ENDIAN_NATIVE__ 3412
  31. # endif
  32. #elif defined(_MSC_VER)
  33. # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
  34. #else
  35. # error Not implemented.
  36. #endif
  37. #define __stdbit_generic_type_func(func, value) \
  38. _Generic (value, \
  39. unsigned long long: stdc_##func##_ull((unsigned long long)(value)), \
  40. unsigned long: stdc_##func##_ul((unsigned long)(value)), \
  41. unsigned int: stdc_##func##_ui((unsigned int)(value)), \
  42. unsigned short: stdc_##func##_us((unsigned short)(value)), \
  43. unsigned char: stdc_##func##_uc((unsigned char)(value)))
  44. #if defined (__GNUC__) || defined (__clang__)
  45. static inline unsigned int stdc_leading_zeros_ull(unsigned long long value)
  46. {
  47. return value ? __builtin_clzll(value) : (CHAR_BIT * sizeof (value));
  48. }
  49. static inline unsigned int stdc_leading_zeros_ul(unsigned long value)
  50. {
  51. return value ? __builtin_clzl(value) : (CHAR_BIT * sizeof (value));
  52. }
  53. static inline unsigned int stdc_leading_zeros_ui(unsigned int value)
  54. {
  55. return value ? __builtin_clz(value) : (CHAR_BIT * sizeof (value));
  56. }
  57. static inline unsigned int stdc_leading_zeros_us(unsigned short value)
  58. {
  59. return stdc_leading_zeros_ui(value)
  60. - CHAR_BIT * (sizeof (int) - sizeof (value));
  61. }
  62. static inline unsigned int stdc_leading_zeros_uc(unsigned char value)
  63. {
  64. return stdc_leading_zeros_ui(value) - (CHAR_BIT * (sizeof (int) - 1));
  65. }
  66. #else
  67. static inline unsigned int __stdc_leading_zeros(unsigned long long value,
  68. unsigned int size)
  69. {
  70. unsigned int zeros = size * CHAR_BIT;
  71. while (value != 0) {
  72. value >>= 1;
  73. zeros--;
  74. }
  75. return zeros;
  76. }
  77. static inline unsigned int stdc_leading_zeros_ull(unsigned long long value)
  78. {
  79. return __stdc_leading_zeros(value, sizeof (value));
  80. }
  81. static inline unsigned int stdc_leading_zeros_ul(unsigned long value)
  82. {
  83. return __stdc_leading_zeros(value, sizeof (value));
  84. }
  85. static inline unsigned int stdc_leading_zeros_ui(unsigned int value)
  86. {
  87. return __stdc_leading_zeros(value, sizeof (value));
  88. }
  89. static inline unsigned int stdc_leading_zeros_us(unsigned short value)
  90. {
  91. return __stdc_leading_zeros(value, sizeof (value));
  92. }
  93. static inline unsigned int stdc_leading_zeros_uc(unsigned char value)
  94. {
  95. return __stdc_leading_zeros(value, sizeof (value));
  96. }
  97. #endif
  98. #define stdc_leading_zeros(value) \
  99. __stdbit_generic_type_func(leading_zeros, value)
  100. static inline unsigned int stdc_leading_ones_ull(unsigned long long value)
  101. {
  102. return stdc_leading_zeros_ull(~value);
  103. }
  104. static inline unsigned int stdc_leading_ones_ul(unsigned long value)
  105. {
  106. return stdc_leading_zeros_ul(~value);
  107. }
  108. static inline unsigned int stdc_leading_ones_ui(unsigned int value)
  109. {
  110. return stdc_leading_zeros_ui(~value);
  111. }
  112. static inline unsigned int stdc_leading_ones_us(unsigned short value)
  113. {
  114. return stdc_leading_zeros_us(~value);
  115. }
  116. static inline unsigned int stdc_leading_ones_uc(unsigned char value)
  117. {
  118. return stdc_leading_zeros_uc(~value);
  119. }
  120. #define stdc_leading_ones(value) \
  121. __stdbit_generic_type_func(leading_ones, value)
  122. #if defined (__GNUC__) || defined (__clang__)
  123. static inline unsigned int stdc_trailing_zeros_ull(unsigned long long value)
  124. {
  125. return value ? (unsigned int)__builtin_ctzll(value)
  126. : (CHAR_BIT * sizeof (value));
  127. }
  128. static inline unsigned int stdc_trailing_zeros_ul(unsigned long value)
  129. {
  130. return value ? (unsigned int)__builtin_ctzl(value)
  131. : (CHAR_BIT * sizeof (value));
  132. }
  133. static inline unsigned int stdc_trailing_zeros_ui(unsigned int value)
  134. {
  135. return value ? (unsigned int)__builtin_ctz(value)
  136. : (CHAR_BIT * sizeof (value));
  137. }
  138. static inline unsigned int stdc_trailing_zeros_us(unsigned short value)
  139. {
  140. return value ? (unsigned int)__builtin_ctz(value)
  141. : (CHAR_BIT * sizeof (value));
  142. }
  143. static inline unsigned int stdc_trailing_zeros_uc(unsigned char value)
  144. {
  145. return value ? (unsigned int)__builtin_ctz(value)
  146. : (CHAR_BIT * sizeof (value));
  147. }
  148. #else
  149. static inline unsigned int __stdc_trailing_zeros(unsigned long long value,
  150. unsigned int size)
  151. {
  152. unsigned int zeros = 0;
  153. if (!value)
  154. return size * CHAR_BIT;
  155. while ((value & 1) == 0) {
  156. value >>= 1;
  157. zeros++;
  158. }
  159. return zeros;
  160. }
  161. static inline unsigned int stdc_trailing_zeros_ull(unsigned long long value)
  162. {
  163. return __stdc_trailing_zeros(value, sizeof (value));
  164. }
  165. static inline unsigned int stdc_trailing_zeros_ul(unsigned long value)
  166. {
  167. return __stdc_trailing_zeros(value, sizeof (value));
  168. }
  169. static inline unsigned int stdc_trailing_zeros_ui(unsigned int value)
  170. {
  171. return __stdc_trailing_zeros(value, sizeof (value));
  172. }
  173. static inline unsigned int stdc_trailing_zeros_us(unsigned short value)
  174. {
  175. return __stdc_trailing_zeros(value, sizeof (value));
  176. }
  177. static inline unsigned int stdc_trailing_zeros_uc(unsigned char value)
  178. {
  179. return __stdc_trailing_zeros(value, sizeof (value));
  180. }
  181. #endif
  182. #define stdc_trailing_zeros(value) \
  183. __stdbit_generic_type_func(trailing_zeros, value)
  184. static inline unsigned int stdc_trailing_ones_ull(unsigned long long value)
  185. {
  186. return stdc_trailing_zeros_ull(~value);
  187. }
  188. static inline unsigned int stdc_trailing_ones_ul(unsigned long value)
  189. {
  190. return stdc_trailing_zeros_ul(~value);
  191. }
  192. static inline unsigned int stdc_trailing_ones_ui(unsigned int value)
  193. {
  194. return stdc_trailing_zeros_ui(~value);
  195. }
  196. static inline unsigned int stdc_trailing_ones_us(unsigned short value)
  197. {
  198. return stdc_trailing_zeros_us(~value);
  199. }
  200. static inline unsigned int stdc_trailing_ones_uc(unsigned char value)
  201. {
  202. return stdc_trailing_zeros_uc(~value);
  203. }
  204. #define stdc_trailing_ones(value) \
  205. __stdbit_generic_type_func(trailing_ones, value)
  206. static inline unsigned int stdc_first_leading_one_ull(unsigned long long value)
  207. {
  208. return value ? (stdc_leading_zeros_ull(value) + 1) : 0;
  209. }
  210. static inline unsigned int stdc_first_leading_one_ul(unsigned long value)
  211. {
  212. return value ? (stdc_leading_zeros_ul(value) + 1) : 0;
  213. }
  214. static inline unsigned int stdc_first_leading_one_ui(unsigned int value)
  215. {
  216. return value ? (stdc_leading_zeros_ui(value) + 1) : 0;
  217. }
  218. static inline unsigned int stdc_first_leading_one_us(unsigned short value)
  219. {
  220. return value ? (stdc_leading_zeros_us(value) + 1) : 0;
  221. }
  222. static inline unsigned int stdc_first_leading_one_uc(unsigned char value)
  223. {
  224. return value ? (stdc_leading_zeros_uc(value) + 1) : 0;
  225. }
  226. #define stdc_first_leading_one(value) \
  227. __stdbit_generic_type_func(first_leading_one, value)
  228. static inline unsigned int stdc_first_leading_zero_ull(unsigned long long value)
  229. {
  230. return stdc_leading_ones_ull(~value);
  231. }
  232. static inline unsigned int stdc_first_leading_zero_ul(unsigned long value)
  233. {
  234. return stdc_leading_ones_ul(~value);
  235. }
  236. static inline unsigned int stdc_first_leading_zero_ui(unsigned int value)
  237. {
  238. return stdc_leading_ones_ui(~value);
  239. }
  240. static inline unsigned int stdc_first_leading_zero_us(unsigned short value)
  241. {
  242. return stdc_leading_ones_us(~value);
  243. }
  244. static inline unsigned int stdc_first_leading_zero_uc(unsigned char value)
  245. {
  246. return stdc_leading_ones_uc(~value);
  247. }
  248. #define stdc_first_leading_zero(value) \
  249. __stdbit_generic_type_func(first_leading_zero, value)
  250. #if defined (__GNUC__) || defined (__clang__)
  251. static inline unsigned int stdc_first_trailing_one_ull(unsigned long long value)
  252. {
  253. return __builtin_ffsll(value);
  254. }
  255. static inline unsigned int stdc_first_trailing_one_ul(unsigned long value)
  256. {
  257. return __builtin_ffsl(value);
  258. }
  259. static inline unsigned int stdc_first_trailing_one_ui(unsigned int value)
  260. {
  261. return __builtin_ffs(value);
  262. }
  263. static inline unsigned int stdc_first_trailing_one_us(unsigned short value)
  264. {
  265. return __builtin_ffs(value);
  266. }
  267. static inline unsigned int stdc_first_trailing_one_uc(unsigned char value)
  268. {
  269. return __builtin_ffs(value);
  270. }
  271. #else
  272. static inline unsigned int stdc_first_trailing_one_ull(unsigned long long value)
  273. {
  274. return value ? (1 + stdc_trailing_zeros_ull(value)) : 0;
  275. }
  276. static inline unsigned int stdc_first_trailing_one_ul(unsigned long value)
  277. {
  278. return value ? (1 + stdc_trailing_zeros_ul(value)) : 0;
  279. }
  280. static inline unsigned int stdc_first_trailing_one_ui(unsigned int value)
  281. {
  282. return value ? (1 + stdc_trailing_zeros_ui(value)) : 0;
  283. }
  284. static inline unsigned int stdc_first_trailing_one_us(unsigned short value)
  285. {
  286. return value ? (1 + stdc_trailing_zeros_us(value)) : 0;
  287. }
  288. static inline unsigned int stdc_first_trailing_one_uc(unsigned char value)
  289. {
  290. return value ? (1 + stdc_trailing_zeros_uc(value)) : 0;
  291. }
  292. #endif
  293. #define stdc_first_trailing_one(value) \
  294. __stdbit_generic_type_func(first_trailing_one, value)
  295. static inline unsigned int stdc_first_trailing_zero_ull(unsigned long long value)
  296. {
  297. return stdc_first_trailing_one_ull(~value);
  298. }
  299. static inline unsigned int stdc_first_trailing_zero_ul(unsigned long value)
  300. {
  301. return stdc_first_trailing_one_ul(~value);
  302. }
  303. static inline unsigned int stdc_first_trailing_zero_ui(unsigned int value)
  304. {
  305. return stdc_first_trailing_one_ui(~value);
  306. }
  307. static inline unsigned int stdc_first_trailing_zero_us(unsigned short value)
  308. {
  309. return stdc_first_trailing_one_us(~value);
  310. }
  311. static inline unsigned int stdc_first_trailing_zero_uc(unsigned char value)
  312. {
  313. return stdc_first_trailing_one_uc(~value);
  314. }
  315. #define stdc_first_trailing_zero(value) \
  316. __stdbit_generic_type_func(first_trailing_zero, value)
  317. #if defined (__GNUC__) || defined (__clang__)
  318. static inline unsigned int stdc_count_ones_ull(unsigned long long value)
  319. {
  320. return __builtin_popcountll(value);
  321. }
  322. static inline unsigned int stdc_count_ones_ul(unsigned long value)
  323. {
  324. return __builtin_popcountl(value);
  325. }
  326. static inline unsigned int stdc_count_ones_ui(unsigned int value)
  327. {
  328. return __builtin_popcount(value);
  329. }
  330. static inline unsigned int stdc_count_ones_us(unsigned short value)
  331. {
  332. return __builtin_popcount(value);
  333. }
  334. static inline unsigned int stdc_count_ones_uc(unsigned char value)
  335. {
  336. return __builtin_popcount(value);
  337. }
  338. #else
  339. static inline unsigned int __stdc_count_ones(unsigned long long value,
  340. unsigned int size)
  341. {
  342. unsigned int ones = 0;
  343. for (unsigned int c = 0; c < (size * CHAR_BIT); c++) {
  344. ones += value & 1;
  345. value >>= 1;
  346. }
  347. return ones;
  348. }
  349. static inline unsigned int stdc_count_ones_ull(unsigned long long value)
  350. {
  351. return __stdc_count_ones(value, sizeof (value));
  352. }
  353. static inline unsigned int stdc_count_ones_ul(unsigned long value)
  354. {
  355. return __stdc_count_ones(value, sizeof (value));
  356. }
  357. static inline unsigned int stdc_count_ones_ui(unsigned int value)
  358. {
  359. return __stdc_count_ones(value, sizeof (value));
  360. }
  361. static inline unsigned int stdc_count_ones_us(unsigned short value)
  362. {
  363. return __stdc_count_ones(value, sizeof (value));
  364. }
  365. static inline unsigned int stdc_count_ones_uc(unsigned char value)
  366. {
  367. return __stdc_count_ones(value, sizeof (value));
  368. }
  369. #endif
  370. #define stdc_count_ones(value) \
  371. __stdbit_generic_type_func(count_ones, value)
  372. static inline unsigned int stdc_count_zeros_ull(unsigned long long value)
  373. {
  374. return stdc_count_ones_ull(~value);
  375. }
  376. static inline unsigned int stdc_count_zeros_ul(unsigned long value)
  377. {
  378. return stdc_count_ones_ul(~value);
  379. }
  380. static inline unsigned int stdc_count_zeros_ui(unsigned int value)
  381. {
  382. return stdc_count_ones_ui(~value);
  383. }
  384. static inline unsigned int stdc_count_zeros_us(unsigned short value)
  385. {
  386. return stdc_count_ones_us(~value);
  387. }
  388. static inline unsigned int stdc_count_zeros_uc(unsigned char value)
  389. {
  390. return stdc_count_ones_uc(~value);
  391. }
  392. #define stdc_count_zeros(value) \
  393. __stdbit_generic_type_func(count_zeros, value)
  394. static inline bool stdc_has_single_bit_ull(unsigned long long value)
  395. {
  396. return value && (value & (value - 1)) == 0;
  397. }
  398. static inline bool stdc_has_single_bit_ul(unsigned long value)
  399. {
  400. return value && (value & (value - 1)) == 0;
  401. }
  402. static inline bool stdc_has_single_bit_ui(unsigned int value)
  403. {
  404. return value && (value & (value - 1)) == 0;
  405. }
  406. static inline bool stdc_has_single_bit_us(unsigned short value)
  407. {
  408. return value && (value & (value - 1)) == 0;
  409. }
  410. static inline bool stdc_has_single_bit_uc(unsigned char value)
  411. {
  412. return value && (value & (value - 1)) == 0;
  413. }
  414. #define stdc_has_single_bit(value) \
  415. __stdbit_generic_type_func(has_single_bit, value)
  416. static inline unsigned int stdc_bit_width_ull(unsigned long long value)
  417. {
  418. return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ull(value);
  419. }
  420. static inline unsigned int stdc_bit_width_ul(unsigned long value)
  421. {
  422. return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ul(value);
  423. }
  424. static inline unsigned int stdc_bit_width_ui(unsigned int value)
  425. {
  426. return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ui(value);
  427. }
  428. static inline unsigned int stdc_bit_width_us(unsigned short value)
  429. {
  430. return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_us(value);
  431. }
  432. static inline unsigned int stdc_bit_width_uc(unsigned char value)
  433. {
  434. return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_uc(value);
  435. }
  436. #define stdc_bit_width(value) \
  437. __stdbit_generic_type_func(bit_width, value)
  438. static inline unsigned long long stdc_bit_floor_ull(unsigned long long value)
  439. {
  440. return value ? (1ULL << (stdc_bit_width_ull(value) - 1)) : 0ULL;
  441. }
  442. static inline unsigned long stdc_bit_floor_ul(unsigned long value)
  443. {
  444. return value ? (1UL << (stdc_bit_width_ul(value) - 1)) : 0UL;
  445. }
  446. static inline unsigned int stdc_bit_floor_ui(unsigned int value)
  447. {
  448. return value ? (1U << (stdc_bit_width_ui(value) - 1)) : 0U;
  449. }
  450. static inline unsigned short stdc_bit_floor_us(unsigned short value)
  451. {
  452. return value ? (1U << (stdc_bit_width_us(value) - 1)) : 0U;
  453. }
  454. static inline unsigned int stdc_bit_floor_uc(unsigned char value)
  455. {
  456. return value ? (1U << (stdc_bit_width_uc(value) - 1)) : 0U;
  457. }
  458. #define stdc_bit_floor(value) \
  459. __stdbit_generic_type_func(bit_floor, value)
  460. /* NOTE: Bit ceiling undefines overflow. */
  461. static inline unsigned long long stdc_bit_ceil_ull(unsigned long long value)
  462. {
  463. return 1ULL << (value ? stdc_bit_width_ull(value - 1) : 0);
  464. }
  465. static inline unsigned long stdc_bit_ceil_ul(unsigned long value)
  466. {
  467. return 1UL << (value ? stdc_bit_width_ul(value - 1) : 0);
  468. }
  469. static inline unsigned int stdc_bit_ceil_ui(unsigned int value)
  470. {
  471. return 1U << (value ? stdc_bit_width_ui(value - 1) : 0);
  472. }
  473. static inline unsigned short stdc_bit_ceil_us(unsigned short value)
  474. {
  475. return 1U << (value ? stdc_bit_width_us(value - 1) : 0);
  476. }
  477. static inline unsigned int stdc_bit_ceil_uc(unsigned char value)
  478. {
  479. return 1U << (value ? stdc_bit_width_uc(value - 1) : 0);
  480. }
  481. #define stdc_bit_ceil(value) \
  482. __stdbit_generic_type_func(bit_ceil, value)
  483. #endif /* __STDC_VERSION_STDBIT_H__ */