Skip to content

Commit

Permalink
Run unit tests in 64 and 32 bit mode. Fix #41 and should prevent 32-b…
Browse files Browse the repository at this point in the history
…it bugs in the future
  • Loading branch information
Hackerpilot committed Mar 28, 2016
1 parent e5c21fc commit 84692a5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
*.lst
*.s
test/tests
test/tests_32
test/looptest
perf.data
doc/
.dub/
dub.selections.json
test/compile_test
test/compile_test_32
test/external_allocator_test
test/external_allocator_test_32
.gdb_history
29 changes: 29 additions & 0 deletions src/containers/internal/backwards.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module containers.internal.backwards;

static if (__VERSION__ < 2071)
{
/// 64-bit popcnt
int popcnt(ulong v) pure nothrow @nogc @safe
{
import core.bitop : popcnt;

return popcnt(cast(uint) v) + popcnt(cast(uint)(v >>> 32));
}

version (X86_64)
public import core.bitop : bsf;
else
{
/// Allow 64-bit bsf on old compilers
int bsf(ulong v) pure nothrow @nogc @safe
{
import core.bitop : bsf;

immutable uint lower = cast(uint) v;
immutable uint upper = cast(uint)(v >>> 32);
return lower == 0 ? bsf(upper) + 32 : bsf(lower);
}
}
}
else
public import core.bitop : bsf, popcnt;
15 changes: 9 additions & 6 deletions src/containers/ttree.d
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ private:
alias N = FatNodeInfo!(T.sizeof, 3, cacheLineSize, ulong.sizeof);
enum size_t nodeCapacity = N[0];
alias BookkeepingType = N[1];
static assert (nodeCapacity <= (uint.sizeof * 8), "cannot fit height info and registry in ulong");
enum HEIGHT_BIT_OFFSET = 48UL;
static assert (nodeCapacity <= HEIGHT_BIT_OFFSET, "cannot fit height info and registry in ulong");
enum fullBitPattern = fullBits!(ulong, nodeCapacity);

enum RangeType : ubyte { all, lower, equal, upper }
Expand Down Expand Up @@ -454,7 +455,8 @@ private:
{
private size_t nextAvailableIndex() const nothrow pure @nogc @safe
{
import core.bitop : bsf;
import containers.internal.backwards : bsf;

return bsf(~(registry & fullBitPattern));
}

Expand Down Expand Up @@ -501,14 +503,15 @@ private:
immutable ulong l = left !is null ? left.height() : 0;
immutable ulong r = right !is null ? right.height() : 0;
immutable ulong h = 1 + (l > r ? l : r);
assert (h < ushort.max);
registry &= fullBitPattern;
registry |= (h << 32UL);
registry |= (h << HEIGHT_BIT_OFFSET);
return h;
}

ulong height() const nothrow pure @nogc @safe
{
return registry >>> 32UL;
return registry >>> HEIGHT_BIT_OFFSET;
}

int imbalanced() const nothrow pure @nogc @safe
Expand Down Expand Up @@ -720,7 +723,7 @@ private:
{
if (left is null && right is null)
{
size_t i = nextAvailableIndex() - 1;
immutable size_t i = nextAvailableIndex() - 1;
Value r = values[i];
markUnused(i);
return r;
Expand Down Expand Up @@ -871,7 +874,7 @@ private:
Node* parent;

Value[nodeCapacity] values;
ulong registry = 1UL << 32UL;
ulong registry = 1UL << HEIGHT_BIT_OFFSET;
}

AllocatorType allocator;
Expand Down
34 changes: 13 additions & 21 deletions src/containers/unrolledlist.d
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct UnrolledList(T, Allocator = Mallocator,
*/
bool remove(T item)
{
import core.bitop : popcnt;
import containers.internal.backwards : popcnt;
if (_front is null)
return false;
bool retVal = false;
Expand Down Expand Up @@ -218,7 +218,7 @@ struct UnrolledList(T, Allocator = Mallocator,
}
body
{
import core.bitop : bsf, popcnt;
import containers.internal.backwards : bsf;
size_t index = bsf(_front.registry);
T r = _front.items[index];
_front.markUnused(index);
Expand Down Expand Up @@ -268,7 +268,7 @@ struct UnrolledList(T, Allocator = Mallocator,
}
body
{
import core.bitop: bsf;
import containers.internal.backwards : bsf;
import std.string: format;
size_t index = bsf(_front.registry);
assert (index < nodeCapacity, format("%d", index));
Expand Down Expand Up @@ -308,7 +308,7 @@ struct UnrolledList(T, Allocator = Mallocator,
}
body
{
import core.bitop : popcnt;
import containers.internal.backwards : popcnt;
size_t i = nodeCapacity - 1;
while (_back.isFree(i))
{
Expand Down Expand Up @@ -346,13 +346,15 @@ struct UnrolledList(T, Allocator = Mallocator,

this(inout(Node)* current, size_t l)
{
import core.bitop: bsf;
import containers.internal.backwards : bsf;
version(assert) import std.format:format;

this.current = current;
this.length = l;
if (current !is null)
{
index = bsf(current.registry);
assert (index < nodeCapacity);
assert (index < nodeCapacity, "index = %d, nodeCapacity = %d".format(index, nodeCapacity));
}
}

Expand Down Expand Up @@ -456,22 +458,12 @@ private:

static bool shouldMerge(const Node* first, const Node* second)
{
import core.bitop : popcnt;
import containers.internal.backwards : popcnt;

if (first is null || second is null)
return false;
static if (first.registry.sizeof > uint.sizeof)
{
immutable f = popcnt(cast(uint) first.registry)
+ popcnt(cast(uint) (first.registry >>> 32));
immutable s = popcnt(cast(uint) second.registry)
+ popcnt(cast(uint) (second.registry >>> 32));
}
else
{
immutable f = popcnt(first.registry);
immutable s = popcnt(second.registry);
}
immutable f = popcnt(first.registry);
immutable s = popcnt(second.registry);
return f + s <= nodeCapacity;
}

Expand All @@ -484,7 +476,7 @@ private:
}
body
{
import core.bitop: bsf;
import containers.internal.backwards : bsf;
size_t i;
ContainerStorageType!T[nodeCapacity] temp;
foreach (j; 0 .. nodeCapacity)
Expand All @@ -505,7 +497,7 @@ private:
{
size_t nextAvailableIndex() const nothrow pure @safe @nogc
{
import core.bitop: bsf;
import containers.internal.backwards : bsf;
return bsf(~registry);
}

Expand Down
24 changes: 18 additions & 6 deletions test/makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
.PHONY: test graphs clean

SRC=$(shell find ../src/ -name "*.d") $(shell find ../experimental_allocator -name "*.d")
FLAGS=-unittest -main -g -cov -I../src/ -debug -wi
SRC:=$(shell find ../src/ -name "*.d") $(shell find ../experimental_allocator -name "*.d")
FLAGS:=-unittest -main -g -cov -I../src/ -debug -wi
FLAGS32:=$(FLAGS) -m32

test: compile_test external_allocator_test
dmd $(FLAGS) $(SRC) -oftests
all: test test_32 compile_test compile_test_32 external_allocator_test external_allocator_test_32
./tests
./tests_32
./compile_test
./compile_test_32
./external_allocator_test
./external_allocator_test_32

test: $(SRC)
dmd $(FLAGS) $(SRC) -oftests
test_32: $(SRC)
dmd $(FLAGS32) $(SRC) -oftests_32

compile_test: compile_test.d
compile_test: compile_test.d $(SRC)
dmd $(FLAGS) compile_test.d $(SRC) -ofcompile_test
compile_test_32: compile_test.d $(SRC)
dmd $(FLAGS32) compile_test.d $(SRC) -ofcompile_test_32

external_allocator_test: external_allocator_test.d
external_allocator_test: external_allocator_test.d $(SRC)
dmd $(FLAGS) external_allocator_test.d $(SRC) -ofexternal_allocator_test
external_allocator_test_32: external_allocator_test.d $(SRC)
dmd $(FLAGS32) external_allocator_test.d $(SRC) -ofexternal_allocator_test_32

looptest: looptest.d
dmd looptest.d -inline -O -release \
Expand Down

0 comments on commit 84692a5

Please sign in to comment.