unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org
Cc: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Subject: [PATCH v3 1/7] math: Simplify hypotf implementation
Date: Mon,  1 Nov 2021 17:20:53 -0300	[thread overview]
Message-ID: <20211101202059.1026032-2-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20211101202059.1026032-1-adhemerval.zanella@linaro.org>

Use a more optimized comparison for check for NaN and infinite and
add an inlined issignaling implementation for float.  With gcc it
results in 2 FP comparisons.

The file Copyright is also changed to use  GPL, the implementation was
completely changed by 7c10fd3515f to use double precision instead of
scaling and this change removes all the GET_FLOAT_WORD usage.

Checked on x86_64-linux-gnu.
---
 sysdeps/ieee754/flt-32/e_hypotf.c    | 60 ++++++++++++----------------
 sysdeps/ieee754/flt-32/math_config.h |  9 +++++
 2 files changed, 35 insertions(+), 34 deletions(-)

diff --git a/sysdeps/ieee754/flt-32/e_hypotf.c b/sysdeps/ieee754/flt-32/e_hypotf.c
index e770947dc1..ea18775829 100644
--- a/sysdeps/ieee754/flt-32/e_hypotf.c
+++ b/sysdeps/ieee754/flt-32/e_hypotf.c
@@ -1,46 +1,38 @@
-/* e_hypotf.c -- float version of e_hypot.c.
- */
+/* Euclidean distance function.  Float/Binary32 version.
+   Copyright (C) 2012-2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #include <math.h>
 #include <math_private.h>
+#include "math_config.h"
 #include <libm-alias-finite.h>
 
 float
-__ieee754_hypotf(float x, float y)
+__ieee754_hypotf (float x, float y)
 {
-	double d_x, d_y;
-	int32_t ha, hb;
-
-	GET_FLOAT_WORD(ha,x);
-	ha &= 0x7fffffff;
-	GET_FLOAT_WORD(hb,y);
-	hb &= 0x7fffffff;
-	if (ha == 0x7f800000 && !issignaling (y))
-	  return fabsf(x);
-	else if (hb == 0x7f800000 && !issignaling (x))
-	  return fabsf(y);
-	else if (ha > 0x7f800000 || hb > 0x7f800000)
-	  return fabsf(x) * fabsf(y);
-	else if (ha == 0)
-	  return fabsf(y);
-	else if (hb == 0)
-	  return fabsf(x);
-
-	d_x = (double) x;
-	d_y = (double) y;
+   if (!isfinite(x) || !isfinite(y))
+     {
+       if ((isinf (x) || isinf (y))
+	   && !issignalingf_inline (x) && !issignalingf_inline (y))
+	 return INFINITY;
+       return x + y;
+     }
 
-	return (float) sqrt(d_x * d_x + d_y * d_y);
+  return sqrt ((double) x * (double) x + (double) y * (double) y);
 }
 #ifndef __ieee754_hypotf
 libm_alias_finite (__ieee754_hypotf, __hypotf)
diff --git a/sysdeps/ieee754/flt-32/math_config.h b/sysdeps/ieee754/flt-32/math_config.h
index 513454a297..daa8a82f99 100644
--- a/sysdeps/ieee754/flt-32/math_config.h
+++ b/sysdeps/ieee754/flt-32/math_config.h
@@ -101,6 +101,15 @@ asdouble (uint64_t i)
   return u.f;
 }
 
+static inline int
+issignalingf_inline (float x)
+{
+  uint32_t ix = asuint (x);
+  if (HIGH_ORDER_BIT_IS_SET_FOR_SNAN)
+    return (ix & 0x7fc00000) == 0x7fc00000;
+  return 2 * (ix ^ 0x00400000) > 2 * 0x7fc00000UL;
+}
+
 #define NOINLINE __attribute__ ((noinline))
 
 attribute_hidden float __math_oflowf (uint32_t);
-- 
2.32.0


  reply	other threads:[~2021-11-01 20:22 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-01 20:20 [PATCH v3 0/7] Improve hypot() Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` Adhemerval Zanella via Libc-alpha [this message]
2021-11-01 20:20 ` [PATCH v3 2/7] math: Use an improved algorithm for hypot (dbl-64) Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` [PATCH v3 3/7] math: Use an improved algorithm for hypotl (ldbl-96) Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` [PATCH v3 4/7] math: Use an improved algorithm for hypotl (ldbl-128) Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` [PATCH v3 5/7] math: Remove powerpc e_hypot Adhemerval Zanella via Libc-alpha
2021-11-09 19:28   ` Paul A. Clarke via Libc-alpha
2021-11-10 14:34     ` Wilco Dijkstra via Libc-alpha
2021-11-10 14:43       ` Paul A. Clarke via Libc-alpha
2021-11-10 14:47       ` Adhemerval Zanella via Libc-alpha
2021-11-11 17:05         ` Wilco Dijkstra via Libc-alpha
2021-11-11 17:13           ` Adhemerval Zanella via Libc-alpha
2021-11-11 19:48             ` Wilco Dijkstra via Libc-alpha
2021-11-11 20:54               ` Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` [PATCH v3 6/7] i386: Move hypot implementation to C Adhemerval Zanella via Libc-alpha
2021-11-01 20:20 ` [PATCH v3 7/7] math: Remove the error handling wrapper from hypot and hypotf Adhemerval Zanella via Libc-alpha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211101202059.1026032-2-adhemerval.zanella@linaro.org \
    --to=libc-alpha@sourceware.org \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=adhemerval.zanella@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).