Re: [RFC] intdiv()
| From: | Nikita Popov | Date: | Thu, 17 Jul 2014 09:24:42 +0000 |
| Subject: | Re: [RFC] intdiv() | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75661@lists.php.net to get a copy of this message | ||
On Thu, Jul 17, 2014 at 2:25 AM, Sara Golemon <pollita@php.net> wrote:
> On Wed, Jul 16, 2014 at 8:15 AM, Andrea Faulds <ajf@ajf.me> wrote:
> > Nikita Popov doesn’t seem to be a fan of the %% syntax, so it may be
> subject to change, though I think it’s the best I’ve heard so far. ;)
> >
> Nor am I. Here's a thought though: How about just making / return int
> when there's no remainder.
>
> Looking at this code, you might think it's inefficent:
>
> double dres = a / b;
> long lres = a / b;
> if (a % b) {
> return dres;
> } else {
> return lres;
> }
>
> But in fact at -O1, gcc will optimize this (probably clang and others
> as well) to a single idivq instruction and only do the cvtsi2sdq in
> the dres case.
>
> My point being, we can just make division with an integral result
> return a result of integer division without altering the syntax or
> adding a perf hit.
>
This is already what is currently happening, see
http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_operators.c#1067.
Andreas proposal is only useful in the case that the numbers don't divide
exactly and you need round-down/truncation behavior and your numbers are in
a range where the indirection through double arithmetic results in
precision loss.
Nikita