From patchwork Fri Apr 25 20:44:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884426 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4157410wrs; Fri, 25 Apr 2025 13:54:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXEx5GrykeS01RXGurtmR7m3V1r9upOaL/Ip6bHptI4KwNe+T6bBA2Nje+zjOPbMay1B3nmkA==@linaro.org X-Google-Smtp-Source: AGHT+IG2U1AiYVjqoV1WXuqWLwCC7HJOK7xD6hbEukltDJitKBSnp6HCi6CtNYfNKlRymi1pUFis X-Received: by 2002:a05:6214:518a:b0:6e8:fcc9:a291 with SMTP id 6a1803df08f44-6f4d1f1a173mr18706676d6.23.1745614453018; Fri, 25 Apr 2025 13:54:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745614453; cv=pass; d=google.com; s=arc-20240605; b=hs6Pn0W5A4oUzocBH/4QYZHnFucebAcL879Lvx0FGQW9cugMC4B/Ur1kjTwVjC45t1 wZ90BvL8wJ/7w6QCW7gRTTsRecECCV86skfIvia0BjRT6twxi9+PcoPZmqGp9e0tiHA6 iWy0ho+AC+NoLvTkhlAiGQDHjvCPh2MKBwAkDwwA0mmQPapSYzBm+KJtG3NZWYFpwpjy EoEqOV4PjRQH1E2FEcWM2/+nxH35aO2jFIRI1L562Hy9tPOQLqBnh1IvkC32m+ePaJ3n zXSrxjGReuc7iaoW6fu7InEAA8Qplb6rrz28pCd+009Qg5Woxk1tuu2GP63GHbQm9IFL FVCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=Seds4+MBGg10lo84t7xp2Eeit4NKyicpdgysIWrA8fUiuM5i2MYEWv1VM2YQ1EuEy6 NKmgjz/nh5voR75BtIK7VH+R7JZg4oDTfBd51iXUAIhOBeaon22SW5e9x9ZaI7l92sFR czcU2flLu/pYZAD9K2nis19CTCAJCktLX4XOIlckqWv1lngO2/nkjxdZHWVZC3ejrjjX X86dMPVsAYOdZ8QNkP3xslOKW2SQCgFE6TyQVxSDJar7m3j+kHQksOeeeJ/0eCyOOguX xM+xouQeKj6Bau/f16hn9JBMH+6FfkzL6g0w5t8afG/10BK/SAlNn4vNzqygDWgDDKDG Ajkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=q7hYLiUi; 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-6f4c08d675csi47504196d6.3.2025.04.25.13.54.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:54:12 -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=q7hYLiUi; 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 949AC3858433 for ; Fri, 25 Apr 2025 20:54:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 949AC3858433 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=q7hYLiUi X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id 882DD3857B94 for ; Fri, 25 Apr 2025 20:53:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 882DD3857B94 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 882DD3857B94 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1036 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614396; cv=none; b=PKnYvg8InZpqiVszHelhopbRxX6HbRY+XyO/R3L9mhh0aJhxtcfp9XnUtE7EKCnmT1cr9u6B3+YBFAYSszeSP4i1evPwFoa9/PgxGyKeMYR7CpBCLyzj78ihcbnNzKkTAZ+SoJzGNp00L41ZxzSi5eJWS6ycoe8O+XjjQ3tWMys= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614396; c=relaxed/simple; bh=vm4GV3mSzgBQAlfatnEHYao32yPuNi9GGPmlnPW81mw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EWLug3WatasBP5SO9gpyQRQX6BHElNfrOKMVJJP6D+eC8yeA3phTAsaDxu67uIasKo0TmRFKtBd6mq239AhaH+fIVL1GCJo3SP0My7lwugRtpoCXIHIb84VuwScs/nO32XQYCg4oji7koOensPu5eKLt7HLfPpPTMUWV7jzi06g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 882DD3857B94 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-301e05b90caso2930686a91.2 for ; Fri, 25 Apr 2025 13:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745614395; x=1746219195; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; b=q7hYLiUiasOC92wR24f+f2j5m65Euk2SBwONlSUj2WqHbb8SIfH5B+WuyaUKHTgkFv WFXAKluaqi9/6QO3pH1WbWhVFwrslci47mGN5XizoZYceu8UT7geHSBre7tmbe8vMdAL ya3v2XJ3dsn9ctk78ux+9k6Ru2jZTGJKKQ1RZcKpXCsy5xjwgPzHNKS0bdCzwiJVIWML h6xKfJ/htV1XLk943Y4jXgB7jB3vjv9p716GyXloTGvXLKmKrrvGx/wNexlTc+0b9Tf6 b0fdk6P/BUqGNXV703T/NHguBlsjuIreem0Zqx8/E+E/WEhgw7oUPNDtJMEXbuhiKImc 5M5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745614395; x=1746219195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9qByV/z7W2+pndrAqQZUwvdFrIt7m8Hac+R4dpiQzc4=; b=TL7GDCLa0xKQlHDT/nZ+CJImI4ALC0hvV8BY2l4WdZJlKp56JVuJ24hmkmp0B9l7Hx boiAr5PIppd92ogCuxJs1BMh52BeN/XeMxMI1Lbift98A62vQfapCVQvc4MBXW/qw23Q yAXTG6ThVTHsT1HqQL6dVxjxV6lgJ9exz7jJj0zoYFaf9Dniup5dCNHFZTzMjyY+pAqd 5n9nT2h5cOvQ5mal0WoY9rU3b5ubLUbaqaklQymB5vzsWJsygbGDGkFrqIUT9HW+00+7 /bT3jh26/TC3Dqu/LigwjHoxqu/nuxrEhmRoD51wwMQa0mRn8+lzetW2ClKtBOK/uhGT werg== X-Gm-Message-State: AOJu0YyCx8mf2JmN7LyUCEATNxEtyx7SIQGPiCwtVnJCUlQVdxTSExgT cjylB6bbIWM0kIvzbUmgsvtE/k/hhGC4A8JgkuCduMu0ulgUo3NzfSWcPtNby0iSQNElmC7ZgyQ + X-Gm-Gg: ASbGncv/LbOxLWT0oLsLFJntzPWNxWMWhcUb7MgAtQvYcQ0a1+Go8vVpKEdWQfzaSVt utZJ0Hgmnmef5w8dYzl7W6UPHH1XWGJhtIy+qSJe6uANFib4+kpg3Ll/OBveqcEM2ALwZ3Ji77d YoQ5kBKHd6pqfZ4rNrQh/vDDfOX6h8X4/IO65H1I81ZegaYeVMwk5RWb9HqQkibHb72bjXo4eY/ 3FWMUtNfl2U1ivJGW9XqAYrQptqwiS5Q1+UpkeOO7ESjvHxs6Qjys6GrT9FRLFmUNIREuCvk3wg LKox2RvfkJD1eQr0vyJPirQBQL1yMk+8inPIsSW3cg4wMgwwxAxXkQ== X-Received: by 2002:a17:90b:586f:b0:2ee:ad18:b309 with SMTP id 98e67ed59e1d1-30a01300251mr1399026a91.3.1745614395047; Fri, 25 Apr 2025 13:53:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77371efsm2385260a91.9.2025.04.25.13.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:53:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 1/4] math: Remove UB and optimize double ilogb Date: Fri, 25 Apr 2025 17:44:07 -0300 Message-ID: <20250425205309.3866442-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> References: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org The subnormal exponent calculation invokes UB by left shifting the signed expoenent to find the first leading bit. The implementation also used 32 bits operations, which is 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 Fri Apr 25 20:44:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884431 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4158607wrs; Fri, 25 Apr 2025 13:58:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgGx3r9fJT87CWn9TA2/dw3sEleBB+a5Od8VSqSe6pvZMzvL1fU8nxMzvqR8mN/YACFtBAjg==@linaro.org X-Google-Smtp-Source: AGHT+IHx2Lh2k7X4KgYqGEi511WbWBOIJJ6TrfwvcmEvzuPlxwYfaLyqGzbYRgNC5rrxMkaqJKD9 X-Received: by 2002:a05:622a:178a:b0:477:1134:3ee with SMTP id d75a77b69052e-4801c1cc4e8mr68006551cf.4.1745614724941; Fri, 25 Apr 2025 13:58:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745614724; cv=pass; d=google.com; s=arc-20240605; b=Ecvmb0w799Uw4LrDYIo1xOWeyDNz1YR3OwbKgLPV9PjVsEHs86Ea6oSsm6AqFf45tx AExtuuHkIDxVRa8Y8BcmpwewXoc/1yriomXP8lmtBuFvUUtPDsF9RXON8Sn0NW7Zmyt7 MZTBcmc8tJGuIQ2pNC/PZ+CtXm5loDhQf2KGr2rkiWAA7MAbvPiZorjV2iUxDZIWw6Nh 0IGAqNwftidg4nI4U9WfRDscbQLhlyKpazHuq1vjoWA/yCMMHR0NdOsJ2s1p6zBDF4zb 11ENZLTj0qeGgFmDqqrIJhloMVlmfJB8/bpj6nbDukL/jiPGq+pKamgU8NZ1uT0FJ7oQ udow== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=8Z9EPWVIDQFJAUS4pfEVqhk6j/q3sAd88n4z+M/0XSM=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=IakhU0irPSKyWMsBwiVG0nNMVIE3o4zNteKi0tv8Y+yg5Lz5Yv6++jSJNIpKIjoHiq W58KnisiKbfiR2My62+jcB4JV4sK5uWlBzYMrvVp5Fv03bGZYNIUUNJ9ASjMQNqqa2xo EeO7a7GmGDZVbBxJdagwAp1hxQoS6uAvOxALLjraU9Q70bxS6S9ZyeSMZoH+B+zRXXoD N+NABeoKLCQv1eY2ir1lJF7QQWPGg1hVp9zOycNDnFuxKvHa8mZl1opiYieJjqh3+I7y vHFhvyIsojU4EMiTjYYRgY+w2Pg+alOFQDyYC2mW3GPc6tve1SFKVuQuk3F87PMNzeNW qtcg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="YaX/EIvg"; 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-47ea1e95dc3si46394691cf.371.2025.04.25.13.58.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:58:44 -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="YaX/EIvg"; 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 792C8385840F for ; Fri, 25 Apr 2025 20:58:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 792C8385840F 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=YaX/EIvg X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id A3FDE3858D38 for ; Fri, 25 Apr 2025 20:53:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A3FDE3858D38 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 A3FDE3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614397; cv=none; b=k/txYymQic/L7CHBHoGkPs7mq3If5UgeYF1FAmqcNXLQdZvSeXJHR5gG4ptR6o/mh6CC2lwPZZq0f5aOdM9fTxariCm8zuC/8RzjYcvMrcEFfn8K9KH2XN+FYsVZ0KVgWhnjALLiYwk8OB7vXhSqZqRpchjZSrSgNdxCnIbjP7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614397; c=relaxed/simple; bh=Jg2QpW4ri5d/T1T7m4edquh23vSCWHDM+G5yXOV3MFw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CAJdmkUJPBmV8LryZCt46q4dmY5ysXLOWzrEnUDW1pGLL1TdQUmSFx4G7WeGpMptUdnCsBiLG2Q5E5+BIIq8tXnV+fiv9xqF9dWBfKM70G8wd27eBSjnt5p9iRHuldu++riaf6g5eDJqkfZyZcfbfLkjUbC6b9X5EPXyokR+IQY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3FDE3858D38 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b0b2ce7cc81so2683285a12.3 for ; Fri, 25 Apr 2025 13:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745614396; x=1746219196; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=8Z9EPWVIDQFJAUS4pfEVqhk6j/q3sAd88n4z+M/0XSM=; b=YaX/EIvgGxnHjCDsrNph3hMHHb2egTCRGcK4u5dNNVmAQ0b5d8mh5mHMwsKKFtfZs8 Q79rErBLfwFSseMexJI15TIcDl964esTT6gU+iOD2g8HpP+UlypdpBh3DYKq8MlrEGE/ w0SH0HUBZBA51A98bA8EMNx6MEq/27VEga3xmgYuVSTCrP8SWBtvMnIvyzyM5UOrjOPQ rPTf6IiJNVAX6m4CMzsfRIlAvTmdybv5PboN/SQXEAIEN3NkfV6GU1RMvmhPaSfTyBS1 Y+r6cHFHa0/pLJoOJvyLYc+a3fVXio0LEs4TQUS3eKCHFeH76RphNlBewA292veYIWUU EpEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745614396; x=1746219196; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8Z9EPWVIDQFJAUS4pfEVqhk6j/q3sAd88n4z+M/0XSM=; b=LRXvm95zPAcgYeCM2Ed2q0WuUEVsowH/suofDOmJ+gQLApcvYCRiFm37bGfLF9ZMYf Af75JQtZcnMLppgoglC0dtI5JaEh4goHOT7F3R7kxd9ZoMRs2nGJxRaTd1ColPvOn1Bv XeytvAXYLEHgjYMq+EBeGnojmAyINFnThVCH7d8x/kSMSKjmGSnB/Kst3DzbQqZVwEmK xOIZ7C8nOzBKxOmHFHGGZMHhUObPAXbdrziGyXZEnOO73UD/iXBWoEFPYEn/ZaNTEMYV bct9wThY1uMYQ/q1j9VXuzLLLDzA5Nmq4c6C2K1SE3yVfdALWVhVafeL75TYLqfGRBD6 jx3g== X-Gm-Message-State: AOJu0YyAIvvMsMzzi7NV4P5KSdKtsueKbbV+EAMGTNY5c8jI5syCns3H tgP+KPGL1IuJQE1npa2APzjy+p+mAA/7w9ZkwAcTs4JrRypuaGWHWUWYFxaS6rwYC3XBNW2jYAB W X-Gm-Gg: ASbGncsTT+ifS4wZEu8ctoMisOIJiu6CIzVZAoyCWXKIFq1alsDH3NWrQNOlcplSUfR j1FJReszbNXdKfSc+VrUEzwfwCUNUJQXeZ4sXoioQfRTYgQKqgSlyO06B4UkmipxrITfmkOThGM 0MmBFf/lxtU47r1+tmDeDnWNBZGKW9pWdI+ts9/zNpW/JeGVoIRuIMK9NRJiuv4VQDlPlxC39A8 4iF8PNQRBAg0poDDwAXAQ5qda8kzubV6Re/KXMM2Us4lFyBgilEMZW65JkH1co1raBm+UM5AO5d AfgAhl1FxRsCQQ5tbHqSw2B0lPgWRRTy4ZrilTeSPFdQ8jEEpdrzkw== X-Received: by 2002:a17:90b:258f:b0:309:f407:5ad1 with SMTP id 98e67ed59e1d1-309f7dd8b16mr6230674a91.14.1745614396347; Fri, 25 Apr 2025 13:53:16 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77371efsm2385260a91.9.2025.04.25.13.53.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:53:15 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 2/4] math: Optimize double ilogb/llogb Date: Fri, 25 Apr 2025 17:44:08 -0300 Message-ID: <20250425205309.3866442-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> References: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org By removing the wrapper and setting FE_INVALID and errno inline. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/dbl-64/e_ilogb.c | 42 +------------------ sysdeps/ieee754/dbl-64/w_ilogb.c | 69 ++++++++++++++++++++++++++++++++ sysdeps/ieee754/dbl-64/w_llogb.c | 2 + 3 files changed, 72 insertions(+), 41 deletions(-) create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c create mode 100644 sysdeps/ieee754/dbl-64/w_llogb.c 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/w_ilogb.c b/sysdeps/ieee754/dbl-64/w_ilogb.c new file mode 100644 index 0000000000..45a59588c0 --- /dev/null +++ b/sysdeps/ieee754/dbl-64/w_ilogb.c @@ -0,0 +1,69 @@ +/* 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 +#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 +#endif + +static inline RET_TYPE +invalid_ret (RET_TYPE r) +{ + __set_errno (EDOM); + __feraiseexcept (FE_INVALID); + return r; +} + +RET_TYPE +IMPL_NAME (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 invalid_ret (RET_LOGB0); + /* subnormal */ + return (RET_TYPE)-1023 - stdc_leading_zeros (ux); + } + if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ + return invalid_ret (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" From patchwork Fri Apr 25 20:44:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884428 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4158301wrs; Fri, 25 Apr 2025 13:57:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+wUrKe4G/DwIDsqAHDgciB/2e7Al4xzmAO2g9tOAVPCCuYw/ngdQpSKMhDX20WqqUAa3EtA==@linaro.org X-Google-Smtp-Source: AGHT+IGWHI2RvMptPj4kQDdZsgSWlsTejQYKo8gds2SfKE4fP0XXIW9//BTmaWUaKvHskgoMMq75 X-Received: by 2002:a05:620a:40d2:b0:7c7:a494:bbfa with SMTP id af79cd13be357-7c9607ac8bdmr609593485a.52.1745614657835; Fri, 25 Apr 2025 13:57:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745614657; cv=pass; d=google.com; s=arc-20240605; b=HY9YDRrr6d1dPMWpAgVbEtlFLyvCHQEDgLfaEiaplmTnloOVV6DJXPWBoEOE2fEVDC yBST4kxTXd4InczfDZhS9X3N47nBbJL1EIEwbtUNAFt9LlOpqFqD80w+TyYLaq9QAg12 2bhZ7Xn/BYBsY/4On3WzSAeB3DJw8mAey9X0DwayesTHlmAWEE9a/a/AWF3ypAev5sv7 409OLu6ShsLNUUSdV1onFk1grzlr+PKArZTSAdm//eZAt7cD31UiUGRYTX9sw0g4UUqg 2aypAeamqGIRTbwjs/5sK57N7Vrck417sd3wUlyjwK3Love616AJ7yDx5I5B1MSeDCrT UOdg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=lICPiURykqbhuTQ1fOgv1l8TNFIII5JsC/bdbXeQbT449lDwYce+xIMmfUQuru/z3p jNK9+0j/gRp4DS50s1xUUkWlEfSAM+VvWsrSC6wzINxT+uKGT8PGUePvVwQ4mfBEne4J ZthdgXXBjgWq9ZOv5aMDq5ouPS6hPp4zvdsgAir0NN6MlIRf4W6EBAmtTdE9CDqM2rPB a4w2hQT2wwAwvXUjniMSt30lldUjWbDzsfstMoZ+wze0+vJw/8e+8Ro9zrFXnwhokmnW q0ZCd1xUfrB6XV8tlRDb6KyOYNohxC+mSwwTdLTgCjOagmC6hHHy6/PKCUBLNMy3eXY0 SCVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=flLBrP7l; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id af79cd13be357-7c958d85651si425596185a.222.2025.04.25.13.57.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:57:37 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=flLBrP7l; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 65F483858C60 for ; Fri, 25 Apr 2025 20:57:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65F483858C60 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=flLBrP7l X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id EC1CC3857B91 for ; Fri, 25 Apr 2025 20:53:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC1CC3857B91 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EC1CC3857B91 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1031 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614399; cv=none; b=wM9mFt2KSI6ptd5ZDqRgV9A4IfbUOe7aUfLJ4Au6ZcOO0RXWsEoE6QJTNd9siXoUDv7BV10nlQjAzgPA3zDtUU0871gsR2suCTPQhYuMLli25d5BrBSXWWt2Ul0coPKn9xqVCtoBqv+OSu1eoEQxoZPx5EU1USKei0yNllquT9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614399; c=relaxed/simple; bh=9PwBxR+/+391tENgbEoxHPTSKGxvxLfJLCcNQbEI9XY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=t2CIn83W/jtcNg0Ig6x0lA2DxMWUdsicXPVRWbCz1cXsM3UR3zZt1yQSKElNYi4zIc3mka2WYxnNBkhNWpfvLId1h1ICVCdFd5EubymxRCiqGUt5g7wYgoNetsvtmHY5xBx6UIizAWVNKa2P+zRxQSWeHa3wQ5sDIm65nFDjC1w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC1CC3857B91 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-303a66af07eso2138773a91.2 for ; Fri, 25 Apr 2025 13:53:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745614398; x=1746219198; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=flLBrP7lU4bFNjsxyLbOty4fmJs9N/WjBi38fegv1qJT7e6Dy+ef9eNFFnP8Jkve/M NWt9v8YLZo5zkQQjXOMXmB9AGvoVSixvJ5wMoAYAtm2Y02WBzqh2NzRgRbL8cXlbFJIk R+0uOw9lauyUz61/ip0SV9kUynH+Vg+76ay7vJeImfTssjciorUSM5xpwf3saE+vJ5Rz z7smgktBSHdqbndmfnB3B0Led8ouzohVq5UW99m1gUbyh29tGpWZ9lYJru4mCQt4BQ7v CAMn7HAGYLCD69j4LrKo3PQv9JJeJf5T2IS/3JvAalYvjRbis1Z3L3xpbMThcd292dlr JqQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745614398; x=1746219198; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zjjRatLomi/g78ekYupP+P/hOTL/dPeipld6JVB0F2o=; b=QUvn1OwjHzkNcuBnsZVcyHh1jZgVX+WC4IbrhRfjPbkoZp6Sxk1o00rUkOK7zCvXph jmUJv0MvZ58g59cFQlPntHPODoCoZB4y3zl4RqHUMAV+FWYE+gShRq65U3s2aLT9GSRA 7p2qkn2BIcoLeolQ8yiqIFn64G9MtR5CV2jchCsJMbeutcUI7oLDAcAA5ciTnsfzCUqT 9AZ2Z78b6JRL/X0LgnMStuq3pDNAPii5RShfVs+BMivCkW0stJVWuC+vB5CbDXHYawem MFLJSLSyGIGUHFD9RbIT24QdJpzGLWSlUvctdv3PzWMPRxoRGisqrxuOGkdO+lzfwW5A 05aA== X-Gm-Message-State: AOJu0YzusRlnhIjkkBe5sI9laQ9YB9KyrKIxP/egW1eZ0dTUFtJogKD+ 5DxFSN6d3OMcltqhEX46QdIz5lra+vuQmzL9vXVhQ3tS+LZ9/9k+3e5YBAnNZKbaIhasJICpJBz a X-Gm-Gg: ASbGncs6U40SypWfGC8tp2e9yL8I0UhG7TdOwie6oePBLvto0I4Tes7/IowohvkT/hj PzUH1JCiZWxJM7TnEfs6SQllG8pxSgtPGd8esmiO0vUx6U4ApXtucCmpSAhNpyUYxJySSvxoJZc G8r/avktoYjoeV9Ln6tOjg5uUCJYfQ2T8kfmwyF/tFU31NKOnqihcjB2s3jHogRMTgV7/AdwZn0 rGZjv/Rk7OdI2L+cmpCWqAaGAA/kphHuAMnGkJyxom/J0DnGxjCIgHFufkYW7Uk6VLGmEAUl88F vYqFjwqRNs4vTGWJ1UFIzQHy8mEaeqHM9eb8vZu+sq8oLRefgvIHPw== X-Received: by 2002:a17:90a:d888:b0:2ee:b4bf:2d06 with SMTP id 98e67ed59e1d1-309f7dfdd54mr4919435a91.19.1745614397620; Fri, 25 Apr 2025 13:53:17 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77371efsm2385260a91.9.2025.04.25.13.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:53:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/4] math: Remove UB and optimize double ilogbf Date: Fri, 25 Apr 2025 17:44:09 -0300 Message-ID: <20250425205309.3866442-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> References: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org The subnormal exponent calculation invokes UB by left shifting the signed expoenent to find the first leading bit. The patch reimplements ilogb using the math_config.h macros and uses the new stdbit function to simplify the subnormal handling. On aarch64 it generates better code: * master: 0000000000000000 <__ieee754_ilogbf>: 0: 1e260000 fmov w0, s0 4: 12007801 and w1, w0, #0x7fffffff 8: 72091c1f tst w0, #0x7f800000 c: 54000141 b.ne 34 <__ieee754_ilogbf+0x34> // b.any 10: 34000201 cbz w1, 50 <__ieee754_ilogbf+0x50> 14: 53185c21 lsl w1, w1, #8 18: 12800fa0 mov w0, #0xffffff82 // #-126 1c: d503201f nop 20: 531f7821 lsl w1, w1, #1 24: 51000400 sub w0, w0, #0x1 28: 7100003f cmp w1, #0x0 2c: 54ffffac b.gt 20 <__ieee754_ilogbf+0x20> 30: d65f03c0 ret 34: 13177c20 asr w0, w1, #23 38: 12b01002 mov w2, #0x7f7fffff // #2139095039 3c: 5101fc00 sub w0, w0, #0x7f 40: 6b02003f cmp w1, w2 44: 12b00001 mov w1, #0x7fffffff // #2147483647 48: 1a819000 csel w0, w0, w1, ls // ls = plast 4c: d65f03c0 ret 50: 320107e0 mov w0, #0x80000001 // #-2147483647 54: d65f03c0 ret * patch: 0000000000000000 <__ieee754_ilogbf>: 0: 1e260001 fmov w1, s0 4: d3577820 ubfx x0, x1, #23, #8 8: 350000e0 cbnz w0, 24 <__ieee754_ilogbf+0x24> c: 53175821 lsl w1, w1, #9 10: 34000141 cbz w1, 38 <__ieee754_ilogbf+0x38> 14: 5ac01021 clz w1, w1 18: 12800fc0 mov w0, #0xffffff81 // #-127 1c: 4b010000 sub w0, w0, w1 20: d65f03c0 ret 24: 7103fc1f cmp w0, #0xff 28: 5101fc00 sub w0, w0, #0x7f 2c: 12b00001 mov w1, #0x7fffffff // #2147483647 30: 1a811000 csel w0, w0, w1, ne // ne = any 34: d65f03c0 ret 38: 320107e0 mov w0, #0x80000001 // #-2147483647 3c: d65f03c0 ret Other architecture with support for stdc_leading_zeros and/or __builtin_clzll should have similar improvements. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/e_ilogbf.c | 68 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/sysdeps/ieee754/flt-32/e_ilogbf.c b/sysdeps/ieee754/flt-32/e_ilogbf.c index db24012eb4..024b114638 100644 --- a/sysdeps/ieee754/flt-32/e_ilogbf.c +++ b/sysdeps/ieee754/flt-32/e_ilogbf.c @@ -1,43 +1,41 @@ -/* s_ilogbf.c -- float version of s_ilogb.c. - */ +/* Get integer exponent of a floating-point value. + Copyright (C) 1999-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. -/* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. - * - * Developed at SunPro, a Sun Microsystems, Inc. business. - * Permission to use, copy, modify, and distribute this - * software is freely granted, provided that this notice - * is preserved. - * ==================================================== - */ + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. -#if defined(LIBM_SCCS) && !defined(lint) -static char rcsid[] = "$NetBSD: s_ilogbf.c,v 1.4 1995/05/10 20:47:31 jtc Exp $"; -#endif + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ #include #include -#include +#include +#include "math_config.h" -int __ieee754_ilogbf(float x) +int +__ieee754_ilogbf (float x) { - int32_t hx,ix; - - GET_FLOAT_WORD(hx,x); - hx &= 0x7fffffff; - if(hx<0x00800000) { - if(hx==0) - return FP_ILOGB0; /* ilogb(0) = FP_ILOGB0 */ - else /* subnormal x */ - for (ix = -126,hx<<=8; hx>0; hx<<=1) ix -=1; - return ix; - } - else if (hx<0x7f800000) return (hx>>23)-127; - else if (FP_ILOGBNAN != INT_MAX) { - /* ISO C99 requires ilogbf(+-Inf) == INT_MAX. */ - if (hx==0x7f800000) - return INT_MAX; - } - return FP_ILOGBNAN; + uint32_t ux = asuint (x); + int ex = (ux & ~SIGN_MASK) >> MANTISSA_WIDTH; + if (ex == 0) /* zero or subnormal */ + { + /* Clear sign and exponent. */ + ux <<= 1 + EXPONENT_WIDTH; + if (ux == 0) + return FP_ILOGB0; + /* sbunormal */ + return -127 - stdc_leading_zeros (ux); + } + if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ + return ux << (1 + EXPONENT_WIDTH) ? FP_ILOGBNAN : INT_MAX; + return ex - 127; } From patchwork Fri Apr 25 20:44:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884433 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4160445wrs; Fri, 25 Apr 2025 14:02:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU/0T1k97mevuL4PNm5A8o6slYiS7xlrJVR1WrR/n3/gZ0Aq8EUZ/t9a/bnxNUGZK/bJqPoPA==@linaro.org X-Google-Smtp-Source: AGHT+IEYmNnHYxHmrWpOj9EBpErzlb0F9p8V1mI3UrwVs8cYt5wnoZchZ+CwjwwGToGrnFXMzJEn X-Received: by 2002:a05:620a:44d6:b0:7c9:443b:84e9 with SMTP id af79cd13be357-7c966863abfmr165409085a.3.1745614963474; Fri, 25 Apr 2025 14:02:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745614963; cv=pass; d=google.com; s=arc-20240605; b=PN++ycFUeXUjZERL4t5RpxIMWeZl/TsdCind26+d/qmZwCtVRW0TZ2qxSfAflGiiu7 BZtyDkJqfYGVQM7bh0KAORbYtcLv7J3n/umUIHf2WgI6bMsnKhWzMCv164oQNERtGYS0 rWdZkv2RsyKoFPZ97QZLCrmIp1yopu1Xdd65w+c6Rel0Nxwu0IQLt1BzzAZEoLr1xLk9 m/xzDNFtfExt6TRTKYv5h+kq9Qf3iQYa4egTYFL9PDm9FwZCMm3pv4cjS4j2AC2IePwW 8NKNwKqmfNqtSpdbb6Vg1X5ks7MuTbAyTMkysBiVYujAUnt2tq9DCIDSYIdY4xlOiVap Dozw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=DL6X6wJeQeN9fQLlMuT5plFpq0oUcSpw/523iWeL9yU=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=aSr7MjHtfqnHHF8hXt1mffe3MXa9rczMHem4UsDpgTBM+lC4qvnw2BrIun/BZ/lDNn zsGfX/rKHkqjsuLI7cr1987wu/vx+TTpbQ7srSBPwjSR9pQLg4zs9Fo5pIBtx2vKg1Ii LAnlP1lFgHhtQIWYQDTVWZUUdoTbiKZzafPJdiT/2W3CDbFE/fDPZETBg97AmVmDt2km sNnxAYaIVTo8afpVk0OpM2JId/xfVwgYtS0HlNSs7Lv4+oDAr/gZGi+Xi6sQ5AKsD1tb q9mxAFtFaSZCf61PGCTUvaluCOVtrkDJzrKBpBsq8R85UA8bCq3IYBjkEMk5aGuf4iyV YkKw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hNRqo9Mn; 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 ada2fe7eead31-4d3d71d6762si1391260137.393.2025.04.25.14.02.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 14:02:43 -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=hNRqo9Mn; 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 0A7AE3857836 for ; Fri, 25 Apr 2025 21:02:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A7AE3857836 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=hNRqo9Mn X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by sourceware.org (Postfix) with ESMTPS id 594AA385840F for ; Fri, 25 Apr 2025 20:53:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 594AA385840F 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 594AA385840F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614400; cv=none; b=XEMhKv4UvcDfHIG8lBOyHR8HYfhbsAVgvw3pogg2cHkjf0bAkMomvpoJofvdosLw/iShmSZd6YTfxUQAsHXl/MMFEtd6qAxzf25Kj0QNlQw14Tc+eW1/19MYo+9bWgl4w9mLnYoc389i/WzLGEwCatrhTWUaHafNPDnJCEMvJf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745614400; c=relaxed/simple; bh=9FV7UechfvkoAWH6AeVI25rzdIZ64jDAMUZUWk1T76I=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z/W/crQ8+zcIgkb9r6Bx3SYBaeNu/1b+PUbhp287a3wPPG3U6IpTLKUO8Ca9Iov9AQkGQMlg/Sv5ZVR6WFis4EJD8TW/6qg7S4AD+5moeW+G2Ui00GyzKEJDA4lr67T63bNY45DZd7tgKCKc+L9iRzJ1ABY6mmjP8vebZRMdRZg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 594AA385840F Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-3012a0c8496so2190730a91.2 for ; Fri, 25 Apr 2025 13:53:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745614399; x=1746219199; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=DL6X6wJeQeN9fQLlMuT5plFpq0oUcSpw/523iWeL9yU=; b=hNRqo9Mn886WQMu06R3qEL/dpz6dMqUfid8TJ8a5tEHv89I81CrQkrFZ0ffQpTA/el cT/ONqACC/OpPUtqsLL2BV4YkzgCfH6a9cVj11cgFR1yG8+t0A5bG5E+MqzV9t7HyDMo hMkv5pKnb+HIgx5GdbFUn2WRiFDi1MB7CCUjWYBSR4rWZIQfrIF9A3pKFW3FIIb7EwLZ H2Wrw1U8AgvElr48bEzVAKXekBtVFtwmqFmIaUeU/OsxmWBuW/eSutQ2ixfxhzX0mRm4 hDU/zbXlW/C7uuwDjBVGzC+JafJlu/+FoDKhFMVrOdSvRMYqPyJn9g5xf2LOMMjpWgkD x9TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745614399; x=1746219199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DL6X6wJeQeN9fQLlMuT5plFpq0oUcSpw/523iWeL9yU=; b=sKb0sx4X0c11/U3gEr5kZGP8KeGCkofRh6RadwC6mlVj0al1oNc5SOz94YdMeYsUlH 55NxWdgFbe3vWDnJYj9cK7Nbg5sOKqIfPW7Fr7o23Bdx3rF38HfucwePoqv008OL4FZg zMXQqbFtUBH/TQcIbBwLfVa2IzNhy7oJ8weLqiPuda63vuuPukKCzfDPyDsb6BuGtZkI VsbT2rgAk9Qg3KtEdnAv5cY6wuCe4DMQt7lZ/6bQAifjHFh9Pnr0o4rAmHzJ7Kl0Hkvb Jr9mloAItBElh3f9kaHlYRVcZDj8o3D6sCJ1+82okAtG4iG/4ISD7p8b8uDywpmnk3d/ Yhhg== X-Gm-Message-State: AOJu0Yyrmw2rZOIC2YF9wyM8MlUBtJYv2g+bCgyf4xuc9W7ahYTmtVW1 AmIXeJHrqDmki9Gkrg8NSuDnl1ZGJRvtjAPLI7ajyftE8urX1/AUXGH2leokKZbSZkd7DOjPJ5I O X-Gm-Gg: ASbGnctxDW/gNEdrhKX355S1LStiM+OmL+nAdKI4khiQ9fFWfYcRbnQv35bINnwrszZ J1CCY9RZkCwu9A9N4YKj0tA+wsQNcdv824oiVHQRXfLJgedblMTUyRvKASYLcdFGuKrr+sp+CvT Tx0jXIZCWYTtxlrYEvTWQ1tTzcdcWjlntz+j4AzqeaT4aAg8loBTRdoazySCLaTuIYnY1akh7C/ xHqGSS4o9aWMsB7FgXXfgVzUPYzJIxolHGXsZ2T7DZ0Kf+Ks16NH1ABnZCMMwi2Ms2TBzzDm/e/ ipf5ssWulixqrUtaSvonsphnJu+3dqzpoNnfmEP51PSVYwX0G3x8hg== X-Received: by 2002:a17:90b:270b:b0:2f7:4cce:ae37 with SMTP id 98e67ed59e1d1-30a01339f6fmr1513403a91.18.1745614398931; Fri, 25 Apr 2025 13:53:18 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77371efsm2385260a91.9.2025.04.25.13.53.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:53:18 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 4/4] math: Optimize float ilogb/llogb Date: Fri, 25 Apr 2025 17:44:10 -0300 Message-ID: <20250425205309.3866442-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> References: <20250425205309.3866442-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org By removing the wrapper and setting FE_INVALID and errno inline. Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/e_ilogbf.c | 42 +------------------ sysdeps/ieee754/flt-32/w_ilogbf.c | 70 +++++++++++++++++++++++++++++++ sysdeps/ieee754/flt-32/w_llogbf.c | 2 + 3 files changed, 73 insertions(+), 41 deletions(-) create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf.c create mode 100644 sysdeps/ieee754/flt-32/w_llogbf.c 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/w_ilogbf.c b/sysdeps/ieee754/flt-32/w_ilogbf.c new file mode 100644 index 0000000000..21e40c7721 --- /dev/null +++ b/sysdeps/ieee754/flt-32/w_ilogbf.c @@ -0,0 +1,70 @@ +/* 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 +#include "math_config.h" + +#ifdef DEF_AS_LLOGBF +# 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 +#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 +#endif + +static inline RET_TYPE +invalid_ret (RET_TYPE r) +{ + __set_errno (EDOM); + __feraiseexcept (FE_INVALID); + return r; +} + +RET_TYPE +M_DECL_FUNC (IMPL_NAME) (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 invalid_ret (RET_LOGB0); + /* sbunormal */ + return (RET_TYPE)-127 - stdc_leading_zeros (ux); + } + if (ex == EXPONENT_MASK >> MANTISSA_WIDTH) /* NaN or Inf */ + return invalid_ret (ux << (1 + EXPONENT_WIDTH) ? RET_LOGBNAN : RET_LOGMAX); + return ex - 127; +} +libm_alias_float (IMPL_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"