This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2] Change default to -fno-math-errno


Joseph Myers wrote:
  
> No existing glibc version defines math_errhandling based on
> __NO_MATH_ERRNO__.  I'd expect such a change to come with a glibc patch, 
> and indeed a GCC execution test of the value of math_errhandling to make 
> sure the compiler's behavior isn't contradicting what's declared by the 
> runtime libraries.  Also, the default should not be changed for pre-C99 C 
> standard modes (or pre-C++11 C++, I suppose).

The math_errhandling change has been committed in GLIBC in a separate patch.
I've updated the patch to exclude pre-C99 C/C++/ObjC:

GCC currently defaults to -fmath-errno.  This generates code assuming math
functions set errno and the application checks errno.  Few applications
test errno and various systems and math libraries no longer set errno since it
is optional.  GCC generates much faster code for simple math functions with
-fno-math-errno such as sqrt and lround (avoiding a call and PLT redirection).
Therefore it is reasonable to change the default to -fno-math-errno.  Only change
the default for C99 and later (Fortan, Ada etc already force -fno-math-errno).

long f(float x) { return lroundf(x) + 1; }

Currently generates:

f:
	str	x30, [sp, -16]!
	bl	lroundf
	add	x0, x0, 1
	ldr	x30, [sp], 16
	ret

lroundf in GLIBC doesn't set errno, so all the inefficiency was for nothing:

0000000000000000 <__lroundf>:
   0:	9e240000 	fcvtas	x0, s0
   4:	d65f03c0 	ret

With -fno-math-errno:

f:
	fcvtas	x0, s0
	add	x0, x0, 1
	ret

OK for commit?

2018-01-12  Wilco Dijkstra  <wdijkstr@arm.com>


	* common.opt (fmath-errno): Change default to 0.
	* opts.c (set_fast_math_flags): Force -fno-math-errno with -ffast-math.
	* c-family/c-opts.c (c_common_init_options_struct): Set flag_errno_math
	to special value.
	(c_common_post_options): Set flag_errno_math default based on language.

doc/
	* invoke.texi (-fmath-errno) Update documentation.

--
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index f84ecf4539cb98b0d46a8d8e429f3860db1c79e8..62ae94b7016e3d5f7a7ff1f075c2667ff71975a4 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -207,6 +207,9 @@ c_common_init_options_struct (struct gcc_options *opts)
 
   /* By default, C99-like requirements for complex multiply and divide.  */
   opts->x_flag_complex_method = 2;
+
+  /* Use a special value so the default can be set after option parsing.  */
+  opts->x_flag_errno_math = 2;
 }
 
 /* Common initialization before calling option handlers.  */
@@ -828,6 +831,11 @@ c_common_post_options (const char **pfilename)
   else if (!flag_gnu89_inline && !flag_isoc99)
     error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
 
+  /* If -fmath-errno isn't set (or implied by another math option),
+     set the default to -fno-math-errno for C99 and later.  */
+  if (flag_errno_math == 2)
+    flag_errno_math = !flag_isoc99 || c_dialect_objc ();
+
   /* Default to ObjC sjlj exception handling if NeXT runtime.  */
   if (flag_objc_sjlj_exceptions < 0)
     flag_objc_sjlj_exceptions = flag_next_runtime;
diff --git a/gcc/common.opt b/gcc/common.opt
index ac7e62620a407a1aedd1205c126d109f0b3577f8..dc183977471c332abc48c9b13a139b2f84a13acc 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1846,7 +1846,7 @@ Common Report Var(flag_lto_report_wpa) Init(0)
 Report various link-time optimization statistics for WPA only.
 
 fmath-errno
-Common Report Var(flag_errno_math) Init(1) Optimization SetByCombined
+Common Report Var(flag_errno_math) Init(0) Optimization SetByCombined
 Set errno after built-in math functions.
 
 fmax-errors=
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 49fc5bf39ddbecbd735ffda9cc418908eb644786..442b4bfb6bc1067b4f8b0e3ceb9fdfc1fb6d8bbc 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -9531,24 +9531,16 @@ that depend on an exact implementation of IEEE or ISO rules/specifications
 for math functions. It may, however, yield faster code for programs
 that do not require the guarantees of these specifications.
 
-@item -fno-math-errno
-@opindex fno-math-errno
-Do not set @code{errno} after calling math functions that are executed
-with a single instruction, e.g., @code{sqrt}.  A program that relies on
-IEEE exceptions for math error handling may want to use this flag
-for speed while maintaining IEEE arithmetic compatibility.
+@item -fmath-errno
+@opindex fmath-errno
+Generate code that assumes math functions may set errno.  This disables
+inlining of simple math functions like @code{sqrt} and @code{lround}.
 
-This option is not turned on by any @option{-O} option since
-it can result in incorrect output for programs that depend on
-an exact implementation of IEEE or ISO rules/specifications for
-math functions. It may, however, yield faster code for programs
-that do not require the guarantees of these specifications.
-
-The default is @option{-fmath-errno}.
+A program which relies on math functions setting errno may need to
+use this flag.  However note various systems and math libraries never
+set errno.
 
-On Darwin systems, the math library never sets @code{errno}.  There is
-therefore no reason for the compiler to consider the possibility that
-it might, and @option{-fno-math-errno} is the default.
+The default is @option{-fno-math-errno} except in ISO C90 and C++98 modes.
 
 @item -funsafe-math-optimizations
 @opindex funsafe-math-optimizations
@@ -21564,8 +21556,8 @@ truncation towards zero.
 @item @samp{round}
 Conversion from single-precision floating point to signed integer,
 rounding to the nearest integer and ties away from zero.
-This corresponds to the @code{__builtin_lroundf} function when
-@option{-fno-math-errno} is used.
+This corresponds to the @code{__builtin_lroundf} function unless
+@option{-fmath-errno} is used.
 
 @item @samp{floatis}, @samp{floatus}, @samp{floatid}, @samp{floatud}
 Conversion from signed or unsigned integer types to floating-point types.
diff --git a/gcc/opts.c b/gcc/opts.c
index e9df6f6a6ae7a6c62b0ca1fdf6a57554916586b8..e3c806b38a1d98f9212375c4aa2062dc8df5edfb 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -2553,8 +2553,6 @@ set_fast_math_flags (struct gcc_options *opts, int set)
     }
   if (!opts->frontend_set_flag_finite_math_only)
     opts->x_flag_finite_math_only = set;
-  if (!opts->frontend_set_flag_errno_math)
-    opts->x_flag_errno_math = !set;
   if (set)
     {
       if (opts->frontend_set_flag_excess_precision_cmdline
@@ -2567,6 +2565,8 @@ set_fast_math_flags (struct gcc_options *opts, int set)
 	opts->x_flag_rounding_math = 0;
       if (!opts->frontend_set_flag_cx_limited_range)
 	opts->x_flag_cx_limited_range = 1;
+      if (!opts->frontend_set_flag_errno_math)
+	opts->x_flag_errno_math = 0;
     }
 }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]