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; }