From patchwork Mon Nov 1 20:20:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 46917 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7CC8385843A for ; Mon, 1 Nov 2021 20:22:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7CC8385843A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1635798130; bh=NYvcMKQdqXVn9nonTtd1e5HedPpgt2AdUuiaIyF/bOA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=E/+yLPNonn8ddr75eGTLF7j3D+QAEQAJdmao+qgAiZoEl+GV0Ia2Y5k/AkkcuH9e6 X5W4wlHtH2eZilo0iBp/RbvUQgv3rz3hAu3Hgw3omM0PSUSNtkJ0Jw4mCEeUFxQRPd 2V3IlXIWhyE5XBJ682ke5YK2MqNN5ujSfB7ARYQ4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by sourceware.org (Postfix) with ESMTPS id 1DBFA385842E for ; Mon, 1 Nov 2021 20:21:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1DBFA385842E Received: by mail-qt1-x830.google.com with SMTP id s1so15623293qta.13 for ; Mon, 01 Nov 2021 13:21:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NYvcMKQdqXVn9nonTtd1e5HedPpgt2AdUuiaIyF/bOA=; b=bgtbY40kZ91zpDWqpAfmE5UUUMgFrnwpZSvnBIspC3sk1vjIKFDz1qrYq6B4DachyN Nib705rDpFcEfgZIzXY647i6O14dSl9fILInXgVYoT9PlUxhzNa2Rnt6MH+NBXTv5LeL GAnthwdzsTtIjvsdGCr9T9QLLd+Nj18BXKAlGYDUIsNhyLHB9PFo5lzkWe5ISEn0/Nai vB6dCS6M3oRqXvunuFHald2zoW1Btvsh/G18vruj48s5QZ+7cPpMdUhFFgYcA65wgr2/ QnbgWIQpcqTE7SQgtPnluPaojQkeCc+puHL9CZ3DS+FoCA46ZKBzb6IvE67D9yLswupz cDVA== X-Gm-Message-State: AOAM53003q/68dUZeEBsqQMOBwg4OFXH1PrkTZVOErge177lcvIHTd1/ qGsoocAEm2IeacbdThpthdVqFhUAOw37AA== X-Google-Smtp-Source: ABdhPJx1U5ElHE0M9KW82RPrcLmnBHV1MyZiBagCZYd5N9p/hJlhDRdD0k/j155eVz5PyVp0sse3Dg== X-Received: by 2002:a05:622a:14c7:: with SMTP id u7mr6655376qtx.241.1635798064397; Mon, 01 Nov 2021 13:21:04 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b64f:7c54:165f:8728:a193]) by smtp.gmail.com with ESMTPSA id d11sm10023965qtx.81.2021.11.01.13.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 13:21:04 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 1/7] math: Simplify hypotf implementation Date: Mon, 1 Nov 2021 17:20:53 -0300 Message-Id: <20211101202059.1026032-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211101202059.1026032-1-adhemerval.zanella@linaro.org> References: <20211101202059.1026032-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Cc: Wilco Dijkstra Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 + . */ #include #include +#include "math_config.h" #include 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);