Implementing C99's roundf(), round(), and roundl()

David Schultz das at FreeBSD.ORG
Sat Nov 29 00:11:37 PST 2003


On Fri, Nov 28, 2003, Steve Kargl wrote:
> Can the math functions round[fl]() be implemented in
> terms of other math(3) functions and still conform to the 
> C99 and POSIX standard?  For example,
> 
> #include <math.h>
> 
> float roundf(float x) {
> 	float t;
> 	if (x >= 0.0) {
> 		t = ceilf(x);
> 		if ((t - x) > 0.5) t -= 1.0;
> 		return t;
> 	} else {
> 		t = ceilf(-x);
> 		if ((t + x) > 0.5) t -= 1.0;
> 		return -t;
> 	}
> }

This looks correct to me at first glance, modulo possible problems
with overflow.  It's valuable to have simple MI implementations of
these functions to avoid hampering efforts to port FreeBSD to new
architectures.  Faster MD versions can always be added later.  (I
noticed the other day that Intel has actually released an
optimized IA64 libm, which we should consider importing.)


More information about the freebsd-standards mailing list