[U-Boot-Users] 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

Steven Scholz wrote:
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

Steven Scholz wrote:
Hi Jerry,
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?

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

In message 426CA9EF.6080003@imc-berlin.de 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

Wolfgang Denk wrote:
In message 426CA9EF.6080003@imc-berlin.de 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

Steven Scholz wrote:
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
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
participants (6)
-
Jerry Van Baren
-
Reinhard Meyer
-
Sergei Sharonov
-
Steven Scholz
-
Udi Finkelstein
-
Wolfgang Denk