Re: Simulating computed goto

2020-11-26 Thread NonNull via Digitalmars-d-learn

On Thursday, 26 November 2020 at 04:42:08 UTC, Dukc wrote:

On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:

Is there a good way to simulate computed goto in D?


I haven't used assembly myself, but it's possible that you can 
define a mixin that does this, using inline assembly.


Interesting idea!


Re: Simulating computed goto

2020-11-26 Thread NonNull via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 18:57:35 UTC, Paul Backus wrote:

On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:
How good is optimization in ldc2, gdc, dmd at compiling 
chained jumps into one jump each time?


The easiest way to find the answer to a question like this is 
to use the compiler explorer:


https://d.godbolt.org/


Very good tool! Thanks.


Re: Simulating computed goto

2020-11-26 Thread Dukc via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 20:05:28 UTC, NonNull wrote:

So to simulate computed goto have to
1. wrap switch(x) in a loop [ while(0) ]
2. inside each case recompute x (instead of jump to computed y)
3. jump back to execute switch again [ continue ]

It does look as if a nested switch can contain case labels from 
an outer switch which is very good. Did not try this out.


Any more ideas, advice?


I quess you could define a string or a template mixin to automate 
this, if there are many functions doing this.


Then again, you probably should NOT have many functions working 
this way -it is not at all a scalable way to program. It might be 
good for something that is called very often and thus needs to be 
fast, but not for regular code. Computed `goto`s are even worse 
in maintainability than normal `goto`s.


Re: Simulating computed goto

2020-11-25 Thread Dukc via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:

Is there a good way to simulate computed goto in D?


I haven't used assembly myself, but it's possible that you can 
define a mixin that does this, using inline assembly.


Re: Simulating computed goto

2020-11-25 Thread NonNull via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 19:04:45 UTC, H. S. Teoh wrote:
FWIW, D's switch statement is flexible enough to directly write 
Duff's device.



How good is optimization in ldc2, gdc, dmd at compiling 
chained jumps into one jump each time?


I'm pretty sure ldc2 and gdc will optimize away any such 
chained jumps. But if performance is important to you, I 
recommend *not* bothering with dmd.


Yes this is about efficiency.


Is there a good way to simulate computed goto in D?


With a switch statement. ;)


OK, so I took a look at switch documentation and tried out 
something. It looked like switch can have computed goto using 
case labels.


case 5:
  //...
  goto case ; //doesn't compile when  is 
not a constant


So to simulate computed goto have to
1. wrap switch(x) in a loop [ while(0) ]
2. inside each case recompute x (instead of jump to computed y)
3. jump back to execute switch again [ continue ]

It does look as if a nested switch can contain case labels from 
an outer switch which is very good. Did not try this out.


Any more ideas, advice?





Re: Simulating computed goto

2020-11-25 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 25, 2020 at 06:44:52PM +, NonNull via Digitalmars-d-learn wrote:
> For automatically generated code of some low level kinds it is
> convenient to have "computed goto" like this:
> 
> https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
> 
> and D does not have this.
> 
> A switch could be used to simulate it. But this would lead to what
> could have been a single jump being chained jumps.

FWIW, D's switch statement is flexible enough to directly write Duff's
device.


> How good is optimization in ldc2, gdc, dmd at compiling chained jumps
> into one jump each time?

I'm pretty sure ldc2 and gdc will optimize away any such chained jumps.
But if performance is important to you, I recommend *not* bothering with
dmd.


> Is there a good way to simulate computed goto in D?

With a switch statement. ;)


T

-- 
They pretend to pay us, and we pretend to work. -- Russian saying


Re: Simulating computed goto

2020-11-25 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:
How good is optimization in ldc2, gdc, dmd at compiling chained 
jumps into one jump each time?


The easiest way to find the answer to a question like this is to 
use the compiler explorer:


https://d.godbolt.org/


Simulating computed goto

2020-11-25 Thread NonNull via Digitalmars-d-learn
For automatically generated code of some low level kinds it is 
convenient to have "computed goto" like this:


https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

and D does not have this.

A switch could be used to simulate it. But this would lead to 
what could have been a single jump being chained jumps.


How good is optimization in ldc2, gdc, dmd at compiling chained 
jumps into one jump each time?


Is there a good way to simulate computed goto in D?