C: 8x8 & # 8594; 16-bit multiplication accuracy guaranteed by whole promotions?

I am trying to find out if the standard C (C90, although I work with the annotated book C99 from Derek Jones) ensures that I do not lose accuracy by multiplying two unsigned 8-bit values ​​and saving them by 16-bits. An example operator is as follows:

unsigned char foo;
unsigned int foo_u16 = foo * 10;

Our Keil 8051 compiler (v7.50 at present) will generate the MUL AB instruction, which stores the MSB in register B and the LSB in the battery. If I first flush foo to unsigned int:

unsigned int foo_u16 = (unsigned int)foo * 10;

then the compiler correctly decides that I want an unsigned int there, and generates an expensive call to a 16x16-bit integer subroutine. I would like to substantiate the doubt that this defensive measure is not needed. When I read the whole stock described in 6.3.1.1, the effect of the first line should be as if foo and 10 were translated into unsigned int, the multiplication performed and the result saved as unsigned int in foo_u16. If the compiler knows an instruction that does 8x8-> 16 bits of multiplication without loss of precision, all the better; but accuracy is guaranteed. Am I reading it right?

Regards, Craig Blom

+5
source share
1 answer

, signed int, unsigned char signed int. ( , )

unsigned int foo_u16 = foo * 10; 

unsigned int foo_u16 = (signed) foo * 10; 

, -, ,

unsigned int foo_u16 = (unsigned) foo * 10; 

, () signed int.

-, ​​ ( , , unsigned char signed int).

+5

All Articles