public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: how to generate x86 "narrowing" divide instruction
       [not found] ` <mcr62ruzsr8.fsf@google.com>
@ 2011-03-07 19:12   ` Jay Foad
  2011-03-07 20:06     ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Jay Foad @ 2011-03-07 19:12 UTC (permalink / raw)
  To: gcc-help; +Cc: Ian Lance Taylor

On 7 March 2011 18:32, Ian Lance Taylor <iant@google.com> wrote:
>> $ cat rand.c
>> #include <stdint.h>
>> uint32_t rand(uint32_t x) { return (uint64_t)x * 16807 % 0x7FFFFFFF; }

> The div instruction is difficult for gcc to use for 64->32 division
> because it generates an exception if the quotient is too large.  For
> code like the above gcc would have to insert runtime checks to make sure
> that the division did not overflow, even though the code only cares
> about the remainder.

I should have said explicitly: the division above can't overflow,
because the dividend can't be bigger than about 2^31 * 16807 (in
absolute value), so the quotient won't be bigger than about 16807. I
was hoping that value range propagation could work that out, so that
the compiler would know it's safe to use div.

Thanks,
Jay.

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

* Re: how to generate x86 "narrowing" divide instruction
  2011-03-07 19:12   ` how to generate x86 "narrowing" divide instruction Jay Foad
@ 2011-03-07 20:06     ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2011-03-07 20:06 UTC (permalink / raw)
  To: Jay Foad; +Cc: gcc-help

Jay Foad <jay.foad@gmail.com> writes:

> On 7 March 2011 18:32, Ian Lance Taylor <iant@google.com> wrote:
>>> $ cat rand.c
>>> #include <stdint.h>
>>> uint32_t rand(uint32_t x) { return (uint64_t)x * 16807 % 0x7FFFFFFF; }
>
>> The div instruction is difficult for gcc to use for 64->32 division
>> because it generates an exception if the quotient is too large.  For
>> code like the above gcc would have to insert runtime checks to make sure
>> that the division did not overflow, even though the code only cares
>> about the remainder.
>
> I should have said explicitly: the division above can't overflow,
> because the dividend can't be bigger than about 2^31 * 16807 (in
> absolute value), so the quotient won't be bigger than about 16807. I
> was hoping that value range propagation could work that out, so that
> the compiler would know it's safe to use div.

It's a reasonable point.  Viewed that way one could call it a phase
ordering problem.  The knowledge that the range of the value is limited
is long gone by the time it comes to code generation.  And gcc never
generates div for 64->32 anyhow, because that knowledge is long gone.  I
would still describe this as a missed optimization.  It's fixable.  It's
a highly processor specific optimization, applicable to relatively few
programs, which has not been implemented.

Ian

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

end of thread, other threads:[~2011-03-07 20:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <AANLkTinqnD7SyTJAuWdWkccQL_np7s9vsAFkfQxjrBry@mail.gmail.com>
     [not found] ` <mcr62ruzsr8.fsf@google.com>
2011-03-07 19:12   ` how to generate x86 "narrowing" divide instruction Jay Foad
2011-03-07 20:06     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).