Compiler question: integer division?
Hi there,
I just wondered if the code
return (dtt_read(sensor, DTT_READ_TEMP) / 256);
would be result in a "shift by 8" or and interger division?
Thanks,
Steven
Quoted 11 lines · Steven Scholz
Hi there,
I just wondered if the code
return (dtt_read(sensor, DTT_READ_TEMP) / 256);would be result in a "shift by 8" or and interger division?
Thanks,
Steven
Short answer: Yes.
Longer answer: It depends on the compiler, target processor, and optimization levels. Usually it will be a "shift by 8" (which may or may not actually be a shift -- see the PowerPC "Swiss Army Knife[tm]" instruction rlwimi).
gvb
Hi Jerry,
Quoted 7 lines
Quoted 5 lines
I just wondered if the code
return (dtt_read(sensor, DTT_READ_TEMP) / 256);would be result in a "shift by 8" or and interger division?
Short answer: Yes.
Thanks! Have a nice weekend!
Steven
Quoted 10 lines · Steven Scholz
Hi Jerry,
Quoted 8 lines
Quoted 5 lines
I just wondered if the code
return (dtt_read(sensor, DTT_READ_TEMP) / 256);would be result in a "shift by 8" or and interger division?
Short answer: Yes.
How about
a = b / 5;
?
Will that result in
a = (b >> 2) - b;
, i.e. still no division but only shift operations?
Signature · 1 line
Steven
Quoted 10 lines
How about
a = b / 5;?
Will that result in
a = (b >> 2) - b;, i.e. still no division but only shift operations?
I sincerely hope NOT!
A = (B >> 2) - B = B/4 - B = -3/4 B
IMHO, divisions by non-powers of two cannot be achieved by simple shifts and adds/subtracts in one formula - you'd always need conditional jumps and loop constructs.
Reinhard
Quoted 6 lines · In message <[email protected]> you wrote:
How about
a = b / 5; ? Will that result in a = (b >> 2) - b;
We don't know which architecture you're using, and which version of which compiler, so how can we know this?
Why don't you simply try this out yourself?
Best regards,
Wolfgang Denk
Signature · 4 lines
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The price of curiosity is a terminal experience.
- Terry Pratchett, _The Dark Side of the Sun_Quoted 11 lines · Wolfgang Denk
Quoted 5 lines · In message <[email protected]> you wrote:
How about
a = b / 5; ? Will that result in a = (b >> 2) - b;We don't know which architecture you're using, and which version of which compiler, so how can we know this?
I have hoped there will be a general answer, like "gcc always/never does this ..." no matter wich architecture...
Thanks a million for all your comments.
Steven
Quoted 11 lines · Steven Scholz
How about
a = b / 5;?
Will that result in
a = (b >> 2) - b;, i.e. still no division but only shift operations?
I must be missing something here, because as far as I can see, the shift/substract combination above does not yield the same result as dividing by 5.
Using b=40 for example:
b/5 -> 8
(b >> 2) - b -> -30
Udi
Hi
Quoted 5 lines
I just wondered if the code
return (dtt_read(sensor, DTT_READ_TEMP) / 256);would be result in a "shift by 8" or and interger division?
I believe that would depend on compiler, compiler optimization options and processor architecture. AFAIK, gcc will even replace multiplication with shift/add/subtract sequences on 486 (that does have multiply opcode) because it will be faster.
Sergei Sharonov