From patchwork Tue Apr 9 06:24:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1921234 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Cn/wtToy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VDGCk6Tf4z1yYM for ; Tue, 9 Apr 2024 16:25:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C46A13858CDA for ; Tue, 9 Apr 2024 06:25:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 10E773858CD1 for ; Tue, 9 Apr 2024 06:24:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 10E773858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 10E773858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712643889; cv=none; b=XPe0dKjgkz/I6DPXsrsqN05bnSBGp5qbd4SzZpbfsFz8+Uucn+SPNWmEEHigBnfKGduK9f1eloRek2B1unzgSyUv/fe8a7jIbW6UlOsYhPbq8FnQvj8YD5kjvmiNo7dZO+KYZ8WUnSsCsssNEuP83Ei9/LB+qI7tbgW46Bui4fE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712643889; c=relaxed/simple; bh=JIQAfC20ZSC2qxP0Ijezi2cNiVawiMvM0g8Px8ynV3U=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=FJ2/pfEDu/QwodbDd5Ali/JS7jb7Ghf9vKQRAwzuNw4UnVq+T7oXgmBpRxPwmxn7uqclW26JfRzA7SznIE7xzp4CGjQCVLCdzWQVVdcv6hueYw+fTozeawRM3XG6bGmj3RYBSvZgwqyM7VEzGVG25ogRqnoNSndWqTEPlYJZaRk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712643886; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type; bh=v22H3u2hkp51nEeakO1I5067+goHJSzo9N2Xue7tEo0=; b=Cn/wtToyyQxxlyflm7yE6u4rxAuLzhiRN2x30+Mn3ncIndzYiCjveHj+XBh2rjBo+RaaaX Q99UtOsBshM3uU/TCGyj2Py1Pj1p+g+ioohIy4U276XMncHPRNJC2cH6SQAumRH11vvY7B BA4EEpbBf6dwLa6N8oU0rBAcDr6oU2w= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-428-kH9ay4Z-OJeKaeN-VPc3aA-1; Tue, 09 Apr 2024 02:24:45 -0400 X-MC-Unique: kH9ay4Z-OJeKaeN-VPc3aA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 075E23806709 for ; Tue, 9 Apr 2024 06:24:45 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C243017AB1 for ; Tue, 9 Apr 2024 06:24:44 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4396OYmQ1298812 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT) for ; Tue, 9 Apr 2024 08:24:34 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4396OYEf1298811 for gcc-patches@gcc.gnu.org; Tue, 9 Apr 2024 08:24:34 +0200 Date: Tue, 9 Apr 2024 08:24:33 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [committed] libquadmath: Use soft-fp for sqrtq finite positive arguments [PR114623] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! sqrt should be 0.5ulp precise, but the current implementation is less precise than that. The following patch uses the soft-fp code (like e.g. glibc for x86) for it if possible. I didn't want to replicate the libgcc infrastructure for choosing the right sfp-machine.h, so the patch just uses a single generic implementation. As the code is used solely for the finite positive arguments, it shouldn't generate NaNs (so the exact form of canonical QNaN/SNaN is irrelevant), and sqrt for these shouldn't produce underflows/overflows either, for < 1.0 arguments it always returns larger values than the argument and for > 1.0 smaller values than the argument. Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2024-04-09 Jakub Jelinek PR libquadmath/114623 * sfp-machine.h: New file. * math/sqrtq.c: Include from libgcc/soft-fp also soft-fp.h and quad.h if possible. (USE_SOFT_FP): Define in that case. (sqrtq): Use soft-fp based implementation for the finite positive arguments if possible. Jakub --- libquadmath/sfp-machine.h.jj 2024-04-08 11:47:59.604124562 +0200 +++ libquadmath/sfp-machine.h 2024-04-08 13:13:10.950342552 +0200 @@ -0,0 +1,54 @@ +/* libquadmath uses soft-fp only for sqrtq and only for + the positive finite case, so it doesn't care about + NaN representation, nor tininess after rounding vs. + before rounding, all it cares about is current rounding + mode and raising inexact exceptions. */ +#if __SIZEOF_LONG__ == 8 +#define _FP_W_TYPE_SIZE 64 +#define _FP_I_TYPE long long +#define _FP_NANFRAC_Q _FP_QNANBIT_Q, 0 +#else +#define _FP_W_TYPE_SIZE 32 +#define _FP_I_TYPE int +#define _FP_NANFRAC_Q _FP_QNANBIT_Q, 0, 0, 0 +#endif +#define _FP_W_TYPE unsigned _FP_I_TYPE +#define _FP_WS_TYPE signed _FP_I_TYPE +#define _FP_QNANNEGATEDP 0 +#define _FP_NANSIGN_Q 1 +#define _FP_KEEPNANFRACP 1 +#define _FP_TININESS_AFTER_ROUNDING 0 +#define _FP_DECL_EX \ + unsigned int fp_roundmode __attribute__ ((unused)) = FP_RND_NEAREST; +#define FP_ROUNDMODE fp_roundmode +#define FP_INIT_ROUNDMODE \ + do \ + { \ + switch (fegetround ()) \ + { \ + case FE_UPWARD: \ + fp_roundmode = FP_RND_PINF; \ + break; \ + case FE_DOWNWARD: \ + fp_roundmode = FP_RND_MINF; \ + break; \ + case FE_TOWARDZERO: \ + fp_roundmode = FP_RND_ZERO; \ + break; \ + default: \ + break; \ + } \ + } \ + while (0) +#define FP_HANDLE_EXCEPTIONS \ + do \ + { \ + if (_fex & FP_EX_INEXACT) \ + { \ + volatile double eight = 8.0; \ + volatile double eps \ + = DBL_EPSILON; \ + eight += eps; \ + } \ + } \ + while (0) --- libquadmath/math/sqrtq.c.jj 2020-01-12 11:54:39.786362520 +0100 +++ libquadmath/math/sqrtq.c 2024-04-08 12:53:41.280187715 +0200 @@ -1,6 +1,17 @@ #include "quadmath-imp.h" #include #include +#if __has_include("../../libgcc/soft-fp/soft-fp.h") \ + && __has_include("../../libgcc/soft-fp/quad.h") \ + && defined(FE_TONEAREST) \ + && defined(FE_UPWARD) \ + && defined(FE_DOWNWARD) \ + && defined(FE_TOWARDZERO) \ + && defined(FE_INEXACT) +#define USE_SOFT_FP 1 +#include "../../libgcc/soft-fp/soft-fp.h" +#include "../../libgcc/soft-fp/quad.h" +#endif __float128 sqrtq (const __float128 x) @@ -20,6 +31,18 @@ sqrtq (const __float128 x) return (x - x) / (x - x); } +#if USE_SOFT_FP + FP_DECL_EX; + FP_DECL_Q (X); + FP_DECL_Q (Y); + + FP_INIT_ROUNDMODE; + FP_UNPACK_Q (X, x); + FP_SQRT_Q (Y, X); + FP_PACK_Q (y, Y); + FP_HANDLE_EXCEPTIONS; + return y; +#else if (x <= DBL_MAX && x >= DBL_MIN) { /* Use double result as starting point. */ @@ -59,5 +82,5 @@ sqrtq (const __float128 x) y -= 0.5q * (y - x / y); y -= 0.5q * (y - x / y); return y; +#endif } -