svn commit: r300956 - head/lib/libc/stdlib

Bruce Evans brde at optusnet.com.au
Wed Jun 1 13:57:04 UTC 2016


On Tue, 31 May 2016, Chris Torek wrote:

>> That was what I was complaining about.  div.c is for C90 (misspelled
>> "ANSI").
>
> It wasn't misspelled when I wrote it.  :-)  The 1989 ANSI C

:-)

> standard was formally ratified in Dec 1989, and the draft was
> pretty firm by the time I wrote the code (which I am sure was also
> 1989, despite the 1990 copyright; we added or updated all the
> copyrights at the last minute, for net-2).  ISO's quick adoption,
> and hence the name C90, post-date all of that.

The weren't quick enough to be in the same year.  I usually spell the
year as 90, but this can be confusing when discussing the compiler
c89 or the compiler flag -std=c89.  Hmm, old gcc doesn't have -std=c90,
but clang does.

>> 
>> Correct rounding for a
>> positive divisor is towards minus infinity so that the remainder is
>> not negative.  This is modulo arithmetic and has good algebraic
>> properties.  C99 requires rounding minus infinity, at least for positive
>> divisors, under the extension "reliable integer division".
>
> Did you state this backwards?  For integer divison I see:

Oops.  I seem to have misedited a whole clause.  "towards" is also
missing.

>    When integers are divided, the result of the / operator is the
>    algebraic quotient with any fractional part discarded.[105] If
>    the quotient a/b is representable, the expression (a/b)*b + a%b
>    shall equal a; otherwise, the behavior of both a/b and a%b is
>    undefined.
>
> which (as footnote 105 notes) is "truncation towards zero", so that
> (-1)/2 is 0 and not -1.
>
>> In C90,
>> the rounding is implementation-defined, so it may be correct, but it
>> is "unreliable" since it can be anything.
>>
>> In C90, div() is specified as giving "reliable" division, and that is
>> what the fixups implement.  This now wastes time to change nothing.
>
> Right -- as long as the compiler must meet C99 rules, div.c
> can just use the / and % operators.

ache added the ifdef.

I checked that compilers (old gcc and current clang on amd64) don't
auto-inline div().  It is very suitable for inlining without the
fixup.

Bruce


More information about the svn-src-head mailing list