public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot-Users] Compiler question: integer division?
@ 2005-04-22 16:54 Steven Scholz
  2005-04-22 17:10 ` Jerry Van Baren
  2005-04-25 15:13 ` [U-Boot-Users] " Sergei Sharonov
  0 siblings, 2 replies; 10+ messages in thread
From: Steven Scholz @ 2005-04-22 16:54 UTC (permalink / raw)
  To: u-boot

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-22 16:54 [U-Boot-Users] Compiler question: integer division? Steven Scholz
@ 2005-04-22 17:10 ` Jerry Van Baren
  2005-04-22 17:17   ` Steven Scholz
  2005-04-25 15:13 ` [U-Boot-Users] " Sergei Sharonov
  1 sibling, 1 reply; 10+ messages in thread
From: Jerry Van Baren @ 2005-04-22 17:10 UTC (permalink / raw)
  To: u-boot

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-22 17:10 ` Jerry Van Baren
@ 2005-04-22 17:17   ` Steven Scholz
  2005-04-25  8:27     ` Steven Scholz
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Scholz @ 2005-04-22 17:17 UTC (permalink / raw)
  To: u-boot

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.

Thanks! Have a nice weekend!

Steven

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-22 17:17   ` Steven Scholz
@ 2005-04-25  8:27     ` Steven Scholz
  2005-04-25  9:04       ` AW: " Reinhard Meyer
                         ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Steven Scholz @ 2005-04-25  8:27 UTC (permalink / raw)
  To: u-boot

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?

-- 

Steven

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
@ 2005-04-25  8:35 Fillod Stephane
  0 siblings, 0 replies; 10+ messages in thread
From: Fillod Stephane @ 2005-04-25  8:35 UTC (permalink / raw)
  To: u-boot

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?

Short answer:

	${CROSS_COMPILE}gcc a.c -O2 -c -g -o a.o
	${CROSS_COMPILE}objdump -S a.o

In other words, YMMV (arch, flags, gcc version, etc.).
Some arch pipelines may be faster at division (with divisor on 8 bits),
than shift and subtract which must serialize.

Feel free to ask/contribute to the GCC project :-)

-- 
Stephane

^ permalink raw reply	[flat|nested] 10+ messages in thread

* AW: [U-Boot-Users] Compiler question: integer division?
  2005-04-25  8:27     ` Steven Scholz
@ 2005-04-25  9:04       ` Reinhard Meyer
  2005-04-25  9:35       ` Wolfgang Denk
  2005-04-25 14:31       ` Udi Finkelstein
  2 siblings, 0 replies; 10+ messages in thread
From: Reinhard Meyer @ 2005-04-25  9:04 UTC (permalink / raw)
  To: u-boot

> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-25  8:27     ` Steven Scholz
  2005-04-25  9:04       ` AW: " Reinhard Meyer
@ 2005-04-25  9:35       ` Wolfgang Denk
  2005-04-25 10:30         ` Steven Scholz
  2005-04-25 14:31       ` Udi Finkelstein
  2 siblings, 1 reply; 10+ messages in thread
From: Wolfgang Denk @ 2005-04-25  9:35 UTC (permalink / raw)
  To: u-boot

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

-- 
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_

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-25  9:35       ` Wolfgang Denk
@ 2005-04-25 10:30         ` Steven Scholz
  0 siblings, 0 replies; 10+ messages in thread
From: Steven Scholz @ 2005-04-25 10:30 UTC (permalink / raw)
  To: u-boot

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Compiler question: integer division?
  2005-04-25  8:27     ` Steven Scholz
  2005-04-25  9:04       ` AW: " Reinhard Meyer
  2005-04-25  9:35       ` Wolfgang Denk
@ 2005-04-25 14:31       ` Udi Finkelstein
  2 siblings, 0 replies; 10+ messages in thread
From: Udi Finkelstein @ 2005-04-25 14:31 UTC (permalink / raw)
  To: u-boot

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [U-Boot-Users] Re: Compiler question: integer division?
  2005-04-22 16:54 [U-Boot-Users] Compiler question: integer division? Steven Scholz
  2005-04-22 17:10 ` Jerry Van Baren
@ 2005-04-25 15:13 ` Sergei Sharonov
  1 sibling, 0 replies; 10+ messages in thread
From: Sergei Sharonov @ 2005-04-25 15:13 UTC (permalink / raw)
  To: u-boot

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-04-25 15:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-22 16:54 [U-Boot-Users] Compiler question: integer division? Steven Scholz
2005-04-22 17:10 ` Jerry Van Baren
2005-04-22 17:17   ` Steven Scholz
2005-04-25  8:27     ` Steven Scholz
2005-04-25  9:04       ` AW: " Reinhard Meyer
2005-04-25  9:35       ` Wolfgang Denk
2005-04-25 10:30         ` Steven Scholz
2005-04-25 14:31       ` Udi Finkelstein
2005-04-25 15:13 ` [U-Boot-Users] " Sergei Sharonov
  -- strict thread matches above, loose matches on Subject: below --
2005-04-25  8:35 [U-Boot-Users] " Fillod Stephane

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox