From bb384eba43f6fa8d7c4e8bc5904985d7438b3ed4 Mon Sep 17 00:00:00 2001 From: das Date: Sun, 16 Dec 2007 21:19:28 +0000 Subject: Implement and document nan(), nanf(), and nanl(). This commit adds two new directories in msun: ld80 and ld128. These are for long double functions specific to the 80-bit long double format used on x86-derived architectures, and the 128-bit format used on sparc64, respectively. --- lib/libc/stdlib/strtod.3 | 27 +++++++++++-- lib/msun/Makefile | 15 ++++++-- lib/msun/Symbol.map | 3 ++ lib/msun/ld128/s_nanl.c | 47 +++++++++++++++++++++++ lib/msun/ld80/s_nanl.c | 47 +++++++++++++++++++++++ lib/msun/man/nan.3 | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/msun/src/math.h | 6 ++- lib/msun/src/s_nan.c | 66 ++++++++++++++++++++++++++++++++ 8 files changed, 302 insertions(+), 8 deletions(-) create mode 100644 lib/msun/ld128/s_nanl.c create mode 100644 lib/msun/ld80/s_nanl.c create mode 100644 lib/msun/man/nan.3 create mode 100644 lib/msun/src/s_nan.c (limited to 'lib') diff --git a/lib/libc/stdlib/strtod.3 b/lib/libc/stdlib/strtod.3 index 33dd452..ec8ad51 100644 --- a/lib/libc/stdlib/strtod.3 +++ b/lib/libc/stdlib/strtod.3 @@ -86,8 +86,28 @@ For hexadecimal constants, the scaling is instead done by powers of 2. .Pp Alternatively, if the portion of the string following the optional -plus or minus sign begins with ``INFINITY'' or ``NAN'', ignoring -case, it is interpreted as an infinity or a quiet NaN, respectively. +plus or minus sign begins with +.Dq INFINITY +or +.Dq NAN , +ignoring case, it is interpreted as an infinity or a quiet \*(Na, +respectively. +The syntax +.Dq Xo Pf NAN( Ar "s" ) Xc , +where +.Ar s +is an alphanumeric string, produces the same value as the call +.Fo nan +.Qq Ar s Ns +.Fc +(respectively, +.Fo nanf +.Qq Ar s Ns +.Fc +and +.Fo nanl +.Qq Ar s Ns +.Fc . ) .Pp In any of the above cases, leading white-space characters in the string (as defined by the @@ -140,6 +160,7 @@ Overflow or underflow occurred. .Xr atof 3 , .Xr atoi 3 , .Xr atol 3 , +.Xr nan 3 , .Xr strtol 3 , .Xr strtoul 3 , .Xr wcstod 3 @@ -177,5 +198,3 @@ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .Ed -.Sh BUGS -These routines do not recognize the C99 ``NaN(...)'' syntax. diff --git a/lib/msun/Makefile b/lib/msun/Makefile index eaad0f6..69a76f5e 100644 --- a/lib/msun/Makefile +++ b/lib/msun/Makefile @@ -24,6 +24,13 @@ ARCH_SUBDIR= ${MACHINE_ARCH} .PATH: ${.CURDIR}/man .PATH: ${.CURDIR}/src +# long double format +.if ${LDBL_PREC} == 64 +.PATH: ${.CURDIR}/ld80 +.elif ${LDBL_PREC} == 113 +.PATH: ${.CURDIR}/ld128 +.endif + LIB= m SHLIBDIR?= /lib SHLIB_MAJOR= 5 @@ -51,7 +58,7 @@ COMMON_SRCS= b_exp.c b_log.c b_tgamma.c \ s_llrint.c s_llrintf.c s_llround.c s_llroundf.c s_llroundl.c \ s_log1p.c s_log1pf.c s_logb.c s_logbf.c s_lrint.c s_lrintf.c \ s_lround.c s_lroundf.c s_lroundl.c s_modff.c \ - s_nearbyint.c s_nextafter.c s_nextafterf.c \ + s_nan.c s_nearbyint.c s_nextafter.c s_nextafterf.c \ s_nexttowardf.c s_remquo.c s_remquof.c \ s_rint.c s_rintf.c s_round.c s_roundf.c s_roundl.c \ s_scalbln.c s_scalbn.c s_scalbnf.c s_signbit.c \ @@ -71,7 +78,8 @@ SYMBOL_MAPS= ${SYM_MAPS} COMMON_SRCS+= s_copysignl.c s_fabsl.c s_modfl.c .if ${LDBL_PREC} != 53 # If long double != double use these; otherwise, we alias the double versions. -COMMON_SRCS+= s_fmal.c s_frexpl.c s_nextafterl.c s_nexttoward.c s_scalbnl.c +COMMON_SRCS+= s_fmal.c s_frexpl.c s_nanl.c s_nextafterl.c s_nexttoward.c \ + s_scalbnl.c .endif # C99 complex functions @@ -98,7 +106,7 @@ MAN= acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 ceil.3 \ feclearexcept.3 feenableexcept.3 fegetenv.3 \ fegetround.3 fenv.3 floor.3 \ fma.3 fmax.3 fmod.3 hypot.3 ieee.3 ieee_test.3 ilogb.3 j0.3 \ - lgamma.3 lrint.3 lround.3 math.3 nextafter.3 remainder.3 rint.3 \ + lgamma.3 lrint.3 lround.3 math.3 nan.3 nextafter.3 remainder.3 rint.3 \ round.3 scalbn.3 signbit.3 sin.3 sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 MLINKS+=acos.3 acosf.3 @@ -145,6 +153,7 @@ MLINKS+=lgamma.3 gamma.3 lgamma.3 gammaf.3 lgamma.3 lgammaf.3 lgamma.3 tgamma.3 MLINKS+=lrint.3 llrint.3 lrint.3 llrintf.3 lrint.3 lrintf.3 MLINKS+=lround.3 llround.3 lround.3 llroundf.3 lround.3 llroundl.3 \ lround.3 lroundf.3 lround.3 lroundl.3 +MLINKS+=nan.3 nanf.3 nan.3 nanl.3 MLINKS+=nextafter.3 nextafterf.3 nextafter.3 nextafterl.3 MLINKS+=nextafter.3 nexttoward.3 nextafter.3 nexttowardf.3 MLINKS+=nextafter.3 nexttowardl.3 diff --git a/lib/msun/Symbol.map b/lib/msun/Symbol.map index 304678a..e1e12e5 100644 --- a/lib/msun/Symbol.map +++ b/lib/msun/Symbol.map @@ -190,4 +190,7 @@ FBSD_1.1 { cargf; csqrt; csqrtf; + nan; + nanf; + nanl; }; diff --git a/lib/msun/ld128/s_nanl.c b/lib/msun/ld128/s_nanl.c new file mode 100644 index 0000000..6a9bfbf --- /dev/null +++ b/lib/msun/ld128/s_nanl.c @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2007 David Schultz + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include + +#include "fpmath.h" +#include "../../../contrib/gdtoa/gdtoaimp.h" + +long double +nanl(const char *s) +{ + static FPI fpi = { 113, -16494, 16271, 1, SI }; + + union IEEEl2bits result; + ULong bits[2]; + int k; + + s--; + k = hexnan(&s, &fpi, bits); + ULtoQ((UShort *)&result.e, bits, 16272, k); + return (result.e); +} diff --git a/lib/msun/ld80/s_nanl.c b/lib/msun/ld80/s_nanl.c new file mode 100644 index 0000000..0128f45 --- /dev/null +++ b/lib/msun/ld80/s_nanl.c @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2007 David Schultz + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include + +#include "fpmath.h" +#include "../../../contrib/gdtoa/gdtoaimp.h" + +long double +nanl(const char *s) +{ + static FPI fpi = { 64, -16445, 16320, 1, SI }; + + union IEEEl2bits result; + ULong bits[2]; + int k; + + s--; + k = hexnan(&s, &fpi, bits); + ULtox((UShort *)&result.e, bits, 16321, k); + return (result.e); +} diff --git a/lib/msun/man/nan.3 b/lib/msun/man/nan.3 new file mode 100644 index 0000000..6162641 --- /dev/null +++ b/lib/msun/man/nan.3 @@ -0,0 +1,99 @@ +.\" Copyright (c) 2007 David Schultz +.\" 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 AUTHOR 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 AUTHOR 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. +.\" +.\" $FreeBSD$ +.\" +.Dd December 16, 2007 +.Dt NAN 3 +.Os +.Sh NAME +.Nm nan , +.Nm nanf , +.Nm nanl +.Nd quiet \*(Nas +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn nan "const char *s" +.Ft float +.Fn nanf "const char *s" +.Ft long double +.Fn nanl "const char *s" +.Sh DESCRIPTION +The +.Dv NAN +macro expands to a quiet \*(Na (Not A Number). +Similarly, each of the +.Fn nan , +.Fn nanf , +and +.Fn nanl +functions generate a quiet \*(Na value without raising an invalid exception. +The argument +.Fa s +should point to either an empty string or a hexadecimal representation +of a non-negative integer (e.g., "0x1234".) +In the latter case, the integer is encoded in some free bits in the +representation of the \*(Na, which sometimes store +machine-specific information about why a particular \*(Na was generated. +There are 22 such bits available for +.Vt float +variables, 51 bits for +.Vt double +variables, and at least 51 bits for a +.Vt long double . +If +.Fa s +is improperly formatted or represents an integer that is too large, +then the particular encoding of the quiet \*(Na that is returned +is indeterminate. +.Sh COMPATIBILITY +Calling these functions with a non-empty string isn't portable. +Another operating system may translate the string into a different +\*(Na encoding, and furthermore, the meaning of a given \*(Na encoding +varies across machine architectures. +If you understood the innards of a particular platform well enough to +know what string to use, then you would have no need for these functions +anyway, so don't use them. +Use the +.Dv NAN +macro instead. +.Sh SEE ALSO +.Xr fenv 3 , +.Xr ieee 3 , +.Xr isnan 3 , +.Xr math 3 , +.Xr strtod 3 +.Sh STANDARDS +The +.Fn nan , +.Fn nanf , +and +.Fn nanl +functions and the +.Dv NAN +macro conform to +.St -isoC-99 . diff --git a/lib/msun/src/math.h b/lib/msun/src/math.h index a5d16c5..fc281c9 100644 --- a/lib/msun/src/math.h +++ b/lib/msun/src/math.h @@ -238,6 +238,7 @@ double log1p(double); double logb(double); long lrint(double); long lround(double); +double nan(const char *) __pure2; double nextafter(double, double); double remainder(double, double); double remquo(double, double, int *); @@ -341,6 +342,7 @@ long long llrintf(float); long long llroundf(float); long lrintf(float); long lroundf(float); +float nanf(const char *) __pure2; float nearbyintf(float); float nextafterf(float, float); float remainderf(float, float); @@ -435,13 +437,15 @@ long double log10l(long double); long double log1pl(long double); long double log2l(long double); long double logbl(long double); +#endif long double logl(long double); +#if 0 long lrintl(long double); #endif long lroundl(long double); long double modfl(long double, long double *); /* fundamentally !__pure2 */ -#if 0 long double nanl(const char *) __pure2; +#if 0 long double nearbyintl(long double); #endif long double nextafterl(long double, long double); diff --git a/lib/msun/src/s_nan.c b/lib/msun/src/s_nan.c new file mode 100644 index 0000000..10014f6 --- /dev/null +++ b/lib/msun/src/s_nan.c @@ -0,0 +1,66 @@ +/*- + * Copyright (c) 2007 David Schultz + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include +#include + +#include "../../../contrib/gdtoa/gdtoaimp.h" +#include "math_private.h" + +double +nan(const char *s) +{ + static FPI fpi = { 52, -1074, 971, 1, SI }; + + double result; + ULong bits[2]; + + s--; + hexnan(&s, &fpi, bits); + SET_HIGH_WORD(result, 0x7ff80000 | bits[1]); + SET_LOW_WORD(result, bits[0]); + return (result); +} + +float +nanf(const char *s) +{ + static FPI fpi = { 24, -149, 104, 1, SI }; + + float result; + ULong bits[1]; + + s--; + hexnan(&s, &fpi, bits); + SET_FLOAT_WORD(result, 0x7fc00000 | bits[0]); + return (result); +} + +#if (LDBL_MANT_DIG == 53) +__weak_reference(nan, nanl); +#endif -- cgit v1.1