]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix hypot sNaN handling (bug 20940).
authorJoseph Myers <joseph@codesourcery.com>
Wed, 7 Dec 2016 01:16:36 +0000 (01:16 +0000)
committerJoseph Myers <joseph@codesourcery.com>
Wed, 7 Dec 2016 01:16:36 +0000 (01:16 +0000)
TS 18661-1 generally defines libm functions taking sNaN arguments to
return qNaN and raise "invalid", even for the cases where a
corresponding qNaN argument would not result in a qNaN return.  This
includes hypot with one argument being an infinity and the other being
an sNaN.  This patch duly fixes hypot implementatations in glibc
(generic and powerpc) to ensure qNaN, computed by arithmetic on the
arguments, is returned in that case.

Various implementations do their checks for infinities and NaNs inline
by manipulating the representations of the arguments.  For simplicity,
this patch just uses issignaling to check for sNaN arguments.  This
could be inlined like the existing code (with due care about reversed
quiet NaN conventions, for implementations where that is relevant),
but given that all these checks are in cases where it's already known
at least one argument is not finite, which should be the uncommon
case, that doesn't seem worthwhile unless performance issues are
observed in practice.

Tested for x86_64, x86, mips64 and powerpc.

[BZ #20940]
* sysdeps/ieee754/dbl-64/e_hypot.c (__ieee754_hypot): Do not
return Inf for arguments Inf and sNaN.
* sysdeps/ieee754/flt-32/e_hypotf.c (__ieee754_hypotf): Likewise.
* sysdeps/ieee754/ldbl-128/e_hypotl.c (__ieee754_hypotl):
Likewise.
* sysdeps/ieee754/ldbl-128ibm/e_hypotl.c (__ieee754_hypotl):
Likewise.
* sysdeps/ieee754/ldbl-96/e_hypotl.c (__ieee754_hypotl): Likewise.
* sysdeps/powerpc/fpu/e_hypot.c (TEST_INF_NAN): Do not return Inf
for arguments Inf and sNaN.  When returning a NaN, compute it by
arithmetic on the arguments.
* sysdeps/powerpc/fpu/e_hypotf.c (TEST_INF_NAN): Likewise.
* math/libm-test.inc (pow_test_data): Add tests of sNaN arguments.

ChangeLog
math/libm-test.inc
sysdeps/ieee754/dbl-64/e_hypot.c
sysdeps/ieee754/flt-32/e_hypotf.c
sysdeps/ieee754/ldbl-128/e_hypotl.c
sysdeps/ieee754/ldbl-128ibm/e_hypotl.c
sysdeps/ieee754/ldbl-96/e_hypotl.c
sysdeps/powerpc/fpu/e_hypot.c
sysdeps/powerpc/fpu/e_hypotf.c

index ab723ebfbb877847e2eafccd64c2802081e440ae..039bba6657e753f0f20dc8f733534db2fb402f37 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2016-12-07  Joseph Myers  <joseph@codesourcery.com>
+
+       [BZ #20940]
+       * sysdeps/ieee754/dbl-64/e_hypot.c (__ieee754_hypot): Do not
+       return Inf for arguments Inf and sNaN.
+       * sysdeps/ieee754/flt-32/e_hypotf.c (__ieee754_hypotf): Likewise.
+       * sysdeps/ieee754/ldbl-128/e_hypotl.c (__ieee754_hypotl):
+       Likewise.
+       * sysdeps/ieee754/ldbl-128ibm/e_hypotl.c (__ieee754_hypotl):
+       Likewise.
+       * sysdeps/ieee754/ldbl-96/e_hypotl.c (__ieee754_hypotl): Likewise.
+       * sysdeps/powerpc/fpu/e_hypot.c (TEST_INF_NAN): Do not return Inf
+       for arguments Inf and sNaN.  When returning a NaN, compute it by
+       arithmetic on the arguments.
+       * sysdeps/powerpc/fpu/e_hypotf.c (TEST_INF_NAN): Likewise.
+       * math/libm-test.inc (pow_test_data): Add tests of sNaN arguments.
+
 2016-12-06  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #20916]
index 9123dcfc48f904b574ad18c5f3742ee697bf6dc7..e973a3f6aea6dd85b88a05037dab5ff0618e9f28 100644 (file)
@@ -8344,6 +8344,14 @@ static const struct test_ff_f_data hypot_test_data[] =
     TEST_ff_f (hypot, -qnan_value, plus_infty, plus_infty, ERRNO_UNCHANGED|NO_TEST_INLINE),
     TEST_ff_f (hypot, qnan_value, minus_infty, plus_infty, ERRNO_UNCHANGED|NO_TEST_INLINE),
     TEST_ff_f (hypot, -qnan_value, minus_infty, plus_infty, ERRNO_UNCHANGED|NO_TEST_INLINE),
+    TEST_ff_f (hypot, plus_infty, snan_value, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, plus_infty, -snan_value, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, minus_infty, snan_value, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, minus_infty, -snan_value, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, snan_value, plus_infty, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, -snan_value, plus_infty, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, snan_value, minus_infty, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
+    TEST_ff_f (hypot, -snan_value, minus_infty, qnan_value, INVALID_EXCEPTION|NO_TEST_INLINE),
 
     TEST_ff_f (hypot, 0, qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, 0, -qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
@@ -8361,6 +8369,22 @@ static const struct test_ff_f_data hypot_test_data[] =
     TEST_ff_f (hypot, min_subnorm_value, -qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -min_subnorm_value, qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -min_subnorm_value, -qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_ff_f (hypot, 0, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, 0, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, minus_zero, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, minus_zero, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, max_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, max_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -max_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -max_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, min_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, min_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -min_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -min_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, min_subnorm_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, min_subnorm_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -min_subnorm_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -min_subnorm_value, -snan_value, qnan_value, INVALID_EXCEPTION),
     TEST_ff_f (hypot, qnan_value, 0, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -qnan_value, 0, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, qnan_value, minus_zero, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
@@ -8377,11 +8401,39 @@ static const struct test_ff_f_data hypot_test_data[] =
     TEST_ff_f (hypot, -qnan_value, min_subnorm_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, qnan_value, -min_subnorm_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -qnan_value, -min_subnorm_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_ff_f (hypot, snan_value, 0, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, 0, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, minus_zero, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, minus_zero, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, max_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, max_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, -max_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, -max_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, min_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, min_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, -min_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, -min_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, min_subnorm_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, min_subnorm_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, -min_subnorm_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, -min_subnorm_value, qnan_value, INVALID_EXCEPTION),
 
     TEST_ff_f (hypot, qnan_value, qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, qnan_value, -qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -qnan_value, qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_ff_f (hypot, -qnan_value, -qnan_value, qnan_value, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_ff_f (hypot, qnan_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, qnan_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -qnan_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -qnan_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, qnan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, -qnan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, qnan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, -qnan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, snan_value, -snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, snan_value, qnan_value, INVALID_EXCEPTION),
+    TEST_ff_f (hypot, -snan_value, -snan_value, qnan_value, INVALID_EXCEPTION),
 
     AUTO_TESTS_ff_f (hypot),
   };
index f142c450a2e27cc884808b12939e64febbef38b5..76eb4083482657064996c7223b3f424fbedfaf71 100644 (file)
@@ -76,6 +76,8 @@ __ieee754_hypot (double x, double y)
        {
          u_int32_t low;
          w = a + b;                    /* for sNaN */
+         if (issignaling (a) || issignaling (b))
+           return w;
          GET_LOW_WORD (low, a);
          if (((ha & 0xfffff) | low) == 0)
            w = a;
index 717b82e42f08a8180405fb0a17430b50b692e1a9..fda2651a846815e70631c1f9fe66be1a7561e540 100644 (file)
@@ -26,9 +26,9 @@ __ieee754_hypotf(float x, float y)
        ha &= 0x7fffffff;
        GET_FLOAT_WORD(hb,y);
        hb &= 0x7fffffff;
-       if (ha == 0x7f800000)
+       if (ha == 0x7f800000 && !issignaling (y))
          return fabsf(x);
-       else if (hb == 0x7f800000)
+       else if (hb == 0x7f800000 && !issignaling (x))
          return fabsf(y);
        else if (ha > 0x7f800000 || hb > 0x7f800000)
          return fabsf(x) * fabsf(y);
index a93f5a4c8fa068b8d94d595c32064fdf5edd4761..6c4e178fbe63ed1a96eb09f2afe0855352e4b000 100644 (file)
@@ -67,6 +67,8 @@ __ieee754_hypotl(_Float128 x, _Float128 y)
           if(ha >= 0x7fff000000000000LL) {     /* Inf or NaN */
               u_int64_t low;
               w = a+b;                 /* for sNaN */
+              if (issignaling (a) || issignaling (b))
+                return w;
               GET_LDOUBLE_LSW64(low,a);
               if(((ha&0xffffffffffffLL)|low)==0) w = a;
               GET_LDOUBLE_LSW64(low,b);
index c68dac03b0ee9e1ebb7a15cfb0e29441232a580b..de5a66ab058c5760bd660e772beb688e5d095657 100644 (file)
@@ -67,6 +67,8 @@ __ieee754_hypotl(long double x, long double y)
        if(ha > 0x5f30000000000000LL) { /* a>2**500 */
           if(ha >= 0x7ff0000000000000LL) {     /* Inf or NaN */
               w = a+b;                 /* for sNaN */
+              if (issignaling (a) || issignaling (b))
+                return w;
               if(ha == 0x7ff0000000000000LL)
                 w = a;
               if(hb == 0x7ff0000000000000LL)
index ee3a07055b721bec54cd6b85d52aa91a7cd4ead6..6b55b6d8ee8817eb9394417575224df5a92df7d2 100644 (file)
@@ -68,6 +68,8 @@ long double __ieee754_hypotl(long double x, long double y)
               u_int32_t exp __attribute__ ((unused));
               u_int32_t high,low;
               w = a+b;                 /* for sNaN */
+              if (issignaling (a) || issignaling (b))
+                return w;
               GET_LDOUBLE_WORDS(exp,high,low,a);
               if(((high&0x7fffffff)|low)==0) w = a;
               GET_LDOUBLE_WORDS(exp,high,low,b);
index da0f2daed3c779ead554d52d7888839458f44ace..65314c6ff5652d2d6218d99df3bdb10b458ad0a1 100644 (file)
@@ -41,10 +41,11 @@ static const double pdnum   = 2.225073858507201e-308;
 #ifdef _ARCH_PWR7
 /* POWER7 isinf and isnan optimization are fast. */
 # define TEST_INF_NAN(x, y)                                       \
-   if (isinf(x) || isinf(y))                                      \
+   if ((isinf(x) || isinf(y))                                    \
+       && !issignaling (x) && !issignaling (y))                          \
        return INFINITY;                                           \
    if (isnan(x) || isnan(y))                                      \
-       return NAN;
+       return x + y;
 # else
 /* For POWER6 and below isinf/isnan triggers LHS and PLT calls are
  * costly (especially for POWER6). */
@@ -66,9 +67,10 @@ static const double pdnum   = 2.225073858507201e-308;
      uint32_t ht = hx; hx = hy; hy = ht;                         \
    }                                                             \
    if (hx >= 0x7ff00000) {                                       \
-     if (hx == 0x7ff00000 || hy == 0x7ff00000)                   \
+     if ((hx == 0x7ff00000 || hy == 0x7ff00000)                         \
+        && !issignaling (x) && !issignaling (y))                \
        return INFINITY;                                          \
-     return NAN;                                                 \
+     return x + y;                                              \
    }                                                             \
  } while (0)
 
index 48360828c36b771c151eb5e38170bbf69c163e51..c18281503c81f4671d262ad47692caa52bde4fa3 100644 (file)
 #ifdef _ARCH_PWR7
 /* POWER7 isinf and isnan optimizations are fast. */
 # define TEST_INF_NAN(x, y)                                      \
-   if (isinff(x) || isinff(y))                                   \
+   if ((isinff(x) || isinff(y))                                         \
+       && !issignaling (x) && !issignaling (y))                         \
      return INFINITY;                                            \
    if (isnanf(x) || isnanf(y))                                   \
-     return NAN;
+     return x + y;
 # else
 /* For POWER6 and below isinf/isnan triggers LHS and PLT calls are
  * costly (especially for POWER6). */
      uint32_t ht = hx; hx = hy; hy = ht;                         \
    }                                                             \
    if (hx >= 0x7f800000) {                                       \
-     if (hx == 0x7f800000 || hy == 0x7f800000)                   \
+     if ((hx == 0x7f800000 || hy == 0x7f800000)                         \
+        && !issignaling (x) && !issignaling (y))                \
        return INFINITY;                                          \
-     return NAN;                                                 \
+     return x + y;                                              \
    }                                                             \
  } while (0)
 #endif