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

Alexander Panek a.panek at brainsware.org
Wed Dec 6 22:45:50 PST 2006


Have you tried to do the same with DMD 0.176 ?
Maybe the bug is already solved in one of the two big massacres!

BCS wrote:
> 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