www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - bit array - restricted data type and compiler error

reply Tiago Gasiba <tiago.gasiba gmail.com> writes:
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.length=L;
  Y.length=L;

  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]);
  printf("\n");

  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
cases:
  - 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 :)

BR,
Tiago
Jun 19 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 20 Jun 2005 00:01:47 +0200, Tiago Gasiba wrote:

[snip]
   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); } -- Derek Melbourne, Australia 20/06/2005 10:16:56 AM
Jun 19 2005
parent tiago.gasiba gmail.com writes:
Thanks!

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
view.
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... :)

BR,
Tiago
Jun 20 2005