public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Convert 0.0/0.0 to NAN
@ 2017-04-19 23:50 Richard Allen
  2017-04-20  0:01 ` Richard Allen
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Allen @ 2017-04-19 23:50 UTC (permalink / raw)
  To: newlib

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

With newlib's default compiler flags(-O2 and whatnot),
the GCC I'm using creates calls to __aeabi_(f|d)div.
This causes a little extra register copying
and an unnecessary branch.

Additionally, __aeabi_?div may throw exceptions,
which is probably not what we want a
simple assignment to do.

Compiler version tested:
arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919

-Richard

[-- Attachment #2: 0001-Convert-rint-to-rintf.patch --]
[-- Type: text/x-patch, Size: 1105 bytes --]

From 2369a552cf62dfb53f14978587a338072f6abd2c Mon Sep 17 00:00:00 2001
From: Richard <rsaxvc@rsaxvc.net>
Date: Wed, 19 Apr 2017 18:37:17 -0500
Subject: [PATCH] Convert rint() to rintf()

This was causing an unnecessary increase
in precision, as well as additional function
calls to do float->double conversion on
platforms with only a single-precision FPU.
---
 newlib/libm/math/wf_pow.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/newlib/libm/math/wf_pow.c b/newlib/libm/math/wf_pow.c
index bd9de0ad0..5b79dd151 100644
--- a/newlib/libm/math/wf_pow.c
+++ b/newlib/libm/math/wf_pow.c
@@ -126,11 +126,11 @@
 		    if (_LIB_VERSION == _SVID_) {
 		       exc.retval = HUGE;
 		       y *= 0.5;
-		       if(x<0.0&&rint(y)!=y) exc.retval = -HUGE;
+		       if(x<0.0&&rintf(y)!=y) exc.retval = -HUGE;
 		    } else {
 		       exc.retval = HUGE_VAL;
                        y *= 0.5;
-		       if(x<0.0&&rint(y)!=y) exc.retval = -HUGE_VAL;
+		       if(x<0.0&&rintf(y)!=y) exc.retval = -HUGE_VAL;
 		    }
 		    if (_LIB_VERSION == _POSIX_)
 		        errno = ERANGE;
-- 
2.11.0


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

* Re: [PATCH] Convert 0.0/0.0 to NAN
  2017-04-19 23:50 [PATCH] Convert 0.0/0.0 to NAN Richard Allen
@ 2017-04-20  0:01 ` Richard Allen
  2017-04-20  0:25   ` Craig Howland
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Allen @ 2017-04-20  0:01 UTC (permalink / raw)
  To: newlib

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

Whoops, that was the wrong patch. Attached is correct patch.

On Wed, Apr 19, 2017 at 6:50 PM, Richard Allen <rsaxvc@rsaxvc.net> wrote:
> With newlib's default compiler flags(-O2 and whatnot),
> the GCC I'm using creates calls to __aeabi_(f|d)div.
> This causes a little extra register copying
> and an unnecessary branch.
>
> Additionally, __aeabi_?div may throw exceptions,
> which is probably not what we want a
> simple assignment to do.
>
> Compiler version tested:
> arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919
>
> -Richard

[-- Attachment #2: 0001-Convert-0.0-0.0-to-NAN.patch --]
[-- Type: text/x-patch, Size: 6642 bytes --]

From 823701d0b25a00b892f55498f611a5222a1b80ee Mon Sep 17 00:00:00 2001
From: Richard <rsaxvc@rsaxvc.net>
Date: Wed, 19 Apr 2017 18:24:22 -0500
Subject: [PATCH 1/2] Convert 0.0/0.0 to NAN

With newlib's default compiler flags(-O2 and whatnot),
the GCC I'm using creates calls to __aeabi_fdiv
or __aeabi_ddiv. This causes a little extra
register allocation and an unnecessary branch.

Additionally, __aeabi_*div may throw exceptions,
which is almost certainly not what we want those
lines to do.

Compiler version tested:
arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919
---
 newlib/libm/math/w_acosh.c      | 2 +-
 newlib/libm/math/w_atanh.c      | 2 +-
 newlib/libm/math/w_fmod.c       | 2 +-
 newlib/libm/math/w_pow.c        | 2 +-
 newlib/libm/math/w_remainder.c  | 2 +-
 newlib/libm/math/w_sqrt.c       | 2 +-
 newlib/libm/math/wf_acosh.c     | 2 +-
 newlib/libm/math/wf_atanh.c     | 2 +-
 newlib/libm/math/wf_fmod.c      | 2 +-
 newlib/libm/math/wf_pow.c       | 2 +-
 newlib/libm/math/wf_remainder.c | 2 +-
 newlib/libm/math/wf_sqrt.c      | 2 +-
 12 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/newlib/libm/math/w_acosh.c b/newlib/libm/math/w_acosh.c
index 93032600b..f40d9f291 100644
--- a/newlib/libm/math/w_acosh.c
+++ b/newlib/libm/math/w_acosh.c
@@ -106,7 +106,7 @@ MATHREF
             exc.name = "acosh";
 	    exc.err = 0;
 	    exc.arg1 = exc.arg2 = x;
-            exc.retval = 0.0/0.0;
+            exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/w_atanh.c b/newlib/libm/math/w_atanh.c
index 07fd45962..9ffa29e48 100644
--- a/newlib/libm/math/w_atanh.c
+++ b/newlib/libm/math/w_atanh.c
@@ -106,7 +106,7 @@ QUICKREF
                 exc.name = "atanh";
 		exc.err = 0;
 		exc.arg1 = exc.arg2 = x;
-                exc.retval = 0.0/0.0;
+                exc.retval = NAN;
                 if (_LIB_VERSION == _POSIX_)
                   errno = EDOM;
                 else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/w_fmod.c b/newlib/libm/math/w_fmod.c
index f9f72c745..616a291bd 100644
--- a/newlib/libm/math/w_fmod.c
+++ b/newlib/libm/math/w_fmod.c
@@ -90,7 +90,7 @@ PORTABILITY
             if (_LIB_VERSION == _SVID_)
                exc.retval = x;
 	    else
-	       exc.retval = 0.0/0.0;
+	       exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/w_pow.c b/newlib/libm/math/w_pow.c
index ebf7be328..685507124 100644
--- a/newlib/libm/math/w_pow.c
+++ b/newlib/libm/math/w_pow.c
@@ -157,7 +157,7 @@ PORTABILITY
 		    if (_LIB_VERSION == _SVID_) 
 		        exc.retval = 0.0;
 		    else 
-		        exc.retval = 0.0/0.0;	/* X/Open allow NaN */
+		        exc.retval = NAN;	/* X/Open allow NaN */
 		    if (_LIB_VERSION == _POSIX_) 
 		        errno = EDOM;
 		    else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/w_remainder.c b/newlib/libm/math/w_remainder.c
index e4c196716..01245755c 100644
--- a/newlib/libm/math/w_remainder.c
+++ b/newlib/libm/math/w_remainder.c
@@ -74,7 +74,7 @@ PORTABILITY
 	    exc.err = 0;
 	    exc.arg1 = x;
 	    exc.arg2 = y;
-            exc.retval = 0.0/0.0;
+            exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/w_sqrt.c b/newlib/libm/math/w_sqrt.c
index 23a793ce7..b2b1f8d78 100644
--- a/newlib/libm/math/w_sqrt.c
+++ b/newlib/libm/math/w_sqrt.c
@@ -76,7 +76,7 @@ PORTABILITY
 	  if (_LIB_VERSION == _SVID_)
 	    exc.retval = 0.0;
           else
-            exc.retval = 0.0/0.0;
+            exc.retval = NAN;
           if (_LIB_VERSION == _POSIX_)
             errno = EDOM;
           else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_acosh.c b/newlib/libm/math/wf_acosh.c
index fc8ec3a0a..3c25447e8 100644
--- a/newlib/libm/math/wf_acosh.c
+++ b/newlib/libm/math/wf_acosh.c
@@ -41,7 +41,7 @@
             exc.name = "acoshf";
 	    exc.err = 0;
 	    exc.arg1 = exc.arg2 = (double)x;
-            exc.retval = 0.0/0.0;
+            exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_atanh.c b/newlib/libm/math/wf_atanh.c
index 565630411..a6a0a28cf 100644
--- a/newlib/libm/math/wf_atanh.c
+++ b/newlib/libm/math/wf_atanh.c
@@ -41,7 +41,7 @@
                 exc.name = "atanhf";
 		exc.err = 0;
 		exc.arg1 = exc.arg2 = (double)x;
-                exc.retval = 0.0/0.0;
+                exc.retval = NAN;
                 if (_LIB_VERSION == _POSIX_)
                   errno = EDOM;
                 else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_fmod.c b/newlib/libm/math/wf_fmod.c
index 030ca3e7a..b14339a20 100644
--- a/newlib/libm/math/wf_fmod.c
+++ b/newlib/libm/math/wf_fmod.c
@@ -44,7 +44,7 @@
             if (_LIB_VERSION == _SVID_)
                exc.retval = x;
 	    else
-	       exc.retval = 0.0/0.0;
+	       exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_pow.c b/newlib/libm/math/wf_pow.c
index a30f8808e..bd9de0ad0 100644
--- a/newlib/libm/math/wf_pow.c
+++ b/newlib/libm/math/wf_pow.c
@@ -107,7 +107,7 @@
 		    if (_LIB_VERSION == _SVID_) 
 		        exc.retval = 0.0;
 		    else 
-		        exc.retval = 0.0/0.0;	/* X/Open allow NaN */
+		        exc.retval = NAN;	/* X/Open allow NaN */
 		    if (_LIB_VERSION == _POSIX_) 
 		        errno = EDOM;
 		    else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_remainder.c b/newlib/libm/math/wf_remainder.c
index f38c23785..48c395f9c 100644
--- a/newlib/libm/math/wf_remainder.c
+++ b/newlib/libm/math/wf_remainder.c
@@ -41,7 +41,7 @@
 	    exc.err = 0;
 	    exc.arg1 = (double)x;
 	    exc.arg2 = (double)y;
-            exc.retval = 0.0/0.0;
+            exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_)
                errno = EDOM;
             else if (!matherr(&exc)) {
diff --git a/newlib/libm/math/wf_sqrt.c b/newlib/libm/math/wf_sqrt.c
index 4536ba0ac..96814bd73 100644
--- a/newlib/libm/math/wf_sqrt.c
+++ b/newlib/libm/math/wf_sqrt.c
@@ -43,7 +43,7 @@
             if (_LIB_VERSION == _SVID_)
               exc.retval = 0.0;
             else
-              exc.retval = 0.0/0.0;
+              exc.retval = NAN;
             if (_LIB_VERSION == _POSIX_) 
               errno = EDOM;
             else if (!matherr(&exc)) {
-- 
2.11.0


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

* Re: [PATCH] Convert 0.0/0.0 to NAN
  2017-04-20  0:01 ` Richard Allen
@ 2017-04-20  0:25   ` Craig Howland
  0 siblings, 0 replies; 3+ messages in thread
From: Craig Howland @ 2017-04-20  0:25 UTC (permalink / raw)
  To: newlib

On 04/19/2017 08:01 PM, Richard Allen wrote:
> Whoops, that was the wrong patch. Attached is correct patch.
>
> On Wed, Apr 19, 2017 at 6:50 PM, Richard Allen <rsaxvc@rsaxvc.net> wrote:
>> With newlib's default compiler flags(-O2 and whatnot),
>> the GCC I'm using creates calls to __aeabi_(f|d)div.
>> This causes a little extra register copying
>> and an unnecessary branch.
>>
>> Additionally, __aeabi_?div may throw exceptions,
>> which is probably not what we want a
>> simple assignment to do.
>>
>> Compiler version tested:
>> arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919
>>
>> -Richard
- exc.retval = 0.0/0.0;
+ exc.retval = NAN;

Well, actually, an exception is exactly why it is coded that way; yes, this is 
what this simple assignment is intended to do.  The 0.0/0.0 construct is used so 
that at runtime the floating point flags gets set and yield the NAN result; a 
floating point exception is supposed to be raised.  (The compiler is not allowed 
to optimize it away because of the side effects--which is why you're seeing the 
divide even with -O2.  Although I would guess that in an implementation which 
does not have the side effects, the compiler would be able to substitute the NAN 
at compile time.)
Craig

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

end of thread, other threads:[~2017-04-20  0:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-19 23:50 [PATCH] Convert 0.0/0.0 to NAN Richard Allen
2017-04-20  0:01 ` Richard Allen
2017-04-20  0:25   ` Craig Howland

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).