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

Steven G. Kargl kargl at troutmask.apl.washington.edu
Sat Nov 29 10:10:23 PST 2003


>Number:         59797
>Category:       standards
>Synopsis:       Implement C99's round[f]() math fucntions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Nov 29 10:10:18 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Steven G. Kargl
>Release:        FreeBSD 5.1-CURRENT i386
>Organization:
APL/UW
>Environment:
System: FreeBSD troutmask.apl.washington.edu 5.1-CURRENT FreeBSD
        5.1-CURRENT #1: Thu Sep 18 11:50:56 PDT 2003

>Description:

The enclose diff contains an implementation of the round() and
roundf() math functions found in C99.  This is C language 
implementation and a MD implementation may be preferred, but it
appears to at least supply the missing functionality.

Note, future versions of GCC will require these functions.

>How-To-Repeat:
	
>Fix:

diff -uNr msun.orig/Makefile msun/Makefile
--- msun.orig/Makefile	Sat Oct 25 02:32:18 2003
+++ msun/Makefile	Sat Nov 29 09:52:42 2003
@@ -84,7 +84,8 @@
 	s_floor.c s_floorf.c s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.c \
 	s_isnanf.c s_ldexpf.c s_lib_version.c s_log1p.c \
 	s_log1pf.c s_logb.c s_logbf.c s_matherr.c s_modff.c \
-	s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c s_scalbn.c s_scalbnf.c \
+	s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c s_round.c s_roundf.c \
+	s_scalbn.c s_scalbnf.c \
 	s_signgam.c s_significand.c s_significandf.c s_sin.c s_sinf.c s_tan.c \
 	s_tanf.c s_tanh.c s_tanhf.c \
 	w_acos.c w_acosf.c w_acosh.c w_acoshf.c w_asin.c w_asinf.c w_atan2.c \
@@ -121,7 +122,7 @@
 
 MAN=	acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 ceil.3 \
 	cos.3 cosh.3 erf.3 exp.3 fabs.3 floor.3 fmod.3 hypot.3 ieee.3 \
-	ieee_test.3 j0.3 lgamma.3 math.3 rint.3 sin.3 sinh.3 sqrt.3 \
+	ieee_test.3 j0.3 lgamma.3 math.3 rint.3 round.3 sin.3 sinh.3 sqrt.3 \
 	tan.3 tanh.3
 
 MLINKS+=acos.3 acosf.3
diff -uNr msun.orig/man/round.3 msun/man/round.3
--- msun.orig/man/round.3	Wed Dec 31 16:00:00 1969
+++ msun/man/round.3	Sat Nov 29 09:52:17 2003
@@ -0,0 +1,63 @@
+.\" Copyright (c) 2003, Steven G. Kargl
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd November 29, 2003
+.Dt ROUND 3
+.Os
+.Sh NAME
+.Nm round ,
+.Nm roundf
+.Nd nearest integral value; if the argument is halfway between two integral
+values then round away from zero
+.Sh LIBRARY
+.Lb libm
+.Sh SYNOPSIS
+.In math.h
+.Ft double
+.Fn round "double x"
+.Ft float
+.Fn roundf "float x"
+.Sh DESCRIPTION
+The
+.Fn round
+and the
+.Fn roundf
+functions return the nearest integral value to
+.Fa x ;
+if 
+.Fa x
+lies halfway between two integral values, then these
+functions return the integral value with the larger
+absolute value (i.e., they round away from zero).
+.Sh SEE ALSO
+.Xr ceil 3 ,
+.Xr floor 3 ,
+.Xr ieee 3 ,
+.Xr math 3 ,
+.Xr rint 3
+.Sh STANDARDS
+The
+.Fn round
+function conforms to
+.St -isoC-99 .
diff -uNr msun.orig/src/math.h msun/src/math.h
--- msun.orig/src/math.h	Thu Oct 23 01:23:38 2003
+++ msun/src/math.h	Sat Nov 29 09:53:48 2003
@@ -191,6 +191,7 @@
 double	fabs(double);
 double	floor(double);
 double	fmod(double, double);
+double	round(double);
 
 /*
  * These functions are not in C90 so they can be "right".  The ones that
@@ -281,6 +282,7 @@
 float	fabsf(float);
 float	floorf(float);
 float	fmodf(float, float);
+float	roundf(float);
 
 float	erff(float);
 float	erfcf(float) __pure2;
diff -uNr msun.orig/src/s_round.c msun/src/s_round.c
--- msun.orig/src/s_round.c	Wed Dec 31 16:00:00 1969
+++ msun/src/s_round.c	Sat Nov 29 09:49:01 2003
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2003, Steven G. Kargl
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <math.h>
+
+double round(double x) {
+	double t;
+	if (x >= 0.0) {
+		t = ceil(x);
+		if (t - x > 0.5) t -= 1.0;
+		return t;
+	} else {
+		t = ceil(-x);
+		if (t + x > 0.5) t -= 1.0;
+		return -t;
+	}
+}
diff -uNr msun.orig/src/s_roundf.c msun/src/s_roundf.c
--- msun.orig/src/s_roundf.c	Wed Dec 31 16:00:00 1969
+++ msun/src/s_roundf.c	Sat Nov 29 09:48:56 2003
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2003, Steven G. Kargl
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#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;
+	}
+}
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-standards mailing list