Skip to content

Commit 4061bee

Browse files
authored
Merge pull request #10620 from MoonlightSentinel/fix-bitarray
Fix Issue 20418: Unittest failure in bitarray.d on Win32 merged-on-behalf-of: Rainer Schuetze <rainers@users.noreply.github.com>
2 parents ebea763 + e719175 commit 4061bee

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/dmd/root/bitarray.d

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ nothrow:
3232

3333
void length(size_t nlen) pure nothrow
3434
{
35-
immutable ochunks = ( len + BitsPerChunk - 1) / BitsPerChunk;
36-
immutable nchunks = (nlen + BitsPerChunk - 1) / BitsPerChunk;
35+
immutable ochunks = chunks(len);
36+
immutable nchunks = chunks(nlen);
3737
if (ochunks != nchunks)
3838
{
3939
ptr = cast(size_t*)mem.xrealloc_noscan(ptr, nchunks * ChunkSize);
@@ -50,7 +50,7 @@ nothrow:
5050
if (!len)
5151
length(b.len);
5252
assert(len == b.len);
53-
memcpy(ptr, b.ptr, (len + BitsPerChunk - 1) / 8);
53+
memcpy(ptr, b.ptr, bytes(len));
5454
}
5555

5656
bool opIndex(size_t idx) const pure nothrow @nogc
@@ -74,12 +74,12 @@ nothrow:
7474

7575
bool opEquals(const ref BitArray b) const
7676
{
77-
return len == b.len && memcmp(ptr, b.ptr, (len + BitsPerChunk - 1) / 8) == 0;
77+
return len == b.len && memcmp(ptr, b.ptr, bytes(len)) == 0;
7878
}
7979

8080
void zero()
8181
{
82-
memset(ptr, 0, (len + BitsPerChunk - 1) / 8);
82+
memset(ptr, 0, bytes(len));
8383
}
8484

8585
/******
@@ -88,7 +88,7 @@ nothrow:
8888
*/
8989
bool isZero()
9090
{
91-
const nchunks = (len + BitsPerChunk - 1) / BitsPerChunk;
91+
const nchunks = chunks(len);
9292
foreach (i; 0 .. nchunks)
9393
{
9494
if (ptr[i])
@@ -100,7 +100,7 @@ nothrow:
100100
void or(const ref BitArray b)
101101
{
102102
assert(len == b.len);
103-
const nchunks = (len + BitsPerChunk - 1) / BitsPerChunk;
103+
const nchunks = chunks(len);
104104
foreach (i; 0 .. nchunks)
105105
ptr[i] |= b.ptr[i];
106106
}
@@ -110,7 +110,7 @@ nothrow:
110110
void swap(ref BitArray b)
111111
{
112112
assert(len == b.len);
113-
const nchunks = (len + BitsPerChunk - 1) / BitsPerChunk;
113+
const nchunks = chunks(len);
114114
foreach (i; 0 .. nchunks)
115115
{
116116
const chunk = ptr[i];
@@ -124,7 +124,7 @@ nothrow:
124124
debug
125125
{
126126
// Stomp the allocated memory
127-
const nchunks = (len + BitsPerChunk - 1) / BitsPerChunk;
127+
const nchunks = chunks(len);
128128
foreach (i; 0 .. nchunks)
129129
{
130130
ptr[i] = cast(Chunk_t)0xFEFEFEFE_FEFEFEFE;
@@ -142,6 +142,18 @@ nothrow:
142142
private:
143143
size_t len; // length in bits
144144
size_t *ptr;
145+
146+
/// Returns: The amount of chunks used to store len bits
147+
static size_t chunks(const size_t len) @nogc nothrow pure @safe
148+
{
149+
return (len + BitsPerChunk - 1) / BitsPerChunk;
150+
}
151+
152+
/// Returns: The amount of bytes used to store len bits
153+
static size_t bytes(const size_t len) @nogc nothrow pure @safe
154+
{
155+
return chunks(len) * ChunkSize;
156+
}
145157
}
146158

147159
unittest

0 commit comments

Comments
 (0)