2014-09-02 07:43 PM
For evaluation purposes I got the STM32F0 discovery kit (STM32F030R8T6) with Cortex-M0, and I am using it with the IAR embedded workbench.
From the datasheet I know it has no FPU, which is what I need. In my project, in my C program, if I want to use only uint32 variables, can anyone tell me how to deal with theinteger overflow
when I go above thefixed-width
, and avoid thewrap-around
? I know one solution might besaturation arithmetic
, I wonder if there is a library for this. Any other way? Thanks Eli Jim #integer-overflow #confused! #bound-check2014-09-03 12:07 AM
...can anyone tell me how to deal with the
integer overflow
when I go above thefixed-width
, and avoid thewrap-around
?Just avoid any overflow. By the way, this is a general C issue, and not Cortex M or STM32 specific. In fact, integer math in C is on purpose designed to wrap around. I suggest the following options: 1. by knowing your algorithm and calculations, use appropriate comparisions to avoid overflow; 2. use
long
orlong long
datatype for variables prone to overflow issues (the size of those datatypes depends on your toolchain); 3. use a float datatype (float or double) for such variables; Methods 2 and 3 usually come with a penalty in runtime and code size, but are the easiest in implementation and testing.2014-09-03 12:33 AM
''I know one solution might be
saturation arithmetic
, I wonder if there is a library for this''''Any other way?''As already noted, appropriate scaling & sizing of your variables will help...2014-09-03 01:04 AM
Hi,
Probably the description of my problem was not enough. I cannot use any other datatype. I am limited to only use uint32 due to limitations on the real implementation. Runtime and code size are a big issue for me. I can put it like this: --- uint32 a,b,c a=2^32-1; b=a; c=(a+b)/2; --- Despite c can handle the result, I get it incorrect due to the wrap-around in a code like this: ADDS R0,R0,R1 Any idea how to get it right using only uint32?2014-09-03 01:35 AM
Any idea how to get it right using only uint32?
Scaled math ? Instead of (a+b)/2, you could calculate a/2 + b/2, i.e. scale down by 2. The result would not overflow, but last bit(s) are lost, however.
2014-09-03 03:17 AM
Scaled math is one good option.
But I was thinking in something more rudimentary, like concatenation using an additional register. Does it ring any bell?2014-09-03 04:08 AM
But I was thinking in something more rudimentary, like concatenation using an additional register.
Does it ring any bell?
There is no such option in C, apart from larger datatypes. The ''wrap around'' information, usually present in form of the carry flag on machine instruction level, is not seen in C. However, you could use assembler routines for your math operations, or at least those prone to overflow. By the way: Depending on the operations, using a Cortex M3 instead of a M0/M0+ can speed up execution by a factor about two for the same core clock frequency. Besides of higher throughput of the M3, the M0 does not have an integer division instruction.
2014-09-03 06:18 AM
''I am limited to only use uint32 due to limitations on the real implementation''
Eh??''Runtime and code size are a big issue for me''And yet you were considering a library?