a case where static foreach dosn't work "correctly"

BCS BCS at pathilink.com
Wed Dec 6 21:40:00 PST 2006


If a static foreach is given a tuple of const ints, the ints don't 
behave like consts inside of the foreach body. I havent checked other 
cases but you can't use them for array indexes in ASM operations:


voif fn(V...)()
{
  const int i = 0
  int[3] array;

  foreach(j, V)
  {
   asm
   {
    mov EAX, array[i]; // works
    mov EAX, array[j]; // fails
   }
  }
}


Why does this matter you ask? It would allow this to work:


/***** unsigned big int with "size" words of precision */

template BigNum(uint size)
{
   static if(size == 0)
     static assert(false, "BigNum!(1) is not valid");
   else static if(size == 1)
     static assert(false, "BigNum!(1) is not implemented");
   else static if(size == 2)
     alias use!(1, 1) start;
   else
     alias build!(size, size-2, size-1) start;
}

template build(uint size, uint i, V...)
{
   static if(i==1)
     alias use!(size, 1, V) build;
   else
     alias build!(size, i-1, i, V) build;
}

template use(uint size, V...)
{
   struct use
   {
     uint[size] valuse;
     use!(size, V).use opAdd(use!(size, V).use to)
     {
       uint[size] mine, other;
       mine[] = valuse;
       other[] = to.valuse[];
       asm
       {
         mov EAX, mine[0];
         add other[0], EAX;
       }
       foreach(i; V)
       {
         asm
         {
           mov EAX, mine[i];	// this fails
           adc other[i], EAX;
         }
       }
       use!(size, V).use ret;
       ret.valuse[] = other;
       return ret;
     }
   }
}


DMD 0.174



More information about the Digitalmars-d mailing list