Bug 64173

Summary: C99 requires some <math.h> macros to be constant expressions
Product: Base System Reporter: Stefan Farfeleder <stefan>
Component: standardsAssignee: Stefan Farfeleder <stefanf>
Status: Closed FIXED    
Severity: Affects Only Me CC: stefan
Priority: Normal    
Version: 5.2-CURRENT   
Hardware: Any   
OS: Any   

Description Stefan Farfeleder 2004-03-12 18:50:20 UTC
Quoting from ISO/IEC 9899:1999:

# 7.12 Mathematics <math.h>

# 3 The macro
#           HUGE_VAL
#   expands to a positive double constant expression, not necessarily representable as a
#   float. The macros
#           HUGE_VALF
#           HUGE_VALL
#   are respectively float and long double analogs of HUGE_VAL.191)
# 
# 4 The macro
#            INFINITY
#   expands to a constant expression of type float representing positive or unsigned
#   infinity, if available; else to a positive constant of type float that overflows at
#   translation time.192)
# 
# 5 The macro
#           NAN
#   is defined if and only if the implementation supports quiet NaNs for the float type. It
#   expands to a constant expression of type float representing a quiet NaN.

These macros expand to the variables __infinity and __nan currently (defined in
src/lib/${ARCH}/gen/infinity.c), thus violating the requirement of being
constant expressions.

Fix: 

None yet.
How-To-Repeat: This strictly conformant C99 program fails to compile:

$ cat test.c
#include <math.h>
double d = HUGE_VAL;
int main(void) {}
$ c99 test.c
test.c:2: error: initializer element is not constant
Comment 1 Stefan Farfeleder freebsd_committer freebsd_triage 2004-05-06 14:38:45 UTC
Responsible Changed
From-To: freebsd-standards->stefanf

Over to me.
Comment 2 Stefan Farfeleder freebsd_committer freebsd_triage 2004-05-07 17:46:23 UTC
I don't think there's a strictly conforming way to produce constant expressions
with the values infinity and NaN but since GCC has builtins exactly for that
reason, we might as well use them:

Index: src/lib/msun/src/math.h
===================================================================
RCS file: /usr/home/ncvs/src/lib/msun/src/math.h,v
retrieving revision 1.31
diff -u -r1.31 math.h
--- src/lib/msun/src/math.h	25 Apr 2004 02:35:42 -0000	1.31
+++ src/lib/msun/src/math.h	7 May 2004 10:01:06 -0000
@@ -32,7 +32,11 @@
 	float		__uf;
 } __nan;
 
+#ifdef __GNUC__
+#define	HUGE_VAL	__builtin_huge_val()
+#else
 #define	HUGE_VAL	(__infinity.__ud)
+#endif
 
 #if __ISO_C_VISIBLE >= 1999
 #define	FP_ILOGB0	(-0x7fffffff - 1)	/* INT_MIN */
@@ -40,7 +44,11 @@
 #define	HUGE_VALF	(float)HUGE_VAL
 #define	HUGE_VALL	(long double)HUGE_VAL
 #define	INFINITY	HUGE_VALF
+#ifdef __GNUC__
+#define	NAN		__builtin_nanf("")
+#else
 #define	NAN		(__nan.__uf)
+#endif
 
 #define	MATH_ERRNO	1
 #define	MATH_ERREXCEPT	2
Comment 3 Stefan Farfeleder freebsd_committer freebsd_triage 2004-07-11 22:02:32 UTC
State Changed
From-To: open->closed

Fixed for GCC in math.h's revision 1.39.