Hi, I need some long long (64 bit) additions in a fast interrupt code.
After each addition, I need to limit the result to MIN/MAX_INT64 if some overflow occurred.
In assembler, I would use the brach command BVC/BVS to check the overflow flag after the addition - this would be only one additional command, very nice (in normal case, where no overflow occurs).
Is there any way to implement / insert such functionality also in C / C++ code?
I tried to include dspfncts.h, and then check the variable Overflow, but this will add 3 additional assembly lines (to load the value of the overflow bit - it does NOT use the very compact commands BVC/BVS). As I have quite many such additions, this will blow up my code quite a bit.
Is there any possibility to do this overflow check in an efficient way in C/C++?
The whole ISR is the PID control code of my motor control. Writing this completely in assembler would be a bit too much, sorry. It comprises readout of complicated encoders, the PID control loop and the trajetory calculation - this is really quite much stuff.
No need to be sorry, I'm only coming up with suggestions. If you think an individual call might create too much overhead, then consider restructuring to minimise the number of calls and do more work in each call.
This is quite a standard request, I think - it sounds a bit strange for me that this should not be possible
This is not something I've ever been concerned with, but do wonder whether you've come across any other compilers that contain this 'must have' feature.
Microchip has a very nice-to-use inline assembler in the form "asm("...some assembly code", in-flags, change-flags, out-flags)") (for dsPIC33). With such inline assembly it is easy to define many additional things.
But the definition of "in-flags, change-flags, out-flags" is very much required, I think. Otherwise inline assembler makes not much sense together with a well optimizing compiler.
(it is not only possibly to specify not only the used registers in these "in-flags, ...", but also the important bits of the PSR register like C, N, V, Z).
But saturated add/sub is quite a normal condition for writing safe code ... I think nearly all dsp engines e. g. have some sort of saturation control.
Note that the dsPIC33 is specifically intended for DSP applications. So Microchip probably had to spend quite some time to try to add some extra support to make it possible to maximize the DSP capabilities.
But do you know of compilers that have the option?
No, I do not know other compilers with this option. But why not being the first ... .
Saturation control really is very important.
Just with the Keil compiler currently I have the problem, that I do not see any way how to implement this saturation functionality for 64 bit math (long long). (while keeping reasonable speed of the code).
With the inline assembly of the Microchip compiler this would be quite easy.
And Per Westermark, this inline assembly has nothing to do with the DSP functionality of dsPIC33 - Microchip uses the same inline assembly command also for all the standard PIC devices. (I referred to DSP only, because reluctant consultant came with the comment, that the wish for saturated add/sub seemed very new to him ...).
PS: I just looked at the inline assembly in GCC - they have the possibility to define at least "output-flags : input-flags". (But I have no experience with GCC so far - I am definitely a fan of the Keil compiler ...).