View mode: basic / threaded / horizontal-split · Log in · Help
June 19, 2005
bit array - restricted data type and compiler error
Hi *,

 I'm trying to port a C library I have written in C to the D programming
language; This library manages matrixes of bits, in which a single row of a
matrix is represented (in C) by an array of ints. Each int holds 32 bits -
operations are really done at the bit-level - a mess with C.
 Therefore, I need to implement a very fast bit-array in D. For this, I try
to use the data structure "bit []", but it is not flexible enough,
according to my experiments. For example, the following code does not
compile, or even worse, the compiler gets crazy :)

------- test.d -------
import std.c.stdio;

const int L = 10;

int main( char [][] argv ){
 bit [] X;
 bit [] Y;
 int    ii;


 X[] = true;
 Y[] = true;

 X[] ^= Y[];                  (1)

 for( ii=0; ii<L; ii++ )      (2)
   X[ii] ^= Y[ii];            (3)

 for( ii=0; ii<L; ii++ )
   printf("%d ",X[ii]);

 return 0;

 Internally, a "bit []" is represented with longs (or ints) or whatever;
The expression "X[] ^= Y[]", i.e number 1, should work at bit-level with
the longs; an implementation like number 2 and number 3 would be extremely
slow, specially for very large arrays.
 The other problem is that, if you comment out line (1), the compiler gets
crazy: "Internal error: ../ztc/cgcs.c 213".
 I'm using "Digital Mars D Compiler v0.127" under Linux SuSE 9.2.
 Any comments / solutions?
 Can a fast bit-array manipulation routine be implemented in the next
version of D? If this is not the case, I need to have to consider two
 - continue to use the code already in C, and import my functions through a 
".o" object - ugly solution
 - forget about D, i.e. D does not bring advantage, therefore continue
programming as before in C.

 I think that an internal D implementation is very very easy, i.e. just
consider that an bit-array is composed by, for example, N integers, and OR,
XOR or AND each integer as required, and the net effect is that the
individual bits also go through the same operation... This is what I
currently have implemented in C :)

June 20, 2005
Re: bit array - restricted data type and compiler error
On Mon, 20 Jun 2005 00:01:47 +0200, Tiago Gasiba wrote:

>   X[] ^= Y[];                  (1)

I believe that array operations are planned for D after v1.0 has been
released, as well as with orthogonal arrays. In the mean time we are
restricted to slower (manual?) methods such as you coded ...

>  for( ii=0; ii<L; ii++ )      (2)
>    X[ii] ^= Y[ii];            (3)

The compiler crash here is obviously a bug and I'm sure will be fixed up.
I'll post this to the bugs list for attention.

However, the code below works okay ...

 foreach(int i, bit yb; Y)
   X[i] = cast(bit)(X[i] ^ yb);

Melbourne, Australia
20/06/2005 10:16:56 AM
June 20, 2005
Re: bit array - restricted data type and compiler error

I'm crossing my fingers and waiting for the time it starts working, then.
Array operations with bits are obvious and should be supported, from my point of
I have found another debugger crash, one which is much more ugly - it crashes
with "segmentation errors" :)  very very nasty
I'll try to strip down the code and then post it on the bugs news group.

For now, I think that I'll just use he C code and import it into the D
environment. I really cnat afford loosing speed... :)

Top | Discussion index | About this forum | D home