Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/uint-bset.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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
26
#include "uint-bset.h"
27
28
#ifdef DEBUGBUILD
29
#define CURL_UINT32_BSET_MAGIC  0x62757473
30
#endif
31
32
void Curl_uint32_bset_init(struct uint32_bset *bset)
33
0
{
34
0
  memset(bset, 0, sizeof(*bset));
35
#ifdef DEBUGBUILD
36
  bset->init = CURL_UINT32_BSET_MAGIC;
37
#endif
38
0
}
39
40
CURLcode Curl_uint32_bset_resize(struct uint32_bset *bset, uint32_t nmax)
41
0
{
42
0
  uint32_t nslots = (nmax < (UINT32_MAX - 63)) ?
43
0
                    ((nmax + 63) / 64) : (UINT32_MAX / 64);
44
45
0
  DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
46
0
  if(nslots != bset->nslots) {
47
0
    uint64_t *slots = curlx_calloc(nslots, sizeof(uint64_t));
48
0
    if(!slots)
49
0
      return CURLE_OUT_OF_MEMORY;
50
51
0
    if(bset->slots) {
52
0
      memcpy(slots, bset->slots,
53
0
             (CURLMIN(nslots, bset->nslots) * sizeof(uint64_t)));
54
0
      curlx_free(bset->slots);
55
0
    }
56
0
    bset->slots = slots;
57
0
    bset->nslots = nslots;
58
0
    bset->first_slot_used = 0;
59
0
  }
60
0
  return CURLE_OK;
61
0
}
62
63
void Curl_uint32_bset_destroy(struct uint32_bset *bset)
64
0
{
65
0
  DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
66
0
  curlx_free(bset->slots);
67
0
  memset(bset, 0, sizeof(*bset));
68
0
}
69
70
#ifdef UNITTESTS
71
/* @unittest 3211 */
72
UNITTEST uint32_t uint32_bset_capacity(struct uint32_bset *bset);
73
UNITTEST uint32_t uint32_bset_capacity(struct uint32_bset *bset)
74
{
75
  return bset->nslots * 64;
76
}
77
#endif
78
79
uint32_t Curl_uint32_bset_count(struct uint32_bset *bset)
80
0
{
81
0
  uint32_t i;
82
0
  uint32_t n = 0;
83
0
  for(i = 0; i < bset->nslots; ++i) {
84
0
    if(bset->slots[i])
85
0
      n += CURL_POPCOUNT64(bset->slots[i]);
86
0
  }
87
0
  return n;
88
0
}
89
90
bool Curl_uint32_bset_empty(struct uint32_bset *bset)
91
0
{
92
0
  uint32_t i;
93
0
  for(i = bset->first_slot_used; i < bset->nslots; ++i) {
94
0
    if(bset->slots[i])
95
0
      return FALSE;
96
0
  }
97
0
  return TRUE;
98
0
}
99
100
void Curl_uint32_bset_clear(struct uint32_bset *bset)
101
0
{
102
0
  if(bset->nslots) {
103
0
    memset(bset->slots, 0, bset->nslots * sizeof(uint64_t));
104
0
    bset->first_slot_used = UINT32_MAX;
105
0
  }
106
0
}
107
108
bool Curl_uint32_bset_add(struct uint32_bset *bset, uint32_t i)
109
0
{
110
0
  uint32_t islot = i / 64;
111
0
  if(islot >= bset->nslots)
112
0
    return FALSE;
113
0
  bset->slots[islot] |= ((uint64_t)1 << (i % 64));
114
0
  if(islot < bset->first_slot_used)
115
0
    bset->first_slot_used = islot;
116
0
  return TRUE;
117
0
}
118
119
void Curl_uint32_bset_remove(struct uint32_bset *bset, uint32_t i)
120
0
{
121
0
  size_t islot = i / 64;
122
0
  if(islot < bset->nslots)
123
0
    bset->slots[islot] &= ~((uint64_t)1 << (i % 64));
124
0
}
125
126
bool Curl_uint32_bset_contains(struct uint32_bset *bset, uint32_t i)
127
0
{
128
0
  uint32_t islot = i / 64;
129
0
  if(islot >= bset->nslots)
130
0
    return FALSE;
131
0
  return (bset->slots[islot] & ((uint64_t)1 << (i % 64))) != 0;
132
0
}
133
134
bool Curl_uint32_bset_first(struct uint32_bset *bset, uint32_t *pfirst)
135
0
{
136
0
  uint32_t i;
137
0
  for(i = bset->first_slot_used; i < bset->nslots; ++i) {
138
0
    if(bset->slots[i]) {
139
0
      *pfirst = (i * 64) + CURL_CTZ64(bset->slots[i]);
140
0
      bset->first_slot_used = i;
141
0
      return TRUE;
142
0
    }
143
0
  }
144
0
  bset->first_slot_used = *pfirst = UINT32_MAX;
145
0
  return FALSE;
146
0
}
147
148
bool Curl_uint32_bset_next(struct uint32_bset *bset, uint32_t last,
149
                           uint32_t *pnext)
150
0
{
151
0
  uint32_t islot;
152
0
  uint64_t x;
153
154
0
  ++last; /* look for number one higher than last */
155
0
  islot = last / 64; /* the slot this would be in */
156
0
  if(islot < bset->nslots) {
157
    /* shift away the bits we already iterated in this slot */
158
0
    x = (bset->slots[islot] >> (last % 64));
159
0
    if(x) {
160
      /* more bits set, next is `last` + trailing0s of the shifted slot */
161
0
      *pnext = last + CURL_CTZ64(x);
162
0
      return TRUE;
163
0
    }
164
    /* no more bits set in the last slot, scan forward */
165
0
    for(islot = islot + 1; islot < bset->nslots; ++islot) {
166
0
      if(bset->slots[islot]) {
167
0
        *pnext = (islot * 64) + CURL_CTZ64(bset->slots[islot]);
168
0
        return TRUE;
169
0
      }
170
0
    }
171
0
  }
172
0
  *pnext = UINT32_MAX; /* a value we cannot store */
173
  return FALSE;
174
0
}
175
176
#ifdef CURL_POPCOUNT64_IMPLEMENT
177
uint32_t Curl_popcount64(uint64_t x)
178
{
179
  /* Compute the "Hamming Distance" between 'x' and 0,
180
   * which is the number of set bits in 'x'.
181
   * See: https://en.wikipedia.org/wiki/Hamming_weight */
182
  const uint64_t m1  = 0x5555555555555555LL; /* 0101+ */
183
  const uint64_t m2  = 0x3333333333333333LL; /* 00110011+ */
184
  const uint64_t m4  = 0x0f0f0f0f0f0f0f0fLL; /* 00001111+ */
185
   /* 1 + 256^1 + 256^2 + 256^3 + ... + 256^7 */
186
  const uint64_t h01 = 0x0101010101010101LL;
187
  x -= (x >> 1) & m1;             /* replace every 2 bits with bits present */
188
  x = (x & m2) + ((x >> 2) & m2); /* replace every nibble with bits present */
189
  x = (x + (x >> 4)) & m4;        /* replace every byte with bits present */
190
  /* top 8 bits of x + (x << 8) + (x << 16) + (x << 24) + ... which makes the
191
   * top byte the sum of all individual 8 bytes, throw away the rest */
192
  return (uint32_t)((x * h01) >> 56);
193
}
194
#endif /* CURL_POPCOUNT64_IMPLEMENT */
195
196
#ifdef CURL_CTZ64_IMPLEMENT
197
uint32_t Curl_ctz64(uint64_t x)
198
{
199
  /* count trailing zeros in a uint64_t.
200
   * divide and conquer to find the number of lower 0 bits */
201
  const uint64_t ml32 = 0xFFFFFFFF; /* lower 32 bits */
202
  const uint64_t ml16 = 0x0000FFFF; /* lower 16 bits */
203
  const uint64_t ml8  = 0x000000FF; /* lower 8 bits */
204
  const uint64_t ml4  = 0x0000000F; /* lower 4 bits */
205
  const uint64_t ml2  = 0x00000003; /* lower 2 bits */
206
  uint32_t n;
207
208
  if(!x)
209
    return 64;
210
  n = 1;
211
  if(!(x & ml32)) {
212
    n = n + 32;
213
    x = x >> 32;
214
  }
215
  if(!(x & ml16)) {
216
    n = n + 16;
217
    x = x >> 16;
218
  }
219
  if(!(x & ml8)) {
220
    n = n + 8;
221
    x = x >> 8;
222
  }
223
  if(!(x & ml4)) {
224
    n = n + 4;
225
    x = x >> 4;
226
  }
227
  if(!(x & ml2)) {
228
    n = n + 2;
229
    x = x >> 2;
230
  }
231
  return n - (uint32_t)(x & 1);
232
}
233
#endif /* CURL_CTZ64_IMPLEMENT */