From patchwork Tue Apr 29 16:29:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 885755 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:38f:210b:807b with SMTP id g4csp425050wrq; Tue, 29 Apr 2025 09:41:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyZa/AQ81HRfjA43yLSpD6yP/5AsNNKcDx04+dJVKhf1/SJf7hbS/897O/uV1HVcMm/xf3BA==@linaro.org X-Google-Smtp-Source: AGHT+IHG1NukDdqYaPqFE8e4iO21tB72flXThmRRQPy1aT7UN5yBDgVVFqwYjo2RDtxWyTE6qANo X-Received: by 2002:a05:622a:19a7:b0:476:980c:10a8 with SMTP id d75a77b69052e-48131806a19mr253846371cf.21.1745944872244; Tue, 29 Apr 2025 09:41:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745944872; cv=pass; d=google.com; s=arc-20240605; b=hkwn2Os6NljSZcQucOp9Y8M5zlAQ3UTbLag2dF3515Wb+NVu25t4Ip0cah5BBAr7hy L4ZaomsOKxG3sNZSJLekIB8AaYwYysJc5IAk8hBkkrFz/C2RPcliyCQsgbjH1yBslAOO PaLS5BHgc+c9pw/IZnl27yJTFuIX/YD5UKpU1P3XMWwcIe9JwHtJkArZtwnOzptJEmvW jHU368jdn9ucufcRgTmzXPuceY6xmsuW4wxcj4Dp4v38abbcgVlLN6NUC1C3Uljoovzq QqT6tESGHQ/FzgQ3bVd8DTNu4+9LnwJB3peanJnVjhIWirEhQZa4gNw4w7xp8zkL0xHR EiVw== 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:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=Dojbj6jx2VzXR2KCJ/EJZfPE9hRclXDIoTLUg+GMjfE=; fh=QEQhASUpKkWwnOlb+v2n9KZRZHJ12Go9dzJxmOcjPvE=; b=Akib9sOSaeyTkSUV3sxgEWw1qLYh2n5Ds7DVCYZz6BNErP6TRO8+w1XUNqM17hRwdu RWAxC9IeI5zRECcacATWtPMLDOYz3iEdbK1twKMNMy/cWpbRQLUbFFz9nUN/+3H4UeZe c50KP7E7ZxvRiegfkvTzAqxCi7xeoACirG8dOAVWgZGyP2TXE/JDJOl8ho3oubC1hn8M 2pECfhV/HrhAg8VsdSpWSAIEk4dF73U/bXpr6o7a0Z/dJVaert1UHKmxGux3RcKrA5ip NEc1GmJVLo4nVTV2EMR1SJ27p/A+tjcHu6g73egO9Vrn3Nw7It9huDmGw4XN/rF02hcF 2DWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DR24YCVu; 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-47ea19871fesi128377331cf.309.2025.04.29.09.41.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:41: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=DR24YCVu; 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 CDB2E3858D3C for ; Tue, 29 Apr 2025 16:41:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDB2E3858D3C 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=DR24YCVu X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 2C5AC3858D26 for ; Tue, 29 Apr 2025 16:40:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C5AC3858D26 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 2C5AC3858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944814; cv=none; b=d/ffh3gNcu/OZG+UgmdxMLrdacl5GmhKvPg4CuFwkO+Tk2woYVM7OET9VI2n4pgKHwIK7R+5zipgaxkPhg35uZKaIaPGL133jno+1wY3UXpB2XuKZ5hoksGaQagUp1KJHg+Qxi3zTByEk2VuJSbdsEEkxwfNReLjxpfyD+e7CMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745944814; c=relaxed/simple; bh=mwbum/+dKV+QEMwinp0eQsX8I3JE7xVmcVp4ilqlL80=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TUeHj2FNYtPWHHkPSxNkXbTPi8zGIr2/0fSCicV7U/jCdIUfvMC2b7EK1C460nDdSJ0CUK/8x3PVXcwUXrH6/xgUDIUx3/C7OL8e1c3gY6i1QSBHPuwd/pqh+soqnKnjdQSDOJDO6lUYmSjpZ0DDDrEKImjMOKpbCxJXXrBhW3Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C5AC3858D26 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-72d3b48d2ffso5705161b3a.2 for ; Tue, 29 Apr 2025 09:40:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745944812; x=1746549612; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Dojbj6jx2VzXR2KCJ/EJZfPE9hRclXDIoTLUg+GMjfE=; b=DR24YCVu4dDp+b5Om9Iecm4Mm3DKklxwWjGnp4aQo+WP/5/8jLSo/NUH4KEh6q4hOY CdmMjUZm3uq+74CHcjSRoPuvKglekg6HtVNp7d06NdDul04Uz6g1KkbT75C6abRAZKxH ah1GiijXM09KAzq3IKSVLxCn1pSnoiTstr5PWmakbT2DvYUDQJOdyrqGjAHCJSozzTOc Yi8k6v9O2eUK7CzMvupqkbfzUgJpqBQ86DbHy5r9brpEooHI5pmW1HDX6EojfkLY1SNn ZAStfYKhnnePljA9cL3VKNoPpxBn7MNwud2WSvncFYGbAjnHT2jMT+PApB5PigG0jXtI CM9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745944812; x=1746549612; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Dojbj6jx2VzXR2KCJ/EJZfPE9hRclXDIoTLUg+GMjfE=; b=e2ae4//pLz9iIuPNP8rrUyImVNbhCS2L4I+kIUN9tmXYosezymDKYqTzkas7bZjOg2 P9ZtyQ4TQW39/PnRG5y9+Gytd8SMFTqqtkZ/qAwuae1GiTTJyke1dW3CEq3ddpY+E1Tg r6kxMNHQ8xoIGQr8iJg2cXVBIzAKMtfAvDcCsjrrpbe+JEB1Xiv8cCPb1Kk71M194kAx 8j+98YBekM6I8UIZUloRHX4+7uotzV6P9WgqkOJ9od4Em4+abred6zfnAMgd6JA8a4gL UpcHBgrWDZJIEpuepEyjQzGVmhZ+kyC0lMvB6U8A2ZEgXvqblWB5TPPYpR1AO3BL2Zp+ uB5g== X-Gm-Message-State: AOJu0YxVp0lVrDG2ULEgYiw95XOnwk6qtejZiVBHKt4ZPmsI2FfzbJ3H XbEjGwdSK6A70mHyem4xQ0b/oEYJ5njpVXwfTcOQv34h8eopCRjlkq0MJ3mgbXglR9LP04Skx1B W X-Gm-Gg: ASbGnctg6c3uLYah69PVkyxcd+GgNyy4Ou9612yXHE/HdEG0qmhixvqrcTG0sM92NIh t8N7hqEGSeoJbsLcR/TrbbONJVTuqsc8p9x5ewP5IoOM5QRCB/uwy+UVt5qpLYnZgtgITRmC2ya 5R6iEzNAY0XLP0wVdMZGmmdYpN71RszHsHgyzNoMlPJMRp1SonEapV7jz4o/TtpQXlsaEfuOCpj /py3kqae0EZnfIEmIhRP5yu28MCVkBFGZrJLXS9J/ZhzePu8vTRrkIY2iqmcj1pWlfIKKoNKhox pze3DAUltd7ErXTsY1aPMsL8oqENsSpZD+LCzKKqcowXHbj8b9AabQ== X-Received: by 2002:a05:6a00:2403:b0:736:34a2:8a20 with SMTP id d2e1a72fcca58-73ff7396a96mr19718333b3a.21.1745944811923; Tue, 29 Apr 2025 09:40:11 -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.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 09:40:11 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Xiaolin Tang , Peter Bergner Subject: [PATCH v3 0/6] Remove UB and optimize ilogbf/ilogb Date: Tue, 29 Apr 2025 13:29:58 -0300 Message-ID: <20250429164007.2928271-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 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 >From a recent experiment to build glibc with -fsanitize=undefined [1], I found that both the float and double ilogb triggered UB due to the use of shift with a signed integer. This patchset fixes the UB issues on both implementations by reimplementing them with slightly better code. Along with this patch, I also optimized the implementation to remove the need for the error handling wrapper and handle error cases inline (like recent newer implementations). These required considering the already-in-place optimizations and arch-specific implementations. For m68k I kept the implementations in place, while for i686 the old ones use x87 math instruction which seems to be slower on recent hardware (and thus these are removed in favor of the generic implementation). The loongarch was straightforward to adapt to keep the fclass.d usage. The powerpc64le xsxexpdp/xsxexpqp turned out to not be an improvement over the new generic implementation, at least for float and double. The last patch refactors the code to use only for float128, along with the wrapper removal (which improved performance a bit). [1] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/ubsan-undef Adhemerval Zanella (6): math: Remove UB and optimize double ilogb math: Optimize double ilogb/llogb math: Remove UB and optimize double ilogbf math: Optimize float ilogb/llogb math: Remove i386 ilogb/ilogbf/llogb/llogbf powerpc: Use generic ilogb/ilogbf and refactor ilogbf128 sysdeps/i386/fpu/e_ilogb.S | 41 ------------ sysdeps/i386/fpu/e_ilogbf.S | 41 ------------ sysdeps/i386/fpu/math_err.c | 1 - sysdeps/ieee754/dbl-64/e_ilogb.c | 64 +------------------ 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 + sysdeps/ieee754/flt-32/e_ilogbf.c | 44 +------------ 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 +----------- .../fpu/{e_ilogb.c => w_ilogb-impl.h} | 12 ++-- sysdeps/loongarch/fpu/w_ilogbf-impl.h | 37 +++++++++++ sysdeps/m68k/m680x0/fpu/math_err.c | 1 - sysdeps/m68k/m680x0/w_ilogb.c | 2 + sysdeps/m68k/m680x0/w_ilogbf.c | 2 + sysdeps/m68k/m680x0/w_llogb.c | 2 + sysdeps/m68k/m680x0/w_llogbf.c | 2 + .../powerpc/powerpc64/le/fpu/e_ilogbf128.c | 1 + .../powerpc/powerpc64/le/fpu/e_llogbf128.c | 1 + .../powerpc64/le/fpu/multiarch/Makefile | 2 +- .../powerpc64/le/fpu/w_ilogb_template.c | 30 --------- .../powerpc64/le/fpu/w_ilogbf128-impl.h | 57 +++++++++++++++++ .../powerpc64/le/fpu/w_ilogbf128-power9.c | 1 + .../powerpc/powerpc64/le/fpu/w_ilogbf128.c | 51 +++++++++++++++ .../powerpc64/le/fpu/w_llogbf128-power9.c | 1 + .../powerpc/powerpc64/le/fpu/w_llogbf128.c | 2 + 32 files changed, 421 insertions(+), 267 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/math_err.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 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 rename sysdeps/loongarch/fpu/{e_ilogb.c => w_ilogb-impl.h} (86%) create mode 100644 sysdeps/loongarch/fpu/w_ilogbf-impl.h 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_ilogbf.c create mode 100644 sysdeps/m68k/m680x0/w_llogb.c create mode 100644 sysdeps/m68k/m680x0/w_llogbf.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_template.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 create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128-power9.c create mode 100644 sysdeps/powerpc/powerpc64/le/fpu/w_llogbf128.c