| |
| Posted by Stewart Gordon | PermalinkReply |
|
Stewart Gordon
| Using GDC 0.17, Mac OS X.3.9.
While trying to figure out why my Sudoku solver works on Windows but refuses to work on this Mac, I seem to have figured that it's a problem with arrays of bit arrays. I'm not sure if this is the whole story, but it's certainly a start.
----------
import std.stdio;
bit[9][] data;
bit[9][] moreData;
void main() {
data.length = 4;
show(data);
moreData.length = 4;
writefln("%x %x", data.ptr, moreData.ptr);
for (; moreData.length < 32; moreData.length = moreData.length+1) {
writefln("%x %x", data.ptr, moreData.ptr);
}
data[0][] = true;
show(data);
data[1][] = true;
show(data);
data[1][0] = true;
data[1][1] = false;
show(data);
data[2][0] = true;
data[2][1] = false;
show(data);
writefln(data.length);
writefln(data[0].length);
writefln(data[0].sizeof);
show(data[0]);
show(data[1]);
show(data[1..3]);
}
void show(void[] data) {
ubyte[] ub = cast(ubyte[]) data;
foreach (ubyte u; ub) writef("%02x ", u);
writefln();
}
----------
[masg2-mac:~/Documents/Prog/D] masg2% a.out
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
608fe0 608fc0
608fe0 608fc0
608fe0 608fc0
608fe0 608fc0
608fe0 608fc0
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 606f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
608fe0 609f80
ff 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ff 01 ff 01 00 00 00 00 00 00 00 00 00 00 00 00
ff 01 fd 01 00 00 00 00 00 00 00 00 00 00 00 00
ff 01 fd 01 01 00 00 00 00 00 00 00 00 00 00 00
4
9
4
ff
fd
fd 01 01 00 00 00 00 00
----------
I've tried it with various numbers of bits. What's happening:
- To allocate the array and to determine its byte length, it's rounding the bit[n] length up to the next multiple of 4 bytes.
- To index into the array, it's rounding the bit[n] length up to the next whole number of bytes.
- The .sizeof property on an element is rounded up to the next multiple of 4.
- When a single element is taken and converted to a void[], it's rounding the bit[n] length down to a whole number of bytes.
- When slicing, the beginning pointer is determined consistently with indexing into the array, but the byte length of the slice is the next multiple of 4 bytes.
On the basis that bit[n] lengths are rounded up to multiples of 4 bytes, then the output (without the memory address testing) should be
----------
ff 01 00 00 00 00 00 00 00 00 00 00 00 00 00
ff 01 00 00 ff 01 00 00 00 00 00 00 00 00 00
ff 01 00 00 fd 01 00 00 00 00 00 00 00 00 00
ff 01 00 00 fd 01 00 00 01 00 00 00 00 00 00
4
9
4
ff 01 00 00
fd 01 00 00
fd 01 00 00 01 00 00 00
----------
OTOH if each bit[n] is packed into the minimal number of bytes, then the output should be
----------
ff 01 00 00 00 00 00 00
ff 01 ff 01 00 00 00 00
ff 01 fd 01 00 00 00 00
ff 01 fd 01 01 00 00 00
4
9
2
ff 01
fd 01
fd 01 01 00
----------
Stewart.
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS-
PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
|