Keil Logo

[C51]Bug report: signed char divided by power of 2

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Ejack Zhang
Posted
29-Sep-2011 01:49 GMT
Toolset
C51
New! [C51]Bug report: signed char divided by power of 2

Bug fact: Following code cannot get right results.

#include "SST89x5xxRD2.H"

signed char x,y,z;

void main()
{
        x = -15;
        y = x / 4;
        z = x % 4;

        while(1);
}

y = -4 and z = -3, while y should be -3...

Reason: Compiler automatically generates RRC for division by power of 2. For a SIGNED CHAR, this could produce unexpected rounding results.

Solution: If a signed char is divided by power of 2, DO NOT use RRC in this case.

Read-Only
Author
Dan Henry
Posted
29-Sep-2011 04:21 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

OK, advice taken, but your report won't go much further when posted to a public discussion forum instead of the normal support channel.

Read-Only
Author
Frustrated Consultant
Posted
29-Sep-2011 09:55 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

According to my book (Harbison and Steele): "Prior to C99, C implementations could choose to truncate toward or away from zero if either of the operands were negative. The div and ldiv library functions were always well defined for negative operands."

Since C51 is not (fully?) C99 compliant, it looks like this is not a bug at all.

Read-Only
Author
Ejack Zhang
Posted
29-Sep-2011 10:09 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

Yes, C implementations could choose to truncate toward or away from zero.

But as KeilC51 library functions choose truncating TOWARD ZERO, isn't it a bug as the compiler truncates AWAY FROM ZERO in this rare situation?

Read-Only
Author
Frustrated Consultant
Posted
29-Sep-2011 10:29 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

But as KeilC51 library functions choose truncating TOWARD ZERO

Where are you using the KeilC51 library functions?

It's not a bug, because the compiler is allowed to act as it chooses.

Read-Only
Author
Andrew Neil
Posted
29-Sep-2011 11:01 GMT
Toolset
C51
New! RE: isn't it a bug

"as KeilC51 library functions choose truncating TOWARD ZERO, isn't it a bug as the compiler truncates AWAY FROM ZERO in this rare situation?"

No.

As already noted, the compiler can do whatever it likes for the division. The library function is well-defined - but there is no requirement for it to be the same!

Read-Only
Author
gear high
Posted
30-Sep-2011 16:05 GMT
Toolset
C51
New! RE: isn't it a bug

Accroding to C89 standard, "If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a"
. y*4+z = -19 != x, so it is a bug.

Read-Only
Author
Frustrated Consultant
Posted
30-Sep-2011 18:44 GMT
Toolset
C51
New! RE: isn't it a bug

But did you enter the whole expression and check the result of that?

So, did you report your bug to Keil support? What did they come back with?

Maybe someone more proficient at interpreting the standards could comment?

Read-Only
Author
Frustrated Consultant
Posted
30-Sep-2011 20:18 GMT
Toolset
C51
New! RE: isn't it a bug

so it is a bug.

Now I'm no expert at this. The way I read the standard is that what you think and state is a bug is certainly not, but the results produced by the division and modulus operator must satisfy the expression you gave.

If they don't then it suggests that the way they truncate the result is inconsistent. That might be a problem.

Read-Only
Author
code guru
Posted
1-Oct-2011 14:38 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

Henry, why do you have to be such a dick head?

Read-Only
Author
Qi Li
Posted
2-Oct-2011 19:23 GMT
Toolset
C51
New! RE: [C51]Bug report: signed char divided by power of 2

You already answered that here: http://www.keil.com/forum/19291/

"Richard is just being Richard."

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.