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