From patchwork Tue Apr 29 16:29:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885756 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp425148wrq; Tue, 29 Apr 2025 09:41:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUER5wyPMdisWyYFmXUoup+M8QYx7m9Cl5nQBPnhL0QC7MxD5yXoshaeVBaJle/Sco1Z2qgCg==@linaro.org X-Google-Smtp-Source: AGHT+IG4ORRjXOAmdE+z5rrYaYKPuMez7BaJ0P/09XZQBZE/aI5h9mSSw+hLUBx4EMEbObNpWx+M X-Received: by 2002:a05:622a:4816:b0:476:71d2:61e0 with SMTP id d75a77b69052e-488138400c4mr61017601cf.23.1745944884818; Tue, 29 Apr 2025 09:41:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745944884; cv=pass; d=google.com; s=arc-20240605; b=LojGqhOSDVhHcdm3h329vZ+ZGVNDbjXs6WJQuZctQF7T6uBAGOq/k2KHSHVIALqJa4 F2Cfv9fu4dfVZfewMvLd76nbbBu/gjY2SJ28Dz+D2RPtPbLarqVhnFyniNajCfwld2wu AwyvYsjxq/t54xouomRTc7Xnf+yEFWyV5ti570gTJ+j2fo7RCBtzC7/BYQW16gFdkh9q GChEbp/7khHZDhL461JI3+sP/yo6B/uPJnC1IDD4ADFCkHJUuSDow+aXd8sfCgKvBNBE YFNEcXpzQH8Cv1D4TrNJAki/ALljNXcHzS6GLhrkXp67vr99oPaxUPJtJ8M6xwOqhprd +BQQ== 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=UN1kHXY/J8CVcD1n+t17YNy3JewyE8g4BuZrlOJl/PU=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=hrW3TVkGoDVjryLL99OfSD55ujcjSfGIoOvnb2+6RsqOanuDls7gAhyzQ1KlPtZZKq 2bJVeNvyYZSSwXFkS1Ch6O6XipAQdqXBkT6r/mse5HCsbw8Q9YXMyE1xbQu+Zfkm+xlu 30oCrxx9tYuCySpz+wUApUyKlDrzfcYXbgidQcQMhVadpTl1Qc842Zlgsoy/LqQo2Jmu x/cVfkkLKCYIR9R9wkzuDnCMhTAIvyl47bUnU7//Q7fs+nX2FYiHJHDHvJL7TPrxjlFU iFlW4GP71s0Kcv1gkVuzm3WncTpPuAz6njs9BRh79wog4iUYADVZEQvadZvyoq0JP2tE U0mg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kIgnjI5v; 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-47ea19874c0si131916641cf.334.2025.04.29.09.41.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:41:24 -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=kIgnjI5v; 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 5CE773858C50 for ; Tue, 29 Apr 2025 16:41:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5CE773858C50 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=kIgnjI5v X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 303373858D33 for ; Tue, 29 Apr 2025 16:40:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 303373858D33 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 303373858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944815; cv=none; b=LI8+ncaGQ5LEtd6uiYq5yzHQu/UKJWR1D2YmxfHZIJS5F7yIWHmUy1nYmQXZCUCXE6/UeQ9TwBOMQ1aUAauWTbRhxJ+5A0BrLvF0acepvgsRE9dK0saOAGTGHKezCdIQiHQsxHSJE8wMbsc/1MwLHyuzkKTxAApz6tUab+U1ju4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944815; c=relaxed/simple; bh=ot0V8DBBMmR9l04H5QJMQ62PY17q41BRZe100LzTCXQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=V6enf3/4K6UGqyJkUZ+om/7WR4JetkB4UlRbwT3ljOcr2VtzLuMxILapc2sJGHo5gtS/GDuaViVWK62yqtBtfnH4fxfiLBtoM0sY5kxbQjvw/uvuXV9V8JEfvScX2hXI1SIAG0RgYzxEtnhgtlO2SaGRVbbybRSXTC3hFthYL8A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 303373858D33 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-736c062b1f5so5345397b3a.0 for ; Tue, 29 Apr 2025 09:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944814; x=1746549614; 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=UN1kHXY/J8CVcD1n+t17YNy3JewyE8g4BuZrlOJl/PU=; b=kIgnjI5vkMaFPA2HbpT3TGKSa/WDVB0Ha/N7wXFE1/ePTe/XDPtr8ImRZcnPPRRrvT zqT2GPgl2sre2x8UP01JwOwy97S3CWfPiSV5wy5wGjpcwM0f67hUxXnVbQjJPCTyfQac 6izXhEsQ929JRFfD5eJRlnUH5+8SSJZIpnReJFl8grvRRnq444xssLX/YInBWkUVG1Ib rWy1Vm93w76BVfoxWGeRLm+t36qSPv1Nq152HqGgWIZiFjjaV0IDhrsFyFjYJlE34wh6 fPUB66T4E09mPnFfLtQxkLxol6LZSIcN8EVV3bThgDB7Hs8og5+CL3qgg38sOOK+jV1D a6zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944814; x=1746549614; 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=UN1kHXY/J8CVcD1n+t17YNy3JewyE8g4BuZrlOJl/PU=; b=Ua+pAEjK4B0Bu8Fp+Bi4/pS/88lU0mVzq7D73brStTrPL+1njfIqb+9Brux4khNTvz /r+PNEPFMeriFZoxYiGfC8FAixfaqoAk5x0/LGSsA8ZRoKCS4UolypODf7l4NbX+dr99 7+YVuAWZ42I0lxfQutzReqK1r+yVsuQG0qFr+STC5ORFqpXV0YXgRvBAonJFV2xm79HV fo5KBUnAebsaLxfdiXkJH2iJiIbLyV/UnIuH/+w5E/Ah9+BrWUWnuzisv5XlOrLMvN7r lxWqi6cvRyDmMccfSjhQLOcoTJL7mnvnvp6dnBJ27BJ5KjFUJBH1ljesftg0gfLdaGIx Trlg== X-Gm-Message-State: AOJu0YyxS7k5ImQLnmP3xum4L7PhRDZGvK5KCtV8ekcy0VyN9BLah+fb 6C7k784pS3OMEL1L8eK3f60fGoo9A9LmAXVBfOWuB2NW2X5gxeleeNCzo9MiKvWleiZF4yhxL5b b X-Gm-Gg: ASbGncs5XJ+dxqzKX2GcQ2KvL7re/SUUxRlSthT8ELHOCez+mX6NeeXvgqPxbsMg39w M9q3tkbvi9NBIugVG+H+eshbdKZDLclouVZGZ2DoG8zMkn34TkE4hYp1YFueKq4RXiKoApqE5Bp y/4JTgZUsexSAsHcmvs2pLgrJgKCVenFA9hkk4hqeqe/X01nm1vZ5FXL6X40I6ODYKrqEn09EMr DTI2TbjNfSiB9ebXmceeskNRyCn9QUH5HYZ6P92TaDWSBjH4NAF0bA0ZX0pOibMv/MOstKUUylk BwLGdruG6tU33/j/Km0HgbDS3WgZUBXP+vTNXoweLAS3hCVDqZ5QxQ== X-Received: by 2002:a05:6a00:114e:b0:73f:1c49:90f3 with SMTP id d2e1a72fcca58-7402715dffcmr5697889b3a.11.1745944813908; Tue, 29 Apr 2025 09:40:13 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:13 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 1/6] math: Remove UB and optimize double ilogb Date: Tue, 29 Apr 2025 13:29:59 -0300 Message-ID: <20250429164007.2928271-2-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 implementation also uses 32 bits operations, which 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 Tue Apr 29 16:30:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885757 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp425212wrq; Tue, 29 Apr 2025 09:41:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUsRNi/FYujuvc7NXw4OvChypNX0WwHjwJkRCeS/VwHQQ8hPv3XM6YtcshwDjbIb10umYNEBQ==@linaro.org X-Google-Smtp-Source: AGHT+IEGFby2pFNDYc18o9cMudJHPRlDqwHSzDMDe1AZSB94UR4Mpzo4BZsVPnCRM6ljbXcdK271 X-Received: by 2002:a05:6214:f64:b0:6e6:6713:3ea8 with SMTP id 6a1803df08f44-6f4fce9dcbemr1234786d6.23.1745944891146; Tue, 29 Apr 2025 09:41:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745944891; cv=pass; d=google.com; s=arc-20240605; b=bb7w7bFA6qoqRIoookhBBX0GYPgB8cEzIhyb6hpQOjzKPMtIirhiu9tjmUi1nYfFYp AfMTWdXlUOCKGuDryRntJUbs8WVe+sr5V5gkADyjB586e2NYvmAT8D5oySu+0ChqbsLV /G24MCqUvVnD6Z0pw1Z6zZwbrlJ5q0faS95k+UPlWQKEYFUHLB+D8N4aWQEmate2wSIM LxmadC+Xyvr1B9sptPHw4Fpttw63CYsfOgIQ4ubVHezFZ0heO+zID/QppBejO8jniKOC oF9stNi3aRkXDjhJFazb0n4SBCoY23+Y4h7LsxNvOC6SvODzgO3xjfWo0YN/FGs4++4x dl0Q== 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=TZ0B7KaibDNvpPu/oWeSX5CMAHs54GU+aI7HtPisn/g=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=AYGecbF3K8CmE/qc83Pgz65dcJbWz01QjRiXo3LJ5EUzeNlviWTZYomVpkTprCWMFq Jw/inesnRqYfDBjoqPtBrrG3SsNcHy/QWduStpJGMpgTwd+2ndhHJXEPPM0PB3eolCmw lw1kDmCS+WvsJTFyRbIpc4vwOsmV2S50HTGLQTNixTFvqNYcVuJLcpmBHTyvrFLV9p5j 5aG/LeKnOvBUGrrWlh/O/Of2bm2X4FEXmdJHkHY0RkQAP8hetMHXabKUl4gACezjYFGY CJ7RCk/1IjzKqQjDczR0+5m7ij6Ytx7t/i3SR0EbN21lC20OcbjyNfdndLvX/FS9Ejt7 qiFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iSTwuuKt; 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 6a1803df08f44-6f4c08d27b5si127770196d6.52.2025.04.29.09.41.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:41:31 -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=iSTwuuKt; 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 B31F63857B84 for ; Tue, 29 Apr 2025 16:41:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B31F63857B84 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=iSTwuuKt X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 6AD043858D3C for ; Tue, 29 Apr 2025 16:40:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6AD043858D3C 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 6AD043858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944817; cv=none; b=g1g4TsPLCo3YK6LDVgx0ye3W+heWkhYxJjZpX8ThUyp4w63mGMJ/zxOyOaR/+HG5fD0+fjZ9nZwNrJZAAgUNu8UYAXfQq8SDsbVNJSLSjmlqWy+q07J4Vrgxl2xkHrKa9A7mIikWY3zN9zVWeLxaty6bVUFXokdRIEdLd6GU+cM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944817; c=relaxed/simple; bh=eFsC7+YRFX/gZKfQ6MYWKKqlM6Ssu6MIUYPyOQIlw6w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dAq+VxyI6Yx0LoCV/M04IYfzhSo3EhBgrd15Bau8oeICWz099fPK5L++LAhJGp8jxR1MluLWe36kWaJ67VMaihekwQthTHhwqJPiCIqXOn2G04M0GkdSFf6BZPur5SdrQh2JXHNzr2P3GtX0jM3107Gi+FD0Nt6UVgTP6wiJE9I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6AD043858D3C Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-736a72220edso6251500b3a.3 for ; Tue, 29 Apr 2025 09:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944816; x=1746549616; 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=TZ0B7KaibDNvpPu/oWeSX5CMAHs54GU+aI7HtPisn/g=; b=iSTwuuKtoOvyFFL1WsQQEXMEo7M15xUaq+lLADnXqEflSNsl7EtXM94/AidwycKSSr N2Nx6QyHf3VSNSfHpPUXs2YB+a0qgc78WHRvqhmeIcC3T2n98v5Ee6HAvl6fKlK6/k/i hJ8OPPdu9w9ScVohMR/ftmrNBUs5ttNaibfcL1Kd43D9fH2U07PSYPXcH6YQtnZAFIUo Rk4cnOtKHFIKvDrBWbAKD/YVGJoufLpHl3juZw8zJ7EQVNG50xvPyVVAfNAjZFDbuI+1 5kcSfiOckq0GguARBFrvCMK0M87DxAf7hJcjR4kZKb7KVGaM9XbnXEzBXLEPec7c830F ZN9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944816; x=1746549616; 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=TZ0B7KaibDNvpPu/oWeSX5CMAHs54GU+aI7HtPisn/g=; b=m3nI4+Ug2hM9RbPZ0YS2+TTBi7DKCBk4+OmQDuGW1eSCf0lDWAiB1c6curj9Vrwwr8 MUk64LJ2STWoYPkGaDr9P5N/oaTLT87NljaZGDAm+0/Bbgfnao7KcEJ0h+0yRAuBMlfi E4U4dGKYu3en7Ofi+pTDpwnEGHisUPiA+iT8orBUXZUXittOHAbBuxXqsF4fOFPQtXCD OL5zZMslFxKoXLjYLeLgbBFrpygbqAz1LY+JWAZpoie6vkU37vAyXzvI65FJVIvtXKrw W09I0vok7J/0gqxzy1uYcMWx+977ZF0BzMyHfLEKnkMBRGAaRfHzhkW+2UI7Je2igjx+ ToTg== X-Gm-Message-State: AOJu0YxUU2Yz8DdSETESRbScQsg4ev0n7YR0RCw53pH+/9GFAfwWltEH 0L5ihBS+vLXAj7lu2RgVFpLZ6zbcyhlEmh7RZ6G3PAiAQ3ljEwEaqXG/iRqAw3ESHPBEhglcyi9 B X-Gm-Gg: ASbGnct9BP3eeLzlffjffRWAP9UE1KKPWVayIIst/opnyW+DMX2jhsj2i2eSQ8Q7RbF 8PFdEgxoci88Stj0B0m+4tFEPp4IZJXSK8EtuRjwJCLK3P66A3diOUwmKskgslASrGCgcN8sO0R 8sLGm+MSVJ1Yz1c+ItvwKc4yQdhy3iFtREhDGSm4OkSo6LaRvH3Eu9vMCrsPoms+2jY0Vv/maJB Cz+f7mI2FGAorMwG75vZ9FUzjp8KsDvP99lwBoEHP5mfs7lK8STie6zOXGFlLp642pZyP0M5KP9 BTp6/2E++T3YIcKU6ks1IRQEg+XQM6XnNj7b5UDIhFbppR59uVjthA== X-Received: by 2002:a05:6a00:4b05:b0:73b:71a9:a5ad with SMTP id d2e1a72fcca58-740293ae420mr5216784b3a.16.1745944815957; Tue, 29 Apr 2025 09:40:15 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:15 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 2/6] math: Optimize double ilogb/llogb Date: Tue, 29 Apr 2025 13:30:00 -0300 Message-ID: <20250429164007.2928271-3-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 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> Some ABI requires additional adjustments: * i386 and m68k requires to use the template version, since both provide __ieee754_ilogb implementatations. * loongarch uses a custom implementation as well. * powerpc64le also has a custom implementation for POWER9, which is also used for float and float128 version. The generic e_ilogb.c implementation is moved on powerpc to keep the current code as-is. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/i386/fpu/math_err.c | 1 - sysdeps/i386/fpu/w_ilogb.c | 2 + sysdeps/i386/fpu/w_llogb.c | 2 + 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-impl.h | 37 +++++++++++++ sysdeps/ieee754/dbl-64/w_ilogb.c | 52 +++++++++++++++++++ sysdeps/ieee754/dbl-64/w_llogb.c | 2 + .../fpu/{e_ilogb.c => w_ilogb-impl.h} | 12 ++--- sysdeps/m68k/m680x0/fpu/math_err.c | 1 - sysdeps/m68k/m680x0/w_ilogb.c | 2 + sysdeps/m68k/m680x0/w_llogb.c | 2 + sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c | 41 +++++++++++++++ sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c | 2 + sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c | 2 + 16 files changed, 184 insertions(+), 50 deletions(-) delete mode 100644 sysdeps/i386/fpu/math_err.c create mode 100644 sysdeps/i386/fpu/w_ilogb.c create mode 100644 sysdeps/i386/fpu/w_llogb.c create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb-impl.h create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c create mode 100644 sysdeps/ieee754/dbl-64/w_llogb.c rename sysdeps/loongarch/fpu/{e_ilogb.c => w_ilogb-impl.h} (86%) delete mode 100644 sysdeps/m68k/m680x0/fpu/math_err.c create mode 100644 sysdeps/m68k/m680x0/w_ilogb.c create mode 100644 sysdeps/m68k/m680x0/w_llogb.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/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/i386/fpu/w_ilogb.c b/sysdeps/i386/fpu/w_ilogb.c new file mode 100644 index 0000000000..9c26217021 --- /dev/null +++ b/sysdeps/i386/fpu/w_ilogb.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/i386/fpu/w_llogb.c b/sysdeps/i386/fpu/w_llogb.c new file mode 100644 index 0000000000..5e8891a668 --- /dev/null +++ b/sysdeps/i386/fpu/w_llogb.c @@ -0,0 +1,2 @@ +#include +#include 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-impl.h b/sysdeps/ieee754/dbl-64/w_ilogb-impl.h new file mode 100644 index 0000000000..c91973507f --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_ilogb-impl.h @@ -0,0 +1,37 @@ +/* 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 + . */ + +static inline 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; +} diff --git a/sysdeps/ieee754/dbl-64/w_ilogb.c b/sysdeps/ieee754/dbl-64/w_ilogb.c new file mode 100644 index 0000000000..e460f14dc9 --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_ilogb.c @@ -0,0 +1,52 @@ +/* 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 "math_config.h" + +#ifdef DEF_AS_LLOGB +# define DECL_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 DECL_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 +#define __IMPL_NAME(x,y) x ## _ ## y +#define _IMPL_NAME(x,y) __IMPL_NAME(x,y) +#define IMPL_NAME _IMPL_NAME(FUNC_NAME, impl) +#include + +RET_TYPE +DECL_NAME (double x) +{ + return IMPL_NAME (x); +} +libm_alias_double (DECL_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" diff --git a/sysdeps/loongarch/fpu/e_ilogb.c b/sysdeps/loongarch/fpu/w_ilogb-impl.h similarity index 86% rename from sysdeps/loongarch/fpu/e_ilogb.c rename to sysdeps/loongarch/fpu/w_ilogb-impl.h index f21fa5c019..1905373189 100644 --- a/sysdeps/loongarch/fpu/e_ilogb.c +++ b/sysdeps/loongarch/fpu/w_ilogb-impl.h @@ -1,4 +1,4 @@ -/* __ieee754_ilogb(). LoongArch version. +/* Get integer exponent of a floating-point value. LoongArch version. Copyright (C) 2022-2025 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -16,20 +16,18 @@ License along with the GNU C Library; if not, see . */ -#define NO_MATH_REDIRECT -#include #include -int -__ieee754_ilogb (double x) +static inline RET_TYPE +IMPL_NAME (double x) { int x_cond; asm volatile ("fclass.d \t%0, %1" : "=f" (x_cond) : "f" (x)); if (__glibc_unlikely (x_cond & _FCLASS_ZERO)) - return FP_ILOGB0; + return RET_INVALID (RET_LOGB0); else if (__glibc_unlikely (x_cond & ( _FCLASS_NAN | _FCLASS_INF))) - return FP_ILOGBNAN; + return RET_INVALID (RET_LOGBNAN); else { asm volatile ("fabs.d \t%0, %1" : "=f" (x) : "f" (x)); diff --git a/sysdeps/m68k/m680x0/fpu/math_err.c b/sysdeps/m68k/m680x0/fpu/math_err.c deleted file mode 100644 index 1cc8931700..0000000000 --- a/sysdeps/m68k/m680x0/fpu/math_err.c +++ /dev/null @@ -1 +0,0 @@ -/* Not needed. */ diff --git a/sysdeps/m68k/m680x0/w_ilogb.c b/sysdeps/m68k/m680x0/w_ilogb.c new file mode 100644 index 0000000000..9c26217021 --- /dev/null +++ b/sysdeps/m68k/m680x0/w_ilogb.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/m68k/m680x0/w_llogb.c b/sysdeps/m68k/m680x0/w_llogb.c new file mode 100644 index 0000000000..5e8891a668 --- /dev/null +++ b/sysdeps/m68k/m680x0/w_llogb.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c new file mode 100644 index 0000000000..89e7498266 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c @@ -0,0 +1,41 @@ +/* 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; +} diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c new file mode 100644 index 0000000000..9c26217021 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c new file mode 100644 index 0000000000..5e8891a668 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c @@ -0,0 +1,2 @@ +#include +#include 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; } From patchwork Tue Apr 29 16:30:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885760 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp427539wrq; Tue, 29 Apr 2025 09:46:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWyBqLVPOKshRmoVvPOZ4WolDA0DMjyRzHqX8BHFalzemIksQJ31jTsgld3iM/NHMUqLub6IA==@linaro.org X-Google-Smtp-Source: AGHT+IGMlVnt4uRNNCUKeOgVi9LJWTR17JUt+p6lM7BZR9duPVWV+af+YO77nyJYdQKIVFz6WDeo X-Received: by 2002:a05:620a:4107:b0:7c7:a524:9fe9 with SMTP id af79cd13be357-7c96687c72amr1893089485a.27.1745945172824; Tue, 29 Apr 2025 09:46:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745945172; cv=pass; d=google.com; s=arc-20240605; b=MkCb7U6DSItuEbrh8Fu8lVJn14vHMyqWUDXeDvcWmiMxTrqQOajr6BrLgZy/fzEpz/ StGWeSocmTMywfmgTxq3GZ6+LdeyFTV2eOQqgL74FVpDPMaOFahYzoCLjMjTX1EpOUtL P/mPe30QqdSqxYf3KpDHtoy019l88yT3MZrzo7g1rVi8LHJrBlzihGOpr0lV2dN8DDJr x9cl9NXA+DWgAeXg7whVWJOVSurxmouc+XafrlKNLqJ7U9KGd0OnObe6+3qKAZrt5zzn MHxHGdwrzrjhBmfKuEbABGD/pHWCkq3p/FiUpzJKKSWng0w//+9Zvr6Svjhr5pOrv/EL unCQ== 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=X+kCrkK/0mN8kbU1flDSMm0e6yE0L7RnooC9AZZqozQ=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=Zmip15GnzTi1CBAWzNrL5D1tCLnSqnr/EhTc8hG+MU632jUFOg+JebrBwEvdXub1x2 xyzzP0RXLi+UcDt+3prX4QFFr76tV9RQbJWCBSRSU12M/gzmKfelqoOS8mVNsCTrkRFb NCbDljff9mEwybdsW4VFD0Jl/Y70MsVJFz61YzVqBuwPsKZFQGn6wAcUvV+vbLgLByV+ BSiV5n4NDeHSkDA5xQbl0FwU2qAxRBh2r4U/1s6rkx0AsMQ07kYpDLqqVc899/LYznm/ iDXE8xmSyvmzsS1A5FigNrDzsbslJ9zEQSZOOdIFBRkkGj+GeyotMo02+z6P81NCvdv7 VQbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nuFMH5js; 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 6a1803df08f44-6f4c192b50bsi125139046d6.270.2025.04.29.09.46.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:46:12 -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=nuFMH5js; 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 639193858280 for ; Tue, 29 Apr 2025 16:46:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 639193858280 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=nuFMH5js 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 77A323858280 for ; Tue, 29 Apr 2025 16:40:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 77A323858280 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 77A323858280 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=1745944821; cv=none; b=BmT+ozcvAxx97jHbyHMLZ3JkiqcZl81bdw0WlOHTKKXeI6uHxzzMtKK3OSAcU0hhqp63SZo4E4R6p6uyuw+mesMWsMt9nWMs+d2MOqLZQ8ue67ShFJYwh0dYvguaQHOmz4Ds6qmx5t0ImMnUdxdyOD8shaRwjqmPGrQ92Agj/xw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944821; c=relaxed/simple; bh=DtJN5ApPj7na1RfGLMHbXcMzFLyuer9rYnh88hzdYJE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aU75Tp0jpM7Wsz8S6m9K5iTQWH7r2Lh7eG0LGAf93seaPPWpt3CZ4PeWXJF86KVTmjRZ/5BKvdnEUZ5iYsWU8INoNa0UeGg2iRUgw45Q3uQ1rZphN1WK3F8nvdIdFvQYeg3xah9Xzlg13lypz8tEj+l8SoG3GfItVY2xNSS9dIc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77A323858280 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-7394945d37eso5423187b3a.3 for ; Tue, 29 Apr 2025 09:40:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944820; x=1746549620; 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=X+kCrkK/0mN8kbU1flDSMm0e6yE0L7RnooC9AZZqozQ=; b=nuFMH5jsmBGAkJs7Ri0AfFJMPzUNXkF7LfiWBFYjK0Er156AFblpvD09nNCrgQn2cx /ljMhE+SFBn9hAAQwK7acr18UYT08EVTkTkZwCrzmS7/qvd+Daw2HN0yTJkJ5+E2KCpM NVO0y+5NBSje6DlJgcC7Sxh9oYfFQZX2Mszn1Qx9kuB/7BndTRgfkODinAq1lvlHZBJm tTnQc0qjW4aH/AlAzKfhBjdaG5uitc3N1JZv9z3OkrTbE0lI824i1TJ4gBdH3zr63Ely 9OGkc7ZC6yqQufYi8LG8rqxI9EZ1SWOXMKLwnEQXJAgMhoVkMc0xZyIbQKUjVBO+9jZX 6svg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944820; x=1746549620; 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=X+kCrkK/0mN8kbU1flDSMm0e6yE0L7RnooC9AZZqozQ=; b=u0nyS3IigOa2HOQNsKvqLCDoeOB8kQu+CbM+bV1IE8wB+3nSg1V1gv0fOai7yJmnyW ASa4YmKaii++F6PMUK+0/SFpoCuC1bchi8c1GkAP/tRfuSudK1tDE6r86KQC9DA2TyoZ tPwxfqDHpYmTF2EEKmwdkwLp+PZXJde5NC7Szaj6ZRmvWzfbGQ1pt3rs9zk7rHRx9KMh 7Eee0F4pS2yDmr7pNQOWOofOXMSpz3kzk319EQnLiQ2oLHcf1gFTI0BMW8lyWdDk/WSL /LaDMtp6LIZQaFBAgUcNC+F5t2d7re6hiOHBpX03FlLiyqicPa6SaTCt1Ldk1L/kt/Pf wDdA== X-Gm-Message-State: AOJu0YzvxPgptb8pcarQQDJt4aEOyuBTWXrWfYezhSYVXiH7pTMJ2s1l Jyyff2kAPu6Wby7C30vjpQe3yROVxhoeps9fEECi7JA+8Yf9qw4Fpaw2CnNO7+kPURJP+x0hZuS 7 X-Gm-Gg: ASbGncu4BLpRxHQUhFDRwXb8KYpnUyYRpCIutWZJJLH6JWzu+wAlV+mBkkLPXuq/j/H wOeQD5Q4BKKzMCGIZSn6ZYh+Z1lNvn59KHWSNczqTgMCzF2pjtgLYjslXJn8uNUh2cpWYD4vB+Y /iNmrVrucuWNseaqh94M/05pqiUIlYPsWaiFJfHWfkCKPOJxzk1cJRz4JsUS1yPZ+3RXiFUcocu syz1e+XhJYcxxmzixTld+yRV2689y+9G85UHUcVW/qCW3T086ouBLBRyKY/1YbouUyC08wbSCZa XdcsAHq2cA6mxyDsRzRKZ9RCESMpNw5jGcS1XiCJkVtr+vcSlTaLVA== X-Received: by 2002:a05:6a00:181c:b0:736:3c77:31fd with SMTP id d2e1a72fcca58-73ff73d813bmr17541981b3a.23.1745944820040; Tue, 29 Apr 2025 09:40:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 4/6] math: Optimize float ilogb/llogb Date: Tue, 29 Apr 2025 13:30:02 -0300 Message-ID: <20250429164007.2928271-5-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 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> Some ABI requires additional adjustments: * i386 and m68k requires to use the template version, since both provide __ieee754_ilogb implementatations. * loongarch uses a custom implementation as well. * powerpc64le also has a custom implementation for POWER9, which is also used for float and float128 version. The generic e_ilogb.c implementation is moved on powerpc to keep the current code as-is. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/i386/fpu/w_ilogbf.c | 2 + sysdeps/i386/fpu/w_llogbf.c | 2 + 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-impl.h | 38 +++++++++++++++ sysdeps/ieee754/flt-32/w_ilogbf.c | 53 +++++++++++++++++++++ sysdeps/ieee754/flt-32/w_llogbf.c | 2 + sysdeps/loongarch/fpu/e_ilogbf.c | 40 +--------------- sysdeps/loongarch/fpu/w_ilogbf-impl.h | 37 ++++++++++++++ sysdeps/m68k/m680x0/w_ilogbf.c | 2 + sysdeps/m68k/m680x0/w_llogbf.c | 2 + sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c | 41 ++++++++++++++++ sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c | 2 + sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c | 2 + 15 files changed, 220 insertions(+), 80 deletions(-) create mode 100644 sysdeps/i386/fpu/w_ilogbf.c create mode 100644 sysdeps/i386/fpu/w_llogbf.c create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf-impl.h create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf.c create mode 100644 sysdeps/ieee754/flt-32/w_llogbf.c create mode 100644 sysdeps/loongarch/fpu/w_ilogbf-impl.h create mode 100644 sysdeps/m68k/m680x0/w_ilogbf.c create mode 100644 sysdeps/m68k/m680x0/w_llogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c diff --git a/sysdeps/i386/fpu/w_ilogbf.c b/sysdeps/i386/fpu/w_ilogbf.c new file mode 100644 index 0000000000..047ad4bf11 --- /dev/null +++ b/sysdeps/i386/fpu/w_ilogbf.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/i386/fpu/w_llogbf.c b/sysdeps/i386/fpu/w_llogbf.c new file mode 100644 index 0000000000..edb7e9a9e6 --- /dev/null +++ b/sysdeps/i386/fpu/w_llogbf.c @@ -0,0 +1,2 @@ +#include +#include 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-impl.h b/sysdeps/ieee754/flt-32/w_ilogbf-impl.h new file mode 100644 index 0000000000..b2055afe69 --- /dev/null +++ b/sysdeps/ieee754/flt-32/w_ilogbf-impl.h @@ -0,0 +1,38 @@ +/* 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 + . */ + +static inline RET_TYPE +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; +} diff --git a/sysdeps/ieee754/flt-32/w_ilogbf.c b/sysdeps/ieee754/flt-32/w_ilogbf.c new file mode 100644 index 0000000000..4e2a707b64 --- /dev/null +++ b/sysdeps/ieee754/flt-32/w_ilogbf.c @@ -0,0 +1,53 @@ +/* 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 "math_config.h" + +#ifdef DEF_AS_LLOGBF +# define DECL_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 DECL_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 +#define __IMPL_NAME(x,y) x ## _ ## y +#define _IMPL_NAME(x,y) __IMPL_NAME(x,y) +#define IMPL_NAME _IMPL_NAME(FUNC_NAME, impl) +#include + +RET_TYPE +M_DECL_FUNC (DECL_NAME) (float x) +{ + return IMPL_NAME (x); +} +libm_alias_float (DECL_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" diff --git a/sysdeps/loongarch/fpu/e_ilogbf.c b/sysdeps/loongarch/fpu/e_ilogbf.c index adced63566..a27fb94732 100644 --- a/sysdeps/loongarch/fpu/e_ilogbf.c +++ b/sysdeps/loongarch/fpu/e_ilogbf.c @@ -1,39 +1 @@ -/* __ieee754_ilogbf(). LoongArch version. - Copyright (C) 2022-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 - . */ - -#define NO_MATH_REDIRECT -#include -#include - -int -__ieee754_ilogbf (float x) -{ - int x_cond; - asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x)); - - if (__glibc_unlikely (x_cond & _FCLASS_ZERO)) - return FP_ILOGB0; - else if (__glibc_unlikely (x_cond & ( _FCLASS_NAN | _FCLASS_INF))) - return FP_ILOGBNAN; - else - { - asm volatile ("fabs.s \t%0, %1" : "=f" (x) : "f" (x)); - asm volatile ("flogb.s \t%0, %1" : "=f" (x) : "f" (x)); - return x; - } -} +/* ilogbf is implemented at w_ilogbf.c */ diff --git a/sysdeps/loongarch/fpu/w_ilogbf-impl.h b/sysdeps/loongarch/fpu/w_ilogbf-impl.h new file mode 100644 index 0000000000..9cb4172c26 --- /dev/null +++ b/sysdeps/loongarch/fpu/w_ilogbf-impl.h @@ -0,0 +1,37 @@ +/* Get integer exponent of a floating-point value. LoongArch version. + Copyright (C) 2022-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 + +static inline RET_TYPE +IMPL_NAME (float x) +{ + int x_cond; + asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x)); + + if (__glibc_unlikely (x_cond & _FCLASS_ZERO)) + return RET_INVALID (RET_LOGB0); + else if (__glibc_unlikely (x_cond & ( _FCLASS_NAN | _FCLASS_INF))) + return RET_INVALID (RET_LOGBNAN); + else + { + asm volatile ("fabs.s \t%0, %1" : "=f" (x) : "f" (x)); + asm volatile ("flogb.s \t%0, %1" : "=f" (x) : "f" (x)); + return x; + } +} diff --git a/sysdeps/m68k/m680x0/w_ilogbf.c b/sysdeps/m68k/m680x0/w_ilogbf.c new file mode 100644 index 0000000000..047ad4bf11 --- /dev/null +++ b/sysdeps/m68k/m680x0/w_ilogbf.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/m68k/m680x0/w_llogbf.c b/sysdeps/m68k/m680x0/w_llogbf.c new file mode 100644 index 0000000000..edb7e9a9e6 --- /dev/null +++ b/sysdeps/m68k/m680x0/w_llogbf.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c new file mode 100644 index 0000000000..1c2a8a5ade --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c @@ -0,0 +1,41 @@ +/* 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 "sysdeps/ieee754/flt-32/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; +} diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c new file mode 100644 index 0000000000..047ad4bf11 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c new file mode 100644 index 0000000000..edb7e9a9e6 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c @@ -0,0 +1,2 @@ +#include +#include From patchwork Tue Apr 29 16:30:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885758 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp425440wrq; Tue, 29 Apr 2025 09:41:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVA0aUwhLxVFAoe8JyVeVI7XbvqJSz9GSpxp6gKPwqXxTy7iZ2lodSUNbISYdAn+KsqjVdYQ==@linaro.org X-Google-Smtp-Source: AGHT+IGwdj11dZfz4XmAM2h/q4/IF/aa5WWtspkTorcyroYymFM1CWraUCQX+LO1xVjht73A9iVS X-Received: by 2002:a05:622a:5448:b0:476:8a27:6b01 with SMTP id d75a77b69052e-4886f5c0930mr69815831cf.47.1745944915673; Tue, 29 Apr 2025 09:41:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745944915; cv=pass; d=google.com; s=arc-20240605; b=Jl8wSJhhWV5Bz431ViVWv+QyEJyfqh7UGUf83znKgD9vNSxN4sOrHx4AtkalzpViI5 sIbdlpxl+UyJFL77aRd7paPNyOZtXXKScchWYK+iyyzM4QsFvWyFJlyIkNqvxipIRuVJ 44z8lVLyJ8m6tmANRdGavHLNqh45giaVi1jFIBAUTn2herPTUsylOEiH9xgbZ4e5ijGv CtI/UAWu9HTeG9jZ34P20EjlvqBsuamIEl5hcrQpgvJbBmF5m1y2vuz6ioUgDQqBA0EY ZdAvLlvxGhwNcWkZfWGppMf9Qk8k91APhXBmJ86qlEZu7+2YX4R1+GtnOy43lt3H56Fa qYvg== 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=peOeEnZ9gmiNY1Evrbli58p7hEmp9XKxwJ12Ou4HLm4=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=OKVXA7HWjodov+tYk2813ARUbJ1KH/M9sjnTy32GC8d/aYsq3bSRWNMFwZz+eclzcW bzQpJaCcbJdzcdgPr13aK0cA4VIIhwi1VJXef6DVfoyk47RctdygZSHkx4g0wbJrrIkY 2HQCxpZShRP3BsCnrONNF1Nx7v6nhbV64ZsTw0fNVRnFIP+p1usBaRv98rVEnjOrpcyE Z3sefqJQNSrInVFtBOP83NwdnPAiv7+Oa3iayZj7vGmD1ere5SKPH4zU1jS17+MV7oDp odfslKrXiMyasOXJlPXLyypBVvku+9YtWtw70Xx2UaXxNE547cxi+DAaBiGqsyJ2pxEP O2Lw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ewa7vW1V; 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 d75a77b69052e-47e9fa9ac4bsi136115971cf.165.2025.04.29.09.41.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:41:55 -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=ewa7vW1V; 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 404CE3858C62 for ; Tue, 29 Apr 2025 16:41:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 404CE3858C62 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=ewa7vW1V X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 3F3D5385842D for ; Tue, 29 Apr 2025 16:40:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F3D5385842D 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 3F3D5385842D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944823; cv=none; b=kLOy2ojl3zuzsIxS0HLJCMR5gRltapQ/4SrTOP4krfzMa30nSDPvTiA61Zp/RTt4RUwDLSTvrqe0ZF7vIIOxZwHS+jv5JwyTSyLgxggW3eoEBgG2aepd/YEHNZBqABDszDcyVOza5sSumoYlsZiRKsXJaikbU9v4ee8dbvEserw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944823; c=relaxed/simple; bh=asAB6KqwTTicusJuGVoh9tkTAgmJdQQKzTMC0lKCbro=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NYX5+A94XYOkV3If7w3A3D0WdHfSJe79VO/HMcZ0htTLu0JbZJvFJAU6YgeX6omGRTTnghHRSvpsqEkPvIrlt2azyG83SEN/w1sW8Tg4AIaJBIMkt4Fzu8/YjqUTKOY00gq5VD7TGMDaZy6NQwvQWBbo/zxeJoxVI5E5AFR33Tg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F3D5385842D Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-736a72220edso6251650b3a.3 for ; Tue, 29 Apr 2025 09:40:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944822; x=1746549622; 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=peOeEnZ9gmiNY1Evrbli58p7hEmp9XKxwJ12Ou4HLm4=; b=ewa7vW1VlaVyZ6uLkNH4Atm8k8eIo7H6Wn2zhymmk8YMt66SHGgcAWzr4sonmpaIvu P7pCBdJflcDrfffuytmBko4vEmaRteVoxECSKXkB+x4bulpvnZGZvhOqghQGJCovjkS7 s6cYq+aCaQt4/0VMAeMk2QXma0ajlv+DjD8ZsrHMhxEBwfRcT/xMn6er9a+7WFluocc3 f2lUNqKL7rAOQa2LGdmMIMOtezj5EkDrEqgLjHeDf/4R4MprmPu2ImVDQbrOwILtpehF mq9lfvXzeHv+t22O5Lg6c6DbSFOAyEk047q+cfzxu33WulZzX4BNfc/20OM1y3cbBvMR ++ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944822; x=1746549622; 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=peOeEnZ9gmiNY1Evrbli58p7hEmp9XKxwJ12Ou4HLm4=; b=r6IIXXYWye8o11BH+BvECkTo+OatihuBsogR6n1Nj6R002dtOIrfr5wdJG+OkjkBUU n3x2x1d0C13nHGlMBATEK19MH3FIZcfSqTthvYsbKhwKkYb12ofJSt2pfDtzaONO1zHR Y1kzbViUQGhRU0ZIJs11g+sKGMXXM0FXj1hnoW8kZdHJsVefjgfof4mqGL/0rFdkWYuP gDm/xnobKFl14ENJAfw6OCZt6/AaIZ/0swRDRBmmPdmXLNXxx5oOzMpZ01i8IWfuG0ax rRVZXT5qYk5fK3zHliSQbgnfV2wSmQbP/P/i56956KIyzrjSreMkR0TViqwpjbipNTDK rIDA== X-Gm-Message-State: AOJu0YymcTVOtUM01oARzJrCz9P1n291W9lNKOAIm+4i+87njl07XVm7 /ZWn0Cb1QOV3hRe+3hHcUnqb65O+YgYJzkY7bZfgp3zWpg7/p5BCdWpanwhfYLAU0e+jBZCjliJ I X-Gm-Gg: ASbGncu1A6XtmH7U+n63zVHcdTHzsIu2SJCcWy7sOd8hHPh2CkKqOJ1mcXnRZCsCGDP wwZPwrj3t2z1KcXuG52HlIK3lEfU7XZ+QGOaHpIxcaBYg/L2W5ymSv6CAQ9duvAM+kXBQcIlkbw 3QFuHi44FmDGa2NszEtqs9Q+EMvZ2QWianRIebbGX7RK5UcyAkQQmxOyZbTUQ4jDAQ87s7oRECh anPgbZ4Ge++86/lDun/xhTiNt8GJiAKQVh+9o7MD1eMiL0Ce6tmS6alULLNP+80HiwlaIds9wFs ps0kHoLIQIlOCNLLg9e3rCxCv0475r/Fl0ByOCsx4R39M6l9wUZFDg== X-Received: by 2002:a05:6a00:4648:b0:73d:b1ff:c758 with SMTP id d2e1a72fcca58-740293ae0bbmr4491629b3a.18.1745944822062; Tue, 29 Apr 2025 09:40:22 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:21 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 5/6] math: Remove i386 ilogb/ilogbf/llogb/llogbf Date: Tue, 29 Apr 2025 13:30:03 -0300 Message-ID: <20250429164007.2928271-6-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 new float and double implementation does not required an extra function call and error handling uses math_err function, which results in better performance on i386 as well. With gcc-14 on AMD AMD Ryzen 9 5900X, master shows: $ ./benchtests/bench-ilogb "ilogb": { "subnormal": { "duration": 3.68863e+09, "iterations": 1.72228e+08, "max": 89.2995, "min": 21.016, "mean": 21.4171 }, "normal": { "duration": 3.68878e+09, "iterations": 1.72948e+08, "max": 78.6065, "min": 21.127, "mean": 21.3288 } } $ ./benchtests/bench-ilogbf "ilogbf": { "subnormal": { "duration": 3.68835e+09, "iterations": 1.66716e+08, "max": 46.953, "min": 21.793, "mean": 22.1236 }, "normal": { "duration": 3.68784e+09, "iterations": 1.66168e+08, "max": 46.9715, "min": 21.904, "mean": 22.1935 } } While with this patch: $ ./benchtests/bench-ilogb "ilogb": { "subnormal": { "duration": 3.68134e+09, "iterations": 4.17516e+08, "max": 32.5045, "min": 8.3245, "mean": 8.81723 }, "normal": { "duration": 3.6677e+09, "iterations": 6.79468e+08, "max": 50.9305, "min": 5.3465, "mean": 5.3979 } } $ ./benchtests/bench-ilogbf "ilogbf": { "subnormal": { "duration": 3.67553e+09, "iterations": 5.11032e+08, "max": 35.927, "min": 7.0485, "mean": 7.19237 }, "normal": { "duration": 3.66877e+09, "iterations": 6.556e+08, "max": 26.3625, "min": 5.5315, "mean": 5.59605 } } Checked on i686-linux-gnu. --- sysdeps/i386/fpu/e_ilogb.S | 41 ------------------------------------- sysdeps/i386/fpu/e_ilogbf.S | 41 ------------------------------------- sysdeps/i386/fpu/w_ilogb.c | 2 -- sysdeps/i386/fpu/w_ilogbf.c | 2 -- sysdeps/i386/fpu/w_llogb.c | 2 -- sysdeps/i386/fpu/w_llogbf.c | 2 -- 6 files changed, 90 deletions(-) delete mode 100644 sysdeps/i386/fpu/e_ilogb.S delete mode 100644 sysdeps/i386/fpu/e_ilogbf.S delete mode 100644 sysdeps/i386/fpu/w_ilogb.c delete mode 100644 sysdeps/i386/fpu/w_ilogbf.c delete mode 100644 sysdeps/i386/fpu/w_llogb.c delete mode 100644 sysdeps/i386/fpu/w_llogbf.c diff --git a/sysdeps/i386/fpu/e_ilogb.S b/sysdeps/i386/fpu/e_ilogb.S deleted file mode 100644 index f4b792c27c..0000000000 --- a/sysdeps/i386/fpu/e_ilogb.S +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Public domain. - */ - -#include - -RCSID("$NetBSD: s_ilogb.S,v 1.5 1995/10/12 15:53:09 jtc Exp $") - -ENTRY(__ieee754_ilogb) - fldl 4(%esp) -/* I added the following ugly construct because ilogb(+-Inf) is - required to return INT_MAX in ISO C99. - -- jakub@redhat.com. */ - fxam /* Is NaN or +-Inf? */ - fstsw %ax - movb $0x45, %dh - andb %ah, %dh - cmpb $0x05, %dh - je 1f /* Is +-Inf, jump. */ - cmpb $0x40, %dh - je 2f /* Is +-0, jump. */ - - fxtract - pushl %eax - cfi_adjust_cfa_offset (4) - fstp %st - - fistpl (%esp) - fwait - popl %eax - cfi_adjust_cfa_offset (-4) - - ret - -1: fstp %st - movl $0x7fffffff, %eax - ret -2: fstp %st - movl $0x80000000, %eax /* FP_ILOGB0 */ - ret -END (__ieee754_ilogb) diff --git a/sysdeps/i386/fpu/e_ilogbf.S b/sysdeps/i386/fpu/e_ilogbf.S deleted file mode 100644 index 37298b9331..0000000000 --- a/sysdeps/i386/fpu/e_ilogbf.S +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Public domain. - */ - -#include - -RCSID("$NetBSD: s_ilogbf.S,v 1.4 1995/10/22 20:32:43 pk Exp $") - -ENTRY(__ieee754_ilogbf) - flds 4(%esp) -/* I added the following ugly construct because ilogb(+-Inf) is - required to return INT_MAX in ISO C99. - -- jakub@redhat.com. */ - fxam /* Is NaN or +-Inf? */ - fstsw %ax - movb $0x45, %dh - andb %ah, %dh - cmpb $0x05, %dh - je 1f /* Is +-Inf, jump. */ - cmpb $0x40, %dh - je 2f /* Is +-0, jump. */ - - fxtract - pushl %eax - cfi_adjust_cfa_offset (4) - fstp %st - - fistpl (%esp) - fwait - popl %eax - cfi_adjust_cfa_offset (-4) - - ret - -1: fstp %st - movl $0x7fffffff, %eax - ret -2: fstp %st - movl $0x80000000, %eax /* FP_ILOGB0 */ - ret -END (__ieee754_ilogbf) diff --git a/sysdeps/i386/fpu/w_ilogb.c b/sysdeps/i386/fpu/w_ilogb.c deleted file mode 100644 index 9c26217021..0000000000 --- a/sysdeps/i386/fpu/w_ilogb.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/i386/fpu/w_ilogbf.c b/sysdeps/i386/fpu/w_ilogbf.c deleted file mode 100644 index 047ad4bf11..0000000000 --- a/sysdeps/i386/fpu/w_ilogbf.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/i386/fpu/w_llogb.c b/sysdeps/i386/fpu/w_llogb.c deleted file mode 100644 index 5e8891a668..0000000000 --- a/sysdeps/i386/fpu/w_llogb.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/i386/fpu/w_llogbf.c b/sysdeps/i386/fpu/w_llogbf.c deleted file mode 100644 index edb7e9a9e6..0000000000 --- a/sysdeps/i386/fpu/w_llogbf.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include From patchwork Tue Apr 29 16:30:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885761 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp428826wrq; Tue, 29 Apr 2025 09:48:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXj1vJZNWWTe6pE/vTPduO7TLMzWBp6LtcmgqPacxcfAsIw7X6YSXFLKZqFjRtPA4ckpfqunA==@linaro.org X-Google-Smtp-Source: AGHT+IGqAkuwtJNS6xexjRdMf/ALagKp1QZ6GgfVKXU0Oy72z5LEchgzYKQ2t5RwjWF2WMgK5R+x X-Received: by 2002:ac8:5a41:0:b0:476:9847:7c6e with SMTP id d75a77b69052e-48131806b46mr215121321cf.19.1745945330854; Tue, 29 Apr 2025 09:48:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745945330; cv=pass; d=google.com; s=arc-20240605; b=Rp9QC7C4TaYHigMu1DcZcVrJih3zOYnYiKBryPZp9zI4yu+8kP4FPids36IejuW/gU AHNbCzCOFZt+9PkAvon+6w3/jmgAEZ8ok+kS8iyTrLZzhAPTqOm4y2SNtLtO/pED5gb/ XgTEhBLrkZ5u0B0BzNRHUD01RlL8mRNVgvTyzCeMulWnDWdXsZnUOodTU5MpybuA7UwU VpkL5eAXLX/Tqj+C5xSCYwHSQI8kngkSTIF8+onCilTpAUfOuoxLKyo2HKmQKqFgYzMV xb4dqFl1/a0kmr78xXaA22AFGbF0zfDRfeqmaQy80iFJoiaGiOX7M8IxDc0UhynSIvlX YNOQ== 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=zGNSlJP39M1RPnDh+109OnWxrYt16t+QNEiMMtXqH8k=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=EmcqfSQfGfkRroTkXO7+yrzpJ0Fh+799WrQ+gNe53rKzCeWfWQFKC6be7MuHnJS1FC P0AwYy7nkEW4707X/WmLHx6VLINNPAirXoZlPcut290TtXO2+TX8YnoYh68tR8+B6oT9 EK6rXedRsdHqXENb1/wIPbuRBFM9/uBQdzp7Rmy0McPjGHvSncc44JpZmiOtUHm1bv9S 5nnvVZfkuat+Ei+6WEsKEl8Y7Rha2X5MWgyF1Oz8yAfWMNIttT3DeE4Y+9TRuYTfbVl5 TBjoJuFvATWeF3vbSX3mrY4Y6xjCAE2OmvN7frK12oJdP4QIn72yqawHqN4fGORIZu59 UffA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HvKnwNKY; 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 d75a77b69052e-47ea26a52c4si128133971cf.571.2025.04.29.09.48.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:48:50 -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=HvKnwNKY; 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 674EC3858031 for ; Tue, 29 Apr 2025 16:48:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 674EC3858031 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=HvKnwNKY X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id 782C53858C50 for ; Tue, 29 Apr 2025 16:40:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 782C53858C50 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 782C53858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::529 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944825; cv=none; b=NZEapYEvU13NcyPeJm+R/uXeQ6FpvzZsUSMV4aESHbO9e1eeIzoz0TbTCbAwF6v5MDNaigo5VA6Y7lSJ79OMlAgaJMGuL4m4qvkrCLwNn7d9iUkKbUGcmDISdlS9ztkhoG6ByTQKl0hetkxxF1DfQRgAxiGyiOGoGLhHWx929DQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944825; c=relaxed/simple; bh=UPoQSJWYfoh7RMBhr8POkdWM7ToBWW2764WZVBax+Dw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=n6t3oJLY/GGdKjhj8NgiaD9xqHqLYgi54btP4OOGO/41Pv2OjIR7v6pnghtepM8nZKSXAyfkvGevkRtbfDso57gBzVXvH6MW73bWNgtV0XER4YByFV3B1AK5UuEFPFUM1AWrYRmOTFWw/Zo0NHPU9emsVkbEqFojJC3COEINSG8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 782C53858C50 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-b06e9bcc986so4850837a12.0 for ; Tue, 29 Apr 2025 09:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944824; x=1746549624; 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=zGNSlJP39M1RPnDh+109OnWxrYt16t+QNEiMMtXqH8k=; b=HvKnwNKYS5j3+Yn5kIW5+pJM+2gUwWQHb5nTihVVRUperzCQddubJn5soZNEG/8KWy mE0+Mnuau12/xZr3MMkhXXtGj3/gfLWiTNI07n6s9BVk2wMJj66y0NoZYC4/pm3eet+5 NuTopYwuEa3rQdnfI5BTHAHfQ7uunSJAo2o5Zw1chXS61i3rtjSUCFjymPiGpaFLu7hV WxeHh5YDHYJvuGKI/2Nst/ZBrYY5IvvNNJRczOozTaVwxiELiI/3l6BwfNBs/AQO86o9 KF/dyYQw/VhkaqY5JZkhRv2tsCTyUWad2oAS8RGtNYkEWWU96HvrEW8RKd3E6FEPELNx l/cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944824; x=1746549624; 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=zGNSlJP39M1RPnDh+109OnWxrYt16t+QNEiMMtXqH8k=; b=VRpTyyL5iHJyQmr/GtgPH5zPsefecgWw6DRXkZvFw3ahcnCpIvK9aCcz8CQGeLCXin KddHuyms4IqGMHd3qRqlOZ6v1qp0eoyin6voyJ1JnXYkwwzIND/825Vo/b8DmbaKxwrK as518PSJK+DwVebr4Oa0TqOX6Hc20Kiw+puCRWYk96MGVdAafWg6nNp0Fn30eO+LKzoc deDc9TYzztrxsZxbvdtY/6ccBxQE6Fw3lP8QwCQyWaYdDu1dZEdNyao4V8DKKGaImCIi HJaBTdSoSYW9Eyxk2omelBNYygAVqXLw+oaVNtniBvQkaLaJXfKRMFuLyxkTDBWqN+FL aN3A== X-Gm-Message-State: AOJu0YytB4TMSssTjD1L1w6tMk5NYyRVXWyvJKXyyBlg7kftnWJEjAIJ cLkwVvTJzBE4KjYZJFvLzfh9bcFqrLRxjFPGQIBWh2vxKZ4YNgS8FICt1q+FrtPC5j2Omup3S/n V X-Gm-Gg: ASbGncscmQv4u3Vd+nigTUZ6c0Z19I1t3ShgD46nzXoMz2ZiapHB2a+XGuwZDXtlx+P nwA18RH0UwCb8F6BzMXQEflhP+dlPUsP0FEguO0G5nOV8PwG7KYgeEh/9Q6xI4zcSEYGM7DDjWQ FCwL+GLILVPBnZ+IG10FMT7xh3wf04bERdii2nGoXuyh8QNZwpKDzC6A20Ekmc1nFG63Hkg2J5/ arzC0tU3iSrK5MHN6vD3rtTmQhFAEECP/0lIYaeAqgpHCYSFhmTOmr/A9Snl85myMSDCB/U+7DO sJDMcsmUTedruKV4F1HPOQIghlgYwnqycGj5iBqa1DqLq6Gq879vCg== X-Received: by 2002:a05:6a21:329b:b0:1f5:852a:dd81 with SMTP id adf61e73a8af0-2046a6c5a2dmr21349804637.34.1745944824048; Tue, 29 Apr 2025 09:40:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:23 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 6/6] powerpc: Use generic ilogb/ilogbf and refactor ilogbf128 Date: Tue, 29 Apr 2025 13:30:04 -0300 Message-ID: <20250429164007.2928271-7-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 powerpc64 leverages the use of xsxexpdp and xsxexpqp for for both ilogb/ilogbf for float, double, and float128 types. However with the new generic ilogb/ilogbf, this is not really a gain anymore. On POWER9 with gcc-13, the xsxexpdp/xsxexpqp shows: $ ./benchtests/bench-ilogb "ilogb": { "subnormal": { "duration": 5.08829e+08, "iterations": 4.4588e+07, "max": 18.761, "min": 6.7005, "mean": 11.4118 }, "normal": { "duration": 5.04674e+08, "iterations": 9.9596e+07, "max": 7.386, "min": 5.0505, "mean": 5.06722 } $ ./benchtests/bench-ilogbf "ilogbf": { "subnormal": { "duration": 5.04918e+08, "iterations": 9.8732e+07, "max": 7.1595, "min": 5.0475, "mean": 5.11402 }, "normal": { "duration": 5.04971e+08, "iterations": 9.8744e+07, "max": 7.771, "min": 5.048, "mean": 5.11394 } } While the new generic implementation shows: $ ./benchtests/bench-ilogb "ilogb": { "subnormal": { "duration": 5.05389e+08, "iterations": 9.2644e+07, "max": 11.0355, "min": 5.4255, "mean": 5.45517 }, "normal": { "duration": 5.04667e+08, "iterations": 1.02388e+08, "max": 9.758, "min": 4.8945, "mean": 4.92897 } }[azanella@cfarm135 powerpc64le-linux-gnu-power9-gcc13]$ ./benchtests/bench-ilogbf "ilogbf": { "subnormal": { "duration": 5.05409e+08, "iterations": 9.238e+07, "max": 7.69, "min": 5.442, "mean": 5.47098 }, "normal": { "duration": 5.0456e+08, "iterations": 1.02012e+08, "max": 6.84, "min": 4.922, "mean": 4.94609 } } The xsxexpdp/xsxexpqp also adds some extra code size overhead since it uses the generic ilogb/ilogbf for 0/inf/NaN handling. It is still kept for float128, and this patch also optimizes it to avoid need to call extra generic symbol to handle not number inputs. On same hardware (POWER9/gcc-13) it shows the improvement: * master "ilogbf128": { "subnormal": { "duration": 5.09608e+08, "iterations": 3.3092e+07, "max": 28.845, "min": 6.824, "mean": 15.3997 }, "normal": { "duration": 5.05148e+08, "iterations": 9.1692e+07, "max": 7.744, "min": 5.377, "mean": 5.50918 } } * patch: "ilogbf128": { "subnormal": { "duration": 5.0586e+08, "iterations": 8.388e+07, "max": 7.3295, "min": 5.952, "mean": 6.03076 }, "normal": { "duration": 5.04783e+08, "iterations": 9.6608e+07, "max": 8.9255, "min": 5.185, "mean": 5.22507 } } Checked on powerpc64le-linux-gnu and powerpc64le-linux-gnu targetting POWER8 and with --disable-multi-arch on POWER9. --- sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c | 41 ------------- sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c | 41 ------------- .../powerpc/powerpc64/le/fpu/e_ilogbf128.c | 1 + .../powerpc/powerpc64/le/fpu/e_llogbf128.c | 1 + .../powerpc64/le/fpu/multiarch/Makefile | 2 +- sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c | 2 - .../powerpc64/le/fpu/w_ilogb_template.c | 30 ---------- sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c | 2 - .../powerpc64/le/fpu/w_ilogbf128-impl.h | 57 +++++++++++++++++++ .../powerpc64/le/fpu/w_ilogbf128-power9.c | 1 + .../powerpc/powerpc64/le/fpu/w_ilogbf128.c | 51 +++++++++++++++++ sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c | 2 - sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c | 2 - .../powerpc64/le/fpu/w_llogbf128-power9.c | 1 + .../powerpc/powerpc64/le/fpu/w_llogbf128.c | 2 + 15 files changed, 115 insertions(+), 121 deletions(-) delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf128.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/e_llogbf128.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-impl.h create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-power9.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c delete mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128-power9.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128.c diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c deleted file mode 100644 index 89e7498266..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogb.c +++ /dev/null @@ -1,41 +0,0 @@ -/* 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; -} diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c deleted file mode 100644 index 1c2a8a5ade..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf.c +++ /dev/null @@ -1,41 +0,0 @@ -/* 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 "sysdeps/ieee754/flt-32/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; -} diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf128.c b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf128.c new file mode 100644 index 0000000000..4ec3582ae4 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/e_ilogbf128.c @@ -0,0 +1 @@ +/* Not needed, implemented at w_ilogbf128.c */ diff --git a/sysdeps/powerpc/powerpc64/le/fpu/e_llogbf128.c b/sysdeps/powerpc/powerpc64/le/fpu/e_llogbf128.c new file mode 100644 index 0000000000..163d9e2523 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/e_llogbf128.c @@ -0,0 +1 @@ +/* Not needed, implemented at w_llogbf128.c */ diff --git a/sysdeps/powerpc/powerpc64/le/fpu/multiarch/Makefile b/sysdeps/powerpc/powerpc64/le/fpu/multiarch/Makefile index 6d74f09912..72adc6463b 100644 --- a/sysdeps/powerpc/powerpc64/le/fpu/multiarch/Makefile +++ b/sysdeps/powerpc/powerpc64/le/fpu/multiarch/Makefile @@ -90,7 +90,7 @@ gen-libm-f128-ifunc-routines = \ e_acosf128 e_acoshf128 e_asinf128 e_atan2f128 e_atanhf128 e_coshf128 \ e_expf128 e_fmodf128 e_hypotf128 e_j0f128 e_j1f128 e_jnf128 \ e_lgammaf128_r e_logf128 e_log10f128 e_powf128 e_remainderf128 \ - e_sinhf128 e_sqrtf128 e_gammaf128_r e_ilogbf128 k_tanf128 s_asinhf128 \ + e_sinhf128 e_sqrtf128 e_gammaf128_r k_tanf128 s_asinhf128 \ s_atanf128 s_cbrtf128 s_ceilf128 s_cosf128 s_erff128 s_exp10m1f128 \ s_exp2m1f128 s_expm1f128 \ s_fabsf128 s_floorf128 s_log1pf128 s_logbf128 \ diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c deleted file mode 100644 index 9c26217021..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c deleted file mode 100644 index b5c1c0aa9d..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include -#include - -#if _GL_HAS_BUILTIN_ILOGB -int -M_DECL_FUNC (__ilogb) (FLOAT x) -{ - int r; - /* Check for exceptional cases. */ - if (! M_SUF(__builtin_test_dc_ilogb) (x, 0x7f)) - r = M_SUF (__builtin_ilogb) (x); - else - /* Fallback to the generic ilogb if x is NaN, Inf or subnormal. */ - r = M_SUF (__ieee754_ilogb) (x); - if (__builtin_expect (r == FP_ILOGB0, 0) - || __builtin_expect (r == FP_ILOGBNAN, 0) - || __builtin_expect (r == INT_MAX, 0)) - { - __set_errno (EDOM); - __feraiseexcept (FE_INVALID); - } - return r; -} -declare_mgen_alias (__ilogb, ilogb) -#else -#include -#endif diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c deleted file mode 100644 index 047ad4bf11..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-impl.h b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-impl.h new file mode 100644 index 0000000000..dff5331331 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-impl.h @@ -0,0 +1,57 @@ +/* Get integer exponent of a floating-point value. + Copyright (C) 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 + . */ + +/* Use the double version of EDOM/invalid operation handling. */ +#include "math_config.h" + +static RET_TYPE +llogb_nan_inf_subnormal (uint64_t hx, uint64_t lx) +{ + if (hx <= 0x0001000000000000ULL) + { + /* Zero or subnormal. */ + if ((hx | lx) == 0) + return RET_INVALID (RET_LOGB0); + /* Subnormal */ + if (hx == 0) + return -16431L - stdc_leading_zeros (lx); + else + return -16382L - stdc_leading_zeros (hx << 15); + } + return RET_INVALID (RET_LOGBNAN); +} + +static inline RET_TYPE +IMPL_NAME (FLOAT x) +{ + uint64_t hx, lx; + GET_FLOAT128_WORDS64 (hx, lx, x); + hx &= 0x7fffffffffffffffULL; + +#if _GL_HAS_BUILTIN_ILOGB + /* Check for exceptional cases. */ + if (__glibc_likely (! M_SUF(__builtin_test_dc_ilogb) (x, 0x7f))) + return M_SUF (__builtin_ilogb) (x); +#else + if (__glibc_likely (hx < 0x7fff000000000000ULL + && hx > 0x0001000000000000ULL)) + return (hx >> 48) - 0x3fff; +#endif + + return llogb_nan_inf_subnormal (hx, lx); +} diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-power9.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-power9.c new file mode 100644 index 0000000000..9de3f2101e --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128-power9.c @@ -0,0 +1 @@ +#include "w_ilogbf128.c" diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128.c new file mode 100644 index 0000000000..ae3e4a9e48 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbf128.c @@ -0,0 +1,51 @@ +/* Get integer exponent of a floating-point value. + Copyright (C) 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 + +#ifdef DEF_AS_LLOGBF128 +# define DECL_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 DECL_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 +#define __IMPL_NAME(x,y) x ## _ ## y +#define _IMPL_NAME(x,y) __IMPL_NAME(x,y) +#define IMPL_NAME _IMPL_NAME(FUNC_NAME, impl) +#include + +RET_TYPE +M_DECL_FUNC (DECL_NAME) (FLOAT x) +{ + return IMPL_NAME (x); +} +declare_mgen_alias (DECL_NAME, FUNC_NAME) diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c deleted file mode 100644 index 5e8891a668..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/w_llogb.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c deleted file mode 100644 index edb7e9a9e6..0000000000 --- a/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128-power9.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128-power9.c new file mode 100644 index 0000000000..9550611b95 --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128-power9.c @@ -0,0 +1 @@ +#include "w_llogbf128.c" diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128.c b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128.c new file mode 100644 index 0000000000..0a67251faa --- /dev/null +++ b/sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128.c @@ -0,0 +1,2 @@ +#define DEF_AS_LLOGBF128 +#include "w_ilogbf128.c"