[TriEmbed] arrays of function addresses Re: optimized switch statements Re: Hacking a fake vintage radio (with Arduino + Pi 0)

Pete Soper pete at soper.us
Wed Jul 1 18:50:10 CDT 2020


Would this constitute "threaded execution"? I recall being told how 
Forth dispatches in a clever way at the end of each operation and the 
use of "NEXT" seems similar (to my vague memory of the description).

One other trick users of C and C++ should consider is function 
references in variables and parameters. Some time back I made a "simple 
pin change" library to give folks three more interrupts with the  
Atmega328, the one used with the Arduino Uno and Nano and others 
(warning: only tested with that chip). It uses an array of functions 
that resolve to either a dummy internal function or the user's desired 
interrupt handler. If anybody is interested the code is here: 
https://bitbucket.org/petesoper/simplepinchange

The C syntax for pointers to functions gets my vote for hardest to remember.

-Pete

On 7/1/20 7:18 PM, Jon Wolfe via TriEmbed wrote:
>
> In an interesting coincidence, the place where I recall seeing the use 
> of goto I described was also in a bytecode interpreter, for the “Pawn 
> Scripting language” (which is really cool by the way, I’ve got it to 
> run on 8 bit AVR, arm cortex-M and also transpiled to JavaScript using 
> emscripten, and run inside a browser. It is extremely fast for a 
> bytecode interpreter)
>
> Check out line 208:
>
> https://github.com/compuphase/pawn/blob/master/amx/amxexec_gcc.c
>
> That ‘NEXT’ macro is using a goto behind the scenes. Each block of 
> code in between that labels (ie the semantic ‘case statement’ 
> equivalents) ends with a NEXT macro, so it may actually be faster than 
> even a compiler optimized switch-case because there is no ‘loop’ logic 
> needed, to cycle back around to the ‘switch’. Its really just a 
> devilish jumping around inside that function.
>
> One could argue that ‘break’ and ‘continue’ statements used inside 
> loops are really just dressed up ‘gotos’ with specific jump 
> destinations. I’ve been writing code almost daily for close to 30 
> years, and much of that time doing C or C++, and I’d estimate the time 
> in between occasions where I used the goto keyword to be 2-5 years, so 
> in other words, pretty rare. One of those uses can be for “breaking’ 
> out of a nested loop. Java has a “break <label>” statement that you 
> can use to break out of an outer loop from within an inner loop. 
> Standard C/C++ doesn’t have anything like that so you have to either 
> use a flag, restructure your loops, or use a goto statement. I think 
> it’s a matter of option,  on a case by case basis which technique will 
> lead to cleaner, easier to follow code in any particular situation.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20200701/54ee069f/attachment.htm>


More information about the TriEmbed mailing list