Skip to main content
Vu.Andy
Associate III
April 3, 2018
Question

How does STM32F0 core handle unsigned long int overflow

  • April 3, 2018
  • 11 replies
  • 4233 views
Posted on April 04, 2018 at 01:50

For example, if I have:

unsigned long int Variable1;

Variable1 = 0xFFFFFFFF;

Variable1++;

What would be the value of Variable1? Would it be back to zero?

Thanks.

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    11 replies

    T J
    Senior III
    April 3, 2018
    Posted on April 04, 2018 at 01:53

    Yes, and it would set the overflow flag.

    also it would become positive not negative.

    Tesla DeLorean
    Guru
    April 4, 2018
    Posted on April 04, 2018 at 03:22

    32-bit math on the CM0 same as CM3/4, etc

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    waclawek.jan
    Super User
    April 4, 2018
    Posted on April 04, 2018 at 09:00

    If this is written in C, then this behaviour (somewhat surprisingly) follows from the language not the processor, see C99 6.2.5#9:

    A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

    Quoting the explanation by Derek Jones:

    This behavior describes what most processor operations on unsigned values already do. In those rare cases

    where a processor does not support unsigned operations, calls to internal library functions have to be made.

    In your particular case, the result depends on definition of unsigned long int - although probably all existing Cortex-M-targeting compilers define it as 32-bit; a compiler may quite well chose it to be other than 32-bit, and then it would not overflow at this particular case.

    JW

    Vu.Andy
    Vu.AndyAuthor
    Associate III
    April 5, 2018
    Posted on April 05, 2018 at 23:48

    I've tested it using Keil and STM32F042 device and the Variable1 goes back to zero.  I guess I just want to be sure it is a known and defined operation, not something that can have a random result.

    Also, if I have two long unsigned int variables and if I perform subtraction, I would do something like this:

    unsigned long int var1, var2;

    int result;

    result = (int)(var1 - var2);  

    result would be negative if var2 > var1, and positive if var1 > var2.

    waclawek.jan
    Super User
    April 6, 2018
    Posted on April 06, 2018 at 07:48

    Even if unsigned long int would be of the same width as int (i.e. both 32-bit in the CortexM world), this can't be guaranteed.

    For example, if var1 = 0xFFFFFFFF = +4294967295 and var2 = 0, as var1>var2 you expect the result to be positive, but with probably all compilers the result will be 0xFFFFFFFF which interpreted as int is -1.

    JW

    henry.dick
    Associate II
    April 5, 2018
    Posted on April 06, 2018 at 00:17

    Assuming a non-buggy C-compliant compiler, yes.