From patchwork Thu Aug 13 14:23:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Zhu X-Patchwork-Id: 141320 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4F93D4BAE7DA for ; Thu, 13 Aug 2026 14:24:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F93D4BAE7DA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=WZgGlp1Y X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id AA7894B9DB49 for ; Thu, 13 Aug 2026 14:23:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA7894B9DB49 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AA7894B9DB49 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631025; cv=none; b=PUNt+HGcphbhtQgEqml8LBe+3EOVVtObg9KvJI+2v9cCdrZ5dM3r5/I0o2oTda4fFxhwq6jVPH3U7bExCZjv/LvtXO7arWN8GN9KyOXuvuJnQDbpSJkw/Z9mozFvYXsCZBY4nDHsY1NoMzwLWlAdkasKJ/gS3Q3msXji/Fw80uA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631025; c=relaxed/simple; bh=DcvNDsjwbxZo0K/BWLjalWSFIWeahuleFbhU0J4SssI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=giCLI+TkLAS/+xuloHwiaaRwszZ9B/ruW47IruQfpIwkLeqcOa14wH5ncfhb4bNBzvx7ongV2mp4l+PEY+elm4Y2YTqKQwWi25PgQ7wQTjhNm/9cvCbZIbgOC+L10Dq0rza/qD4QhbbN+dF6UGh2OU/Kx7LaOC92AAlSVni0xSw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=WZgGlp1Y DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA7894B9DB49 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-493b966dd74so5399565e9.3 for ; Thu, 13 Aug 2026 07:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786631024; x=1787235824; 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:content-type; bh=5gahzVSYWqD1/5rfHYk5YjmHg2Nx6uL4and1yMfirtQ=; b=WZgGlp1YPs8x2DaDw8vU55B1RfsNTfkph4dAOmwOQSSkX+eVsZl0w7yhxEkTSTc2oN kiReLMWvn7iEXapooVDcXkDJcjCqOau+CuJJ4HXcvTWpM2+Zcke212Sb64EB9JX1AyBf AHUrKIUMNuiBKzdJjBKAMiXFDXIo/ytaf98/83T8sx3pjF6g/fDQN67oEi+Ls2tHWVyS Qc8N5NNKazBr9wu1VYuL09p/B7L45/bbH5cRQ7XLJ689h+lplfNMx+zH+06pqYNtvH2b FHkir9QWDBzzDNADKW0gOO02NP4kAO2SKEvWK8NKG1vPHN/FQyT8/a+v5KF5lqSXOh1m gDWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786631024; x=1787235824; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=5gahzVSYWqD1/5rfHYk5YjmHg2Nx6uL4and1yMfirtQ=; b=ffDeYYYIvXjExT79e81YWW7Wi6BdJ52g4kI7s8ajj0gzfaH0lyzuMKHjOBBOyuQtAw ovvKdM3ftwwbCLvhBZpVgfaKZQXlFyaa450w2nslE+7rnXxxfollRMYgpMkSmyIsMmuS yv2BMxcqlSpqx7JI4EIanlKsWa7306DNU+r/nCTfAB0ieXdchvKt82g98I7KMwCKdfoX GR5PjjW4KFdG3o88e0KikKaSKzQOV7FTTG0MM9CBylMf4fPSuGHS012s1Gsdrmnfh3sk zLl/id6PKR4yp31QX2vvNC9hggjiav7hRFY1ZPs4ibAYhSe124xsIJKVmwuzlN+2ZbvT Ic7Q== X-Gm-Message-State: AOJu0YymD3U1u6lY+DtreOxUd9LbvEFRdsYUnbQA+N/il+n7kwXRjjXo 6frG08zVzLjyDyFGdWcHy/ezuYsEf4S4Hox5nQ5tjLiWYzh+VOJ41CgVGNxOJwEapSQMOg== X-Gm-Gg: AR+sD12krNmThTJIMr/jfHGMwnPmRVoZpQFMb1vdaz6L9MIi6jOD2WTI7NnHoxW0vHj UFkPTOzv/rZdQRZeeZ/kc8JO6aEc1b+kMSKEvpKSAOHTj9cfmRMs5S8wxhyU0tMVFdMrjJ0ipr8 3OjJ8ZvmqvCnktTKbCEWNkjcWoCQSbergTEJb3aelrWmizQQnUnRkchItlqbVHg7BrENt1l9HXB S2Agk3DjqCRBINUObAUeMeDNx0jvFn9E2siXhcGaH8MYx9cIY3FZtAHS0oop/tGE/uDOBgthIVV VVLv+ZIs/qf6HinXiMIj0IezY5rrFb7JeXiWIHg4haY0GKuw7qXa8eeazRuSy9q7vcl9gsYTjLb fcJnLdpYEzsSRqCp8oO5+3SnrYhZnsuQjbzjN4i/c6L2jluT+LkFooVOH6A7OTL8xU5mdt9274U hBXYnYuSqxszTuoE1TAqt4EJSe8Z/vk5bnnfXwiwEq X-Received: by 2002:a05:600c:4e87:b0:499:79b9:e226 with SMTP id 5b1f17b1804b1-4998218496amr70317665e9.0.1786631024062; Thu, 13 Aug 2026 07:23:44 -0700 (PDT) Received: from localhost ([2a09:bac1:1e00:8::1d6:189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4998212a3a1sm68430035e9.6.2026.08.13.07.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 07:23:42 -0700 (PDT) From: Julian Zhu To: libc-alpha@sourceware.org Cc: Julian Zhu Subject: [PATCH v2 1/4] riscv: Use builtins for the fmaximum and fminimum Date: Thu, 13 Aug 2026 22:23:17 +0800 Message-ID: <20260813142320.3617764-1-jz531210@gmail.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GB_FREEMAIL_NUM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org 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~patchwork=sourceware.org@sourceware.org Enable the fmax and fmin builtins for the fmaximum{_num} and fminimum{_num}. The generic fmaximum{_num} and fminimum{_num} use them for ordered operands, on rv64gc the double-precision functions drop from 21, 26, 23 and 29 instructions to 8, 8, 14 and 14. fmax and fmin keep their own implementations, which need a signaling NaN check because the instruction returns the number where C requires a quiet NaN. The headers live in rvd because without floating-point registers GCC lowers the builtins to a call, which would recurse. Signed-off-by: Julian Zhu --- sysdeps/riscv/rvd/math-use-builtins-fmax.h | 15 +++++++++++++++ sysdeps/riscv/rvd/math-use-builtins-fmin.h | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 sysdeps/riscv/rvd/math-use-builtins-fmax.h create mode 100644 sysdeps/riscv/rvd/math-use-builtins-fmin.h diff --git a/sysdeps/riscv/rvd/math-use-builtins-fmax.h b/sysdeps/riscv/rvd/math-use-builtins-fmax.h new file mode 100644 index 0000000000..8865377475 --- /dev/null +++ b/sysdeps/riscv/rvd/math-use-builtins-fmax.h @@ -0,0 +1,15 @@ +/* This does not affect fmax and fmaxf, which have their own + implementations in rvd and rvf: the instruction returns the number for + a signaling NaN operand where C requires a quiet NaN, so they need an + explicit check. Only the fmaximum and fminimum families and hypot use + these builtins. */ +#if __GNUC_PREREQ (13, 0) +# define USE_FMAX_BUILTIN 1 +# define USE_FMAXF_BUILTIN 1 +#else +# define USE_FMAX_BUILTIN 0 +# define USE_FMAXF_BUILTIN 0 +#endif + +#define USE_FMAXL_BUILTIN 0 +#define USE_FMAXF128_BUILTIN 0 diff --git a/sysdeps/riscv/rvd/math-use-builtins-fmin.h b/sysdeps/riscv/rvd/math-use-builtins-fmin.h new file mode 100644 index 0000000000..06e46bf7dd --- /dev/null +++ b/sysdeps/riscv/rvd/math-use-builtins-fmin.h @@ -0,0 +1,15 @@ +/* This does not affect fmin and fminf, which have their own + implementations in rvd and rvf: the instruction returns the number for + a signaling NaN operand where C requires a quiet NaN, so they need an + explicit check. Only the fmaximum and fminimum families and hypot use + these builtins. */ +#if __GNUC_PREREQ (13, 0) +# define USE_FMIN_BUILTIN 1 +# define USE_FMINF_BUILTIN 1 +#else +# define USE_FMIN_BUILTIN 0 +# define USE_FMINF_BUILTIN 0 +#endif + +#define USE_FMINL_BUILTIN 0 +#define USE_FMINF128_BUILTIN 0 From patchwork Thu Aug 13 14:23:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Zhu X-Patchwork-Id: 141322 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 247534BA9032 for ; Thu, 13 Aug 2026 14:25:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 247534BA9032 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=M1fkgZzL X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id 6506F4BA902C for ; Thu, 13 Aug 2026 14:23:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6506F4BA902C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6506F4BA902C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::330 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631028; cv=none; b=hCK4zDNRKkCuHXP8W74FUPyfziPGpWGkBZDMYsKospQSWCsNhMD7rm2Cwe6+Yu6x1cIVqJ74gtWpllHjbiLANi5ZZMEI2oPFYOcPuOcKEYDHRistBX2C0rQHwuS2LLAxfEofSfr+KWedcZBARQi4dv5qWnv77zcXL5KAHRKnQGg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631028; c=relaxed/simple; bh=yZYUktUWHN02OHlxldr/J3jqOqOCUaFeTptqRpEjsDE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=j+f/Ou8G8mnpNou1x49RlpjTqVPxRQa+m5KgdsFDMFvALB3hSWUBMKH3gcdchoM/YK5tO4ns9woK1NFg5zsUj83T8lIML8lfKcUD4xAsD1P3LcY0RKVVIdOuNp93WFBWq3VqMx9BWG3xSAYqb7PuH2QYlCtzqqOHJzZsf+VrHtM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=M1fkgZzL DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6506F4BA902C Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4953de5be0aso212095e9.0 for ; Thu, 13 Aug 2026 07:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786631027; x=1787235827; 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:content-type; bh=FEJbq9EATjwJ0ES2QiMVKbBHcW8RUWVhYzlW91qnYP8=; b=M1fkgZzLYaWN02ALDx/+qaLWtRD5+yJeUVsfZZWV0JJlMIl7rhpsbbZ2U9Kk5cThFx rlOtehjwezKRXFhTg7FbVzlgdSXQp6c2JWcN09yhJXpS41gVRFK2uiMPwG1kacXKoMH3 1iF6yvo6w9LKOsk5BiGNdVVaBJsOCsUc5xW7GUL9VSYe3k7Vjb10IxjLgUBgAIhIfNaT b50EVqoSh9A+VXh+ELBRr3uYvQfKklqYAOiVH3SjWte+XBNv9qz+Gmltgcl+JED5vjOp F45IZGmoduMd2zXbCveDtXEgDebj0urzN0nYOporj5FtOjukooT45MDyMEzZvkSZ1HSq qyrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786631027; x=1787235827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=FEJbq9EATjwJ0ES2QiMVKbBHcW8RUWVhYzlW91qnYP8=; b=lyEmdSrXFzU8nOJ/WoNT1B1oxvbRDxYRd0QCGVrprnleWPITljL1U/iyYJW3X/Acwh A42BtElHxdppPL7O4HQUagi+Fur6ri+0BO+A089ewXZajE4o8pAln7K7+4k7YNLxty8A Dd1mi0d0dgNZVj2UPNVnXkYPT54DEItd/AlMHsnUJIJhEWNvyu80cU6wki/oKqhbWSFO WfZqy6nQmjyjYWKIdu2ReDUfT8N/33mC9p1aHhdTOj8vWL8PcGgMSo5O06HFb5k3TcXY RYilQr0vh3dZocQ8DVGmmkwb9FOsNrWYTgIjUKEd0SsovUy0ddv7O9xdIhjwA5RFW6uA cMEw== X-Gm-Message-State: AOJu0Yz0gHhJui9IeUt4MUGl/sZELLRzmZXXFiHu7IV1iZxMy4yRsZmY heQz6jS0V5mVlYip2gm+o3J2x/fLKog2m5mdHIB9Gn/kGe3LPvER7X77L/apMF0RbIyB9w== X-Gm-Gg: AR+sD13tDQaKL021cGFha/0llCb3teTsqxrpQD3bAjTNHRgxs+7q5ZhGqzceuMbPrBR Wu8xBFs5SKKel8EcGySO2IwDbEe7zDweYfPg0gAuFtmnRPrFSPs70nOlEF9D2aUAqs50eF7BedX pCSNmyFsyiMc/bAthmFMb5RYm6RArP60qxx78E03H8hL4mFIK9DElsj+TKHf7nHkNB4udiXarYj J46hkbUNxV1xPPi9fWnJRHVBVrWf7HXrcL3duF6dXblL9DO1bUNS1/xDFVt09YqBpXIJorr3AEF 4HsXEnYmP63D8AqDXaosoYfS4Nd8jhpdI2KkQh6wqHqSroXCWZ7wjt0WP5zbuICO18OpPtDFhiE JB3JGKVSBwwpoDKcr3u3RJaUw0i4HNGgKq0mBDdArTUF2IiOrCFKjxpsQ4dYv8EIQ9cR91nNeIn SwX9NhdJxsfAOomtkqYO4iCQLQRyccjrVbdLXrqKQJGkaOradONCo= X-Received: by 2002:a05:600c:5489:b0:493:f318:3bc6 with SMTP id 5b1f17b1804b1-49982199212mr78275795e9.13.1786631027036; Thu, 13 Aug 2026 07:23:47 -0700 (PDT) Received: from localhost ([2a09:bac1:1e00:8::1d6:189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4998212a3a1sm68430035e9.6.2026.08.13.07.23.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 07:23:45 -0700 (PDT) From: Julian Zhu To: libc-alpha@sourceware.org Cc: Julian Zhu Subject: [PATCH v2 2/4] riscv: Add Zfa implementations for fmaximum and fminimum Date: Thu, 13 Aug 2026 22:23:18 +0800 Message-ID: <20260813142320.3617764-2-jz531210@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260813142320.3617764-1-jz531210@gmail.com> References: <20260813142320.3617764-1-jz531210@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GB_FREEMAIL_NUM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org 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~patchwork=sourceware.org@sourceware.org Use the Zfa fmaxm and fminm instructions for the NaN-propagating C23 maximum and minimum operations in float and double precision, which they implement directly: one instruction and no branch, against the eight instructions and a branch the generic implementation needs even with Zfa in -march. Signed-off-by: Julian Zhu --- sysdeps/riscv/rvd/s_fmaximum.c | 44 +++++++++++++++++++++++++++++++++ sysdeps/riscv/rvd/s_fminimum.c | 42 +++++++++++++++++++++++++++++++ sysdeps/riscv/rvf/s_fmaximumf.c | 42 +++++++++++++++++++++++++++++++ sysdeps/riscv/rvf/s_fminimumf.c | 42 +++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 sysdeps/riscv/rvd/s_fmaximum.c create mode 100644 sysdeps/riscv/rvd/s_fminimum.c create mode 100644 sysdeps/riscv/rvf/s_fmaximumf.c create mode 100644 sysdeps/riscv/rvf/s_fminimumf.c diff --git a/sysdeps/riscv/rvd/s_fmaximum.c b/sysdeps/riscv/rvd/s_fmaximum.c new file mode 100644 index 0000000000..39b0f4658f --- /dev/null +++ b/sysdeps/riscv/rvd/s_fmaximum.c @@ -0,0 +1,44 @@ +/* fmaximum(). RISC-V version. + Copyright (C) 2026 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 + . */ + +/* fmaximum is the IEEE 754-2019 maximum operation: like fmaximum_num but + NaN-propagating, so a NaN operand yields a quiet NaN result, and a + signaling NaN also raises the invalid exception. Zfa's fmaxm.d + implements it in a single instruction; without Zfa no instruction + implements it, so use the generic implementation. */ + +#ifdef __riscv_zfa + +# include +# include + +double +__fmaximum (double x, double y) +{ + double res; + asm ("fmaxm.d %0, %1, %2" : "=f" (res) : "f" (x), "f" (y)); + return res; +} +libm_alias_double (__fmaximum, fmaximum) + +#else + +# include +# include + +#endif diff --git a/sysdeps/riscv/rvd/s_fminimum.c b/sysdeps/riscv/rvd/s_fminimum.c new file mode 100644 index 0000000000..7726269a55 --- /dev/null +++ b/sysdeps/riscv/rvd/s_fminimum.c @@ -0,0 +1,42 @@ +/* fminimum(). RISC-V version. + Copyright (C) 2026 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 + . */ + +/* See s_fmaximum.c for a description of the semantics and of why the + fast path is gated on Zfa; fminm.d implements minimum for double + precision. */ + +#ifdef __riscv_zfa + +# include +# include + +double +__fminimum (double x, double y) +{ + double res; + asm ("fminm.d %0, %1, %2" : "=f" (res) : "f" (x), "f" (y)); + return res; +} +libm_alias_double (__fminimum, fminimum) + +#else + +# include +# include + +#endif diff --git a/sysdeps/riscv/rvf/s_fmaximumf.c b/sysdeps/riscv/rvf/s_fmaximumf.c new file mode 100644 index 0000000000..4b30a5457f --- /dev/null +++ b/sysdeps/riscv/rvf/s_fmaximumf.c @@ -0,0 +1,42 @@ +/* fmaximumf(). RISC-V version. + Copyright (C) 2026 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 + . */ + +/* See s_fmaximum.c for a description of the semantics and of why the + fast path is gated on Zfa; fmaxm.s implements maximum for single + precision. */ + +#ifdef __riscv_zfa + +# include +# include + +float +__fmaximumf (float x, float y) +{ + float res; + asm ("fmaxm.s %0, %1, %2" : "=f" (res) : "f" (x), "f" (y)); + return res; +} +libm_alias_float (__fmaximum, fmaximum) + +#else + +# include +# include + +#endif diff --git a/sysdeps/riscv/rvf/s_fminimumf.c b/sysdeps/riscv/rvf/s_fminimumf.c new file mode 100644 index 0000000000..3faba4b91f --- /dev/null +++ b/sysdeps/riscv/rvf/s_fminimumf.c @@ -0,0 +1,42 @@ +/* fminimumf(). RISC-V version. + Copyright (C) 2026 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 + . */ + +/* See s_fmaximum.c for a description of the semantics and of why the + fast path is gated on Zfa; fminm.s implements minimum for single + precision. */ + +#ifdef __riscv_zfa + +# include +# include + +float +__fminimumf (float x, float y) +{ + float res; + asm ("fminm.s %0, %1, %2" : "=f" (res) : "f" (x), "f" (y)); + return res; +} +libm_alias_float (__fminimum, fminimum) + +#else + +# include +# include + +#endif From patchwork Thu Aug 13 14:23:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Zhu X-Patchwork-Id: 141321 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 715EA4BAE7CD for ; Thu, 13 Aug 2026 14:24:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 715EA4BAE7CD Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=EETAXLUA X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 8A9084BA9035 for ; Thu, 13 Aug 2026 14:23:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A9084BA9035 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8A9084BA9035 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631031; cv=none; b=HTDoitNeySw3cWamnr70YKSNC01ZOwo9MHSJHhz3cNyp05AjDEX+O21DXzoLV5V3zxu49v96qodiyA8/UoHp9q8s03qrp1giByrACsbjiEbofQkP40GsWDGyrkMbQeTggTdrAd2aMzyw9zghRWJ0pcKzLjMIxvuOXU6IJbXTK4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631031; c=relaxed/simple; bh=MaVMMDJQQVLiUdHfBJfchIRPPGJYnU2LkCP8wmYkyuk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=p/FuX5p9/8DSnJEhnk1GyHxaFGAgByjiDnh0bvAvq9P/T/WcWdHVsq/tCEuKVzL/8xCLL6Q+4r9yztKC5dSPf/TuEvDfMn0uFLdfWfjtbweArof7ofxnV+Basx7owB3bQd4PkNuu52rn044SGXZaDW/Hp+NGaL/ios1lB0OWZyE= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=EETAXLUA DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A9084BA9035 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4996f1ee4a4so14729545e9.2 for ; Thu, 13 Aug 2026 07:23:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786631030; x=1787235830; 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:content-type; bh=oQXMm9Db6kVPDx2xWfG+ek5C9O6YGY/kl5HhfPgOpfE=; b=EETAXLUAeshrKadjd97xLw5x8n7MfLRrtLm4d5qM3t/d0QgbtL3zehzsU6vs8xhUCK wuJu8GzAtvEhvh6dGxY0eQB5HQvgnL5xVpuPp7sDBVXwGftCXJ/OVuu9tmLfdIEIlR+/ LxOndlKs/pfPFAh1NQP/3ll8lpAP+Z4Q+7nkMIunt6HkEIrF2zmybAVAACsV3PT6ARGI JETLGL79LIKzZ1bqWiTItMUmSp45uC260HFDEhmMbbbB6IrxNDjUljxmDKmFNdK8/VaD mANFrN4NT1ufO5FNXIx5za8ebGeRnPfmKIXf982fRrrRYStyzppFBj7MSqf8yMmQWO8Q CRWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786631030; x=1787235830; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=oQXMm9Db6kVPDx2xWfG+ek5C9O6YGY/kl5HhfPgOpfE=; b=kS3AA3XShaxm5/o/iT4/vp8LjQ4BvajJZgod6FOCCvKRUhbF6YtWgNJIK2u5wzS883 F+lP+TdZCoY1C3QYbfv87llI+4rgs7FIpoz8uLFFKMrQWe04vzJDguKuzYw9KN4EIc2d 5BrTOH1tyqPS+0j2OreGWyJTUjhT2U2SY+OMOmfDTxoo3IqhOQxABm7APkFR1ZRb1aXc 6Q7cN4KgTSpMnzOxBsqGX/ePAxmx0a35PV8sQ9gGNYQUOPUSYfBkISs3ZkOh+rZ9gDVm 9p3gKTSR87RaGZqe8J9AqSgUvaNC5B1EbZPU3Sj7wyqP5g9N5aCLaBQ5Jv59oMN2gdxt B1PA== X-Gm-Message-State: AOJu0Yx2FjkXfrSIt4cY+9UjYjUzIBk/4iYp3CQKQ7YLr8lp2jbvDPVH LbeawkVJbrXcLXWWspizyi+MbmznxwjnAF2k+PSQzLNx/KZ9KehDnR4k3967pUe00e+iCQ== X-Gm-Gg: AR+sD13I0EamuBP7FB0rVs9MMPeAJrep7zj5ctrAenMEzgb1T9CIIAwXJHLiVfJALGy sPFgLIvU0VXqqe5lxYmwyY55jRS7UwloMUiHrmww1j1y0V0z+N6dJ0Tld+C6Zhn4MS1F6RdQgg2 Ff44FnoGr/GijHYXsXeLF5Bf+NJ7qpVAH20e+CJI98he82Own6YUKTnjlOOyGfynr4jjGVXKiLT yCQ08YOwMxum5qLGypl8T0zLnRtgRdV3rKFGZrneI3m6Xq6F3/AEUOmdBolwFWhIbK7pSM5ZezX Jm20sPTkRWR8b/qwJMXuxEa3IkcFldEStQGTKzTP52XT/speSij3TrW/WZdZmsmHek8ZlzyhBl1 3lIho5cmWrazDe7X9AQ844LWJ2+yXl00h0tgRVhaheAekN433+xESDjzdVXN8fHAcsfrCtBLxNi 0CEteL3xRGftTXxv7AKcMf5MlkxSvlRxJMC5uR/kZF9ZyA3ALvWpo= X-Received: by 2002:a05:600c:e548:20b0:499:841c:2016 with SMTP id 5b1f17b1804b1-499841c2039mr37507555e9.0.1786631030102; Thu, 13 Aug 2026 07:23:50 -0700 (PDT) Received: from localhost ([2a09:bac1:1e00:8::1d6:189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4998212a3a1sm68430035e9.6.2026.08.13.07.23.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 07:23:48 -0700 (PDT) From: Julian Zhu To: libc-alpha@sourceware.org Cc: Julian Zhu Subject: [PATCH v2 3/4] riscv: Add Zfa for the rounding functions Date: Thu, 13 Aug 2026 22:23:19 +0800 Message-ID: <20260813142320.3617764-3-jz531210@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260813142320.3617764-1-jz531210@gmail.com> References: <20260813142320.3617764-1-jz531210@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GB_FREEMAIL_NUM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org 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~patchwork=sourceware.org@sourceware.org Use compiler builtins for the float and double ceil, floor, nearbyint, rint, round, roundeven and trunc implementations when Zfa is enabled. Signed-off-by: Julian Zhu --- sysdeps/riscv/rv64/rvd/s_ceil.c | 6 ++++++ sysdeps/riscv/rv64/rvd/s_floor.c | 6 ++++++ sysdeps/riscv/rv64/rvd/s_nearbyint.c | 7 +++++++ sysdeps/riscv/rv64/rvd/s_rint.c | 7 +++++++ sysdeps/riscv/rv64/rvd/s_round.c | 7 +++++++ sysdeps/riscv/rv64/rvd/s_roundeven.c | 7 +++++++ sysdeps/riscv/rv64/rvd/s_trunc.c | 6 ++++++ sysdeps/riscv/rvf/s_ceilf.c | 6 ++++++ sysdeps/riscv/rvf/s_floorf.c | 6 ++++++ sysdeps/riscv/rvf/s_nearbyintf.c | 7 +++++++ sysdeps/riscv/rvf/s_rintf.c | 7 +++++++ sysdeps/riscv/rvf/s_roundevenf.c | 7 +++++++ sysdeps/riscv/rvf/s_roundf.c | 7 +++++++ sysdeps/riscv/rvf/s_truncf.c | 6 ++++++ 14 files changed, 92 insertions(+) diff --git a/sysdeps/riscv/rv64/rvd/s_ceil.c b/sysdeps/riscv/rv64/rvd/s_ceil.c index 01519aeee7..7f62a72de1 100644 --- a/sysdeps/riscv/rv64/rvd/s_ceil.c +++ b/sysdeps/riscv/rv64/rvd/s_ceil.c @@ -25,6 +25,11 @@ double __ceil (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer with a static rounding mode + (rup for ceil) without raising the inexact exception. */ + return __builtin_ceil (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -48,6 +53,7 @@ __ceil (double x) } return x; +#endif } libm_alias_double (__ceil, ceil) diff --git a/sysdeps/riscv/rv64/rvd/s_floor.c b/sysdeps/riscv/rv64/rvd/s_floor.c index bce3e403c6..1e9a4fc65a 100644 --- a/sysdeps/riscv/rv64/rvd/s_floor.c +++ b/sysdeps/riscv/rv64/rvd/s_floor.c @@ -25,6 +25,11 @@ double __floor (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer with a static rounding mode + (rdn for floor) without raising the inexact exception. */ + return __builtin_floor (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -48,6 +53,7 @@ __floor (double x) } return x; +#endif } libm_alias_double (__floor, floor) diff --git a/sysdeps/riscv/rv64/rvd/s_nearbyint.c b/sysdeps/riscv/rv64/rvd/s_nearbyint.c index 79502f8109..6c5fa9f01d 100644 --- a/sysdeps/riscv/rv64/rvd/s_nearbyint.c +++ b/sysdeps/riscv/rv64/rvd/s_nearbyint.c @@ -24,6 +24,12 @@ double __nearbyint (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer in the current rounding mode + without raising the inexact exception, matching nearbyint, and + handles NaN and out-of-range inputs in one instruction. */ + return __builtin_nearbyint (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -47,6 +53,7 @@ __nearbyint (double x) } return x; +#endif } libm_alias_double (__nearbyint, nearbyint) diff --git a/sysdeps/riscv/rv64/rvd/s_rint.c b/sysdeps/riscv/rv64/rvd/s_rint.c index e5b5e42ed2..df338e98ae 100644 --- a/sysdeps/riscv/rv64/rvd/s_rint.c +++ b/sysdeps/riscv/rv64/rvd/s_rint.c @@ -25,6 +25,12 @@ double __rint (double x) { +#ifdef __riscv_zfa + /* Zfa froundnx.d rounds to an integer in the current rounding mode, + raises the inexact exception for non-integer values, and handles + NaN and out-of-range inputs, implementing rint in one instruction. */ + return __builtin_rint (x); +#else bool nan; double mag; @@ -48,6 +54,7 @@ __rint (double x) } return x; +#endif } libm_alias_double (__rint, rint) diff --git a/sysdeps/riscv/rv64/rvd/s_round.c b/sysdeps/riscv/rv64/rvd/s_round.c index 8c3df99271..541261d205 100644 --- a/sysdeps/riscv/rv64/rvd/s_round.c +++ b/sysdeps/riscv/rv64/rvd/s_round.c @@ -25,6 +25,12 @@ double __round (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer with a static rounding mode + (rmm, round half away from zero, for round) without raising the + inexact exception. */ + return __builtin_round (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -48,6 +54,7 @@ __round (double x) } return x; +#endif } libm_alias_double (__round, round) diff --git a/sysdeps/riscv/rv64/rvd/s_roundeven.c b/sysdeps/riscv/rv64/rvd/s_roundeven.c index ed00098a94..d905a91326 100644 --- a/sysdeps/riscv/rv64/rvd/s_roundeven.c +++ b/sysdeps/riscv/rv64/rvd/s_roundeven.c @@ -25,6 +25,12 @@ double __roundeven (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer with a static rounding mode + (rne, round half to even, for roundeven) without raising the + inexact exception. */ + return __builtin_roundeven (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -48,6 +54,7 @@ __roundeven (double x) } return x; +#endif } libm_alias_double (__roundeven, roundeven) diff --git a/sysdeps/riscv/rv64/rvd/s_trunc.c b/sysdeps/riscv/rv64/rvd/s_trunc.c index 33f690eed3..581a51497c 100644 --- a/sysdeps/riscv/rv64/rvd/s_trunc.c +++ b/sysdeps/riscv/rv64/rvd/s_trunc.c @@ -25,6 +25,11 @@ double __trunc (double x) { +#ifdef __riscv_zfa + /* Zfa fround.d rounds to an integer with a static rounding mode + (rtz for trunc) without raising the inexact exception. */ + return __builtin_trunc (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); double mag = fabs (x); @@ -48,6 +53,7 @@ __trunc (double x) } return x; +#endif } libm_alias_double (__trunc, trunc) diff --git a/sysdeps/riscv/rvf/s_ceilf.c b/sysdeps/riscv/rvf/s_ceilf.c index 8dfbda85cc..2923d6e32d 100644 --- a/sysdeps/riscv/rvf/s_ceilf.c +++ b/sysdeps/riscv/rvf/s_ceilf.c @@ -25,6 +25,11 @@ float __ceilf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer with a static rounding mode + (rup for ceil) without raising the inexact exception. */ + return __builtin_ceilf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -48,6 +53,7 @@ __ceilf (float x) } return x; +#endif } libm_alias_float (__ceil, ceil) diff --git a/sysdeps/riscv/rvf/s_floorf.c b/sysdeps/riscv/rvf/s_floorf.c index 7ecce22ec8..c9de4158d1 100644 --- a/sysdeps/riscv/rvf/s_floorf.c +++ b/sysdeps/riscv/rvf/s_floorf.c @@ -25,6 +25,11 @@ float __floorf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer with a static rounding mode + (rdn for floor) without raising the inexact exception. */ + return __builtin_floorf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -48,6 +53,7 @@ __floorf (float x) } return x; +#endif } libm_alias_float (__floor, floor) diff --git a/sysdeps/riscv/rvf/s_nearbyintf.c b/sysdeps/riscv/rvf/s_nearbyintf.c index b4bed924da..715f64361a 100644 --- a/sysdeps/riscv/rvf/s_nearbyintf.c +++ b/sysdeps/riscv/rvf/s_nearbyintf.c @@ -24,6 +24,12 @@ float __nearbyintf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer in the current rounding mode + without raising the inexact exception, matching nearbyintf, and + handles NaN and out-of-range inputs in one instruction. */ + return __builtin_nearbyintf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -47,6 +53,7 @@ __nearbyintf (float x) } return x; +#endif } libm_alias_float (__nearbyint, nearbyint) diff --git a/sysdeps/riscv/rvf/s_rintf.c b/sysdeps/riscv/rvf/s_rintf.c index 01ce204494..9c2fb1c330 100644 --- a/sysdeps/riscv/rvf/s_rintf.c +++ b/sysdeps/riscv/rvf/s_rintf.c @@ -25,6 +25,12 @@ float __rintf (float x) { +#ifdef __riscv_zfa + /* Zfa froundnx.s rounds to an integer in the current rounding mode, + raises the inexact exception for non-integer values, and handles + NaN and out-of-range inputs, implementing rintf in one instruction. */ + return __builtin_rintf (x); +#else bool nan; float mag; @@ -48,6 +54,7 @@ __rintf (float x) } return x; +#endif } libm_alias_float (__rint, rint) diff --git a/sysdeps/riscv/rvf/s_roundevenf.c b/sysdeps/riscv/rvf/s_roundevenf.c index 5fe395b461..315f8bf046 100644 --- a/sysdeps/riscv/rvf/s_roundevenf.c +++ b/sysdeps/riscv/rvf/s_roundevenf.c @@ -25,6 +25,12 @@ float __roundevenf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer with a static rounding mode + (rne, round half to even, for roundeven) without raising the + inexact exception. */ + return __builtin_roundevenf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -48,6 +54,7 @@ __roundevenf (float x) } return x; +#endif } libm_alias_float (__roundeven, roundeven) diff --git a/sysdeps/riscv/rvf/s_roundf.c b/sysdeps/riscv/rvf/s_roundf.c index 8291ccff74..9872c6c320 100644 --- a/sysdeps/riscv/rvf/s_roundf.c +++ b/sysdeps/riscv/rvf/s_roundf.c @@ -25,6 +25,12 @@ float __roundf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer with a static rounding mode + (rmm, round half away from zero, for round) without raising the + inexact exception. */ + return __builtin_roundf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -48,6 +54,7 @@ __roundf (float x) } return x; +#endif } libm_alias_float (__round, round) diff --git a/sysdeps/riscv/rvf/s_truncf.c b/sysdeps/riscv/rvf/s_truncf.c index aa1f40112f..d325c1ccf3 100644 --- a/sysdeps/riscv/rvf/s_truncf.c +++ b/sysdeps/riscv/rvf/s_truncf.c @@ -25,6 +25,11 @@ float __truncf (float x) { +#ifdef __riscv_zfa + /* Zfa fround.s rounds to an integer with a static rounding mode + (rtz for trunc) without raising the inexact exception. */ + return __builtin_truncf (x); +#else int flags = riscv_getflags (); bool nan = isnan (x); float mag = fabsf (x); @@ -48,6 +53,7 @@ __truncf (float x) } return x; +#endif } libm_alias_float (__trunc, trunc) From patchwork Thu Aug 13 14:23:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Zhu X-Patchwork-Id: 141319 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 41FA94BA79BB for ; Thu, 13 Aug 2026 14:24:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 41FA94BA79BB Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ifhkhLD4 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 4864A4BA9035 for ; Thu, 13 Aug 2026 14:23:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4864A4BA9035 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4864A4BA9035 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631035; cv=none; b=rYBByGgqhRf7KtNxwU48IZ3uj6eoAUJg1U+rlhOaMTJgRJ3PtDM9yJ0C3WEKr1bVcjTs9JETk9jCixwz2cnHDA6WKGQ9Kt5XnpsOb5IfvT1ac3msPruaMAM2LRyouAKoe18Y/fvuidWPdvcTAtD2yNkRN8yJMF9SNcF1e/oKlpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786631035; c=relaxed/simple; bh=XFGEmzcaNQiCCItU+PUqyGUJpIe6N2v9JRHmZEzU/Qs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sg2OHFQxvNr8hX0dtVCw67BNwJDebtV0xjQSbtWuQ0Y9m8xdN47wYHBm5sFtloqvqI/69jkgXRg5cHyR10De7rn5dBGntmFqFOnQRcCHMAOnN0NfUoejIp28/eylTd4evGXBm4hrqd+pn3WU8qN3BgTlEg5oO4BLQTo3/wIYGeY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ifhkhLD4 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4864A4BA9035 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-496b7622a83so5511565e9.2 for ; Thu, 13 Aug 2026 07:23:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786631034; x=1787235834; 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:content-type; bh=qvk+Z6cNXXlTRCwO+FZCcX1uASC1ji+QSVh/b/gK+lg=; b=ifhkhLD40rPnn1DfPliDoxC5lcP2TUByghggdSEz31iOZyTUtnyhFzeyg9GGUEZWCw V+XcAkhuXxnkWN0NEcjuX3ge3Et4ygbFfY/qIPH7DhujTPjfrZDEHNg0MVkq19BVTdCF yEFvd3QoPob53jGQPB8VsYo1Kz4LuQmOm/D/ODa0PjzYJVEx8l3D7cfU6OUZtoBOiRu/ s7mTlVThdauRHIYiXb5HjS/nZber8gZCDGyhjbX+fRNuj1p4b1bM/S4rUG5gsyZq9vVd SVQWQ1LOghlndjc4MoV6+nnt1rdvwKwdc901NMWzl+Mn5BSGaNnAzGDu9O6pbbjdLFZm 2SQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786631034; x=1787235834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=qvk+Z6cNXXlTRCwO+FZCcX1uASC1ji+QSVh/b/gK+lg=; b=VJI/d087+El3UZl6F3T99i0QRPfvXSFRdT/zFZfh4p/u8xA0UKtg8tpRBCs4p3HB2x wWglkM2EGKDxMRFh1JwGrIz2NJ8dEUtZ4sO4AqlR58XBJoHjsSnXWVh/xXO1qO3dYj5F pnKE2TmPvuuC3KWiKZ8Y/i2EkO9ydpAiF4RVW3R/UtSadmo1AWjFDYVWCYdK7EbUbOvz IXt0AcRSszujwYV7ep9yOnMYAz3BuD4NUcdOKD2HFrJ4lE/HyLv/nSF+TMFef1PDuBb3 QyTWjdLkK6/nkTHtqWsu+0iKntYqPJ6HsoHD97nGNE8g9/ronKJzOCrY4l/Guzj6rtFO 0Ezw== X-Gm-Message-State: AOJu0Yzd/UpVK8d0iJ25QxHP5+cv7Ecd5M4DkXUhobj1aE24kwgIprFH kyXYVHjYW2x8sBksAm0gx+m2dLFXTiZVUhO354gVKyXNMP4BOfEu3maoKJvOfJacLIh5Hw== X-Gm-Gg: AR+sD13LGLqHzSB0XLs3M0u1XlWe6jRepIerXBcyjvL/ai9x1ymMOgpGqsSCh2RNCBF 33sDgIkNVAp2577WKedxBKlpsmvoGEpKMfEniJkVpu+gDjPbBtpeTwf2dAJ3nhkaRJ6CTKjGh79 Rpyzp6/NwrCoiZrW/s+STPzL8qnTkkl8ViEkqRAIfH8eb1A2rrOnhWd5RfEM0AwLJBNkd2OfWCW elYLwUxHMHstd0lHZaW4A5AT8DhyJ/MMUUP6DbIz31IlSiWXd4VPTEiG152ZAfoGPScUflKr6Wf Y+cHM0jtMoGSv8cW4cZv3djhQdUaXpJW/3IvfnSTGxlM4dc5QnDKIn7GAb9YpRkdnh01Td65GvX zF4Lqa0Ugo4OHfCRe+mCz2O+VHpmJ/NJrdx4go9S8zHRlzrtnQYAXe5s6SoAosuOJ69sc6TA4/2 vvsC5kCMJLbiLs07xwCJf21OLcHzGjEQNdJXjgZcbN X-Received: by 2002:a05:600c:4692:b0:499:61bb:6c78 with SMTP id 5b1f17b1804b1-499821f2ac4mr69844805e9.14.1786631033830; Thu, 13 Aug 2026 07:23:53 -0700 (PDT) Received: from localhost ([2a09:bac1:1e00:8::1d6:189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4998212a3a1sm68430035e9.6.2026.08.13.07.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 07:23:52 -0700 (PDT) From: Julian Zhu To: libc-alpha@sourceware.org Cc: Julian Zhu Subject: [PATCH v2 4/4] riscv: Enable TOINT_INTRINSICS with Zfa Date: Thu, 13 Aug 2026 22:23:20 +0800 Message-ID: <20260813142320.3617764-4-jz531210@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260813142320.3617764-1-jz531210@gmail.com> References: <20260813142320.3617764-1-jz531210@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GB_FREEMAIL_NUM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org 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~patchwork=sourceware.org@sourceware.org Provide Zfa-gated roundtoint and converttoint helpers for the common libm argument-reduction code. Zfa turns roundtoint into one fround.d instruction while non-Zfa builds continue using the generic reduction path. Signed-off-by: Julian Zhu --- sysdeps/riscv/rvd/math_private.h | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sysdeps/riscv/rvd/math_private.h diff --git a/sysdeps/riscv/rvd/math_private.h b/sysdeps/riscv/rvd/math_private.h new file mode 100644 index 0000000000..9760702560 --- /dev/null +++ b/sysdeps/riscv/rvd/math_private.h @@ -0,0 +1,44 @@ +/* Configure optimized libm functions. RISC-V version. + Copyright (C) 2026 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 + . */ + +#ifndef RISCV_MATH_PRIVATE_H +#define RISCV_MATH_PRIVATE_H 1 + +#include + +#ifdef __riscv_zfa +# define TOINT_INTRINSICS 1 + +static inline double +roundtoint (double x) +{ + return __builtin_round (x); /* Zfa: single fround.d. */ +} + +static inline int32_t +converttoint (double x) +{ + int32_t r; + asm ("fcvt.w.d %0, %1, rmm" : "=r" (r) : "f" (x)); + return r; +} +#endif /* __riscv_zfa */ + +#include_next + +#endif /* RISCV_MATH_PRIVATE_H */