Thread overview
[GDC/Mac] Screwed up bit[n][] indexing
Dec 16, 2005
Stewart Gordon
Dec 16, 2005
Stewart Gordon
Dec 16, 2005
Stewart Gordon
December 16, 2005
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.
December 16, 2005
Stewart Gordon wrote:

> 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.

You mean that if you use "wbit" (byte) instead, then it works OK ?

--anders

PS. It's "Mac OS X 10.3.9", interestingly enough... Ten Ten ?
December 16, 2005
Stewart Gordon wrote:
<snip>
> 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
<snip>

That reminds me.  Why are bit arrays little-endian even on a big-endian platform?  This could screw up bit-shifting operations.  Need to experiment with this a bit more....

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.
December 16, 2005
Anders F Björklund wrote:
> Stewart Gordon wrote:
> 
>> 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.
> 
> You mean that if you use "wbit" (byte) instead, then it works OK ?

Why the name wbit?

But yes.

> PS. It's "Mac OS X 10.3.9", interestingly enough... Ten Ten ?

I know....

Do you reckon they're going to carry on, "Mac OS XI 11", "Mac OS XII 12"?...

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.
December 16, 2005
Stewart Gordon wrote:

>> You mean that if you use "wbit" (byte) instead, then it works OK ?
> 
> Why the name wbit?

It's named wbit, in homage to wchar and Elmer Fudd (equal proportions)

http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/12625

> But yes.

Isn't bit arrays known to be broken ? Especially with slices/pointers ?

At least there are several holes in the spec about their implementation.

>> PS. It's "Mac OS X 10.3.9", interestingly enough... Ten Ten ?
> 
> Do you reckon they're going to carry on, "Mac OS XI 11", "Mac OS XII 12"?...

No. :-) Then again they are just at 10.5, so they have several to go ?

--anders