From patchwork Mon Apr 28 17:03:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 885484 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp5290778wrs; Mon, 28 Apr 2025 10:07:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUvn9dpa/z8xqFGNDio8iT7ZMlgm73A04lbYvV9UAgVUoUqV5Yl0AE1hxebIg6FKhd+R5iP+g==@linaro.org X-Google-Smtp-Source: AGHT+IE7Q9opttJdi55M26B1tbIUE1Avda5MujuHfJP1zsbGZyrqIQVBNUTPbmtWdgd/Gwvx3a1P X-Received: by 2002:a05:6214:40b:b0:6e2:43d1:5fd0 with SMTP id 6a1803df08f44-6f4f06181fcmr9375666d6.31.1745860059319; Mon, 28 Apr 2025 10:07:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745860059; cv=pass; d=google.com; s=arc-20240605; b=i66HnWbNHOaum2UYGdT1O7s8xvlEQK+CfCDiGYyS9KZVFMZUdiDAfmzwRfEkPzjgif 7x+9Ktgk97HK5knBD/FwNnA67R5039HJSffz4dGvcTWY88gafLLRbuBWAuC+NQs76NT+ S1GpTZjkT1N9G5xVe9OnZ7bzsLnw/zQYeYxWmZaYe7mlgeJa8skl9vck3dBjEmy3RzzP KUS7cJ1XK1UCAgTvvn54C4f7DjTL+tyseoCrg+ppxm3wiMYU2xkXFFJdg1EWFDDoIECV XC06P+bhPhlH5LNyAK6MO4PFui15hj8KM9j0WDqZBB6JONz88fF9svDvYIHa8ZgAVxav H3jw== 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=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; fh=GVJhlWdd4NNaBhOZ/uQFycFAyzDMQer7CEL5IpLL3Iw=; b=ZI8f4oFltxZijAh+B1/sog0fz1bSQhtEO4IXWpmyT7Q6v33g8i5qn4TlgIyDQa+jaa OK8swMHzDP7ARLpCvkZ1erCadfDvctKSIFaNtDNZ1Rc5x4sCB/0KGFDA8GrEDijEgLAD CdTtzccQZuJHL0G8s/RdFgbZBaVlI+u5L7L/6fdEDcdlOhfCyBak8nbVN2y27SOEyfBn XRWDf/c9lyIAtO0L0wplMcCxdNC3maVcJzIFisFqqG8n0WGEnHkAMtVvLBDZGmwCeFwm bm2NLPRoCiRxeNuBzYRHxlMgcDLiDVKRWngc+6N7wwGDnHEjj0QJZlsyp2eenJjKXOiL Hjjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tXO1q2b8; 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 6a1803df08f44-6f4c0b213b2si101598326d6.495.2025.04.28.10.07.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:07:39 -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=tXO1q2b8; 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 CDDD73858CD9 for ; Mon, 28 Apr 2025 17:07:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDDD73858CD9 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=tXO1q2b8 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 937E93858C62 for ; Mon, 28 Apr 2025 17:04:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 937E93858C62 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 937E93858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1033 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859882; cv=none; b=qBnByDnmNYkNPzl9mRbWIZNTuLWOwMwV/cAeYwwo2lPrvl/a34Jimc+XgMq5fe+wfzhxX1Ch79fwOfIj+4qf2cOYZTJTetx5o302CcsoSBtiJ29CbOOl3UidSZhpJxa+7iC3FnoqH1nmMFoBbAFByUStsPVeckDxm7iteQ9IHIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745859882; c=relaxed/simple; bh=YTRoPPKATmf6KIIv2HhbsKRpeuPR3sk2QWKNKBfyQjU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cjRnDjfwQc2n2Xkt9F/NzHDeu+i00qTjYlJJlcgOJP+eS895SAFtTUceofKV5F2ae0Cggj0fqWy3N/gxZ0JAD09uW0nQpGSSOW4hg4Q9+70UGw/FiulcBUr96ElHlJuKv/Xjvh3B7TB2TLpiOQCw3uNyVTeBSqpsW+GLD7w9ypU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 937E93858C62 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-301918a4e3bso5711069a91.3 for ; Mon, 28 Apr 2025 10:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745859881; x=1746464681; 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=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; b=tXO1q2b8xpXD+1nlP/xbWy+32zc3+S43NHTfbIu0uxldk/OjIRHNfCNjW1JJo6aYc+ /84xDwv4hp1mRjJN1xLPe4ozCYHIcO3o1NyDwku1AWedpvGdioXpa6cRhf7i8X4PD3NG /cDjZSmJ8l17Jr7if2KvaJorESMZtbYeYz2fJrjZk7/ZDyQ1xxiTN4P04XxqZtynXInz GhBDLpBnSoiaOk3Jari3E180aSKL4qzr0aIknfI4uXfLFHfpUz2oSQpKQeRdxJQ3YI+O QVX3i6StDK6ujeBn9h14S3i7s6mDVUFLDxRUNaeeW1Qxog4NgOHrq2KaFZvmvOM66Syp 0caQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745859881; x=1746464681; 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=XzrhMR6eJ+CeEwBfabyFkb9AxZrMDRiQ9S8q2SMUiN4=; b=qPS8xkKwnQAMqdY2J+Dg6MCMcXNof20f/Y9Jb9Z0QEoonbQ35Bh5Yo192zLE0HM3Wq iuwjERwrpgrBqhef3Cp1gf9p1J30deW+ySygjEMOz7BXa5LCMFD8Tix+gvZvAd3EQ9io j7I1Vn36W1GuyU0K1fdLki5NVqlOW0YyJUf1nl6GwTzXLyxoTg8Z96uLu04iCAfUg2L4 3VZDPDZX+npZnZheH7WHUNoCPckfurNRKagHa+p2fO485FS/uTtgl2+EVp4OYsorgcBs nM/gTDG8nxJlh8q0g72+zTqTQ0aGxEE9Oa2FEMcHx/yvRGIxgp19qzLv1/PTl8vUccXj 95Aw== X-Gm-Message-State: AOJu0YyG4mvWtGOhk8b+F70wT2tqsuR0av0sI+3DX3Li2MmapifPCokB Ph81e18D0G5hx9b9VoLjZz2tsK3KkhC91pzW58LVHJXlKUslGNSYptily6cUFCdgYwm27HM+0Xv S X-Gm-Gg: ASbGncu1De4pkzpkMaWkWx06Z5oKyH7YUjwjFS1D7LsfWWyP3Zii7pJoevwC+kr686X 1IUTXDs92nljWrTxaKGbMEKmpmgoSaiRVKemzQJvlnTazTKn6otaXwduZpqAtV6pgJVanTF6fvn afciLQFimdh51ZMJQxAprh60qeimRhvXT96kSsso83uiJwx2dB5xHAhwDYDjBRHDKu6lmbS0jTB HUiFuejU7yCMXoy9TBxXsQMQSx06z+V7In3hrQ4vDmIfm0PY+8Nuw8qeHN+1vxj/UnIaW8bduyF 48APouHSqRWVIZAMOp2y07zVSG4LUG4l9pkV26elzgkfW8jQnljhDw== X-Received: by 2002:a17:90b:1dcb:b0:2f6:f32e:90ac with SMTP id 98e67ed59e1d1-30a21552b2dmr889935a91.11.1745859880719; Mon, 28 Apr 2025 10:04:40 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:ce18:36e8:dea9:8b39]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f773725csm8322332a91.3.2025.04.28.10.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 10:04:40 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: [PATCH v2 2/4] math: Optimize double ilogb/llogb Date: Mon, 28 Apr 2025 14:03:42 -0300 Message-ID: <20250428170430.2030400-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> References: <20250428170430.2030400-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org 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> Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/i386/fpu/math_err.c | 1 - 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.c | 63 ++++++++++++++++++++++++++++ sysdeps/ieee754/dbl-64/w_llogb.c | 2 + 6 files changed, 100 insertions(+), 42 deletions(-) delete mode 100644 sysdeps/i386/fpu/math_err.c create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c create mode 100644 sysdeps/ieee754/dbl-64/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/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.c b/sysdeps/ieee754/dbl-64/w_ilogb.c new file mode 100644 index 0000000000..8f9641be45 --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_ilogb.c @@ -0,0 +1,63 @@ +/* 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 +#include "math_config.h" + +#ifdef DEF_AS_LLOGB +# define IMPL_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 IMPL_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 + +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; +} +libm_alias_double (IMPL_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"