Go Down

Topic: Exploiting C++14 in the near future? (Read 186 times) previous topic - next topic

jorenheit

Jul 05, 2015, 01:47 pm Last Edit: Jul 05, 2015, 01:50 pm by jorenheit
Hi all,

The year is 2015, and C++14 has been implemented to a great extent in GCC. A feature that has been extended from C++11 is, are constexpr functions. In C++14, these can be far more complicated than before (restricted to only a return-statement). The reason that this is relevant to Arduino is that this could make e.g. digitalWrite significantly faster. The current implementation has to go through lookup tables to match the pin-number to the correct registers, which takes time. Third party implementations like digitalWriteFast make sure these lookups are performed during compile-time when possible.

However, with C++14's enhanced constexpr functions, this might be done all at compile-time in the standard Arduino implementation without losing backward compatibility (it's as easy as adding constexpr to the function declaration). I'm aware that this is not implemented in the current avr-gcc version (4.8) yet, but I'm curious if such optimizations are considered given the evolving C++ standard. If not, I will consider contributing to the source myself. That seems like a fun and useful project.

As a side-question: does anyone have any idea when avr-gcc 5.x comes around? I couldn't find anything online...

robtillaart

not familiar with C++14 ,
would it affect the code size?
as everything on Arduino is a balance between size and performance...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jorenheit

#2
Jul 05, 2015, 01:58 pm Last Edit: Jul 05, 2015, 01:59 pm by jorenheit
It won't effect the code-size at all. It's just a signifier to the compiler to perform a function at compile-time when the parameters to the function are know. E.g. digitalWrite(1, HIGH) could be evaluated at compile-time rather than at runtime, or rather, the lookups inside digitalWrite could. I'm not an expert on this either, so there might be unforseen quirks, but I think it's definately worth exploring.

Maybe even using C++11, declaring the arrays that map the pin-numbers to registers as constexpr could lead to the compiler doing the lookups at compile-time. I'm not sure about this. Has this ever been looked into?

pYro_65

#3
Jul 05, 2015, 02:21 pm Last Edit: Jul 05, 2015, 02:45 pm by pYro_65
There are options you can use already which will provide the same level of speed as direct port manipulation. constexpr functions would only be one more way of reproducing the desired effect.

There is already:
digitalWriteFast maros
Fat16lib's DigitalPin library which I use often.
Cosa
this recent topic
and many other implementations.

The major problem holding back the implementation of these methods in the background of digitalRead/Write is the dramatic timing difference between calling digitalWrite with pin numbers as a compile time constant vs a dynamic run-time deduced pin number (for loop iteration).

EDIT: There is also no support for C++14 in GCC 4.8.1 (what the latest IDE uses), however you can enable C++11.

jorenheit

Quote
There are options you can use already which will provide the same level of speed as direct port manipulation. constexpr functions would only be one more way of reproducing the desired effect.
Which I mentioned in the opening post. However, I don't see why such a simple optimization would not be implemented if the speed-gain can be so significant.

Quote
The major problem holding back the implementation of these methods in the background of digitalRead/Write is the dramatic timing difference between calling digitalWrite with pin numbers as a compile time constant vs a dynamic run-time deduced pin number (for loop iteration).
This could be the answer to my question if you can elaborate on why this would be a problem in the first place. Is there code out there that relies on digitalWrite being slow? Also, with constexpr, such loops would probably performed at compiletime as well (if the bounds of the loop are known). The slow behavior is mostly limited to user-interaction situations.

Quote
EDIT: There is also no support for C++14 in GCC 4.8.1 (what the latest IDE uses), however you can enable C++11.
Which I mentioned in my opening post (and implicitly in the title).

pYro_65

I personally do not care, I use another method as mentioned in my initial post. This is just what was mentioned in the past (you aren't the first to ask this question).

You could also consider contributing your ideas to the project, which you mentioned in your opening post.
However a three minute search on GitHub reveals that that an optimal digitalWrite may be reconsidered for future versions.

Go Up