From patchwork Mon May 13 08:14:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YunQiang Su X-Patchwork-Id: 89992 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 65B0938449C0 for ; Mon, 13 May 2024 08:16:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65B0938449C0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1715588165; bh=N6ky3TI/L9HTsbnUX44qIZsXeXFCCGuS2ndf9EJgttg=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=uJkW+eldAw1b1YbPS0abw6faE/fGVLcE0Q8FowpW2dn7Tx/Cjz/m85qQjvfMljdLE /7g8FeOfCGPjNLdgpshiS7K8ZyYPrlkVWb+0vET6pYqN+CAh/So/OMwsFKE0I3hrMO W2/qop9q2EgFzfkv251XLUp5z7aW25UImEurYOWc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by sourceware.org (Postfix) with ESMTPS id 028F4384388F for ; Mon, 13 May 2024 08:14:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 028F4384388F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 028F4384388F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715588088; cv=none; b=cezdOe/CgDnAP85JNmHGJRI4aev5tG3hHelczFkS16nzO5IvNVZooH9aggKP4RgSqPDtavH9rczVAhOp/FgECiloZxwc7V49mm2j7YRwkWTRlWpuMusAr8N//a8VP+3F1jLMekrGWC0diTrJw6ua8dGWqEXygij6ox9fPOlMRSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715588088; c=relaxed/simple; bh=wdBGmkmKUOOsO2QUeGX1W5uK/IrZotmzO3L8c/Y1pe4=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=qboPvnaKAGaVHW5U9mJBjD0v118QQk/nRCvsJWKTKLCXCtzfSb68wvx8Se+0vRXmnvgms0BXhUX9osa/iF2urCZjnzQrwPOR2PRL1pq397IK1myBPJeNRmsg6oCGwoSbsD/Wg8kA+gc5Xu8RUTbPJnGjX8tNZjiULHjzTptq4Mo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5aa20adda1dso2483781eaf.1 for ; Mon, 13 May 2024 01:14:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715588080; x=1716192880; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N6ky3TI/L9HTsbnUX44qIZsXeXFCCGuS2ndf9EJgttg=; b=ZN2v7A1YLGrF5LBhXv8P1TqA3mlw75c5rku2Chg8nfmCuSdBN01AUKWJmAkcS+q471 Op1g33Xuon9WBx4zPxDHQghYE8wYfQCvvMWTXUct2p/16CiVl9q4be2p8Tksy5kuDMM3 mcKPKneY7UiKFOU00X5wCBer4sT7CyAHS5N/OUB3rF8WqH4LfVJ4hqAl6GvHJRv9kYL/ 1Whyj9mPlH9KOqxbKDeLjqxmp5+v8+PbqGncWFpPWxT7QjIq2BqrSgwXWIQMxmt/uP/C vbtkYLD3AtZqjiiZNXmy8N1Q1kCeBD5vzeM7weIQC9aMBatd0PwvWYGr3PYBaBuwUW8F emwg== X-Gm-Message-State: AOJu0Yxp67CWXj2L0/dk2kiLNQV8UIVqHEKBMGnslJFEPizzY74JcBAQ PGGYu04+4nHvTIv6Dp0rwaavJkKNagkSbln0EOGvDGPA7zm5AA0NEKkZWLfb X-Google-Smtp-Source: AGHT+IFqEbg4rFnXSqHZbtZ5P4I3Zx+tvI0BnluMuAyymbvuapmyT9hL52RE6xNHn1ZclLLZFtaqGQ== X-Received: by 2002:a05:6358:d39b:b0:183:e9f8:19ad with SMTP id e5c5f4694b2df-193bb63ade1mr1133571155d.18.1715588079560; Mon, 13 May 2024 01:14:39 -0700 (PDT) Received: from localhost.localdomain ([2409:8700:2482:720:5054:ff:fe12:3456]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-6340c99b41dsm7219999a12.52.2024.05.13.01.14.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 01:14:39 -0700 (PDT) From: YunQiang Su To: libc-alpha@sourceware.org Subject: [PATCH 3/6] MIPS/math: Implement optimized issignaling(f) Date: Mon, 13 May 2024 16:14:26 +0800 Message-Id: <20240513081429.1749898-4-syq@gcc.gnu.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240513081429.1749898-1-syq@gcc.gnu.org> References: <20240513081429.1749898-1-syq@gcc.gnu.org> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org MIPSr6 introduces class.fmt instructions, which can help us to determine whether a number is sNAN. We define __mips_issignaling(f) as always inline in mips/math_private.h, and call them in s_issignaling(f).c. Issignaling operation is also used by some other functions, such as fmax. Inlining it can introduce better codesize and performance, due to libcall may issue some stack operations. * sysdeps/mips/fpu_control.h: Define FCLASS constants. * sysdeps/mips/ieee754/s_issignaling.c * sysdeps/mips/ieee754/s_issignalingf.c * sysdeps/mips/math_private.h: Define __mips_issignaling(f). Signed-off-by: YunQiang Su --- sysdeps/mips/fpu_control.h | 17 +++++++ sysdeps/mips/ieee754/s_issignaling.c | 28 ++++++++++++ sysdeps/mips/ieee754/s_issignalingf.c | 27 +++++++++++ sysdeps/mips/math_private.h | 65 +++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 sysdeps/mips/ieee754/s_issignaling.c create mode 100644 sysdeps/mips/ieee754/s_issignalingf.c diff --git a/sysdeps/mips/fpu_control.h b/sysdeps/mips/fpu_control.h index 3ceb34fc25..086293117e 100644 --- a/sysdeps/mips/fpu_control.h +++ b/sysdeps/mips/fpu_control.h @@ -127,6 +127,23 @@ extern void __mips_fpu_setcw (fpu_control_t) __THROW; /* Default control word set at startup. */ extern fpu_control_t __fpu_control; +# define _FCLASS_SNAN (1 << 0) +# define _FCLASS_QNAN (1 << 1) +# define _FCLASS_MINF (1 << 2) +# define _FCLASS_MNORM (1 << 3) +# define _FCLASS_MSUBNORM (1 << 4) +# define _FCLASS_MZERO (1 << 5) +# define _FCLASS_PINF (1 << 6) +# define _FCLASS_PNORM (1 << 7) +# define _FCLASS_PSUBNORM (1 << 8) +# define _FCLASS_PZERO (1 << 9) + +# define _FCLASS_ZERO (_FCLASS_MZERO | _FCLASS_PZERO) +# define _FCLASS_SUBNORM (_FCLASS_MSUBNORM | _FCLASS_PSUBNORM) +# define _FCLASS_NORM (_FCLASS_MNORM | _FCLASS_PNORM) +# define _FCLASS_INF (_FCLASS_MINF | _FCLASS_PINF) +# define _FCLASS_NAN (_FCLASS_SNAN | _FCLASS_QNAN) + #endif /* __mips_soft_float */ #endif /* fpu_control.h */ diff --git a/sysdeps/mips/ieee754/s_issignaling.c b/sysdeps/mips/ieee754/s_issignaling.c new file mode 100644 index 0000000000..3bf65f07a5 --- /dev/null +++ b/sysdeps/mips/ieee754/s_issignaling.c @@ -0,0 +1,28 @@ +/* issignaling(). MIPS version. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + +int +__issignaling (double x) +{ + return __mips_issignaling (x); +} +libm_hidden_def (__issignaling) diff --git a/sysdeps/mips/ieee754/s_issignalingf.c b/sysdeps/mips/ieee754/s_issignalingf.c new file mode 100644 index 0000000000..14863595bc --- /dev/null +++ b/sysdeps/mips/ieee754/s_issignalingf.c @@ -0,0 +1,27 @@ +/* issignalingf(). MIPS version. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + +int +__issignalingf (float x) +{ + return __mips_issignalingf (x); +} +libm_hidden_def (__issignalingf) diff --git a/sysdeps/mips/math_private.h b/sysdeps/mips/math_private.h index 4da8b0c2d9..6c388ddb64 100644 --- a/sysdeps/mips/math_private.h +++ b/sysdeps/mips/math_private.h @@ -53,4 +53,69 @@ # endif #endif /* __mips_hard_float && !__mips_single_float */ +/* Copy from sysdeps/ieee754/flt-32/s_issignalingf.c. + Function call can introduce lots for stack operations. Inline can even + reduce codesize. */ +static __always_inline int +__mips_issignalingf (float x) +{ +#if __mips_isa_rev >= 6 && defined(__mips_hard_float) + float c; + int ret; + asm volatile("class.s %0, %1" : "=f"(c) : "f"(x)); + asm volatile("mfc1 %0, %1" : "=r"(ret) : "f"(c)); + return ret & _FCLASS_SNAN; +#else + uint32_t xi; + GET_FLOAT_WORD (xi, x); +# if HIGH_ORDER_BIT_IS_SET_FOR_SNAN + /* We only have to care about the high-order bit of x's significand, because + having it set (sNaN) already makes the significand different from that + used to designate infinity. */ + return (xi & 0x7fc00000) == 0x7fc00000; +# else + /* To keep the following comparison simple, toggle the quiet/signaling bit, + so that it is set for sNaNs. This is inverse to IEEE 754-2008 (as well as + common practice for IEEE 754-1985). */ + xi ^= 0x00400000; + /* We have to compare for greater (instead of greater or equal), because x's + significand being all-zero designates infinity not NaN. */ + return (xi & 0x7fffffff) > 0x7fc00000; +# endif +#endif +} + +/* Copy from sysdeps/ieee754/dbl-64/s_issignaling.c. + Function call can introduce lots for stack operations. Inline can even + reduce codesize. */ +static __always_inline int +__mips_issignaling (double x) +{ +#if __mips_isa_rev >= 6 && defined(__mips_hard_float) \ + && !defined(__mips_single_float) + double c; + int ret; + asm volatile("class.d %0, %1" : "=f"(c) : "f"(x)); + asm volatile("mfc1 %0, %1" : "=r"(ret) : "f"(c)); + return ret & _FCLASS_SNAN; +#else + uint32_t xi; + GET_HIGH_WORD (xi, x); +# if HIGH_ORDER_BIT_IS_SET_FOR_SNAN + /* We only have to care about the high-order bit of x's significand, because + having it set (sNaN) already makes the significand different from that + used to designate infinity. */ + return (xi & UINT32_C (0x7ff80000)) == UINT32_C (0x7ff80000); +# else + /* To keep the following comparison simple, toggle the quiet/signaling bit, + so that it is set for sNaNs. This is inverse to IEEE 754-2008 (as well as + common practice for IEEE 754-1985). */ + xi ^= UINT32_C (0x00080000); + /* We have to compare for greater (instead of greater or equal), because x's + significand being all-zero designates infinity not NaN. */ + return (xi & UINT32_C (0x7fffffff)) > UINT32_C (0x7ff80000); +# endif +#endif +} + #endif /* MIPS_MATH_PRIVATE_H */