uint-bset.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "uint-bset.h"
  26. /* The last 2 #include files should be in this order */
  27. #include "curl_memory.h"
  28. #include "memdebug.h"
  29. #ifdef DEBUGBUILD
  30. #define CURL_UINT_BSET_MAGIC 0x62757473
  31. #endif
  32. void Curl_uint_bset_init(struct uint_bset *bset)
  33. {
  34. memset(bset, 0, sizeof(*bset));
  35. #ifdef DEBUGBUILD
  36. bset->init = CURL_UINT_BSET_MAGIC;
  37. #endif
  38. }
  39. CURLcode Curl_uint_bset_resize(struct uint_bset *bset, unsigned int nmax)
  40. {
  41. unsigned int nslots = (nmax < (UINT_MAX-63)) ?
  42. ((nmax + 63) / 64) : (UINT_MAX / 64);
  43. DEBUGASSERT(bset->init == CURL_UINT_BSET_MAGIC);
  44. if(nslots != bset->nslots) {
  45. curl_uint64_t *slots = calloc(nslots, sizeof(curl_uint64_t));
  46. if(!slots)
  47. return CURLE_OUT_OF_MEMORY;
  48. if(bset->slots) {
  49. memcpy(slots, bset->slots,
  50. (CURLMIN(nslots, bset->nslots) * sizeof(curl_uint64_t)));
  51. free(bset->slots);
  52. }
  53. bset->slots = slots;
  54. bset->nslots = nslots;
  55. bset->first_slot_used = 0;
  56. }
  57. return CURLE_OK;
  58. }
  59. void Curl_uint_bset_destroy(struct uint_bset *bset)
  60. {
  61. DEBUGASSERT(bset->init == CURL_UINT_BSET_MAGIC);
  62. free(bset->slots);
  63. memset(bset, 0, sizeof(*bset));
  64. }
  65. #ifdef UNITTESTS
  66. UNITTEST unsigned int Curl_uint_bset_capacity(struct uint_bset *bset)
  67. {
  68. return bset->nslots * 64;
  69. }
  70. #endif
  71. unsigned int Curl_uint_bset_count(struct uint_bset *bset)
  72. {
  73. unsigned int i;
  74. unsigned int n = 0;
  75. for(i = 0; i < bset->nslots; ++i) {
  76. if(bset->slots[i])
  77. n += CURL_POPCOUNT64(bset->slots[i]);
  78. }
  79. return n;
  80. }
  81. bool Curl_uint_bset_empty(struct uint_bset *bset)
  82. {
  83. unsigned int i;
  84. for(i = bset->first_slot_used; i < bset->nslots; ++i) {
  85. if(bset->slots[i])
  86. return FALSE;
  87. }
  88. return TRUE;
  89. }
  90. void Curl_uint_bset_clear(struct uint_bset *bset)
  91. {
  92. if(bset->nslots) {
  93. memset(bset->slots, 0, bset->nslots * sizeof(curl_uint64_t));
  94. bset->first_slot_used = UINT_MAX;
  95. }
  96. }
  97. bool Curl_uint_bset_add(struct uint_bset *bset, unsigned int i)
  98. {
  99. unsigned int islot = i / 64;
  100. if(islot >= bset->nslots)
  101. return FALSE;
  102. bset->slots[islot] |= ((curl_uint64_t)1 << (i % 64));
  103. if(islot < bset->first_slot_used)
  104. bset->first_slot_used = islot;
  105. return TRUE;
  106. }
  107. void Curl_uint_bset_remove(struct uint_bset *bset, unsigned int i)
  108. {
  109. size_t islot = i / 64;
  110. if(islot < bset->nslots)
  111. bset->slots[islot] &= ~((curl_uint64_t)1 << (i % 64));
  112. }
  113. bool Curl_uint_bset_contains(struct uint_bset *bset, unsigned int i)
  114. {
  115. unsigned int islot = i / 64;
  116. if(islot >= bset->nslots)
  117. return FALSE;
  118. return (bset->slots[islot] & ((curl_uint64_t)1 << (i % 64))) != 0;
  119. }
  120. bool Curl_uint_bset_first(struct uint_bset *bset, unsigned int *pfirst)
  121. {
  122. unsigned int i;
  123. for(i = bset->first_slot_used; i < bset->nslots; ++i) {
  124. if(bset->slots[i]) {
  125. *pfirst = (i * 64) + CURL_CTZ64(bset->slots[i]);
  126. bset->first_slot_used = i;
  127. return TRUE;
  128. }
  129. }
  130. bset->first_slot_used = *pfirst = UINT_MAX;
  131. return FALSE;
  132. }
  133. bool Curl_uint_bset_next(struct uint_bset *bset, unsigned int last,
  134. unsigned int *pnext)
  135. {
  136. unsigned int islot;
  137. curl_uint64_t x;
  138. ++last; /* look for number one higher than last */
  139. islot = last / 64; /* the slot this would be in */
  140. if(islot < bset->nslots) {
  141. /* shift away the bits we already iterated in this slot */
  142. x = (bset->slots[islot] >> (last % 64));
  143. if(x) {
  144. /* more bits set, next is `last` + trailing0s of the shifted slot */
  145. *pnext = last + CURL_CTZ64(x);
  146. return TRUE;
  147. }
  148. /* no more bits set in the last slot, scan forward */
  149. for(islot = islot + 1; islot < bset->nslots; ++islot) {
  150. if(bset->slots[islot]) {
  151. *pnext = (islot * 64) + CURL_CTZ64(bset->slots[islot]);
  152. return TRUE;
  153. }
  154. }
  155. }
  156. *pnext = UINT_MAX; /* a value we cannot store */
  157. return FALSE;
  158. }
  159. #ifdef CURL_POPCOUNT64_IMPLEMENT
  160. unsigned int Curl_popcount64(curl_uint64_t x)
  161. {
  162. /* Compute the "Hamming Distance" between 'x' and 0,
  163. * which is the number of set bits in 'x'.
  164. * See: https://en.wikipedia.org/wiki/Hamming_weight */
  165. const curl_uint64_t m1 = 0x5555555555555555LL; /* 0101+ */
  166. const curl_uint64_t m2 = 0x3333333333333333LL; /* 00110011+ */
  167. const curl_uint64_t m4 = 0x0f0f0f0f0f0f0f0fLL; /* 00001111+ */
  168. /* 1 + 256^1 + 256^2 + 256^3 + ... + 256^7 */
  169. const curl_uint64_t h01 = 0x0101010101010101LL;
  170. x -= (x >> 1) & m1; /* replace every 2 bits with bits present */
  171. x = (x & m2) + ((x >> 2) & m2); /* replace every nibble with bits present */
  172. x = (x + (x >> 4)) & m4; /* replace every byte with bits present */
  173. /* top 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... which makes the
  174. * top byte the sum of all individual 8 bytes, throw away the rest */
  175. return (unsigned int)((x * h01) >> 56);
  176. }
  177. #endif /* CURL_POPCOUNT64_IMPLEMENT */
  178. #ifdef CURL_CTZ64_IMPLEMENT
  179. unsigned int Curl_ctz64(curl_uint64_t x)
  180. {
  181. /* count trailing zeros in a curl_uint64_t.
  182. * divide and conquer to find the number of lower 0 bits */
  183. const curl_uint64_t ml32 = 0xFFFFFFFF; /* lower 32 bits */
  184. const curl_uint64_t ml16 = 0x0000FFFF; /* lower 16 bits */
  185. const curl_uint64_t ml8 = 0x000000FF; /* lower 8 bits */
  186. const curl_uint64_t ml4 = 0x0000000F; /* lower 4 bits */
  187. const curl_uint64_t ml2 = 0x00000003; /* lower 2 bits */
  188. unsigned int n;
  189. if(!x)
  190. return 64;
  191. n = 1;
  192. if(!(x & ml32)) {
  193. n = n + 32;
  194. x = x >> 32;
  195. }
  196. if(!(x & ml16)) {
  197. n = n + 16;
  198. x = x >> 16;
  199. }
  200. if(!(x & ml8)) {
  201. n = n + 8;
  202. x = x >> 8;
  203. }
  204. if(!(x & ml4)) {
  205. n = n + 4;
  206. x = x >> 4;
  207. }
  208. if(!(x & ml2)) {
  209. n = n + 2;
  210. x = x >> 2;
  211. }
  212. return n - (unsigned int)(x & 1);
  213. }
  214. #endif /* CURL_CTZ64_IMPLEMENT */