From patchwork Tue Apr 29 16:30:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885759 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp426704wrq; Tue, 29 Apr 2025 09:44:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW3bSYMnd6LsbxdCHVA4K4xontx4McuIQ6XprFujMQG5hFPuziHyE289DOZRHA2eD0au9fk4Q==@linaro.org X-Google-Smtp-Source: AGHT+IFAcBIsZf+X0vnYYiqpOrpMR+RAMYvR1hGmbpkkj78Gw9AcfD+3Z/M5r53vvIt/DLGuWteG X-Received: by 2002:a05:622a:6085:b0:472:133f:93ae with SMTP id d75a77b69052e-48868e8868cmr56990481cf.48.1745945077781; Tue, 29 Apr 2025 09:44:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745945077; cv=pass; d=google.com; s=arc-20240605; b=dVXeOXrxXlbcS/0Wt9z2fMn8hegkjg/kr5L8/rn+HoZI5Z6p1Ek+dPI/RjAbQ+ei6G 5tU6q2GcZLWyzALW44WuwPCiexmV9gVxhVbqI14Es7kAtVXD/+Yp1LJS9cvu6zHk6MfR CHXFLx8hMl8lfjOeV5ZvcKPltCjyTH3RboG8qR2HwNdPwtzDeihmehp0y4rHSMj+Cb1p IDPbHoArF23jRuHQG3h3MUv1TZDaObyLU5I9YjQT/ERW1WnTehRHRODULfTQrN3R33pl pDkdQ+rZX9GV3ie3s7UkFrAnp4MEAg7Rgpftr3SP9ExxcSN5VpPQIa+wmKaZN/9OXxby r6kA== 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=ALI+AuBRNGwWzLFmAOLr8Eq2GZOc04SZ4j00angPlwY=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=iVwFA/OIXDRTl/DAZGZg8y5lX276sDbJLXsU9T9G7tVegSVNd5dDfq9uoVykRRD9xZ /7VY1kLp/2rK0ID4Vn9AFBEy4tay6TXa6AnLifLf27W+gPCuLj/zaYHZUgmZtfuvBrzR qhLyy9OXi98EAmHTsus9VD9VSePf38iDfofVqzy67ouBS4K3eIFw97ttqCZZLd3oiSxX Xv9EdTO4cD+0MiAe8IjurqA/QoRKXUXKBt13TM0ugIA7ySk34eEHIAW9goG/YZ5MNxGN 2/Td+a1RIQf459NL1ppMpshtUqPh3aHnSeoo70bekHYJHLQ3xhnJind0cgPgXFAt0QwU IU5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m7issrNj; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id d75a77b69052e-47ea1f97be9si133771791cf.427.2025.04.29.09.44.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:44:37 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m7issrNj; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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 549D03858D33 for ; Tue, 29 Apr 2025 16:44:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 549D03858D33 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=m7issrNj X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 40F533858431 for ; Tue, 29 Apr 2025 16:40:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40F533858431 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 40F533858431 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944819; cv=none; b=k0e802oXiSEew/cZfK8+yWs2q2zK4uVQEKt2pe70mG6UG5I6QnWdBS1UjpCuaXOQNZOkDoUUJhy1RwJ7QtUGnwgG1lcXmbPyFWVqo26uwQ2x/m10VW75QoTM6pW1Ql5n33BoINbv9fUrUZHAZeqo+enHRZsL2aCxsYY6TyPzg/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944819; c=relaxed/simple; bh=4ILwetyX07ZYWxbQn/N38o/8Ewq369zgcIKUwinFfXo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VQ7mFHhtFvg/pROiCxwH1MnL/fc6fuv8oDFyW/cD9hFF6WI9lq7po39hq1BPb8cECIbmKPfnYl4Hbzf7YoWpIji28VXjcgDIVscoSjk55biAzgGhZ+Xblv5I6q6XSff6U/mwmLFprNOLPwQsJUCT5Pdgoxy+fORzD3tL4PnIpSQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40F533858431 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-736b350a22cso5474952b3a.1 for ; Tue, 29 Apr 2025 09:40:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944818; x=1746549618; 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=ALI+AuBRNGwWzLFmAOLr8Eq2GZOc04SZ4j00angPlwY=; b=m7issrNjN25JCRxw8KHJxeS36TOjdqJZbpy8s27yYen5qOfskhkYdgBqUj1FAFXrAC K1HJM6sG728JZBsIRYav4FjJeDpiSq2yc6wK9U+49EKx/BxJ6yX0j9KpNuBxR+joFX8R GADOsPaiGWocMUc7spSthlIS20loPvk1e4FLGSn3ernylLP9lpdh5tymYS0jB6XjmaME EHBH1j6whNNf8DrY8TmxO3AlLBx7/MJ3WhUIA1wJJUBovX+baziLaxpVreGNykA5Jjiv bAB6BCNPRdxSogLaXHWV63Sew0ivn5Q5Qe2nr3wBXe3hSar+qgvYsVlHT27PCqiqfNG8 pOnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944818; x=1746549618; 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=ALI+AuBRNGwWzLFmAOLr8Eq2GZOc04SZ4j00angPlwY=; b=Opd374ByG8ENS0mcBDGWhSWrSrc/vuDKMOo4uClDg/1BET9a8i1lff5vuLlj5WfXE8 IroyOW4IFayG8TyawaWwyi75CKdrF0rpRLshmX51mx3x7QYaKH+QZT05z6ejm4WPgz81 cYsCfBS8cG5bNybK8aFa7qIQ8upEhYYYUZgfz8FsxawR1RUh/8JWMkRdWvWeoAYV/Yya kBhx9ygSee6F7I6n1Wkoi4QJy5cR0/WYrWj5dKWhqsPS/Zrr8ojckKiq1iMpHI6aRqYj eAVYkpeNtcesED6nPmaDFZhhQzG7IG6VTaSXw6MG/73PZ2U4AcA5tas0u8I2bnMLHZBn AK+w== X-Gm-Message-State: AOJu0YwE5+GaIyu8mL5sVbYsqGBjAeUlC0EznLBSmIbvcWlLP/rbcg2a fFNqSBsbqDnZgSv5qt+Ox3VTf99WF3ZSIW837cX646BRPWEoJ1KhSJ06m7sjD+MWtiXJgQv7kqH w X-Gm-Gg: ASbGncvSh6YRcks/5CgqB/fcE1pcrEUUdrL7Vxy0tSlmYpJgZ5y+Yi+wIGqOyuClQFK hltpJrL0wxtV1gJS+9JrpAqN25FCvbVsmaj7/MOzJ0WI7/7w+w/U7n8nlimNA9nBh5KnarGyrSM ji8Ssa1bKX1HTnCmCsl8amal7RHckS+Dc590aAkLm0z/BQUobs+w4r0WQkoUjIdYrjf/Fr8zdpC Y7PVLP8fHkBqkJ5ZGBPETUpYVP3aBiONaNt7q7ZjuQ5vwxEHmEdofL60I3lBmgK05Cn+nhFGBXn Jd1/a7rSU8NTOqT4LOmiL97PaGR5JNFwP9cjoH4XJTB4y05e9QVlMA== X-Received: by 2002:a05:6a00:14c3:b0:737:9b:582a with SMTP id d2e1a72fcca58-74029145d45mr5643793b3a.24.1745944817980; Tue, 29 Apr 2025 09:40:17 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:b7a9:b8c3:bb3a:fca6]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e259134d5sm10492409b3a.19.2025.04.29.09.40.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 3/6] math: Remove UB and optimize double ilogbf Date: Tue, 29 Apr 2025 13:30:01 -0300 Message-ID: <20250429164007.2928271-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250429164007.2928271-1-adhemerval.zanella@linaro.org> References: <20250429164007.2928271-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.h 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; }