public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fixing improper conversion from sin() to sinf() in optimization mode.
@ 2013-08-20 23:19 Cong Hou
  2013-08-23 21:04 ` Joseph S. Myers
  0 siblings, 1 reply; 27+ messages in thread
From: Cong Hou @ 2013-08-20 23:19 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Li

[-- Attachment #1: Type: text/plain, Size: 1851 bytes --]

When a sin() (cos(), log(), etc.) function is called on a value of
float type and the returned double value is converted to another value
of float type, GCC converts this function call into a float version
(sinf()) in the optimization mode. This avoids two type conversions
and the float version function call usually takes less time. However,
this can result in different result and therefore is unsafe.

For example, the following code produces different results using -O0
(correct), but the same results using -Ox other than -O0 (incorrect).


#include <stdio.h>
#include <math.h>

int main()
{
  float v = 1;
  printf("%.20f\n", (float)sin(v));
  printf("%.20f\n", sinf(v));
}


In this patch, we do this conversion only when the flag
-funsafe-math-optimizations is set. The patch is shown below.


thanks,

Cong




Index: gcc/testsuite/gcc.c-torture/execute/20030125-1.c
===================================================================
--- gcc/testsuite/gcc.c-torture/execute/20030125-1.c (revision 201891)
+++ gcc/testsuite/gcc.c-torture/execute/20030125-1.c (working copy)
@@ -44,11 +44,11 @@ __attribute__ ((noinline))
 double
 sin(double a)
 {
- abort ();
+ return a;
 }
 __attribute__ ((noinline))
 float
 sinf(float a)
 {
- return a;
+ abort ();
 }
Index: gcc/convert.c
===================================================================
--- gcc/convert.c (revision 201891)
+++ gcc/convert.c (working copy)
@@ -99,7 +99,7 @@ convert_to_real (tree type, tree expr)
   /* Disable until we figure out how to decide whether the functions are
      present in runtime.  */
   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
-  if (optimize
+  if (optimize && flag_unsafe_math_optimizations
       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
     {

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 1030 bytes --]

Index: gcc/testsuite/gcc.c-torture/execute/20030125-1.c
===================================================================
--- gcc/testsuite/gcc.c-torture/execute/20030125-1.c	(revision 201891)
+++ gcc/testsuite/gcc.c-torture/execute/20030125-1.c	(working copy)
@@ -44,11 +44,11 @@ __attribute__ ((noinline))
 double
 sin(double a)
 {
-	abort ();
+	return a;
 }
 __attribute__ ((noinline))
 float
 sinf(float a)
 {
-	return a;
+	abort ();
 }
Index: gcc/convert.c
===================================================================
--- gcc/convert.c	(revision 201891)
+++ gcc/convert.c	(working copy)
@@ -99,7 +99,7 @@ convert_to_real (tree type, tree expr)
   /* Disable until we figure out how to decide whether the functions are
      present in runtime.  */
   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
-  if (optimize
+  if (optimize && flag_unsafe_math_optimizations
       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
     {

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2013-10-24 18:23 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-20 23:19 [PATCH] Fixing improper conversion from sin() to sinf() in optimization mode Cong Hou
2013-08-23 21:04 ` Joseph S. Myers
     [not found]   ` <CAK=A3=3=gLhTso3+AF-BmiONPsEpP3dGTFtOAZPbh+oteYPTNA@mail.gmail.com>
2013-08-30 21:53     ` Joseph S. Myers
     [not found]       ` <CAK=A3=0bQkcvprFZTtuJ0ZNbknSJixhMP559tiF3FFUL0zkmfw@mail.gmail.com>
2013-08-31 17:14         ` Joseph S. Myers
2013-09-03 20:59           ` Cong Hou
2013-09-03 21:27             ` Joseph S. Myers
2013-09-03 21:31               ` Xinliang David Li
2013-09-03 21:43                 ` Joseph S. Myers
2013-09-03 22:17                   ` Cong Hou
2013-09-03 22:38                     ` Joseph S. Myers
2013-09-04 20:53                       ` Cong Hou
2013-09-04 20:59                         ` Joseph S. Myers
2013-09-04 21:21                           ` Xinliang David Li
2013-09-04 21:48                             ` Cong Hou
2013-09-04 22:26                             ` Joseph S. Myers
2013-09-06 22:24                               ` Cong Hou
2013-09-10  2:20                                 ` Xinliang David Li
2013-09-10  4:02                                   ` Cong Hou
2013-09-20 17:05                                     ` Cong Hou
2013-10-03 23:19                                       ` Cong Hou
2013-10-04  0:06                                 ` Joseph S. Myers
2013-10-07 17:15                                   ` Cong Hou
2013-10-17 18:30                                     ` Cong Hou
2013-10-23 21:13                                     ` Joseph S. Myers
2013-10-24 19:10                                       ` Cong Hou
2013-09-04 21:18                         ` Xinliang David Li
2013-09-03 22:53                     ` Bernhard Reutner-Fischer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).