kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Embedded System open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Embedded System

No Popular Releases are available at this moment for Embedded System

Popular Libraries in Embedded System

No Trending Libraries are available at this moment for Embedded System

Trending New libraries in Embedded System

No Trending Libraries are available at this moment for Embedded System

Top Authors in Embedded System

No Top Authors are available at this moment for Embedded System.

Trending Kits in Embedded System

No Trending Kits are available at this moment for Embedded System

Trending Discussions on Embedded System

    Is relying on integer promotion a bad programming practice?
    Python coding standard for Safety Critical Applications
    Forward declaration issue, two compilers
    How can I save local changes that have been done in Yocto repository?
    Building a static array at compile time
    dpkg-buildpackage reapplies patches to debian/rules
    How to create a frequency table of each subject from a given timetable using pandas?
    Python module 'datetime' has no attribute 'datetime_CAPI'
    Initialising an array while extracting __VA_ARGS__ from a macro function
    Python: pixel manipulation pefrormance. Virtual desktop for embedded device

QUESTION

Is relying on integer promotion a bad programming practice?

Asked 2022-Mar-31 at 19:52

I'm currently writing some code for embedded systems (both in c and c++) and in trying to minimize memory use I've noticed that I used a lot of code that relies on integer promotions. For example (to my knowledge this code is identical in c and c++):

1uint8_t brightness = 40;
2uint8_t maxval = 255;
3uint8_t localoutput = (brightness * maxval) / 100;
4

So even though brightness * 255 is larger than what can be stored in an uint8_t, this still yields the correct result due to, if I'm correct, integer promotions. Brightness is a percentage so it should never be higher than 100 and therefore localoutput should never be higher than 255. My question is then whether or not any unexpected behaviour (such as brightness * maxval being larger than 255 therefore having overflow) or any significant differences between how this syntax is handled between c++ and c are the case. It seems to just output the correct answer, or would be more recommended to have the variables be of type uint16_t as the intermediate calculations may be higher than 255, and just take the memory loss for granted.

ANSWER

Answered 2022-Mar-31 at 19:52

Your question raises an important issue in C programming and in programming in general: does the program behave as expected in all cases?

The expression (brightness * maxval) / 100 computes an intermediary value brightness * maxval that may exceed the range of the type used to compute it. In Python and some other languages, this is not an issue because integers do not have a restricted range, but in C, C++, java, javascript and many other languages, integer types have a fixed number of bits so the multiplication can exceed this range.

It is the programmer's responsibility to ascertain that the range of the operands ensures that the multiplication does not overflow. This requires a good understanding of the integer promotion and conversion rules, which vary from one language to another and are somewhat tricky in C, especially with operands mixing signed and unsigned types.

In your particular case, both brightness and maxval have a type smaller than int so they are promoted to int with the same value and the multiplication produces an int value. If brightness is a percentage in the range 0 to 100, the result is in the range 0 to 25500, which the C Standard guarantees to be in the range of type int, and dividing this number by 100 produces a value in the range 0 to 100, in the range of int, and also in the range of the destination type uint8_t, so the operation is fully defined.

Whether this process should be documented in a comment or verified with debugging assertions is a matter of local coding rules. Changing the order of the operands to maxval * brightness / 100 and possibly using more explicit values and variable names might help the reader:

copy icondownload icon

1uint8_t brightness = 40;
2uint8_t maxval = 255;
3uint8_t localoutput = (brightness * maxval) / 100;
4uint8_t brightness100 = 40;
5uint8_t localoutput = 255 * brightness100 / 100;
6

The problem is more general than just a question of integer promotions, all such computations should be analyzed for corner cases and value ranges. Automated tools can help perform range analysis and optimizing compilers do it to improve code generation, but it is a difficult problem.

Source https://stackoverflow.com/questions/71340614

Community Discussions contain sources that include Stack Exchange Network

    Is relying on integer promotion a bad programming practice?
    Python coding standard for Safety Critical Applications
    Forward declaration issue, two compilers
    How can I save local changes that have been done in Yocto repository?
    Building a static array at compile time
    dpkg-buildpackage reapplies patches to debian/rules
    How to create a frequency table of each subject from a given timetable using pandas?
    Python module 'datetime' has no attribute 'datetime_CAPI'
    Initialising an array while extracting __VA_ARGS__ from a macro function
    Python: pixel manipulation pefrormance. Virtual desktop for embedded device

QUESTION

Is relying on integer promotion a bad programming practice?

Asked 2022-Mar-31 at 19:52

I'm currently writing some code for embedded systems (both in c and c++) and in trying to minimize memory use I've noticed that I used a lot of code that relies on integer promotions. For example (to my knowledge this code is identical in c and c++):

1uint8_t brightness = 40;
2uint8_t maxval = 255;
3uint8_t localoutput = (brightness * maxval) / 100;
4

So even though brightness * 255 is larger than what can be stored in an uint8_t, this still yields the correct result due to, if I'm correct, integer promotions. Brightness is a percentage so it should never be higher than 100 and therefore localoutput should never be higher than 255. My question is then whether or not any unexpected behaviour (such as brightness * maxval being larger than 255 therefore having overflow) or any significant differences between how this syntax is handled between c++ and c are the case. It seems to just output the correct answer, or would be more recommended to have the variables be of type uint16_t as the intermediate calculations may be higher than 255, and just take the memory loss for granted.

ANSWER

Answered 2022-Mar-31 at 19:52

Your question raises an important issue in C programming and in programming in general: does the program behave as expected in all cases?

The expression (brightness * maxval) / 100 computes an intermediary value brightness * maxval that may exceed the range of the type used to compute it. In Python and some other languages, this is not an issue because integers do not have a restricted range, but in C, C++, java, javascript and many other languages, integer types have a fixed number of bits so the multiplication can exceed this range.

It is the programmer's responsibility to ascertain that the range of the operands ensures that the multiplication does not overflow. This requires a good understanding of the integer promotion and conversion rules, which vary from one language to another and are somewhat tricky in C, especially with operands mixing signed and unsigned types.

In your particular case, both brightness and maxval have a type smaller than int so they are promoted to int with the same value and the multiplication produces an int value. If brightness is a percentage in the range 0 to 100, the result is in the range 0 to 25500, which the C Standard guarantees to be in the range of type int, and dividing this number by 100 produces a value in the range 0 to 100, in the range of int, and also in the range of the destination type uint8_t, so the operation is fully defined.

Whether this process should be documented in a comment or verified with debugging assertions is a matter of local coding rules. Changing the order of the operands to maxval * brightness / 100 and possibly using more explicit values and variable names might help the reader:

copy icondownload icon

1uint8_t brightness = 40;
2uint8_t maxval = 255;
3uint8_t localoutput = (brightness * maxval) / 100;
4uint8_t brightness100 = 40;
5uint8_t localoutput = 255 * brightness100 / 100;
6

The problem is more general than just a question of integer promotions, all such computations should be analyzed for corner cases and value ranges. Automated tools can help perform range analysis and optimizing compilers do it to improve code generation, but it is a difficult problem.

Source https://stackoverflow.com/questions/71340614

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Embedded System

Tutorials and Learning Resources are not available at this moment for Embedded System

Share this Page

share link

Get latest updates on Embedded System

  • © 2022 Open Weaver Inc.