From patchwork Wed Aug 3 17:42:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 56527 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A93D3885C0B for ; Wed, 3 Aug 2022 17:44:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 0720D3857349 for ; Wed, 3 Aug 2022 17:43:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0720D3857349 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pj1-x102a.google.com with SMTP id t2-20020a17090a4e4200b001f21572f3a4so2918057pjl.0 for ; Wed, 03 Aug 2022 10:43:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=to:from:cc:content-transfer-encoding:mime-version:message-id:date :subject:from:to:cc; bh=mJ8dh72LFEj0h42zs09uEJict/0Kn400cbTH+mh97Gs=; b=3hFwpBYmfetNlcJ8685eSLDH4riZbI756kTWpAdw4yMh5MsElUkYokBHezA5jqSTzD T4nUWCD0M1kd0drYVqmziR2QR61RDPqcNYsqzKzCkt7tVX+Zuk2Nl/u9ApqVnBslXO1B FyKdrSLrMr2vE9Y5YRz9fcx/P/s6EFOgROYaWK0IdAffqiCSmrhpSwyzsDWiabY9vsvj +TZHQ/CLekA+4xmceLTd0id2hqHhORpomvsKFHJDqmTfcBYfHd4iFVz64Onq9xekzvAr y4lspTQWZfPG5qLRLonS8hnjRhVc/J8ssavVJTZzcS0w34lK8NUSzxjNM68EADF7PgAx 1S2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:from:cc:content-transfer-encoding:mime-version:message-id:date :subject:x-gm-message-state:from:to:cc; bh=mJ8dh72LFEj0h42zs09uEJict/0Kn400cbTH+mh97Gs=; b=heTXcJs4pq1zoQlcfdILh2IWSxKkRGB+b8XUgVlqj2AiJOOtu8GDiLqjPP8D56H+a9 rdZhQfebM1ZlbberkkbH+28LckWhHorOOmDaxk/GOR2hkZg2f/CICjhgnZnrAmUt4KKZ lv5eRIVPJaLQxiiu0BvksqwpasFwLFWMMwT+VAQqwC5PvQNvjOSfT/OO0wGcs2jKhSLk zhwzi1zsUAaHPyAfD51t/SdB2ce17mpmwSXshRqzLgZXHqw6AylVDcS1TUpP95HQl0E8 pLgwP1qV9q0SjBzoe6Gbqnuvrdgn0+FDRvbo6KsYR3ci6tE9Qvk0isNO3N0tq4bR3bMn EBmA== X-Gm-Message-State: ACgBeo0+vC0+rX7xMCakSAWP3DLelLT9RfI46ouEBtEfHLhRtxHqM5GA 4q96p7GFpyJoxLTWNxoa0xKRmw== X-Google-Smtp-Source: AA6agR7vDGpkb74X4nmunwxZBsrlJKd/hzkl5VpcEhr+6oKQYow39YLXCB85gLSfKm/1F+k6yPEa/A== X-Received: by 2002:a17:90a:4287:b0:1f2:2a61:e479 with SMTP id p7-20020a17090a428700b001f22a61e479mr6124235pjg.118.1659548625954; Wed, 03 Aug 2022 10:43:45 -0700 (PDT) Received: from localhost ([50.221.140.186]) by smtp.gmail.com with ESMTPSA id ja17-20020a170902efd100b0016c46ff9741sm2259541plb.67.2022.08.03.10.43.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Aug 2022 10:43:45 -0700 (PDT) Subject: [RFC] RISC-V: Implement {convert,round}toint() Date: Wed, 3 Aug 2022 10:42:59 -0700 Message-Id: <20220803174258.4235-1-palmer@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 From: Palmer Dabbelt To: libc-alpha@sourceware.org, Vineet Gupta X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 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 Sender: "Libc-alpha" We currently have fairly inefficient rounding sequences on RISC-V because we lack direct float->float round instructions. This results in a bunch of unnecessary handling of FP exceptions in the exp() and pow(). Luckily TOINT_INTRINSICS seems to exist in order to handle exactly these problems. --- Thanks to Vineet for finding this one. I haven't had a chance to test it yet, but I figured it'd be best to send this out as an RFC so it doesn't get lost. --- sysdeps/riscv/rvd/math_private.h | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 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..74a5aef07c --- /dev/null +++ b/sysdeps/riscv/rvd/math_private.h @@ -0,0 +1,54 @@ +/* Configure optimized libm functions. RISC-V version. + Copyright (C) 2017-2022 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 +#include + +/* Use inline round and lround instructions. */ +#define TOINT_INTRINSICS 1 + +/* The results of these two functions only need to be specified if they can be + representable in an int32_t. The rounding just has to be consistent with + each other, here we're using the dynamic rounding mode under the assumption + that callers avoid changing it. */ +static inline int32_t +converttoint (double_t x) +{ + int32_t o; + /* This returns a poorly-formed int32_t when the input exceeds its range. + That's a pretty hefty use of the unspecified behavior, as it also breaks + the ABI, but it's slightly faster. */ + __asm__ ("fcvt.w.d %0, %1" : "=r"(o) : "f"(x)); + return o; +} + +static inline double_t +roundtoint (double_t x) +{ + double o; + int32_t i = converttoint(x); + __asm__ ("fcvt.d.w %0, %1" : "=f"(o) : "r"(i)); + return o; +} + +#include_next + +#endif