This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: question regarding div / std::div implementation
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: Daniel Gutson <daniel dot gutson at tallertechnologies dot com>, Adhemerval Zanella <adhemerval dot zanella at linaro dot org>
- Cc: GNU C Library <libc-alpha at sourceware dot org>
- Date: Wed, 20 Apr 2016 14:38:05 -0700
- Subject: Re: question regarding div / std::div implementation
- Authentication-results: sourceware.org; auth=none
- References: <CAF5HaEXKZ7j-gbZPiWPhDpx7=R0zm1xYvXNYCNUMG4WeZS532Q at mail dot gmail dot com> <5717DF65 dot 5060606 at linaro dot org> <CAF5HaEWdpAGiXtCO36u3F0QGAXfVHL+qkY+RLsszpv7paPVdMg at mail dot gmail dot com> <5717E68D dot 2020905 at linaro dot org> <CAF5HaEWuSS7dEEM6ogU6TCqQ-Y7OM9DC1HFCeuuAGL0vKtL2Kw at mail dot gmail dot com> <5717EB44 dot 5020508 at linaro dot org> <CAF5HaEVWnU9_8Ae3gCR9Ns8sD_B8QoD794sp59XQNrqEw034EQ at mail dot gmail dot com>
On 04/20/2016 02:15 PM, Daniel Gutson wrote:
OK with no inline asm, but a libcall might be expensive specially in a
tight loop and messes with predictions;
a builtin is nonportable as well.
In practice, C programs that need integer quotient and remainder
typically don't call 'div'. They just use % and /, and compilers are now
smart enough to do just one machine-level operation to get both quotient
and remainder. For example, time/offtime.c has this macro:
#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
which should work just fine as-is. In theory one could change this to
use div/ldiv/lldiv, but why bother making the code way more complicated?
As the 'div' function family was designed back when C compilers were not
that smart and is largely obsolete now, simplicity would appear to be
more important than performance here. Perhaps someone someday will work
up the energy to get 'div' removed from the C standard.