standards/59797: Implement C99's round[f]() math fucntions

David Schultz das at FreeBSD.ORG
Sat Jun 5 02:54:12 GMT 2004


Sorry, I've put this off way too long.  The good news is that I'm
now going to do something about it.  The bad news is that I found
a significant bug in the proposed implementation.  Namely, round()
and roundf() often get the wrong answer for halfway cases.  In
IEEE-754 round-to-nearest mode, numbers that are halfway between
two representable numbers are supposed to be rounded to even.  For
instance, 0.5 becomes 0, and 1.5 becomes 2.  I don't see an easy
way to make this work in the present implementation without
fiddling with the underlying bits.  Perhaps we need to implement
it more similarly to fdlibm's rint().

BTW, benchmarking shows that using the sample implementation that
appears in the C99 standard results in a slowdown of two orders of
magnitude over your round() implementation and four orders of
magnitude over the x87 frndint instruction.  Just setting the
rounding mode and calling rint() also results in a significant
slowdown.  Thus, we definitely want something that's in the spirit
of what you wrote, but perhaps one that operates on the bits directly.


More information about the freebsd-standards mailing list