Feature request: First class labels

janderson askme at me.com
Sun May 6 18:23:45 PDT 2007


Daniel Keep wrote:
> 
> 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
> 


What about if the compiler just detected the use of a constant, and you 
didn't have to do anything be specify the variables as const.  Also the 
compiler could do a better optimisation on those loops by taking the 
jump out all together and having 4 separate loops.

-Joel



More information about the Digitalmars-d mailing list