From patchwork Fri Apr 25 20:44:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884428 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4158301wrs; Fri, 25 Apr 2025 13:57:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+wUrKe4G/DwIDsqAHDgciB/2e7Al4xzmAO2g9tOAVPCCuYw/ngdQpSKMhDX20WqqUAa3EtA==@linaro.org X-Google-Smtp-Source: AGHT+IGWHI2RvMptPj4kQDdZsgSWlsTejQYKo8gds2SfKE4fP0XXIW9//BTmaWUaKvHskgoMMq75 X-Received: by 2002:a05:620a:40d2:b0:7c7:a494:bbfa with SMTP id af79cd13be357-7c9607ac8bdmr609593485a.52.1745614657835; Fri, 25 Apr 2025 13:57:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745614657; cv=pass; d=google.com; s=arc-20240605; b=HY9YDRrr6d1dPMWpAgVbEtlFLyvCHQEDgLfaEiaplmTnloOVV6DJXPWBoEOE2fEVDC yBST4kxTXd4InczfDZhS9X3N47nBbJL1EIEwbtUNAFt9LlOpqFqD80w+TyYLaq9QAg12 2bhZ7Xn/BYBsY/4On3WzSAeB3DJw8mAey9X0DwayesTHlmAWEE9a/a/AWF3ypAev5sv7 409OLu6ShsLNUUSdV1onFk1grzlr+PKArZTSAdm//eZAt7cD31UiUGRYTX9sw0g4UUqg 2aypAeamqGIRTbwjs/5sK57N7Vrck417sd3wUlyjwK3Love616AJ7yDx5I5B1MSeDCrT UOdg== 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:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=lICPiURykqbhuTQ1fOgv1l8TNFIII5JsC/bdbXeQbT449lDwYce+xIMmfUQuru/z3p jNK9+0j/gRp4DS50s1xUUkWlEfSAM+VvWsrSC6wzINxT+uKGT8PGUePvVwQ4mfBEne4J ZthdgXXBjgWq9ZOv5aMDq5ouPS6hPp4zvdsgAir0NN6MlIRf4W6EBAmtTdE9CDqM2rPB a4w2hQT2wwAwvXUjniMSt30lldUjWbDzsfstMoZ+wze0+vJw/8e+8Ro9zrFXnwhokmnW q0ZCd1xUfrB6XV8tlRDb6KyOYNohxC+mSwwTdLTgCjOagmC6hHHy6/PKCUBLNMy3eXY0 SCVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=flLBrP7l; 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-7c958d85651si425596185a.222.2025.04.25.13.57.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:57:37 -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=flLBrP7l; 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 65F483858C60 for ; Fri, 25 Apr 2025 20:57:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65F483858C60 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=flLBrP7l X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id EC1CC3857B91 for ; Fri, 25 Apr 2025 20:53:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC1CC3857B91 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 EC1CC3857B91 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1031 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614399; cv=none; b=wM9mFt2KSI6ptd5ZDqRgV9A4IfbUOe7aUfLJ4Au6ZcOO0RXWsEoE6QJTNd9siXoUDv7BV10nlQjAzgPA3zDtUU0871gsR2suCTPQhYuMLli25d5BrBSXWWt2Ul0coPKn9xqVCtoBqv+OSu1eoEQxoZPx5EU1USKei0yNllquT9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614399; c=relaxed/simple; bh=9PwBxR+/+391tENgbEoxHPTSKGxvxLfJLCcNQbEI9XY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=t2CIn83W/jtcNg0Ig6x0lA2DxMWUdsicXPVRWbCz1cXsM3UR3zZt1yQSKElNYi4zIc3mka2WYxnNBkhNWpfvLId1h1ICVCdFd5EubymxRCiqGUt5g7wYgoNetsvtmHY5xBx6UIizAWVNKa2P+zRxQSWeHa3wQ5sDIm65nFDjC1w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC1CC3857B91 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-303a66af07eso2138773a91.2 for ; Fri, 25 Apr 2025 13:53:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745614398; x=1746219198; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=flLBrP7lU4bFNjsxyLbOty4fmJs9N/WjBi38fegv1qJT7e6Dy+ef9eNFFnP8Jkve/M NWt9v8YLZo5zkQQjXOMXmB9AGvoVSixvJ5wMoAYAtm2Y02WBzqh2NzRgRbL8cXlbFJIk R+0uOw9lauyUz61/ip0SV9kUynH+Vg+76ay7vJeImfTssjciorUSM5xpwf3saE+vJ5Rz z7smgktBSHdqbndmfnB3B0Led8ouzohVq5UW99m1gUbyh29tGpWZ9lYJru4mCQt4BQ7v CAMn7HAGYLCD69j4LrKo3PQv9JJeJf5T2IS/3JvAalYvjRbis1Z3L3xpbMThcd292dlr JqQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745614398; x=1746219198; 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=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=QUvn1OwjHzkNcuBnsZVcyHh1jZgVX+WC4IbrhRfjPbkoZp6Sxk1o00rUkOK7zCvXph jmUJv0MvZ58g59cFQlPntHPODoCoZB4y3zl4RqHUMAV+FWYE+gShRq65U3s2aLT9GSRA 7p2qkn2BIcoLeolQ8yiqIFn64G9MtR5CV2jchCsJMbeutcUI7oLDAcAA5ciTnsfzCUqT 9AZ2Z78b6JRL/X0LgnMStuq3pDNAPii5RShfVs+BMivCkW0stJVWuC+vB5CbDXHYawem MFLJSLSyGIGUHFD9RbIT24QdJpzGLWSlUvctdv3PzWMPRxoRGisqrxuOGkdO+lzfwW5A 05aA== X-Gm-Message-State: AOJu0YzusRlnhIjkkBe5sI9laQ9YB9KyrKIxP/egW1eZ0dTUFtJogKD+ 5DxFSN6d3OMcltqhEX46QdIz5lra+vuQmzL9vXVhQ3tS+LZ9/9k+3e5YBAnNZKbaIhasJICpJBz a X-Gm-Gg: ASbGncs6U40SypWfGC8tp2e9yL8I0UhG7TdOwie6oePBLvto0I4Tes7/IowohvkT/hj PzUH1JCiZWxJM7TnEfs6SQllG8pxSgtPGd8esmiO0vUx6U4ApXtucCmpSAhNpyUYxJySSvxoJZc G8r/avktoYjoeV9Ln6tOjg5uUCJYfQ2T8kfmwyF/tFU31NKOnqihcjB2s3jHogRMTgV7/AdwZn0 rGZjv/Rk7OdI2L+cmpCWqAaGAA/kphHuAMnGkJyxom/J0DnGxjCIgHFufkYW7Uk6VLGmEAUl88F vYqFjwqRNs4vTGWJ1UFIzQHy8mEaeqHM9eb8vZu+sq8oLRefgvIHPw== X-Received: by 2002:a17:90a:d888:b0:2ee:b4bf:2d06 with SMTP id 98e67ed59e1d1-309f7dfdd54mr4919435a91.19.1745614397620; Fri, 25 Apr 2025 13:53:17 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77371efsm2385260a91.9.2025.04.25.13.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:53:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/4] math: Remove UB and optimize double ilogbf Date: Fri, 25 Apr 2025 17:44:09 -0300 Message-ID: <20250425205309.3866442-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> References: <20250425205309.3866442-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; }