fdim(), fdimf() and fdiml() radically simplified

Submitted by Stefan Kanthak on Dec. 11, 2019, 9:55 a.m.

Details

Message ID 530261068B3547C5A40104AC03005B28@H270
State New
Series "fdim(), fdimf() and fdiml() radically simplified"
Headers show

Commit Message

Stefan Kanthak Dec. 11, 2019, 9:55 a.m.
Yet another optimisation/simplification in the math subtree.

JFTR: I'm NOT subscribed to your mailing list, so CC: me in replies!

Patch hide | download patch | download mbox

--- -/src/math/fdim.c
+++ +/src/math/fdim.c
@@ -3,8 +3,4 @@ 
 double fdim(double x, double y)
 {
-        if (isnan(x))
-                return x;
-        if (isnan(y))
-                return y;
-        return x > y ? x - y : 0;
+        return x <= y ? 0.0 : x - y;
 }

--- -/src/math/fdimf.c
+++ +/src/math/fdimf.c
@@ -3,8 +3,4 @@ 
 float fdimf(float x, float y)
 {
-        if (isnan(x))
-                return x;
-        if (isnan(y))
-                return y;
-        return x > y ? x - y : 0;
+        return x <= y ? 0.0 : x - y;
 }

--- -/src/math/fdiml.c
+++ +/src/math/fdiml.c
@@ -10,8 +10,4 @@ 
 long double fdiml(long double x, long double y)
 {
-        if (isnan(x))
-                return x;
-        if (isnan(y))
-                return y;
-        return x > y ? x - y : 0;
+        return x <= y ? 0.0 : x - y;
 }

Comments

Szabolcs Nagy Dec. 11, 2019, 10:39 a.m.
* Stefan Kanthak <stefan.kanthak@nexgo.de> [2019-12-11 10:55:01 +0100]:
> Yet another optimisation/simplification in the math subtree.

these changes have incorrect fenv behaviour
(they signal the invalid exception on qnan input),
the result is fine though.

> 
> JFTR: I'm NOT subscribed to your mailing list, so CC: me in replies!
> 
> --- -/src/math/fdim.c
> +++ +/src/math/fdim.c
> @@ -3,8 +3,4 @@
>  double fdim(double x, double y)
>  {
> -        if (isnan(x))
> -                return x;
> -        if (isnan(y))
> -                return y;
> -        return x > y ? x - y : 0;
> +        return x <= y ? 0.0 : x - y;
>  }
> 
> --- -/src/math/fdimf.c
> +++ +/src/math/fdimf.c
> @@ -3,8 +3,4 @@
>  float fdimf(float x, float y)
>  {
> -        if (isnan(x))
> -                return x;
> -        if (isnan(y))
> -                return y;
> -        return x > y ? x - y : 0;
> +        return x <= y ? 0.0 : x - y;
>  }
> 
> --- -/src/math/fdiml.c
> +++ +/src/math/fdiml.c
> @@ -10,8 +10,4 @@
>  long double fdiml(long double x, long double y)
>  {
> -        if (isnan(x))
> -                return x;
> -        if (isnan(y))
> -                return y;
> -        return x > y ? x - y : 0;
> +        return x <= y ? 0.0 : x - y;
>  }