standards/59797: Implement C99's round[f]() math fucntions
Steven G. Kargl
kargl at troutmask.apl.washington.edu
Wed Dec 10 09:20:17 PST 2003
The following reply was made to PR standards/59797; it has been noted by GNATS.
From: "Steven G. Kargl" <kargl at troutmask.apl.washington.edu>
To: FreeBSD-gnats-submit at FreeBSD.org, freebsd-standards at FreeBSD.org
Cc:
Subject: Re: standards/59797: Implement C99's round[f]() math fucntions
Date: Wed, 10 Dec 2003 09:11:50 -0800 (PST)
--ELM1071076310-7460-0_
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII
FreeBSD-gnats-submit at FreeBSD.org wrote:
> Thank you very much for your problem report.
> It has the internal identification `standards/59797'.
> The individual assigned to look at your
> report is: freebsd-standards.
>
> You can access the state of your problem report at any time
> via this link:
>
> http://www.freebsd.org/cgi/query-pr.cgi?pr=59797
>
> >Category: standards
> >Responsible: freebsd-standards
> >Synopsis: Implement C99's round[f]() math fucntions
> >Arrival-Date: Sat Nov 29 10:10:18 PST 2003
>
Aargh. Here the actual patch that
1) handle Inf and NaN on input.
2) add a BUGS section to the man page about FP exception handling
3) hopefully comply better with style(9)
--
Steve
http://troutmask.apl.washington.edu/~kargl/
--ELM1071076310-7460-0_
Content-Transfer-Encoding: 7bit
Content-Type: text/x-patch
Content-Disposition: attachment; filename=msun.patch
Content-Description:
diff -Nbur msun.orig/Makefile msun/Makefile
--- msun.orig/Makefile Sat Oct 25 02:32:18 2003
+++ msun/Makefile Tue Dec 9 20:39:18 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
@@ -153,6 +154,7 @@
MLINKS+=j0.3 j0f.3 j0.3 j1f.3 j0.3 jnf.3 j0.3 y0f.3 j0.3 ynf.3
MLINKS+=lgamma.3 gamma.3 lgamma.3 gammaf.3 lgamma.3 lgammaf.3 lgamma.3 tgamma.3
MLINKS+=rint.3 rintf.3
+MLINKS+=round.3 roundf.3
MLINKS+=sin.3 sinf.3
MLINKS+=sinh.3 sinhf.3
MLINKS+=sqrt.3 cbrt.3 sqrt.3 cbrtf.3 sqrt.3 sqrtf.3
diff -Nbur 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 Tue Dec 9 20:35:48 2003
@@ -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
+do currently support floating-point exception handling as
+specified by
+.St -isoC-99 .
+.Sh STANDARDS
+The
+.Fn round
+function conforms to
+.St -isoC-99 .
+
diff -Nbur 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 Tue Dec 9 20:29:39 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 -Nbur 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 Tue Dec 9 20:33:41 2003
@@ -0,0 +1,50 @@
+/*-
+ * 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>
+
+
+#include <math.h>
+
+double
+round(double x)
+{
+ double t;
+ int i;
+
+ if ((i = fpclassify(x)) == FP_INFINITE || i == FP_NAN)
+ return (x);
+
+ 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 -Nbur 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 Tue Dec 9 20:32:58 2003
@@ -0,0 +1,47 @@
+/*-
+ * 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;
+ int i;
+
+ if ((i = fpclassify(x)) == FP_INFINITE || i == FP_NAN)
+ return (x);
+
+ 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;
+ }
+}
--ELM1071076310-7460-0_--
More information about the freebsd-standards
mailing list