Feature request: First class labels

Daniel Keep daniel.keep.lists at gmail.com
Sun May 6 16:43:45 PDT 2007



Luís Marques wrote:
> BCS wrote:
>> // no tests in loop.
>> |void* l1 = complex_loop_invariant ? &&a : &&b;
>> |void* l2 = complex_loop_invariant2 ? &&c : &&d;
>> |
>> |for(int i = 1; i<1000000; i++)
>> |{
>> | goto *l1
>> | a:  nothing();
>> |     goto l2:
>> | b:  that()
>> |     goto l2:
>> | c:  other();
>> |     break;
>> | d:  bat();
>> |}
> 
> I hope people don't use that except in critical inner-loops or
> encapsulated in some form :-)
> 
> -- 
> Luís

What about this?

|for(int i = 1; i<1000000; i++)
|{
| invariant if(complex_loop_invariant)
|  nothing();
| else
|  that()
| invariant if(complex_loop_invariant2)
|  other();
| else
|  bat();
|}

Where "invariant if" is defined as "a conditional statement whose result
is invariant in the current scope, allowing the compiler to optimise and
elide subsequent evaluations until the scope is left."  Or something
like that.

It'd be really cool if this could be extended to the general case inside
of functions, provided there's a way to "reset" the switch.  Maybe
something like this:

> void padd(ubyte[] a, ubyte[] b)
> {
> selectSSE2:
>     invariant if( cpuid.hasSSE2 )
>     {
>         // SSE2 implementation
>     }
>     else
>     {
>         // Software implementation
>     }
> }
>
> void reset_padd()
> {
>     break padd.selectSSE2;
> }

Which could translate to:

> void* padd_selectSSE2 = &&padd.selectSSE2_init;
>
> void padd(ubyte[] a, ubyte[] b)
> {
>     goto *padd_selectSSE2;
>
> selectSSE2_init:
>     if( cpuid.hasSSE2 )
>         padd_selectSSE2 = &&selectSSE2_true;
>     else
>         padd_selectSSE2 = &&selectSSE2_false;
>     goto *padd_selectSSE2;
>
> selectSSE2_true:
>         // SSE2 implementation
>         goto selectSSE2_end;
> selectSSE2_false:
>         // Software implementation
>         goto selectSSE2_end;
> selectSSE2_end:
> }
>
> void reset_padd()
> {
>     padd_selectSSE2 = &&padd.selectSSE2_init;
> }

That might even please the people talking about multi-platform binaries,
especially if the compiler is clever enough to actually re-write the
GOTO on the fly :)

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list