X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f Date: Sun, 07 Sep 2003 21:43:23 +0100 From: "Richard Dawe" Sender: rich AT phekda DOT freeserve DOT co DOT uk To: djgpp-workers AT delorie DOT com X-Mailer: Emacs 21.3.50 (via feedmail 8.3.emacs20_6 I) and Blat ver 1.8.6 Subject: nan, nanf, nanl [PATCH] Message-Id: Reply-To: djgpp-workers AT delorie DOT com Hello. Below is an untested implementation of nan, nanf, nanl. At some point I will write tests. But "it looks right". ;) This needs to be done as part of merging K. B. Williams's maths work, but I'm doing it separately for sanity's sake. I split __dj_nan into a separate file, so that nan.c could contain the function "nan". (There was an error about clashing types otherwise, due __dj_nan being declared as float in math.h and defined as float_t in nan.c.) Bye, Rich =] Index: include/math.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/math.h,v retrieving revision 1.9 diff -p -u -3 -r1.9 math.h --- include/math.h 3 Sep 2003 17:05:22 -0000 1.9 +++ include/math.h 7 Sep 2003 20:28:19 -0000 @@ -75,9 +75,12 @@ extern float __dj_nan; #define isnan(x) (fpclassify(x)==FP_NAN) #define isnormal(x) (fpclassify(x)==FP_NORMAL) -int __fpclassifyf(float) __attribute__((const)); -int __fpclassifyd(double) __attribute__((const)); -int __fpclassifyld(long double) __attribute__((const)); +int __fpclassifyf(float) __attribute__((const)); +int __fpclassifyd(double) __attribute__((const)); +int __fpclassifyld(long double) __attribute__((const)); +double nan(const char *); +float nanf(const char *); +long double nanl(const char *); #endif /* (__STDC_VERSION__ >= 199901L) || !__STRICT_ANSI__ */ @@ -139,7 +142,6 @@ extern double j0(double); extern double j1(double); extern double jn(int, double); extern double lgamma(double); -extern double nan(void); extern double y0(double); extern double y1(double); extern double yn(int, double); @@ -191,7 +193,6 @@ extern float j0f(float); extern float j1f(float); extern float jnf(int, float); extern float lgammaf(float); -extern float nanf(void); extern float y0f(float); extern float y1f(float); extern float ynf(int, float); Index: src/libc/c99/math/makefile =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/c99/math/makefile,v retrieving revision 1.3 diff -p -u -3 -r1.3 makefile --- src/libc/c99/math/makefile 3 Sep 2003 17:05:23 -0000 1.3 +++ src/libc/c99/math/makefile 7 Sep 2003 20:28:19 -0000 @@ -1,9 +1,13 @@ +# Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details # Copyright (C) 2002 DJ Delorie, see COPYING.DJ for details TOP=../.. SRC += hugevalf.c SRC += hugevall.c +SRC += nan_def.c SRC += nan.c +SRC += nanf.c +SRC += nanl.c SRC += fpclassf.S SRC += fpclassd.S SRC += fpclassl.S Index: src/libc/c99/math/nan.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/c99/math/nan.c,v retrieving revision 1.1 diff -p -u -3 -r1.1 nan.c --- src/libc/c99/math/nan.c 22 Mar 2003 11:59:57 -0000 1.1 +++ src/libc/c99/math/nan.c 7 Sep 2003 20:28:29 -0000 @@ -1,4 +1,26 @@ /* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ -#include +#include +#include +#include +#include -float_t __dj_nan = { 0x7fffff, 0xff, 0x0 }; +double +nan(const char *tagp) +{ + double ret = NAN; + char buf[256]; + ssize_t s; + + if (tagp) + { + /* + * If we can't fit NAN() in the buffer, just return NAN. + * It seems better to return a plain NAN than some possibly bogus NAN. + */ + s = snprintf(buf, sizeof(buf), "NAN(%s)", tagp); + if (s < (ssize_t) sizeof(buf)) + ret = strtod(buf, NULL); + } + + return ret; +} Index: src/docs/kb/wc204.txi =================================================================== RCS file: /cvs/djgpp/djgpp/src/docs/kb/wc204.txi,v retrieving revision 1.160 diff -p -u -3 -r1.160 wc204.txi --- src/docs/kb/wc204.txi 3 Sep 2003 17:05:22 -0000 1.160 +++ src/docs/kb/wc204.txi 7 Sep 2003 20:28:34 -0000 @@ -1008,3 +1008,8 @@ into itself, when the current directory The C99 macro @code{fpclassify} and the supporting functions @code{__fpclassifyf}, @code{__fpclassifyd} and @code{__fpclassifyld} were added. + +@findex nan AT r{ added} +@findex nanf AT r{ added} +@findex nanl AT r{ added} +The C99 functions @code{nan}, @code{nanf} and @code{nanl} were added. --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nan_def.c 2003-09-07 21:26:28.000000000 +0000 @@ -0,0 +1,4 @@ +/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ +#include + +float_t __dj_nan = { 0x7fffff, 0xff, 0x0 }; --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nan.txh 2003-09-07 21:34:48.000000000 +0000 @@ -0,0 +1,30 @@ +@node nan, math +@findex nan +@subheading Syntax + +@example +#include + +double nan(const char *tagp); +@end example + +@subheading Description + +@code{nan} returns a quiet NaN with contents indicated by @var{tagp}. + +@subheading Return Value + +The quiet NaN. + +If quiet NaNs are not supported, zero is returned. DJGPP supports +quiet NaNs. + +@subheading Portability + +@portability !ansi-c89, ansi-c99 + +@subheading Example + +@example +double d = nan("0x1234"); +@end example --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nanf.c 2003-09-07 21:26:24.000000000 +0000 @@ -0,0 +1,26 @@ +/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ +#include +#include +#include +#include + +float +nanf(const char *tagp) +{ + float ret = NAN; + char buf[256]; + int s; + + if (tagp) + { + /* + * If we can't fit NAN() in the buffer, just return NAN. + * It seems better to return a plain NAN than some possibly bogus NAN. + */ + s = snprintf(buf, sizeof(buf), "NAN(%s)", tagp); + if (s < (ssize_t) sizeof(buf)) + ret = strtof(buf, NULL); + } + + return ret; +} --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nanf.txh 2003-09-07 21:34:54.000000000 +0000 @@ -0,0 +1,30 @@ +@node nanf, math +@findex nanf +@subheading Syntax + +@example +#include + +float nanf(const char *tagp); +@end example + +@subheading Description + +@code{nanf} returns a quiet NaN with contents indicated by @var{tagp}. + +@subheading Return Value + +The quiet NaN. + +If quiet NaNs are not supported, zero is returned. DJGPP supports +quiet NaNs. + +@subheading Portability + +@portability !ansi-c89, ansi-c99 + +@subheading Example + +@example +float f = nanf("0x1234"); +@end example --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nanl.c 2003-09-07 21:24:30.000000000 +0000 @@ -0,0 +1,26 @@ +/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ +#include +#include +#include +#include + +long double +nanl(const char *tagp) +{ + float ret = NAN; + char buf[256]; + int s; + + if (tagp) + { + /* + * If we can't fit NAN() in the buffer, just return NAN. + * It seems better to return a plain NAN than some possibly bogus NAN. + */ + s = snprintf(buf, sizeof(buf), "NAN(%s)", tagp); + if (s < (ssize_t) sizeof(buf)) + ret = strtold(buf, NULL); + } + + return ret; +} --- /dev/null 2003-09-07 21:38:00.000000000 +0000 +++ src/libc/c99/math/nanl.txh 2003-09-07 21:35:10.000000000 +0000 @@ -0,0 +1,30 @@ +@node nanl, math +@findex nanl +@subheading Syntax + +@example +#include + +long double nanl(const char *tagp); +@end example + +@subheading Description + +@code{nanl} returns a quiet NaN with contents indicated by @var{tagp}. + +@subheading Return Value + +The quiet NaN. + +If quiet NaNs are not supported, zero is returned. DJGPP supports +quiet NaNs. + +@subheading Portability + +@portability !ansi-c89, ansi-c99 + +@subheading Example + +@example +long double ld = nanl("0x1234"); +@end example