cancel
Showing results for 
Search instead for 
Did you mean: 

How to deal with Integer Overflow?

jimenezserrano
Associate II
Posted on September 03, 2014 at 04:43

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 the

integer overflow

when I go above the

fixed-width

, and avoid the

wrap-around

?

I know one solution might be

saturation arithmetic

, I wonder if there is a library for this.

Any other way?

Thanks

Eli Jim

#integer-overflow #confused! #bound-check
7 REPLIES 7
frankmeyer9
Associate II
Posted on September 03, 2014 at 09:07

...can anyone tell me how to deal with the

integer overflow

when I go above the

fixed-width

, and avoid the

wrap-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

or

long 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.

Andrew Neil
Evangelist III
Posted on September 03, 2014 at 09:33

''I know one solution might be

saturation arithmetic

, I wonder if there is a library for this''

http://www.lmgtfy.com?q=saturating+arithmetic+c+library

''Any other way?''

As already noted, appropriate scaling & sizing of your variables will help...
jimenezserrano
Associate II
Posted on September 03, 2014 at 10:04

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?

frankmeyer9
Associate II
Posted on September 03, 2014 at 10:35

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.

jimenezserrano
Associate II
Posted on September 03, 2014 at 12:17

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?

frankmeyer9
Associate II
Posted on September 03, 2014 at 13:08

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.

Andrew Neil
Evangelist III
Posted on September 03, 2014 at 15:18

''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?