From owner-freebsd-standards@FreeBSD.ORG Mon Sep 15 16:00:43 2003 Return-Path: Delivered-To: freebsd-standards@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8CA6116A4B3 for ; Mon, 15 Sep 2003 16:00:43 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id B60B743F85 for ; Mon, 15 Sep 2003 16:00:41 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.9/8.12.9) with ESMTP id h8FN0fUp042683 for ; Mon, 15 Sep 2003 16:00:41 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.9/8.12.9/Submit) id h8FN0fd0042682; Mon, 15 Sep 2003 16:00:41 -0700 (PDT) Resent-Date: Mon, 15 Sep 2003 16:00:41 -0700 (PDT) Resent-Message-Id: <200309152300.h8FN0fd0042682@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-standards@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Stefan Farfeleder Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 63E4F16A4B3 for ; Mon, 15 Sep 2003 15:53:36 -0700 (PDT) Received: from fafoe.narf.at (chello212186121237.14.vie.surfer.at [212.186.121.237]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4573C43F75 for ; Mon, 15 Sep 2003 15:53:35 -0700 (PDT) (envelope-from stefan@fafoe.dyndns.org) Received: from frog.fafoe.narf.at (frog.fafoe.narf.at [192.168.2.101]) by fafoe.narf.at (Postfix) with ESMTP id 566943FA9; Tue, 16 Sep 2003 00:53:31 +0200 (CEST) Received: by frog.fafoe.narf.at (Postfix, from userid 1001) id 95B30498; Tue, 16 Sep 2003 00:53:29 +0200 (CEST) Message-Id: <20030915225329.95B30498@frog.fafoe.narf.at> Date: Tue, 16 Sep 2003 00:53:29 +0200 (CEST) From: Stefan Farfeleder To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 cc: stefan@fafoe.narf.at Subject: standards/56906: Several math(3) functions fail to set errno on a domain error X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Stefan Farfeleder List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Sep 2003 23:00:43 -0000 >Number: 56906 >Category: standards >Synopsis: Several math(3) functions fail to set errno on a domain error >Confidential: no >Severity: serious >Priority: low >Responsible: freebsd-standards >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Mon Sep 15 16:00:34 PDT 2003 >Closed-Date: >Last-Modified: >Originator: Stefan Farfeleder >Release: FreeBSD 5.1-CURRENT i386 >Organization: >Environment: System: FreeBSD frog.fafoe.narf.at 5.1-CURRENT FreeBSD 5.1-CURRENT #8: Thu Sep 11 14:09:49 CEST 2003 freebsd@frog.fafoe.narf.at:/freebsd/frog/obj/freebsd/frog/src/sys/FROG i386 >Description: ISO C90 says that errno must be set to EDOM if a domain error occurs. The cases where a domain error definitely has to occur are: acos(x) if |x| > 1, asin(x) if |x| > 1, log(x) if x < 0, log10(x) if x < 0, sqrt(x) if x < 0 and pow(x, y) if x < 0 && y not an integer or if x == 0 && y <= 0. FreeBSD's libm fails to set errno on each of these cases even though the man pages of acos, asin and sqrt claim conformance to C89/90. >How-To-Repeat: This program should set errno to EDOM seven times but it currently only prints zeroes. %% #include #include #include #define TEST1(func, arg) { func, arg, #func } #define TEST2(func, arg1, arg2) { func, arg1, arg2, #func } struct { double (*func)(double); double arg; char *name; } test1[] = { TEST1(acos, 2), TEST1(asin, 2), TEST1(log, -1), TEST1(log10, -1), TEST1(sqrt, -1), }; struct { double (*func)(double, double); double arg1; double arg2; char *name; } test2[] = { TEST2(pow, -1, 0.5), TEST2(pow, 0, -1), }; int main(void) { int i; for (i = 0; i < sizeof(test1) / sizeof(*test1); i++) { errno = 0; test1[i].func(test1[i].arg); printf("%s(%f): errno = %d\n", test1[i].name, test1[i].arg, errno); } for (i = 0; i < sizeof(test2) / sizeof(*test2); i++) { errno = 0; test2[i].func(test2[i].arg1, test2[i].arg2); printf("%s(%f, %f): errno = %d\n", test2[i].name, test2[i].arg1, test2[i].arg2, errno); } return (0); } %% >Fix: Compiling msun with -D_POSIX_MODE fixes the setting of errno. I have no idea if this breaks other parts of libm though; there don't seem to be any regression tests for libm. --- msun-Makefile.diff begins here --- Index: src/lib/msun/Makefile =================================================================== RCS file: /usr/home/ncvs/src/lib/msun/Makefile,v retrieving revision 1.35 diff -u -r1.35 Makefile --- src/lib/msun/Makefile 17 Aug 2003 08:28:46 -0000 1.35 +++ src/lib/msun/Makefile 15 Sep 2003 21:50:12 -0000 @@ -63,7 +63,7 @@ .PATH: ${.CURDIR}/man .PATH: ${.CURDIR}/src -CFLAGS+= -D_IEEE_LIBM +CFLAGS+= -D_POSIX_MODE LIB= m SHLIBDIR?= /lib --- msun-Makefile.diff ends here --- >Release-Note: >Audit-Trail: >Unformatted: From owner-freebsd-standards@FreeBSD.ORG Mon Sep 15 18:44:43 2003 Return-Path: Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4A1AB16A4B3; Mon, 15 Sep 2003 18:44:43 -0700 (PDT) Received: from mailman.zeta.org.au (mailman.zeta.org.au [203.26.10.16]) by mx1.FreeBSD.org (Postfix) with ESMTP id 92ACA43FAF; Mon, 15 Sep 2003 18:44:16 -0700 (PDT) (envelope-from bde@zeta.org.au) Received: from gamplex.bde.org (katana.zip.com.au [61.8.7.246]) by mailman.zeta.org.au (8.9.3p2/8.8.7) with ESMTP id LAA18113; Tue, 16 Sep 2003 11:44:06 +1000 Date: Tue, 16 Sep 2003 11:42:44 +1000 (EST) From: Bruce Evans X-X-Sender: bde@gamplex.bde.org To: Stefan Farfeleder In-Reply-To: <20030915225329.95B30498@frog.fafoe.narf.at> Message-ID: <20030916104414.H2924@gamplex.bde.org> References: <20030915225329.95B30498@frog.fafoe.narf.at> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: FreeBSD-gnats-submit@freebsd.org cc: freebsd-standards@freebsd.org Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2003 01:44:43 -0000 On Tue, 16 Sep 2003, Stefan Farfeleder wrote: > >Synopsis: Several math(3) functions fail to set errno on a domain error This is intentional. > >Description: > ISO C90 says that errno must be set to EDOM if a domain error occurs. As you probably know, C99 doesn't require this. FreeBSD, or at least I, decided not to support C90's relatively feeble and broken specification of floating point and wait for C99 to get it right. The wait is long over and the nonstandardness is now a little different. As you may know, this existence of errno is basically a bug. It is especially broken as designed for math functions because requiring the side effect of setting it breaks things like removing sqrt(x) (for x a loop consant) from loops unless the compiler is very clever about this side effect. The compiler may also need to be clever about the side effect of possibly setting IEEE exception flags for sqrt(x), but the exception flags are sticky and low-level (unlike errno), so it doesn't need to be so clever. I believe that C99 relaxed the requirement on setting errno to simplify such optimization. The current nonstandardness is mainly related to setting the exception flags, and many unimplemented functions. The Sun parts of fdlibm attempt to set the exception flags, but were often defeated by invalid compile-time constant folding last time I checked (long ago). The assembler parts are not very careful about the exception flags. > The cases where a domain error definitely has to occur are: > > acos(x) if |x| > 1, > asin(x) if |x| > 1, > log(x) if x < 0, > log10(x) if x < 0, > sqrt(x) if x < 0 and > pow(x, y) if x < 0 && y not an integer or if x == 0 && y <= 0. > > FreeBSD's libm fails to set errno on each of these cases even though the > man pages of acos, asin and sqrt claim conformance to C89/90. Oops. The man pages even claim to set errno. > >Fix: > Compiling msun with -D_POSIX_MODE fixes the setting of errno. I have no > idea if this breaks other parts of libm though; there don't seem to be > any regression tests for libm. It is safe enough for strict C90, but it adds overhead. For unstrict C90 == C90 less errno plus IEEEish extensions, and for C99 with IEEE support, it breaks returning NaNs for domain errors. E.g., it causes acos(2.0) to return the preposterous nonstandard value 0 with errno set to EDOM instead of a relatively standard non-value "NaN" with errno unchanged. See k_standard.c. It should not adjust the value except possibly in the (_LIB_VERSION == _SVID_) case, but k_standard.c is not passed the original value and I was too lazy to fix its interface. Bruce From owner-freebsd-standards@FreeBSD.ORG Mon Sep 15 18:50:23 2003 Return-Path: Delivered-To: freebsd-standards@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 81D1B16A4B3 for ; Mon, 15 Sep 2003 18:50:23 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 863F243FD7 for ; Mon, 15 Sep 2003 18:50:22 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.9/8.12.9) with ESMTP id h8G1oMUp095632 for ; Mon, 15 Sep 2003 18:50:22 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.9/8.12.9/Submit) id h8G1oMMw095631; Mon, 15 Sep 2003 18:50:22 -0700 (PDT) Date: Mon, 15 Sep 2003 18:50:22 -0700 (PDT) Message-Id: <200309160150.h8G1oMMw095631@freefall.freebsd.org> To: freebsd-standards@FreeBSD.org From: Bruce Evans Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Bruce Evans List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2003 01:50:23 -0000 The following reply was made to PR standards/56906; it has been noted by GNATS. From: Bruce Evans To: Stefan Farfeleder Cc: FreeBSD-gnats-submit@freebsd.org, freebsd-standards@freebsd.org Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error Date: Tue, 16 Sep 2003 11:42:44 +1000 (EST) On Tue, 16 Sep 2003, Stefan Farfeleder wrote: > >Synopsis: Several math(3) functions fail to set errno on a domain error This is intentional. > >Description: > ISO C90 says that errno must be set to EDOM if a domain error occurs. As you probably know, C99 doesn't require this. FreeBSD, or at least I, decided not to support C90's relatively feeble and broken specification of floating point and wait for C99 to get it right. The wait is long over and the nonstandardness is now a little different. As you may know, this existence of errno is basically a bug. It is especially broken as designed for math functions because requiring the side effect of setting it breaks things like removing sqrt(x) (for x a loop consant) from loops unless the compiler is very clever about this side effect. The compiler may also need to be clever about the side effect of possibly setting IEEE exception flags for sqrt(x), but the exception flags are sticky and low-level (unlike errno), so it doesn't need to be so clever. I believe that C99 relaxed the requirement on setting errno to simplify such optimization. The current nonstandardness is mainly related to setting the exception flags, and many unimplemented functions. The Sun parts of fdlibm attempt to set the exception flags, but were often defeated by invalid compile-time constant folding last time I checked (long ago). The assembler parts are not very careful about the exception flags. > The cases where a domain error definitely has to occur are: > > acos(x) if |x| > 1, > asin(x) if |x| > 1, > log(x) if x < 0, > log10(x) if x < 0, > sqrt(x) if x < 0 and > pow(x, y) if x < 0 && y not an integer or if x == 0 && y <= 0. > > FreeBSD's libm fails to set errno on each of these cases even though the > man pages of acos, asin and sqrt claim conformance to C89/90. Oops. The man pages even claim to set errno. > >Fix: > Compiling msun with -D_POSIX_MODE fixes the setting of errno. I have no > idea if this breaks other parts of libm though; there don't seem to be > any regression tests for libm. It is safe enough for strict C90, but it adds overhead. For unstrict C90 == C90 less errno plus IEEEish extensions, and for C99 with IEEE support, it breaks returning NaNs for domain errors. E.g., it causes acos(2.0) to return the preposterous nonstandard value 0 with errno set to EDOM instead of a relatively standard non-value "NaN" with errno unchanged. See k_standard.c. It should not adjust the value except possibly in the (_LIB_VERSION == _SVID_) case, but k_standard.c is not passed the original value and I was too lazy to fix its interface. Bruce From owner-freebsd-standards@FreeBSD.ORG Tue Sep 16 06:08:12 2003 Return-Path: Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 34C5916A4B3; Tue, 16 Sep 2003 06:08:12 -0700 (PDT) Received: from fafoe.narf.at (chello212186121237.14.vie.surfer.at [212.186.121.237]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2132243FA3; Tue, 16 Sep 2003 06:08:11 -0700 (PDT) (envelope-from stefan@fafoe.narf.at) Received: from wombat.fafoe.narf.at (wombat.fafoe.narf.at [192.168.2.102]) by fafoe.narf.at (Postfix) with ESMTP id 22364411E; Tue, 16 Sep 2003 15:08:08 +0200 (CEST) Received: by wombat.fafoe.narf.at (Postfix, from userid 1001) id D2602D1; Tue, 16 Sep 2003 15:08:07 +0200 (CEST) Date: Tue, 16 Sep 2003 15:08:07 +0200 From: Stefan Farfeleder To: Bruce Evans Message-ID: <20030916130805.GD697@wombat.fafoe.narf.at> References: <20030915225329.95B30498@frog.fafoe.narf.at> <20030916104414.H2924@gamplex.bde.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="tThc/1wpZn/ma/RB" Content-Disposition: inline In-Reply-To: <20030916104414.H2924@gamplex.bde.org> User-Agent: Mutt/1.5.4i cc: freebsd-standards@freebsd.org cc: bug-followup@freebsd.org Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2003 13:08:12 -0000 --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Sep 16, 2003 at 11:42:44AM +1000, Bruce Evans wrote: > On Tue, 16 Sep 2003, Stefan Farfeleder wrote: > > > >Synopsis: Several math(3) functions fail to set errno on a domain error > > This is intentional. > > > >Description: > > ISO C90 says that errno must be set to EDOM if a domain error occurs. > > As you probably know, C99 doesn't require this. FreeBSD, or at least I, > decided not to support C90's relatively feeble and broken specification > of floating point and wait for C99 to get it right. The wait is long > over and the nonstandardness is now a little different. Hm, having a conforming C90 environment would be nice despite C99's existence as older compliant applications may depend on the former behaviour and don't become magically C99 conformant. Is there a chance to have a separated C90 compliant libm? Anyway, here's a patch to give C99 application at least the possibility to notice that errno doesn't get set for these functions. Because we don't have to access the floating point status flags, I'm setting math_errhandling to 0. Stefan --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="math_errhandling.diff" Index: src/lib/msun/src/math.h =================================================================== RCS file: /usr/home/ncvs/src/lib/msun/src/math.h,v retrieving revision 1.26 diff -u -r1.26 math.h --- src/lib/msun/src/math.h 22 May 2003 17:07:57 -0000 1.26 +++ src/lib/msun/src/math.h 16 Sep 2003 12:31:42 -0000 @@ -339,4 +339,10 @@ #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */ __END_DECLS +#if __ISO_C_VISIBLE >= 1999 +#define MATH_ERRNO 1 +#define MATH_ERREXCEPT 2 +#define math_errhandling 0 +#endif + #endif /* !_MATH_H_ */ --tThc/1wpZn/ma/RB-- From owner-freebsd-standards@FreeBSD.ORG Tue Sep 16 06:10:22 2003 Return-Path: Delivered-To: freebsd-standards@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0046616A4B3 for ; Tue, 16 Sep 2003 06:10:22 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 361C043FA3 for ; Tue, 16 Sep 2003 06:10:21 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.9/8.12.9) with ESMTP id h8GDAKUp054353 for ; Tue, 16 Sep 2003 06:10:20 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.9/8.12.9/Submit) id h8GDAKBc054352; Tue, 16 Sep 2003 06:10:20 -0700 (PDT) Date: Tue, 16 Sep 2003 06:10:20 -0700 (PDT) Message-Id: <200309161310.h8GDAKBc054352@freefall.freebsd.org> To: freebsd-standards@FreeBSD.org From: Stefan Farfeleder Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Stefan Farfeleder List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2003 13:10:22 -0000 The following reply was made to PR standards/56906; it has been noted by GNATS. From: Stefan Farfeleder To: Bruce Evans Cc: bug-followup@freebsd.org, freebsd-standards@freebsd.org Subject: Re: standards/56906: Several math(3) functions fail to set errno on a domain error Date: Tue, 16 Sep 2003 15:08:07 +0200 --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Sep 16, 2003 at 11:42:44AM +1000, Bruce Evans wrote: > On Tue, 16 Sep 2003, Stefan Farfeleder wrote: > > > >Synopsis: Several math(3) functions fail to set errno on a domain error > > This is intentional. > > > >Description: > > ISO C90 says that errno must be set to EDOM if a domain error occurs. > > As you probably know, C99 doesn't require this. FreeBSD, or at least I, > decided not to support C90's relatively feeble and broken specification > of floating point and wait for C99 to get it right. The wait is long > over and the nonstandardness is now a little different. Hm, having a conforming C90 environment would be nice despite C99's existence as older compliant applications may depend on the former behaviour and don't become magically C99 conformant. Is there a chance to have a separated C90 compliant libm? Anyway, here's a patch to give C99 application at least the possibility to notice that errno doesn't get set for these functions. Because we don't have to access the floating point status flags, I'm setting math_errhandling to 0. Stefan --tThc/1wpZn/ma/RB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="math_errhandling.diff" Index: src/lib/msun/src/math.h =================================================================== RCS file: /usr/home/ncvs/src/lib/msun/src/math.h,v retrieving revision 1.26 diff -u -r1.26 math.h --- src/lib/msun/src/math.h 22 May 2003 17:07:57 -0000 1.26 +++ src/lib/msun/src/math.h 16 Sep 2003 12:31:42 -0000 @@ -339,4 +339,10 @@ #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */ __END_DECLS +#if __ISO_C_VISIBLE >= 1999 +#define MATH_ERRNO 1 +#define MATH_ERREXCEPT 2 +#define math_errhandling 0 +#endif + #endif /* !_MATH_H_ */ --tThc/1wpZn/ma/RB--