From patchwork Thu Aug 15 16:43:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineet Gupta X-Patchwork-Id: 1972853 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=Vz+UF8Y2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wl9tM5qYzz1yNr for ; Fri, 16 Aug 2024 02:43:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06B973858D34 for ; Thu, 15 Aug 2024 16:43:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 631EE3858D29 for ; Thu, 15 Aug 2024 16:43:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 631EE3858D29 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 631EE3858D29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723740194; cv=none; b=RMtQC1KelXtYurBL0dNOwAYQ1qliLYvQXvkgyKByB2/NIzK69Hq3nxECfRAc6aAGK5+n3dPtyuQVdVkbKKPTAXIEZK/0esKdLTu+uGzFsb7q+L4yH0YNKHTetbLgyol/QrN2reRMQfBqERS7hQyqqSGlYjiM4vENrKSngKNRbXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723740194; c=relaxed/simple; bh=4UBCF+PvTVbRLm7p5hqV2Fsy3NNoXR/CerGbIDuX+Tc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=iqrbOHa+5DiXhZwWCN//Bha57yIfboBl/SruwKB6O9uHNQSi2TDgvP4zTYCPITSx9wZdw1QUxJ2Mh0Om9f+u05NyqJ3QyHZl6qmMoM6rDCeqgPpybKrJBFTnkmGxWFkfgExdTwrT/bVfku1GTPtyJyXcQ2sCDjwAR+xwvLdHwXQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-70d23caf8ddso904071b3a.0 for ; Thu, 15 Aug 2024 09:43:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1723740190; x=1724344990; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HQOvA7vwrFn+XXAd/K8mmlE1V1AlH5hjZud1ZH2xWYw=; b=Vz+UF8Y2VX0E9gYgmALnXjvMbCT6HTwaOgxg0K8OBzIdCSr+ar2L2pf/1c2gz0Ab39 9rc8fMVmW4GhLNxqAxKyVmaoRRvFTSoE2fK49fqKQJJsQ3uhPs8JE5ut/7TxMXRIUG9C KVnwvWv9hrL0yBUr6HPSOfeQ+3zMJm7mgxS6HH+NqaO3dOU0iGALySh7bUVa/ltsnDiU sJDLtNRa6PqeJKc5ErjTFujePk4YwVNR8EKZiwNgqF1CqGEu4a3MtFdi9HE9HH5CZ369 6Kq29t89+HneTnt+sC0VnhkciPQGdZS4Kw9LYUckBJsnK1XLd4xA6hXHqttmmnabVWuS x0aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723740190; x=1724344990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HQOvA7vwrFn+XXAd/K8mmlE1V1AlH5hjZud1ZH2xWYw=; b=fnbNC58iikDLs3WraZbab7qhX753VKj6E5RrkaejhyEYk4FyCT/RzFLS/zVGPOaT3R we/aE7JlN6ZHi+Z2cmN8ZP7LAdwhJTmfjIpU36Cx95Unh+Dj0ckvsVWqljZUHMkG5xQJ 1W57SPJpsOmbFS+A4i7qgIYLkgTpOsx3nLyxCiDiW/SeXGCEoO1zik6ZGipjeejXMbrJ TL6hS+ixnB4pfsZh4BqACLTbfAGKlE8ZJ4izIz/+TiX8Wn7KoVYLYVIvrIX+Ic3gPknh ViMO7WyPn8rGGyhljCcKbUGM6NXVcLThuMFjWN51Qr8hCWf76lWMsqtuAiPAdwjC/K0m 0MMA== X-Gm-Message-State: AOJu0Yx5lor7Vr5x3XC8uqRZRhiaKJngIox6/5By99LETOYO5P0p6xQZ C/LR8bx9A4OWpO6vVg1WQZU7s9y6QJtwny4s0kFZZn+haanhAEEnsmzKuPwj8ROmI/XNkt1kucD A+Pg= X-Google-Smtp-Source: AGHT+IHtobCB3Y8HXIYE40pervdHfJeM2OO04ABSUCIM42Zf8J8ahJmyN1AF9MkUidUgcSk6yjQWFA== X-Received: by 2002:a05:6a00:66dc:b0:704:2f65:4996 with SMTP id d2e1a72fcca58-713c4e2a5e9mr390269b3a.11.1723740189948; Thu, 15 Aug 2024 09:43:09 -0700 (PDT) Received: from fw-ubuntu.ba.rivosinc.com (c-24-5-188-125.hsd1.ca.comcast.net. [24.5.188.125]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7127aef4480sm1186819b3a.119.2024.08.15.09.43.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2024 09:43:09 -0700 (PDT) From: Vineet Gupta To: gcc-patches@gcc.gnu.org Cc: Jeff Law , gnu-toolchain@rivosinc.com, Vineet Gupta Subject: [RESEND PATCH v5] RISC-V: use fclass insns to implement isfinite, isnormal and isinf builtins Date: Thu, 15 Aug 2024 09:43:07 -0700 Message-ID: <20240815164307.1027212-1-vineetg@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <2b90d80e-49ea-45f2-8f05-1d92857ffefd@gmail.com> References: <2b90d80e-49ea-45f2-8f05-1d92857ffefd@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 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, T_SCC_BODY_TEXT_LINE 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This was approved but resending so CI can retest after the ranges fixes. --- Currently these builtins use float compare instructions which require FP flags to be saved/restored which could be costly in uarch. RV Base ISA already has FCLASS.{d,s,h} instruction to compare/identify FP values w/o disturbing FP exception flags. Now that upstream supports the corresponding optabs, wire them up in the backend. gcc/ChangeLog: * config/riscv/riscv-protos.h (riscv_emit_fp_classify): New function declaration. * config/riscv/riscv.cc (riscv_emit_fp_classify): New helper for the expanders. * config/riscv/riscv.md: Add UNSPEC_FCLASS. define_insn for fclass insn. define_expand for isfinite, isnormal, isinf. gcc/testsuite/ChangeLog: * gcc.target/riscv/fclass.c: New tests. Signed-off-by: Vineet Gupta --- gcc/config/riscv/riscv.md | 63 +++++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/fclass.c | 38 +++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/fclass.c diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 5e3ef789e42e..f8d8162c0f91 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -68,6 +68,7 @@ UNSPEC_FMAX UNSPEC_FMINM UNSPEC_FMAXM + UNSPEC_FCLASS ;; Stack tie UNSPEC_TIE @@ -3478,6 +3479,68 @@ (set_attr "mode" "") (set (attr "length") (const_int 16))]) +;; fclass instruction output bitmap +;; 0 negative infinity +;; 1 negative normal number. +;; 2 negative subnormal number. +;; 3 -0 +;; 4 +0 +;; 5 positive subnormal number. +;; 6 positive normal number. +;; 7 positive infinity +;; 8 signaling NaN. +;; 9 quiet NaN + +(define_insn "fclass" + [(set (match_operand:X 0 "register_operand" "=r") + (unspec [(match_operand:ANYF 1 "register_operand" " f")] + UNSPEC_FCLASS))] + "TARGET_HARD_FLOAT" + "fclass.\t%0,%1"; + [(set_attr "type" "fcmp") + (set_attr "mode" "")]) + +;; Implements optab for isfinite, isnormal, isinf + +(define_int_iterator FCLASS_MASK [126 66 129]) +(define_int_attr fclass_optab + [(126 "isfinite") + (66 "isnormal") + (129 "isinf")]) + +(define_expand "2" + [(match_operand 0 "register_operand" "=r") + (match_operand:ANYF 1 "register_operand" " f") + (const_int FCLASS_MASK)] + "TARGET_HARD_FLOAT" +{ + if (GET_MODE (operands[0]) != SImode + && GET_MODE (operands[0]) != word_mode) + FAIL; + + rtx t = gen_reg_rtx (word_mode); + rtx t_op0 = gen_reg_rtx (word_mode); + + if (TARGET_64BIT) + emit_insn (gen_fclassdi (t, operands[1])); + else + emit_insn (gen_fclasssi (t, operands[1])); + + riscv_emit_binary (AND, t, t, GEN_INT ()); + rtx cmp = gen_rtx_NE (word_mode, t, const0_rtx); + emit_insn (gen_cstore4 (t_op0, cmp, t, const0_rtx)); + + if (TARGET_64BIT) + { + t_op0 = gen_lowpart (SImode, t_op0); + SUBREG_PROMOTED_VAR_P (t_op0) = 1; + SUBREG_PROMOTED_SET (t_op0, SRP_SIGNED); + } + + emit_move_insn (operands[0], t_op0); + DONE; +}) + (define_insn "*seq_zero_" [(set (match_operand:GPR 0 "register_operand" "=r") (eq:GPR (match_operand:X 1 "register_operand" " r") diff --git a/gcc/testsuite/gcc.target/riscv/fclass.c b/gcc/testsuite/gcc.target/riscv/fclass.c new file mode 100644 index 000000000000..ea0f173ecf4b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fclass.c @@ -0,0 +1,38 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target hard_float } */ +/* { dg-options "-march=rv64gc -mabi=lp64d -ftrapping-math" { target { rv64 } } } */ +/* { dg-options "-march=rv32gc -mabi=ilp32d -ftrapping-math" { target { rv32 } } } */ + +int d_isfinite(double a) +{ + return __builtin_isfinite(a); +} + +int d_isnormal(double a) +{ + return __builtin_isnormal(a); +} + +int d_isinf(double a) +{ + return __builtin_isinf(a); +} + +int f_isfinite(float a) +{ + return __builtin_isfinite(a); +} + +int f_isnormal(float a) +{ + return __builtin_isnormal(a); +} + +int f_isinf(float a) +{ + return __builtin_isinf(a); +} + +/* { dg-final { scan-assembler-not {\mfrflags} } } */ +/* { dg-final { scan-assembler-not {\mfsflags} } } */ +/* { dg-final { scan-assembler-times {\tfclass} 6 } } */