From patchwork Mon Apr 28 17:03:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 885485 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp5292206wrs; Mon, 28 Apr 2025 10:10:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVs0cK0VeEYRQiu1brmJVXG0cbQ9sZ9CriTp/jM5hLmZCEGuXt0ZmfhQq/oypdmoeMb/bcsNw==@linaro.org X-Google-Smtp-Source: AGHT+IFKVoBSWX+EqEVSthw7lEHWJkmFA56LC835lhTy+8Wu4rBlDiWzr25BpJS6GgcwK0v2Z3xu X-Received: by 2002:a05:620a:40d3:b0:7c5:61b2:b95 with SMTP id af79cd13be357-7cabdd8f731mr72225285a.30.1745860221624; Mon, 28 Apr 2025 10:10:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745860221; cv=pass; d=google.com; s=arc-20240605; b=JHNgvEyzqwL9D5XlKA3Ok7DErdVz4dFl091m1sECrCv+vjQGYRatZ8w4qSoECdxTbX EUQvEoy0uhwEkXhZATT/Z4U0C6ojfl4ANbiLvCN8qQMCMjs3WhFKf1Jgv9ovb2dCohcX gNoO4kxRSO821diaPcIVJwBKKlXwhdZSWWqaDX4CePXVl0q9at6k/zYoRo9xwH5IRIN/ 4uTiVk3Htv+ECH+Dpq5gYxSTLzrlc3gHVLsXAzVzVD19r3iuzsAfoHv16peHlQAOMJWk VB76TdHS2NaDxqMdO61kmctcWuFEIjZFS1fM9LpGWOaqpUXuwVA+sYZtU9NYuq28dPFK 720A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; fh=GVJhlWdd4NNaBhOZ/uQFycFAyzDMQer7CEL5IpLL3Iw=; b=TBtzxS4A0xu9n4Zp0FhxDu7NvYqc/jOiAEvibN9V2x6mviINIFpSOqQlr4QB9WzyhD yHSw3jfTO1NDR7mGYsGM+3mECVh1D/Rm6uVtmwdIv4lQ4Nu25h4odYevVHA0qpSwmL3t q0TC8Ky0+mGwv77U2EZnxwccd9x889AKgzxAESGnsQ7+CzIwekk7lnyz3odPjnsoNTol t4WlUuJOwP5bC6fyNRN7tM4cFFlbRP5w+L8DQQw67QDOmmP3lHh7V08thvt/dfSY/bAu RjM4WLsPYbLstE4ptaQDqLUnLMrmLb9JZ4itNLduRVPZYlsmeDPDSUQvOKxmdFblouiE iurA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BUi6CA38; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id af79cd13be357-7c958eb8968si1037618485a.523.2025.04.28.10.10.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:10:21 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BUi6CA38; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25E473857C6E for ; Mon, 28 Apr 2025 17:10:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25E473857C6E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=BUi6CA38 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id A0E77385840B for ; Mon, 28 Apr 2025 17:04:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0E77385840B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A0E77385840B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859880; cv=none; b=xUYGUHSMnn0S4QWx9gDbowsmq6B6suTXPXEfE95HK012su1eu9IrjtQ0IT6sB3zelIYO+hDpPy222xPQFPvWJa986YRWrJC0LUQok7hKGbgbpoVgeIPVXOLiCAusQHxIgqGd7kEGUMDBuLtK/YHwIe7T/FkQRrCWkgeT8urzbsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859880; c=relaxed/simple; bh=vm4GV3mSzgBQAlfatnEHYao32yPuNi9GGPmlnPW81mw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=eKSPT1telhowxHpvze2U0/GU8+0U4mufaxIXzSf4VciIN0oR0k7YnFlNHRyYrcdwWDQCytLxIB+UhaNvcObZS30tfSb+ofFLGUZZ2Tzu9o94USn9cx+EKVV7ymPSFXISdznnGeYUGBavv7TczYFHj3CBQXXv+8LMj35BPdSLYPo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0E77385840B Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-306bf444ba2so4907555a91.1 for ; Mon, 28 Apr 2025 10:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745859879; x=1746464679; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; b=BUi6CA38ZeQh3gqkuMt+Wcmt1qVWqAIJFgKvjeKI/ijSv8Yj8Vn8iOAE9UL5+G1ob9 8zgjRKj5v6jjTKSlLCtarOoR5aoqb7xYYMlqEOPF7F3dG19V1t57xZzZRoKkDZV+YK+l vrAqTSDfAYx3tgLNN/4KYrpsnzMeuKsRE00hrIVvo89fjStSmm5detgUf7S6FDRLIgYZ leIyZpElx5skj7BzL1lcHcaZBn1KaxumeKPWBraa6UdYfO46D3g90uV/bCbvVrgJoGW4 uB4Mp+ps02aYP++IQeVFEFBdIfvcPPHzIcuj/eQXKjKtz8umdW0k3Xlzx2O1AP+CIAZM 9MIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745859879; x=1746464679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; b=M0/4pwBA4hWcF8oIs3bN9+fHuO2x9Gt0fDcT76mbW6EubgvJYB/AZl2Wuq09U/6Jt0 PMRKKIl3dccDFcmnNWwhmrGOfnCAr4Qbx2H1vXHvc79cCQk4hRz6iPPsB/rVkNrNgLbO jN44LphBgDUsdBbTyPRD5m51BojXP1tzxMB/XiMoBekrO8RhcE6tlB6DhsJ21Lw8mHB+ /mH+g0rjVjCFZ3luvVX1FDCVc0jSvHT4/LU/6mOeni8f67vAvWoI3YCCDgqNiQD7jlqq MHxsw3pk2ymE6mGLccxquFSY3s0sdHQHJe4uX7XpZ/4O6ljqsrAWjAYFKsXPEGFpFqje HCNA== X-Gm-Message-State: AOJu0YwTq6LoOFpSRzzexcFOQV5BxAOOmoEBuf/Z92I+FyNaKDW0QgsA qCTHozfwqNMU5DhQSf1qeZlasShnP4o9HoDtZh4h9QtMm2KF7pN3GXCWVLdxOZiATrPpFPuiMXI / X-Gm-Gg: ASbGncsjL1mTWU5cRIM0bMQP2RsFqdtqnSVeyErxYiR41ztfcxgBMP3380fsctM+wlS /d6kuFeXW4hPHP4P6Bj1OgjoV1B/JMli4G5W/q9Dk16WdFOCQMbos5Xs/0msQs/nLLlMfD2VRlD /BsVgWmnZW0XgjfA1+XyMU7TScnAu6zxM72Ii47JyKonusJwUe1Iq5InZhVomzfIvoDpSKQ3PQa 6VVs2f1fOPUGTmMnObZbzQByTuz7h8Q7z1C2pjlmIBkzoOVDufst73TL7JIiZozmYPCoNg664VQ 1AIPBhxFAiBfJPVUqG3YzA+ViiUt7HaHiijqOgqQj/bjkjqLc4kHtA== X-Received: by 2002:a17:90b:1cc8:b0:2fa:f8d:65de with SMTP id 98e67ed59e1d1-30a21593a56mr733124a91.22.1745859879202; Mon, 28 Apr 2025 10:04:39 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:ce18:36e8:dea9:8b39]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f773725csm8322332a91.3.2025.04.28.10.04.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:04:38 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: [PATCH v2 1/4] math: Remove UB and optimize double ilogb Date: Mon, 28 Apr 2025 14:03:41 -0300 Message-ID: <20250428170430.2030400-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> References: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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~patch=linaro.org@sourceware.org The subnormal exponent calculation invokes UB by left shifting the signed expoenent to find the first leading bit. The implementation also used 32 bits operations, which is generates suboptimal code in 64 bits architectures. The patch reimplements ilogb using the math_config.h macros and uses the new stdbit function to simplify the subnormal handling. On aarch64 it generates better code: * master: 0000000000000000 <__ieee754_ilogb>: 0: 9e660000 fmov x0, d0 4: d360fc02 lsr x2, x0, #32 8: d360f801 ubfx x1, x0, #32, #31 c: f26c285f tst x2, #0x7ff00000 10: 540001a1 b.ne 44 <__ieee754_ilogb+0x44> // b.any 14: 2a000022 orr w2, w1, w0 18: 34000322 cbz w2, 7c <__ieee754_ilogb+0x7c> 1c: 35000221 cbnz w1, 60 <__ieee754_ilogb+0x60> 20: 2a0003e1 mov w1, w0 24: 7100001f cmp w0, #0x0 28: 12808240 mov w0, #0xfffffbed // #-1043 2c: 540000ad b.le 40 <__ieee754_ilogb+0x40> 30: 531f7821 lsl w1, w1, #1 34: 51000400 sub w0, w0, #0x1 38: 7100003f cmp w1, #0x0 3c: 54ffffac b.gt 30 <__ieee754_ilogb+0x30> 40: d65f03c0 ret 44: 13147c20 asr w0, w1, #20 48: 12b00202 mov w2, #0x7fefffff // #2146435071 4c: 510ffc00 sub w0, w0, #0x3ff 50: 6b02003f cmp w1, w2 54: 12b00001 mov w1, #0x7fffffff // #2147483647 58: 1a819000 csel w0, w0, w1, ls // ls = plast 5c: d65f03c0 ret 60: 53155021 lsl w1, w1, #11 64: 12807fa0 mov w0, #0xfffffc02 // #-1022 68: 531f7821 lsl w1, w1, #1 6c: 51000400 sub w0, w0, #0x1 70: 7100003f cmp w1, #0x0 74: 54ffffac b.gt 68 <__ieee754_ilogb+0x68> 78: d65f03c0 ret 7c: 320107e0 mov w0, #0x80000001 // #-2147483647 80: d65f03c0 ret * patch: 0000000000000000 <__ieee754_ilogb>: 0: 9e660001 fmov x1, d0 4: d374f820 ubfx x0, x1, #52, #11 8: 350000e0 cbnz w0, 24 <__ieee754_ilogb+0x24> c: d374cc21 lsl x1, x1, #12 10: b4000141 cbz x1, 38 <__ieee754_ilogb+0x38> 14: dac01021 clz x1, x1 18: 12807fc0 mov w0, #0xfffffc01 // #-1023 1c: 4b010000 sub w0, w0, w1 20: d65f03c0 ret 24: 711ffc1f cmp w0, #0x7ff 28: 510ffc00 sub w0, w0, #0x3ff 2c: 12b00001 mov w1, #0x7fffffff // #2147483647 30: 1a811000 csel w0, w0, w1, ne // ne = any 34: d65f03c0 ret 38: 320107e0 mov w0, #0x80000001 // #-2147483647 3c: d65f03c0 ret Other architecture with support for stdc_leading_zeros and/or __builtin_clzll should have similar improvements. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/dbl-64/e_ilogb.c | 80 ++++++++++++-------------------- 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/sysdeps/ieee754/dbl-64/e_ilogb.c b/sysdeps/ieee754/dbl-64/e_ilogb.c index 1e338a59c1..89e7498266 100644 --- a/sysdeps/ieee754/dbl-64/e_ilogb.c +++ b/sysdeps/ieee754/dbl-64/e_ilogb.c @@ -1,63 +1,41 @@ -/* @(#)s_ilogb.c 5.1 93/09/24 */ -/* - * ==================================================== - * 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. - * ==================================================== - */ +/* Get integer exponent of a floating-point value. + Copyright (C) 1999-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. -#if defined(LIBM_SCCS) && !defined(lint) -static char rcsid[] = "$NetBSD: s_ilogb.c,v 1.9 1995/05/10 20:47:28 jtc Exp $"; -#endif + 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. -/* ilogb(double x) - * return the binary exponent of non-zero x - * ilogb(0) = FP_ILOGB0 - * ilogb(NaN) = FP_ILOGBNAN (no signal is raised) - * ilogb(+-Inf) = INT_MAX (no signal is raised) - */ + 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 +#include +#include "math_config.h" int __ieee754_ilogb (double x) { - int32_t hx, lx, ix; - - GET_HIGH_WORD (hx, x); - hx &= 0x7fffffff; - if (hx < 0x00100000) + uint64_t ux = asuint64 (x); + int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; + if (ex == 0) /* zero or subnormal */ { - GET_LOW_WORD (lx, x); - if ((hx | lx) == 0) - return FP_ILOGB0; /* ilogb(0) = FP_ILOGB0 */ - else /* subnormal x */ - if (hx == 0) - { - for (ix = -1043; lx > 0; lx <<= 1) - ix -= 1; - } - else - { - for (ix = -1022, hx <<= 11; hx > 0; hx <<= 1) - ix -= 1; - } - return ix; + /* Clear sign and exponent */ + ux <<= 12; + if (ux == 0) + return FP_ILOGB0; + /* subnormal */ + return -1023 - stdc_leading_zeros (ux); } - else if (hx < 0x7ff00000) - return (hx >> 20) - 1023; - else if (FP_ILOGBNAN != INT_MAX) - { - /* ISO C99 requires ilogb(+-Inf) == INT_MAX. */ - GET_LOW_WORD (lx, x); - if (((hx ^ 0x7ff00000) | lx) == 0) - return INT_MAX; - } - return FP_ILOGBNAN; + if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ + return ux << 12 ? FP_ILOGBNAN : INT_MAX; + return ex - 1023; } From patchwork Mon Apr 28 17:03:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 885484 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp5290778wrs; Mon, 28 Apr 2025 10:07:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUvn9dpa/z8xqFGNDio8iT7ZMlgm73A04lbYvV9UAgVUoUqV5Yl0AE1hxebIg6FKhd+R5iP+g==@linaro.org X-Google-Smtp-Source: AGHT+IE7Q9opttJdi55M26B1tbIUE1Avda5MujuHfJP1zsbGZyrqIQVBNUTPbmtWdgd/Gwvx3a1P X-Received: by 2002:a05:6214:40b:b0:6e2:43d1:5fd0 with SMTP id 6a1803df08f44-6f4f06181fcmr9375666d6.31.1745860059319; Mon, 28 Apr 2025 10:07:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745860059; cv=pass; d=google.com; s=arc-20240605; b=i66HnWbNHOaum2UYGdT1O7s8xvlEQK+CfCDiGYyS9KZVFMZUdiDAfmzwRfEkPzjgif 7x+9Ktgk97HK5knBD/FwNnA67R5039HJSffz4dGvcTWY88gafLLRbuBWAuC+NQs76NT+ S1GpTZjkT1N9G5xVe9OnZ7bzsLnw/zQYeYxWmZaYe7mlgeJa8skl9vck3dBjEmy3RzzP KUS7cJ1XK1UCAgTvvn54C4f7DjTL+tyseoCrg+ppxm3wiMYU2xkXFFJdg1EWFDDoIECV XC06P+bhPhlH5LNyAK6MO4PFui15hj8KM9j0WDqZBB6JONz88fF9svDvYIHa8ZgAVxav H3jw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; fh=GVJhlWdd4NNaBhOZ/uQFycFAyzDMQer7CEL5IpLL3Iw=; b=ZI8f4oFltxZijAh+B1/sog0fz1bSQhtEO4IXWpmyT7Q6v33g8i5qn4TlgIyDQa+jaa OK8swMHzDP7ARLpCvkZ1erCadfDvctKSIFaNtDNZ1Rc5x4sCB/0KGFDA8GrEDijEgLAD CdTtzccQZuJHL0G8s/RdFgbZBaVlI+u5L7L/6fdEDcdlOhfCyBak8nbVN2y27SOEyfBn XRWDf/c9lyIAtO0L0wplMcCxdNC3maVcJzIFisFqqG8n0WGEnHkAMtVvLBDZGmwCeFwm bm2NLPRoCiRxeNuBzYRHxlMgcDLiDVKRWngc+6N7wwGDnHEjj0QJZlsyp2eenJjKXOiL Hjjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tXO1q2b8; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 6a1803df08f44-6f4c0b213b2si101598326d6.495.2025.04.28.10.07.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:07:39 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tXO1q2b8; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CDDD73858CD9 for ; Mon, 28 Apr 2025 17:07:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDDD73858CD9 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=tXO1q2b8 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 937E93858C62 for ; Mon, 28 Apr 2025 17:04:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 937E93858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 937E93858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1033 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859882; cv=none; b=qBnByDnmNYkNPzl9mRbWIZNTuLWOwMwV/cAeYwwo2lPrvl/a34Jimc+XgMq5fe+wfzhxX1Ch79fwOfIj+4qf2cOYZTJTetx5o302CcsoSBtiJ29CbOOl3UidSZhpJxa+7iC3FnoqH1nmMFoBbAFByUStsPVeckDxm7iteQ9IHIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859882; c=relaxed/simple; bh=YTRoPPKATmf6KIIv2HhbsKRpeuPR3sk2QWKNKBfyQjU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cjRnDjfwQc2n2Xkt9F/NzHDeu+i00qTjYlJJlcgOJP+eS895SAFtTUceofKV5F2ae0Cggj0fqWy3N/gxZ0JAD09uW0nQpGSSOW4hg4Q9+70UGw/FiulcBUr96ElHlJuKv/Xjvh3B7TB2TLpiOQCw3uNyVTeBSqpsW+GLD7w9ypU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 937E93858C62 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-301918a4e3bso5711069a91.3 for ; Mon, 28 Apr 2025 10:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745859881; x=1746464681; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; b=tXO1q2b8xpXD+1nlP/xbWy+32zc3+S43NHTfbIu0uxldk/OjIRHNfCNjW1JJo6aYc+ /84xDwv4hp1mRjJN1xLPe4ozCYHIcO3o1NyDwku1AWedpvGdioXpa6cRhf7i8X4PD3NG /cDjZSmJ8l17Jr7if2KvaJorESMZtbYeYz2fJrjZk7/ZDyQ1xxiTN4P04XxqZtynXInz GhBDLpBnSoiaOk3Jari3E180aSKL4qzr0aIknfI4uXfLFHfpUz2oSQpKQeRdxJQ3YI+O QVX3i6StDK6ujeBn9h14S3i7s6mDVUFLDxRUNaeeW1Qxog4NgOHrq2KaFZvmvOM66Syp 0caQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745859881; x=1746464681; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; b=qPS8xkKwnQAMqdY2J+Dg6MCMcXNof20f/Y9Jb9Z0QEoonbQ35Bh5Yo192zLE0HM3Wq iuwjERwrpgrBqhef3Cp1gf9p1J30deW+ySygjEMOz7BXa5LCMFD8Tix+gvZvAd3EQ9io j7I1Vn36W1GuyU0K1fdLki5NVqlOW0YyJUf1nl6GwTzXLyxoTg8Z96uLu04iCAfUg2L4 3VZDPDZX+npZnZheH7WHUNoCPckfurNRKagHa+p2fO485FS/uTtgl2+EVp4OYsorgcBs nM/gTDG8nxJlh8q0g72+zTqTQ0aGxEE9Oa2FEMcHx/yvRGIxgp19qzLv1/PTl8vUccXj 95Aw== X-Gm-Message-State: AOJu0YyG4mvWtGOhk8b+F70wT2tqsuR0av0sI+3DX3Li2MmapifPCokB Ph81e18D0G5hx9b9VoLjZz2tsK3KkhC91pzW58LVHJXlKUslGNSYptily6cUFCdgYwm27HM+0Xv S X-Gm-Gg: ASbGncu1De4pkzpkMaWkWx06Z5oKyH7YUjwjFS1D7LsfWWyP3Zii7pJoevwC+kr686X 1IUTXDs92nljWrTxaKGbMEKmpmgoSaiRVKemzQJvlnTazTKn6otaXwduZpqAtV6pgJVanTF6fvn afciLQFimdh51ZMJQxAprh60qeimRhvXT96kSsso83uiJwx2dB5xHAhwDYDjBRHDKu6lmbS0jTB HUiFuejU7yCMXoy9TBxXsQMQSx06z+V7In3hrQ4vDmIfm0PY+8Nuw8qeHN+1vxj/UnIaW8bduyF 48APouHSqRWVIZAMOp2y07zVSG4LUG4l9pkV26elzgkfW8jQnljhDw== X-Received: by 2002:a17:90b:1dcb:b0:2f6:f32e:90ac with SMTP id 98e67ed59e1d1-30a21552b2dmr889935a91.11.1745859880719; Mon, 28 Apr 2025 10:04:40 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:ce18:36e8:dea9:8b39]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f773725csm8322332a91.3.2025.04.28.10.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:04:40 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: [PATCH v2 2/4] math: Optimize double ilogb/llogb Date: Mon, 28 Apr 2025 14:03:42 -0300 Message-ID: <20250428170430.2030400-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> References: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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~patch=linaro.org@sourceware.org It removes the wrapper by moving the error/EDOM handling to an out-of-line implementation (__math_invalid_i/__math_invalid_li). Also, __glibc_unlikely is used on errors case since it helps code generation on recent gcc. The code now builds to with gcc-14 on aarch64: 0000000000000000 <__ilogb>: 0: 9e660000 fmov x0, d0 4: d374f801 ubfx x1, x0, #52, #11 8: 340000e1 cbz w1, 24 <__ilogb+0x24> c: 510ffc20 sub w0, w1, #0x3ff 10: 711ffc3f cmp w1, #0x7ff 14: 54000040 b.eq 1c <__ilogb+0x1c> // b.none 18: d65f03c0 ret 1c: 12b00000 mov w0, #0x7fffffff // #2147483647 20: 14000000 b 0 <__math_invalid_i> 24: d374cc00 lsl x0, x0, #12 28: b40000a0 cbz x0, 3c <__ilogb+0x3c> 2c: dac01000 clz x0, x0 30: 12807fc1 mov w1, #0xfffffc01 // #-1023 34: 4b000020 sub w0, w1, w0 38: d65f03c0 ret 3c: 320107e0 mov w0, #0x80000001 // #-2147483647 40: 14000000 b 0 <__math_invalid_i> Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/i386/fpu/math_err.c | 1 - sysdeps/ieee754/dbl-64/e_ilogb.c | 42 +------------------ sysdeps/ieee754/dbl-64/math_config.h | 2 + sysdeps/ieee754/dbl-64/math_err.c | 32 ++++++++++++++ sysdeps/ieee754/dbl-64/w_ilogb.c | 63 ++++++++++++++++++++++++++++ sysdeps/ieee754/dbl-64/w_llogb.c | 2 + 6 files changed, 100 insertions(+), 42 deletions(-) delete mode 100644 sysdeps/i386/fpu/math_err.c create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c create mode 100644 sysdeps/ieee754/dbl-64/w_llogb.c diff --git a/sysdeps/i386/fpu/math_err.c b/sysdeps/i386/fpu/math_err.c deleted file mode 100644 index 1cc8931700..0000000000 --- a/sysdeps/i386/fpu/math_err.c +++ /dev/null @@ -1 +0,0 @@ -/* Not needed. */ diff --git a/sysdeps/ieee754/dbl-64/e_ilogb.c b/sysdeps/ieee754/dbl-64/e_ilogb.c index 89e7498266..1ea2f23e2f 100644 --- a/sysdeps/ieee754/dbl-64/e_ilogb.c +++ b/sysdeps/ieee754/dbl-64/e_ilogb.c @@ -1,41 +1 @@ -/* Get integer exponent of a floating-point value. - Copyright (C) 1999-2025 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 -#include "math_config.h" - -int -__ieee754_ilogb (double x) -{ - uint64_t ux = asuint64 (x); - int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; - if (ex == 0) /* zero or subnormal */ - { - /* Clear sign and exponent */ - ux <<= 12; - if (ux == 0) - return FP_ILOGB0; - /* subnormal */ - return -1023 - stdc_leading_zeros (ux); - } - if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ - return ux << 12 ? FP_ILOGBNAN : INT_MAX; - return ex - 1023; -} +/* ilogb is implemented at w_ilogb.c */ diff --git a/sysdeps/ieee754/dbl-64/math_config.h b/sysdeps/ieee754/dbl-64/math_config.h index 3382e385f9..5766f6a1db 100644 --- a/sysdeps/ieee754/dbl-64/math_config.h +++ b/sysdeps/ieee754/dbl-64/math_config.h @@ -164,6 +164,8 @@ attribute_hidden double __math_divzero (uint32_t); /* Invalid input unless it is a quiet NaN. */ attribute_hidden double __math_invalid (double); +attribute_hidden int __math_invalid_i (int); +attribute_hidden long int __math_invalid_li (long int); /* Error handling using output checking, only for errno setting. */ diff --git a/sysdeps/ieee754/dbl-64/math_err.c b/sysdeps/ieee754/dbl-64/math_err.c index 4a07fd5714..b8c645a3df 100644 --- a/sysdeps/ieee754/dbl-64/math_err.c +++ b/sysdeps/ieee754/dbl-64/math_err.c @@ -29,8 +29,24 @@ with_errno (double y, int e) errno = e; return y; } + +NOINLINE static int +with_errno_i (int y, int e) +{ + errno = e; + return y; +} + +NOINLINE static long int +with_errno_li (long int y, int e) +{ + errno = e; + return y; +} #else #define with_errno(x, e) (x) +#define with_errno_i(x, e) (x) +#define with_errno_li(x, e) (x) #endif attribute_hidden double @@ -83,6 +99,22 @@ __math_invalid (double x) return isnan (x) ? y : with_errno (y, EDOM); } +attribute_hidden int +__math_invalid_i (int r) +{ + double y = 0.0 / 0.0; + math_force_eval (y); + return with_errno_i (r, EDOM); +} + +attribute_hidden long int +__math_invalid_li (long int r) +{ + double y = 0.0 / 0.0; + math_force_eval (y); + return with_errno_li (r, EDOM); +} + /* Check result and set errno if necessary. */ attribute_hidden double diff --git a/sysdeps/ieee754/dbl-64/w_ilogb.c b/sysdeps/ieee754/dbl-64/w_ilogb.c new file mode 100644 index 0000000000..8f9641be45 --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_ilogb.c @@ -0,0 +1,63 @@ +/* Get integer exponent of a floating-point value. + Copyright (C) 1999-2025 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 +#include +#include +#include +#include "math_config.h" + +#ifdef DEF_AS_LLOGB +# define IMPL_NAME __llogb +# define FUNC_NAME llogb +# define RET_TYPE long int +# define RET_LOGB0 FP_LLOGB0 +# define RET_LOGBNAN FP_LLOGBNAN +# define RET_LOGMAX LONG_MAX +# define RET_INVALID __math_invalid_li +#else +# define IMPL_NAME __ilogb +# define FUNC_NAME ilogb +# define RET_TYPE int +# define RET_LOGB0 FP_ILOGB0 +# define RET_LOGBNAN FP_ILOGBNAN +# define RET_LOGMAX INT_MAX +# define RET_INVALID __math_invalid_i +#endif + +RET_TYPE +IMPL_NAME (double x) +{ + uint64_t ux = asuint64 (x); + int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; + if (__glibc_unlikely (ex == 0)) /* zero or subnormal */ + { + /* Clear sign and exponent */ + ux <<= 12; + if (ux == 0) + return RET_INVALID (RET_LOGB0); + /* subnormal */ + return (RET_TYPE)-1023 - stdc_leading_zeros (ux); + } + if (__glibc_unlikely (ex == EXPONENT_MASK >> MANTISSA_WIDTH)) /* NaN or Inf */ + return RET_INVALID (ux << 12 ? RET_LOGBNAN : RET_LOGMAX); + return ex - 1023; +} +libm_alias_double (IMPL_NAME, FUNC_NAME) diff --git a/sysdeps/ieee754/dbl-64/w_llogb.c b/sysdeps/ieee754/dbl-64/w_llogb.c new file mode 100644 index 0000000000..c984cd151a --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_llogb.c @@ -0,0 +1,2 @@ +#define DEF_AS_LLOGB +#include "w_ilogb.c" From patchwork Mon Apr 28 17:03:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 885482 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp5289558wrs; Mon, 28 Apr 2025 10:05:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXkL+TASeaFOf8KlW+PGhQXCln561r7ZIw+xEwxbDs9+UZZv/bc0CdJLaJMsuPvrUieEf33Dg==@linaro.org X-Google-Smtp-Source: AGHT+IH4fqdONKPIbPfSvoMj8ttTQGNikK/yCNgxleQyXhBABAQyb0mruEV6Ximi+5KbMYlICjCU X-Received: by 2002:a05:600c:524c:b0:439:9434:4f3b with SMTP id 5b1f17b1804b1-440a65e91d1mr96956535e9.8.1745859929434; Mon, 28 Apr 2025 10:05:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745859929; cv=pass; d=google.com; s=arc-20240605; b=TogV0fjpPK4MoO3X5VEtcogmCAIZmN6LB6qeWaL3tM7SazOoSToXbYMDPC6DtiJvlz n/l5eYMoqQ5Kk8GWWqthYevdPnLmlmNZK9RS24pwNKpJWEQop0/LEOTLhiHjHYd34PRq ES28VeC7+WsqWykl9eYCBWcZEuSjiLKm5ATxJeMke5ERBgGjkpqMJMIN9ldTMyYGN1R7 N/mKcZBq03mFew6lJg9cZbz5bQfYTWmxlLIkPeATgpfDKIv6h2ELKbuGtX0WaZZ9gSMo OJc1CoqdePgNAydMfhFyibqc+7ak15m3bGy4jP7IPWErX0zRv1rnRc+oiLU11uSfWJAZ w9RA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; fh=GVJhlWdd4NNaBhOZ/uQFycFAyzDMQer7CEL5IpLL3Iw=; b=XCBGerGRyRBOrad6/SlERYfa74Fxy0KpizW+qJ5FDMfE1W8bjZjYQIqD78G2iKlyOH L+XByZZiMXyX0Xfi1d9XGcJYLEvg5FepRRIYeoIN0YxTvK8hyBDF6sVOWfTw0mF7+mce CoXIUXSHdIV5mvKuEZ0fPv5jAXcAp6ooZkfwXRm8ovNyZnh7iQcO71aCInpEaIO0xJvZ xLjIpX99n2R5r/hK9HXMSXSAtcWSGcfGNMjnRrGnf6j5VPVh3ozb+2YkUKvPeP51fH99 eqgQs6qjSGZ/B5QCtYoktimTd48Kcwd8nlBuSDsFsMpCN4jBAtJL5FggKSb6fu2yvFPW lV/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="n3idi/hv"; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ffacd0b85a97d-3a073ca52cfsi5545793f8f.205.2025.04.28.10.05.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:05:29 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="n3idi/hv"; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 97E133858C2D for ; Mon, 28 Apr 2025 17:05:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97E133858C2D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=n3idi/hv X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id CC888385802C for ; Mon, 28 Apr 2025 17:04:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CC888385802C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CC888385802C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859884; cv=none; b=i5lnZ+o976AXUr3eodgeHvkiSCjXum4PQgwBDRrRZZNueXEwmLolbvIKyGJV8VKRBlgyKXBMDsLGv+eoVOzl+14WKhAAnEHtvtZc0bXuVmewVY9l0Dgy89fn7XlULtIemF6O4xUva3zo/Xgv9gv6Bz39m9FpDCiONCxR5oWzleg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859884; c=relaxed/simple; bh=9PwBxR+/+391tENgbEoxHPTSKGxvxLfJLCcNQbEI9XY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=x7HWRvmgGnJRX+MK4BVp6HulQeipRQ9ismgNsyh78oORm74wFQ7URa8sb5/N6Xrg1111LzrMOQwNLk3syaNXxUvJKR5L/B7Y5QC+PncT5agBcmkCtkBZx8OiJ9+O0WKyJhmAm/Evn/ML4TknhLYZjqxu9gEtSmlnLIbVawZLe4A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC888385802C Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-301a4d5156aso7061818a91.1 for ; Mon, 28 Apr 2025 10:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745859882; x=1746464682; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=n3idi/hvDQedMlQtXtgErw7yzwWNn20CDwvSnG+kJmWiQaYMzvWtwmoFZ5h99sv5Id C8Ff9fwOqyq2Jx8R2ov4bbfwjHXSbuwsGWXqazyNmCoGJncefR2rCUSIvgM4BdUthSLt hZ1arRh98Tfz5AnV3Ai/CIOOD0bPD/U43VZneQjOhbf0DBWYcZLvKIYS43gJBrVAe67d G085Nx2HRsm9TINOBKT2JEH2RMjVU0W4PLPbVYSIkBWOU6bBR0zoSLI5ZfafEGrE3j/L uMSsSNhhj6dIyxP1MQFoSzFza80GvbRojU0Vj6LjcvdUSJtxx9IAdjMnXQ9qYTttqdFi jtnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745859882; x=1746464682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=tC0XCVRgKM/x1L4//+XGNIWny3vYqwUUa9lnlTxISJOVIjyXSeetx5pYBIFtLdfw5a URzlii5m6hzX/tbeI7nToz44ndnNfnFeyFb41kmORWk98dXeO++8P8E2Eo4AB9fDeEiQ g+EzU1uwVhQqXUp5unyper4QCxc+M3Wwj5PoUvGFEoq2oaGI4TqcUvP9vGpnUfAliOjE hBpMbaDNC0aqqt6UOwzGzPScqTruxWFFDx83ZpbvCLzb1F1x1+31acE7jpC5Hdq3nVCS swQQKVEEoR1rUQvuS56mYefD+VPvrw8K+Aumea1vHm7xVExMfubOoQrchK0l1snm6fzw 4UTw== X-Gm-Message-State: AOJu0YwvcYUKsZOJmUEcn3tA/ZBkfqOxHzNpgIdXHD2YJAnmdMDt6GGM 1sGgzmUpIAMB3HBl5X+BQQmKhGW3RaOekwlxNg7t86yVRCpwLmG2tuGY+AcxwZdLgnRAmtJYVmO M X-Gm-Gg: ASbGncsVveF/prRMP+LV3iVZKM/6VyfvPGRYJhXUbJ78nm0I6bWzigWN6Zi/+B+I310 3ISsVF+O3Iho3AVsE9fWOrSvwS2gUy5I8LD1frqbNXziE1x39ynfAc0EALTebmVsYiYuJIUKI+V 3FxtfruyPK5XgqvL7ORQxSvk/s7MJOYYhkyOYqjvy1sjkdY/EhkXO5Et4pNef+Iysizy+qCy63p bL+DuxSYakGB8nsVktd3LrHkcIroFrnMukIviDfiASCiXCrhr3FANlA428Hd8XVlP3VDLMiWRxA 8lAvMXf8mUpBQewNRqS49pc45jpYWlbWaGlvHvaPV3HkGNVtolWabA== X-Received: by 2002:a17:90b:2749:b0:2ee:5958:828 with SMTP id 98e67ed59e1d1-309f7de0143mr19908493a91.9.1745859882291; Mon, 28 Apr 2025 10:04:42 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:ce18:36e8:dea9:8b39]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f773725csm8322332a91.3.2025.04.28.10.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:04:41 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: [PATCH v2 3/4] math: Remove UB and optimize double ilogbf Date: Mon, 28 Apr 2025 14:03:43 -0300 Message-ID: <20250428170430.2030400-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> References: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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~patch=linaro.org@sourceware.org The subnormal exponent calculation invokes UB by left shifting the signed expoenent to find the first leading bit. The patch reimplements ilogb using the math_config.h macros and uses the new stdbit function to simplify the subnormal handling. On aarch64 it generates better code: * master: 0000000000000000 <__ieee754_ilogbf>: 0: 1e260000 fmov w0, s0 4: 12007801 and w1, w0, #0x7fffffff 8: 72091c1f tst w0, #0x7f800000 c: 54000141 b.ne 34 <__ieee754_ilogbf+0x34> // b.any 10: 34000201 cbz w1, 50 <__ieee754_ilogbf+0x50> 14: 53185c21 lsl w1, w1, #8 18: 12800fa0 mov w0, #0xffffff82 // #-126 1c: d503201f nop 20: 531f7821 lsl w1, w1, #1 24: 51000400 sub w0, w0, #0x1 28: 7100003f cmp w1, #0x0 2c: 54ffffac b.gt 20 <__ieee754_ilogbf+0x20> 30: d65f03c0 ret 34: 13177c20 asr w0, w1, #23 38: 12b01002 mov w2, #0x7f7fffff // #2139095039 3c: 5101fc00 sub w0, w0, #0x7f 40: 6b02003f cmp w1, w2 44: 12b00001 mov w1, #0x7fffffff // #2147483647 48: 1a819000 csel w0, w0, w1, ls // ls = plast 4c: d65f03c0 ret 50: 320107e0 mov w0, #0x80000001 // #-2147483647 54: d65f03c0 ret * patch: 0000000000000000 <__ieee754_ilogbf>: 0: 1e260001 fmov w1, s0 4: d3577820 ubfx x0, x1, #23, #8 8: 350000e0 cbnz w0, 24 <__ieee754_ilogbf+0x24> c: 53175821 lsl w1, w1, #9 10: 34000141 cbz w1, 38 <__ieee754_ilogbf+0x38> 14: 5ac01021 clz w1, w1 18: 12800fc0 mov w0, #0xffffff81 // #-127 1c: 4b010000 sub w0, w0, w1 20: d65f03c0 ret 24: 7103fc1f cmp w0, #0xff 28: 5101fc00 sub w0, w0, #0x7f 2c: 12b00001 mov w1, #0x7fffffff // #2147483647 30: 1a811000 csel w0, w0, w1, ne // ne = any 34: d65f03c0 ret 38: 320107e0 mov w0, #0x80000001 // #-2147483647 3c: d65f03c0 ret Other architecture with support for stdc_leading_zeros and/or __builtin_clzll should have similar improvements. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/e_ilogbf.c | 68 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/sysdeps/ieee754/flt-32/e_ilogbf.c b/sysdeps/ieee754/flt-32/e_ilogbf.c index db24012eb4..024b114638 100644 --- a/sysdeps/ieee754/flt-32/e_ilogbf.c +++ b/sysdeps/ieee754/flt-32/e_ilogbf.c @@ -1,43 +1,41 @@ -/* s_ilogbf.c -- float version of s_ilogb.c. - */ +/* Get integer exponent of a floating-point value. + Copyright (C) 1999-2025 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. -#if defined(LIBM_SCCS) && !defined(lint) -static char rcsid[] = "$NetBSD: s_ilogbf.c,v 1.4 1995/05/10 20:47:31 jtc Exp $"; -#endif + 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 +#include +#include "math_config.h" -int __ieee754_ilogbf(float x) +int +__ieee754_ilogbf (float x) { - int32_t hx,ix; - - GET_FLOAT_WORD(hx,x); - hx &= 0x7fffffff; - if(hx<0x00800000) { - if(hx==0) - return FP_ILOGB0; /* ilogb(0) = FP_ILOGB0 */ - else /* subnormal x */ - for (ix = -126,hx<<=8; hx>0; hx<<=1) ix -=1; - return ix; - } - else if (hx<0x7f800000) return (hx>>23)-127; - else if (FP_ILOGBNAN != INT_MAX) { - /* ISO C99 requires ilogbf(+-Inf) == INT_MAX. */ - if (hx==0x7f800000) - return INT_MAX; - } - return FP_ILOGBNAN; + uint32_t ux = asuint (x); + int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; + if (ex == 0) /* zero or subnormal */ + { + /* Clear sign and exponent. */ + ux <<= 1 + EXPONENT_WIDTH; + if (ux == 0) + return FP_ILOGB0; + /* sbunormal */ + return -127 - stdc_leading_zeros (ux); + } + if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ + return ux << (1 + EXPONENT_WIDTH) ? FP_ILOGBNAN : INT_MAX; + return ex - 127; } From patchwork Mon Apr 28 17:03:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 885483 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp5289796wrs; Mon, 28 Apr 2025 10:05:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUWIydgYYKPkvODC21m4AEilDu9W3GMy8xeQZqugXL/8L+DDzUToS2K0C44TLRUNPXu8rTUBA==@linaro.org X-Google-Smtp-Source: AGHT+IHFN/uwN9L4kBYmVE7fnrCxoAbzAgJ83TQtjydaQNwZiJ6K700kmatiJN7hEgkc3ytyUNAt X-Received: by 2002:a05:6000:144d:b0:39c:1258:7e18 with SMTP id ffacd0b85a97d-3a08a3ca147mr40272f8f.57.1745859951768; Mon, 28 Apr 2025 10:05:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745859951; cv=pass; d=google.com; s=arc-20240605; b=Yy+1+YdgKKa14/RrepE5+7hBSbODj8cbQJM4SoXshUoJqD63PYwloB3/ign8fBgq+K 0sM1hy+rXzvadr3eXatoe/PSHt6EfjrrP7Igz6GpXrd3NAnOBzYofAVX75Bys86+0uAv KxfTEumB2Dcil1ntZ4RPIeTqlrNs0VhAeweDaqTeZC4Mf6DsjMFvVqBNTgLm2I6hcBrX NFwgkIgo4ZXdxRATAtT80Ryd9dJDlCAeVkn5XZykfkUQEANszPvxVF9ALKODTMb7V2ne LtYWVcCL8OKe1vnkxjmFs43Y1AnMWvpRy8VVhY8Xjx+yZdPUX7uDENdbytVJKhsO1OPF Gu2A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=ry5mqY52kfDAts9M6u0g0ijReRKvyYAmiYrLWGguSJ8=; fh=GVJhlWdd4NNaBhOZ/uQFycFAyzDMQer7CEL5IpLL3Iw=; b=llxTWpdi1AtXidwQ3U+Kp2wmqxALVbgjclJJiPocxNHQlN++KD44zW7NKaM9DWQ3xD OoziHaZGKNyD17OZR0im8hg+9iQPbUT40UFQU6d8vrNz0MdKb45hPZxLtjJNpnATlsaQ Xqrsoswr9SW+F1M1HanCEw9X3IpzjW9MlDWo/ybx3j4wRmIY/xh/RwAA9NOvd1l7Fr2U xcgo2NLmsrKifgiwrc7iLqrQDCBZJh/wxlzaA9putsAK9Z94pGknzIX5/e674CIMxYdp aXipEldv1uFc+uPsblMMn69oOk+hcivGsl8ct1BzBYl42IC6szhzId88KAyDwPPxjkCR nrxQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dcstaA7S; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 5b1f17b1804b1-440a530436csi60513825e9.66.2025.04.28.10.05.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:05:51 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dcstaA7S; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 84B963858C60 for ; Mon, 28 Apr 2025 17:05:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84B963858C60 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=dcstaA7S X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id B26553858D38 for ; Mon, 28 Apr 2025 17:04:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B26553858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B26553858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1033 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859885; cv=none; b=riG6aWMmocYhrExb2XZQfpZw9rxd3uI7NKq3qHdRWyZMT2ecZm+NxVMK6yo/SnHE17uaAgLCZ6LzhNnG553pi1vwa/knGtamrkNs/mAfUPoB1ONsB5TaHmsAJXKNVWkMJpuofnKuKxlJzRETReAJLM9GZiuzvRFy9UmB9Cikprw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859885; c=relaxed/simple; bh=smJwasGCuS7BvdU3Si9kAG+U9e/Bo2GNkth7/tl7lpQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZGajvHJ7l4Ai5gc73bQ4FtNege23Web9uwbHEaxnlLEGDYEfLebl0FPiOJJaBrlzhx0r0STOoqdyQHCBqFiItvDjQWMON9yfaLZ6+4gjuThru5Lq8FO4D81kEJ1uWLYK0tnctX0glEo3/QcNkz6LRjiCwnm7YKWpdLk6EzVen7k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B26553858D38 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-3031354f134so4229615a91.3 for ; Mon, 28 Apr 2025 10:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745859884; x=1746464684; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ry5mqY52kfDAts9M6u0g0ijReRKvyYAmiYrLWGguSJ8=; b=dcstaA7SSX7JY9XBt1OERx0Z94t5/6zqc/cypC703t4W5PmIm136rz1L19Sr7sfGYc XZIKUwmUaSq44ICl6L8UbBUmT6NqzkGW/V2qDq5gHpWQRVH59QlFNDwfZTuhzDJtk1Zk Jiim++wmimNUTnAvOsRTX/U4GXvGXDtbp2VDatUsHwGVZaXBxSmumNBMTO3rHwDAGo/G leDQzcpunLDHjV/+A0b8ENA4nw/0gCDlGA+Yio6AW2zF6hqSkR0azzj1oEfY9ePrM4AZ CginINYDNyIOwtzkKbth1CQG+E+xotm1jqTRyrBemHYgcY0bCg5WRb0Rca+JVeHq+Bbz X2/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745859884; x=1746464684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ry5mqY52kfDAts9M6u0g0ijReRKvyYAmiYrLWGguSJ8=; b=c0o3YrkXy5Ko+UazqQEDIUbUosx6APwo8yMHkZYta2Ic4PxvGCUDzDk+u20aFBXXnQ oVKBcMCvuSHvxpvzj9U3mgm7iwXCEwXVMxN9yTDDK9/RyRx80EjIWuFBXfX0H0jNjdVm 7pS3o3rw7fULXgCXFSmpoLo7zX5Uy9vo8+PNA861X65xoxaMIorYBUC1nSayQDlWnEb2 ouYmnbtiIKM+9vIN3zdOQkER5Adlt8d4Lc5jJtc76baclkwgdckHbjJQUffd4rgjZg1E 1lin9reY1yVR/7LVhcJoI/yxuZgydvSUylZh9uZv2Lw0xYbxO9Vp7cwALeLrpZ2nTKy5 QG1A== X-Gm-Message-State: AOJu0YxhDM2wPjYeKvVIptowuyFjJ+bI13weViflCG/lIhBkt23g6Weg 1u372mu5OzzUvJG5LyZTA8VLEaJv1UkzThFFPQdrk1ZiQPewOvy9Vjy+0P5524abE/hOYz/VaQY o X-Gm-Gg: ASbGncsVjPB2SRLnJS3q4JC4ZIHj+NjvhElS2ZovcYvdcs4GVr7fyCrtBGheTVmSzLk JpUPc08lScq2qPbr8tiSkCtCoBNdOt06Hd61orfCrldIgQ5RsIAUoNcS309HIHzvJH0xOuuw2Ul siGOPGk4KbGMjmhnmkGfWhJ8+LHzJu9EUqiFu91C23ANrVP+W7I75dslWPRnYsTUGUU7OAf4+B/ 9dzuiXUQvtUCCcPrmQWaw9YWFoyhiEBJ/SFFTa6xrj6uinJlxE6N+07UPfqjOFlsR2XwOzVntCT n5vfFYwCicYJGF0f+jmL1Un4FfTCZ3dgiz+GrDu7th7emrAE3q2Zuw== X-Received: by 2002:a17:90b:4c04:b0:2ff:5ed8:83d0 with SMTP id 98e67ed59e1d1-30a2155af72mr916247a91.16.1745859883984; Mon, 28 Apr 2025 10:04:43 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:ce18:36e8:dea9:8b39]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f773725csm8322332a91.3.2025.04.28.10.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:04:43 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: [PATCH v2 4/4] math: Optimize float ilogb/llogb Date: Mon, 28 Apr 2025 14:03:44 -0300 Message-ID: <20250428170430.2030400-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> References: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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~patch=linaro.org@sourceware.org It removes the wrapper by moving the error/EDOM handling to an out-of-line implementation (__math_invalidf_i/__math_invalidf_li). Also, __glibc_unlikely is used on errors case since it helps code generation on recent gcc. The code now builds to with gcc-14 on aarch64: 0000000000000000 <__ilogbf>: 0: 1e260000 fmov w0, s0 4: d3577801 ubfx x1, x0, #23, #8 8: 340000e1 cbz w1, 24 <__ilogbf+0x24> c: 5101fc20 sub w0, w1, #0x7f 10: 7103fc3f cmp w1, #0xff 14: 54000040 b.eq 1c <__ilogbf+0x1c> // b.none 18: d65f03c0 ret 1c: 12b00000 mov w0, #0x7fffffff // #2147483647 20: 14000000 b 0 <__math_invalidf_i> 24: 53175800 lsl w0, w0, #9 28: 340000a0 cbz w0, 3c <__ilogbf+0x3c> 2c: 5ac01000 clz w0, w0 30: 12800fc1 mov w1, #0xffffff81 // #-127 34: 4b000020 sub w0, w1, w0 38: d65f03c0 ret 3c: 320107e0 mov w0, #0x80000001 // #-2147483647 40: 14000000 b 0 <__math_invalidf_i> Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/e_ilogbf.c | 42 +----------------- sysdeps/ieee754/flt-32/math_config.h | 2 + sysdeps/ieee754/flt-32/math_errf.c | 33 ++++++++++++++ sysdeps/ieee754/flt-32/w_ilogbf.c | 66 ++++++++++++++++++++++++++++ sysdeps/ieee754/flt-32/w_llogbf.c | 2 + 5 files changed, 104 insertions(+), 41 deletions(-) create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf.c create mode 100644 sysdeps/ieee754/flt-32/w_llogbf.c diff --git a/sysdeps/ieee754/flt-32/e_ilogbf.c b/sysdeps/ieee754/flt-32/e_ilogbf.c index 024b114638..a27fb94732 100644 --- a/sysdeps/ieee754/flt-32/e_ilogbf.c +++ b/sysdeps/ieee754/flt-32/e_ilogbf.c @@ -1,41 +1 @@ -/* Get integer exponent of a floating-point value. - Copyright (C) 1999-2025 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 -#include "math_config.h" - -int -__ieee754_ilogbf (float x) -{ - uint32_t ux = asuint (x); - int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; - if (ex == 0) /* zero or subnormal */ - { - /* Clear sign and exponent. */ - ux <<= 1 + EXPONENT_WIDTH; - if (ux == 0) - return FP_ILOGB0; - /* sbunormal */ - return -127 - stdc_leading_zeros (ux); - } - if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ - return ux << (1 + EXPONENT_WIDTH) ? FP_ILOGBNAN : INT_MAX; - return ex - 127; -} +/* ilogbf is implemented at w_ilogbf.c */ diff --git a/sysdeps/ieee754/flt-32/math_config.h b/sysdeps/ieee754/flt-32/math_config.h index 8d9c8ee3ad..fd9481a016 100644 --- a/sysdeps/ieee754/flt-32/math_config.h +++ b/sysdeps/ieee754/flt-32/math_config.h @@ -208,6 +208,8 @@ attribute_hidden float __math_uflowf (uint32_t); attribute_hidden float __math_may_uflowf (uint32_t); attribute_hidden float __math_divzerof (uint32_t); attribute_hidden float __math_invalidf (float); +attribute_hidden int __math_invalidf_i (int); +attribute_hidden long int __math_invalidf_li (long int); attribute_hidden float __math_edomf (float x); /* Shared between expf, exp2f, exp10f, and powf. */ diff --git a/sysdeps/ieee754/flt-32/math_errf.c b/sysdeps/ieee754/flt-32/math_errf.c index edcc4c094a..244e38a4ce 100644 --- a/sysdeps/ieee754/flt-32/math_errf.c +++ b/sysdeps/ieee754/flt-32/math_errf.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include "math_config.h" #if WANT_ERRNO @@ -27,8 +28,24 @@ with_errnof (float y, int e) errno = e; return y; } + +NOINLINE static int +with_errnof_i (int y, int e) +{ + errno = e; + return y; +} + +NOINLINE static long int +with_errnof_li (long int y, int e) +{ + errno = e; + return y; +} #else # define with_errnof(x, e) (x) +# define with_errnof_i(x, x) (x) +# define with_errnof_li(x, x) (x) #endif attribute_hidden float @@ -80,3 +97,19 @@ __math_invalidf (float x) float y = (x - x) / (x - x); return isnan (x) ? y : with_errnof (y, EDOM); } + +attribute_hidden int +__math_invalidf_i (int x) +{ + float y = 0.0f / 0.0f; + math_force_eval (y); + return with_errnof_i (x, EDOM); +} + +attribute_hidden long int +__math_invalidf_li (long int x) +{ + float y = 0.0f / 0.0f; + math_force_eval (y); + return with_errnof_li (x, EDOM); +} diff --git a/sysdeps/ieee754/flt-32/w_ilogbf.c b/sysdeps/ieee754/flt-32/w_ilogbf.c new file mode 100644 index 0000000000..7f7314797d --- /dev/null +++ b/sysdeps/ieee754/flt-32/w_ilogbf.c @@ -0,0 +1,66 @@ +/* Get integer exponent of a floating-point value. + Copyright (C) 1999-2025 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 +#include +#include +#include +#include +#include "math_config.h" + +#ifdef DEF_AS_LLOGBF +# define IMPL_NAME __llogb +# define FUNC_NAME llogb +# define RET_TYPE long int +# define RET_LOGB0 FP_LLOGB0 +# define RET_LOGBNAN FP_LLOGBNAN +# define RET_LOGMAX LONG_MAX +# define RET_INVALID __math_invalidf_li +#else +# define IMPL_NAME __ilogb +# define FUNC_NAME ilogb +# define RET_TYPE int +# define RET_LOGB0 FP_ILOGB0 +# define RET_LOGBNAN FP_ILOGBNAN +# define RET_LOGMAX INT_MAX +# define RET_INVALID __math_invalidf_i +#endif + +RET_TYPE +M_DECL_FUNC (IMPL_NAME) (float x) +{ + uint32_t ux = asuint (x); + int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; + if (__glibc_unlikely (ex == 0)) + { + /* Zero or subnormal. + Clear sign and exponent. */ + ux <<= 1 + EXPONENT_WIDTH; + if (ux == 0) + return RET_INVALID (RET_LOGB0); + /* sbunormal */ + return (RET_TYPE)-127 - stdc_leading_zeros (ux); + } + if (__glibc_unlikely (ex == EXPONENT_MASK >> MANTISSA_WIDTH)) + /* NaN or Inf */ + return RET_INVALID (ux << (1 + EXPONENT_WIDTH) ? RET_LOGBNAN : RET_LOGMAX); + return ex - 127; +} +libm_alias_float (IMPL_NAME, FUNC_NAME); diff --git a/sysdeps/ieee754/flt-32/w_llogbf.c b/sysdeps/ieee754/flt-32/w_llogbf.c new file mode 100644 index 0000000000..8676434f6e --- /dev/null +++ b/sysdeps/ieee754/flt-32/w_llogbf.c @@ -0,0 +1,2 @@ +#define DEF_AS_LLOGBF +#include "w_ilogbf.c"