Subject: lib/24252: Patch to add C99 round and roundf functions to math library
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <chris.wareham@btinternet.com>
List: netbsd-bugs
Date: 01/26/2004 17:09:22
>Number:         24252
>Category:       lib
>Synopsis:       Patch to add C99 round and roundf functions to math library
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Jan 26 17:10:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Chris Wareham
>Release:        NetBSD 1.6ZH
>Organization:
n/a
>Environment:
NetBSD laptop.io 1.6ZH NetBSD 1.6ZH (LAPTOP) #0: Tue Jan 20 20:54:43 GMT 2004 root@laptop.io:/usr/src/sys/arch/i386/compile/LAPTOP i386
>Description:
The 3.4 release of GCC is slated to include the gfortran Fortran 95 compiler. This requires the round and roundf math functions. I have ported a patch for FreeBSD which implements these functions and their man page to NetBSD. with this patch I can compile and use GCC tree-ssa snapshots, which includes the gfortran compiler.
>How-To-Repeat:
n/a
>Fix:
diff -Naur src.orig/include/math.h src/include/math.h
--- src.orig/include/math.h	2004-01-21 03:08:21.000000000 +0000
+++ src/include/math.h	2004-01-26 10:44:30.000000000 +0000
@@ -201,6 +201,7 @@
 double	fabs __P((double));
 double	floor __P((double));
 double	fmod __P((double, double));
+double	round __P((double));
 
 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
 double	erf __P((double));
@@ -328,6 +329,7 @@
 float	fabsf __P((float));
 float	floorf __P((float));
 float	fmodf __P((float, float));
+float	roundf __P((float));
 
 float	erff __P((float));
 float	erfcf __P((float));
diff -Naur src.orig/lib/libm/Makefile src/lib/libm/Makefile
--- src.orig/lib/libm/Makefile	2002-06-15 11:34:04.000000000 +0100
+++ src/lib/libm/Makefile	2004-01-26 14:57:48.000000000 +0000
@@ -121,9 +121,9 @@
 	s_floor.c s_floorf.c s_frexpf.c s_ilogb.c s_ilogbf.c \
 	s_isinff.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_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 \
+	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 \
 	w_atan2f.c w_atanh.c w_atanhf.c w_cabs.c w_cabsf.c w_cosh.c w_coshf.c \
 	w_drem.c w_dremf.c w_exp.c w_expf.c w_fmod.c w_fmodf.c w_gamma.c \
@@ -168,8 +168,8 @@
 
 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 isinff.3 j0.3 lgamma.3 math.3 rint.3 sin.3 sinh.3 sqrt.3 \
-	tan.3 tanh.3
+	ieee_test.3 isinff.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
 MLINKS+=acosh.3 acoshf.3
@@ -207,6 +207,7 @@
 MLINKS+=sqrt.3 sqrtf.3 sqrt.3 cbrt.3 sqrt.3 cbrtf.3
 MLINKS+=tan.3 tanf.3
 MLINKS+=tanh.3 tanhf.3
+MLINKS+=round.3 roundf.3
 
 .include <bsd.lib.mk>
 .include <bsd.subdir.mk>
diff -Naur src.orig/lib/libm/man/round.3 src/lib/libm/man/round.3
--- src.orig/lib/libm/man/round.3	1970-01-01 01:00:00.000000000 +0100
+++ src/lib/libm/man/round.3	2004-01-26 14:58:51.000000000 +0000
@@ -0,0 +1,71 @@
+.\" 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 BUGS
+.Fn round
+and 
+.Fn roundf
+don't currently support floating-point exception handling as
+specified by 
+.St -isoC-99 .
+.Sh STANDARDS
+The
+.Fn round
+function conforms to
+.St -isoC-99 .
+
diff -Naur src.orig/lib/libm/src/s_round.c src/lib/libm/src/s_round.c
--- src.orig/lib/libm/src/s_round.c	1970-01-01 01:00:00.000000000 +0100
+++ src/lib/libm/src/s_round.c	2004-01-26 14:58:51.000000000 +0000
@@ -0,0 +1,51 @@
+/*-
+ * 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 <sys/cdefs.h>
+
+#include "math.h"
+#include "math_private.h"
+
+/* round(x)
+ * Round to nearest integral value.  If the argument is halfway between two
+ * integral values then round away from zero.
+ */
+
+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 -Naur src.orig/lib/libm/src/s_roundf.c src/lib/libm/src/s_roundf.c
--- src.orig/lib/libm/src/s_roundf.c	1970-01-01 01:00:00.000000000 +0100
+++ src/lib/libm/src/s_roundf.c	2004-01-26 14:58:51.000000000 +0000
@@ -0,0 +1,51 @@
+/*-
+ * 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 <sys/cdefs.h>
+
+#include "math.h"
+#include "math_private.h"
+
+/* roundf(x)
+ * Round to nearest integral value.  If the argument is halfway between two
+ * integral values then round away from zero.
+ */
+
+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: