From patchwork Wed Jun 21 12:57:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Dapp X-Patchwork-Id: 111002 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp4340932vqr; Wed, 21 Jun 2023 05:58:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7FSWZWFEN1rAbh7a/StYWCGQwLaL7Xl2OpGJhm/EHVLgQJt/nqSOv8HGAjSgxeGIws7fUu X-Received: by 2002:a17:907:62a0:b0:96f:d8a9:d045 with SMTP id nd32-20020a17090762a000b0096fd8a9d045mr13464914ejc.59.1687352294089; Wed, 21 Jun 2023 05:58:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687352294; cv=none; d=google.com; s=arc-20160816; b=m4DmhLe/SJpBDMyMOz+bUj65TZby1pbIeUtHhnVSvf9g5e/4xKtYs7ruqa5lf4j2NN bfciNV/SEtnQ7QTFYA2hinweEfHoL3ENebJCEUYI85iX78rCuj696RkdtdCD3weOvaRf 9MMcQUYHmEUp80zl5wdYKI1TNVjQtW8ij0d95wqBTalFMaqwdE75TLvmMLAgdGSH5i7d 5wwSgsldfSUMPk3FRdiTHDVP67z1ldHPmQapAnFVooSZr9kcPbiOrfRfXSatcA+4WWWd sHQEPYGvvzMjh2uewhrVR6/VcsanwPpHYJfqDZPEUgDWpInjspTTJnarjOnD2sBKyzte eUCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:in-reply-to:references:to :content-language:subject:cc:user-agent:mime-version:date:message-id :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ejrnqNPDvcT0GIC8S3CgBUERv+jLgObiZDZgeLwPDfs=; b=BiHf1cEqXlI8+dX0xxisnPacRC3Xu895dND3xxjzZ4VbLeX/66otiaD+mu6rRAaubL 1DboKopdJBafDPhYKzE1seh9c24aKogYAjLghouoC6SP4g3PKenfWAofJq3iRgAF8xQV mZu9qw9yxs9aGPuIiw6Ca7Y3YSN3+S53omCGM0hs09XcMvO/HWloY0HKp/qZ0stQzAFd T2zn8JqeMKXFQcL0XYJrhWXL/xDcX2mtjDCH9yF0gOH8zfaLt52i5hXinrPGGZdRJ3WC kFOXyhg9sIsIBdy478DIYPGhbxR/qlBsl0hwTIgzAlkIa6Ak5m0g/kIA0cV2y33Dd2YF V5WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AOPq4zrl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c13-20020a170906340d00b0098862a7ca0dsi2376501ejb.1037.2023.06.21.05.58.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 05:58:14 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.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=@gcc.gnu.org header.s=default header.b=AOPq4zrl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 963663858C41 for ; Wed, 21 Jun 2023 12:58:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 963663858C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687352292; bh=ejrnqNPDvcT0GIC8S3CgBUERv+jLgObiZDZgeLwPDfs=; h=Date:Cc:Subject:To:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=AOPq4zrlhdOQhtp34jkXiZejIysDhjy9y+zpSrJU4oHcBTpgo+Q1vXjTLHfRnHh3g ieP+vB4288MOwAmlmuwOH2A3iINYZvON7n8jVnhAZqdvrnaw4eFop9XAqMyIQGZCiB iEi1EJg+JZED3fnP54VGAeB6DTYlBIFSX+bySZVU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 8D6DD3858D28 for ; Wed, 21 Jun 2023 12:57:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D6DD3858D28 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-3112f256941so3824968f8f.1 for ; Wed, 21 Jun 2023 05:57:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687352246; x=1689944246; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:cc:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ejrnqNPDvcT0GIC8S3CgBUERv+jLgObiZDZgeLwPDfs=; b=dZ4kYCdHF2LJ+rLcT1JE48GRdZ+lrq7YA/VcO61+HzjOSj+znwZGfoeVsmPnj65ciy F66Ks9+WllSb+uYK08lDZgX7Q9Ze1wR8v6fD/ESEA53Su9y/l2XRz+zFr2T4qPrpxCRA ky3+aUNm6Mgp6F7BTpwbrpnSP7FAIFuGwDDf3xMgS9Wckzxid8Lu/WR1AcGwPQ/GuZUP IE+8GVHVe+VchHgIS27GMZQKDNipEGzE6lRBJoBbH0PImJwJR1csAmH86ESu3xytj9Um C38PZgtGFr0DDOLMScbPQoKnu5P5GY9SSFJ2NqjJU5GZe1Vit3gauzxRa3AH5MwrB57N 4zjg== X-Gm-Message-State: AC+VfDwcy/pQibyAOvKbCzUSFPckeGhh55IVcCG4E+ltaxOPls1jdmYK gJfQk7xuKR+Co02u97Wx8Dg= X-Received: by 2002:a5d:4fc1:0:b0:30f:c1ab:a039 with SMTP id h1-20020a5d4fc1000000b0030fc1aba039mr10180677wrw.40.1687352245980; Wed, 21 Jun 2023 05:57:25 -0700 (PDT) Received: from [192.168.1.23] (ip-046-005-130-086.um12.pools.vodafone-ip.de. [46.5.130.86]) by smtp.gmail.com with ESMTPSA id e14-20020adfef0e000000b0030c2e3c7fb3sm4388148wro.101.2023.06.21.05.57.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jun 2023 05:57:25 -0700 (PDT) Message-ID: Date: Wed, 21 Jun 2023 14:57:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Cc: rdapp.gcc@gmail.com Subject: [PATCH v2] RISC-V: Implement autovec copysign. Content-Language: en-US To: "juzhe.zhong@rivai.ai" , gcc-patches , palmer , "kito.cheng" , jeffreyalaw References: <8ea7c935-ee31-db3d-9672-14833b63d3b0@gmail.com> <01B7D9B1272FA0D2+2023062106254915781354@rivai.ai> <6d7ab96f-188d-8bdb-0e2e-64845a462f2c@gmail.com> <478DD4F8B199C0FA+202306211438500791847@rivai.ai> In-Reply-To: <478DD4F8B199C0FA+202306211438500791847@rivai.ai> X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Robin Dapp via Gcc-patches From: Robin Dapp Reply-To: Robin Dapp Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769225912425116352?= X-GMAIL-MSGID: =?utf-8?q?1769317119295291586?= Hi, changes from v1: - Removed UNSPEC_VNCOPYSIGN - Adjusted xorsign test expectation. Regards Robin This adds vector copysign, ncopysign and xorsign as well as the accompanying tests. gcc/ChangeLog: * config/riscv/autovec.md (copysign3): Add expander. (xorsign3): Dito. * config/riscv/riscv-vector-builtins-bases.cc (class vfsgnjn): New class. * config/riscv/vector-iterators.md (copysign): Remove ncopysign. (xorsign): Dito. (n): Dito. (x): Dito. * config/riscv/vector.md (@pred_ncopysign): Split off. (@pred_ncopysign_scalar): Dito. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/binop/copysign-run.c: New test. * gcc.target/riscv/rvv/autovec/binop/copysign-rv64gcv.c: New test. * gcc.target/riscv/rvv/autovec/binop/copysign-rv32gcv.c: New test. * gcc.target/riscv/rvv/autovec/binop/copysign-template.h: New test. * gcc.target/riscv/rvv/autovec/binop/copysign-zvfh-run.c: New test. --- gcc/config/riscv/autovec.md | 43 +++++++++ .../riscv/riscv-vector-builtins-bases.cc | 18 +++- gcc/config/riscv/vector-iterators.md | 10 +-- gcc/config/riscv/vector.md | 43 +++++++++ .../riscv/rvv/autovec/binop/copysign-run.c | 89 +++++++++++++++++++ .../rvv/autovec/binop/copysign-rv32gcv.c | 11 +++ .../rvv/autovec/binop/copysign-rv64gcv.c | 11 +++ .../rvv/autovec/binop/copysign-template.h | 78 ++++++++++++++++ .../rvv/autovec/binop/copysign-zvfh-run.c | 83 +++++++++++++++++ 9 files changed, 377 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-run.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv32gcv.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv64gcv.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-template.h create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-zvfh-run.c diff --git a/gcc/config/riscv/autovec.md b/gcc/config/riscv/autovec.md index f1641d7e1ea..f2e69aaf102 100644 --- a/gcc/config/riscv/autovec.md +++ b/gcc/config/riscv/autovec.md @@ -804,3 +804,46 @@ (define_expand "3" riscv_vector::RVV_BINOP, operands); DONE; }) + +;; ------------------------------------------------------------------------------- +;; ---- [FP] Sign copying +;; ------------------------------------------------------------------------------- +;; Includes: +;; - vfsgnj.vv/vfsgnjn.vv +;; - vfsgnj.vf/vfsgnjn.vf +;; ------------------------------------------------------------------------------- + +;; Leave the pattern like this as to still allow combine to match +;; a negated copysign (see vector.md) before adding the UNSPEC_VPREDICATE later. +(define_insn_and_split "copysign3" + [(set (match_operand:VF 0 "register_operand" "=vd, vd, vr, vr") + (unspec:VF + [(match_operand:VF 1 "register_operand" " vr, vr, vr, vr") + (match_operand:VF 2 "register_operand" " vr, vr, vr, vr")] UNSPEC_VCOPYSIGN))] + "TARGET_VECTOR && can_create_pseudo_p ()" + "#" + "&& 1" + [(const_int 0)] +{ + riscv_vector::emit_vlmax_insn (code_for_pred (UNSPEC_VCOPYSIGN, mode), + riscv_vector::RVV_BINOP, operands); + DONE; +} + [(set_attr "type" "vfsgnj") + (set_attr "mode" "")]) + +;; ------------------------------------------------------------------------------- +;; Includes: +;; - vfsgnjx.vv +;; - vfsgnjx.vf +;; ------------------------------------------------------------------------------- +(define_expand "xorsign3" + [(match_operand:VF_AUTO 0 "register_operand") + (match_operand:VF_AUTO 1 "register_operand") + (match_operand:VF_AUTO 2 "register_operand")] + "TARGET_VECTOR" +{ + riscv_vector::emit_vlmax_insn (code_for_pred (UNSPEC_VXORSIGN, mode), + riscv_vector::RVV_BINOP, operands); + DONE; +}) diff --git a/gcc/config/riscv/riscv-vector-builtins-bases.cc b/gcc/config/riscv/riscv-vector-builtins-bases.cc index c6c53dc13a5..6b8fec47ac9 100644 --- a/gcc/config/riscv/riscv-vector-builtins-bases.cc +++ b/gcc/config/riscv/riscv-vector-builtins-bases.cc @@ -1212,7 +1212,7 @@ public: } }; -/* Implements vfsqrt7/vfrec7/vfclass/vfsgnj/vfsgnjn/vfsgnjx. */ +/* Implements vfsqrt7/vfrec7/vfclass/vfsgnj/vfsgnjx. */ template class float_misc : public function_base { @@ -1227,6 +1227,20 @@ public: } }; +/* Implements vfsgnjn. */ +class vfsgnjn : public function_base +{ +public: + rtx expand (function_expander &e) const override + { + if (e.op_info->op == OP_TYPE_vf) + return e.use_exact_insn (code_for_pred_ncopysign_scalar (e.vector_mode ())); + if (e.op_info->op == OP_TYPE_vv) + return e.use_exact_insn (code_for_pred_ncopysign (e.vector_mode ())); + gcc_unreachable (); + } +}; + /* Implements vmfeq/vmfne/vmflt/vmfgt/vmfle/vmfge. */ template class fcmp : public function_base @@ -2031,7 +2045,7 @@ static CONSTEXPR const float_misc vfrec7_obj; static CONSTEXPR const binop vfmin_obj; static CONSTEXPR const binop vfmax_obj; static CONSTEXPR const float_misc vfsgnj_obj; -static CONSTEXPR const float_misc vfsgnjn_obj; +static CONSTEXPR const vfsgnjn vfsgnjn_obj; static CONSTEXPR const float_misc vfsgnjx_obj; static CONSTEXPR const unop vfneg_obj; static CONSTEXPR const unop vfabs_obj; diff --git a/gcc/config/riscv/vector-iterators.md b/gcc/config/riscv/vector-iterators.md index 6ca1c54c709..2c0c53c0c28 100644 --- a/gcc/config/riscv/vector-iterators.md +++ b/gcc/config/riscv/vector-iterators.md @@ -61,7 +61,6 @@ (define_c_enum "unspec" [ UNSPEC_VFCLASS UNSPEC_VCOPYSIGN - UNSPEC_VNCOPYSIGN UNSPEC_VXORSIGN UNSPEC_VFCVT @@ -1480,14 +1479,11 @@ (define_int_attr misc_op [(UNSPEC_VMSBF "sbf") (UNSPEC_VMSIF "sif") (UNSPEC_VMSO (define_int_attr float_insn_type [(UNSPEC_VFRSQRT7 "vfsqrt") (UNSPEC_VFREC7 "vfrecp")]) -(define_int_iterator VCOPYSIGNS [UNSPEC_VCOPYSIGN UNSPEC_VNCOPYSIGN UNSPEC_VXORSIGN]) +(define_int_iterator VCOPYSIGNS [UNSPEC_VCOPYSIGN UNSPEC_VXORSIGN]) -(define_int_attr copysign [(UNSPEC_VCOPYSIGN "copysign") - (UNSPEC_VNCOPYSIGN "ncopysign") - (UNSPEC_VXORSIGN "xorsign")]) +(define_int_attr copysign [(UNSPEC_VCOPYSIGN "copysign") (UNSPEC_VXORSIGN "xorsign")]) -(define_int_attr nx [(UNSPEC_VCOPYSIGN "") (UNSPEC_VNCOPYSIGN "n") - (UNSPEC_VXORSIGN "x")]) +(define_int_attr nx [(UNSPEC_VCOPYSIGN "") (UNSPEC_VXORSIGN "x")]) (define_int_attr ud [(UNSPEC_VSLIDEUP "up") (UNSPEC_VSLIDEDOWN "down") (UNSPEC_VSLIDE1UP "1up") (UNSPEC_VSLIDE1DOWN "1down") diff --git a/gcc/config/riscv/vector.md b/gcc/config/riscv/vector.md index 884e7435cc2..aa81b9023d8 100644 --- a/gcc/config/riscv/vector.md +++ b/gcc/config/riscv/vector.md @@ -5820,6 +5820,27 @@ (define_insn "@pred_" [(set_attr "type" "vfsgnj") (set_attr "mode" "")]) +(define_insn "@pred_ncopysign" + [(set (match_operand:VF 0 "register_operand" "=vd, vd, vr, vr") + (if_then_else:VF + (unspec: + [(match_operand: 1 "vector_mask_operand" " vm, vm,Wc1,Wc1") + (match_operand 5 "vector_length_operand" " rK, rK, rK, rK") + (match_operand 6 "const_int_operand" " i, i, i, i") + (match_operand 7 "const_int_operand" " i, i, i, i") + (match_operand 8 "const_int_operand" " i, i, i, i") + (reg:SI VL_REGNUM) + (reg:SI VTYPE_REGNUM)] UNSPEC_VPREDICATE) + (neg:VF + (unspec:VF + [(match_operand:VF 3 "register_operand" " vr, vr, vr, vr") + (match_operand:VF 4 "register_operand" " vr, vr, vr, vr")] UNSPEC_VCOPYSIGN)) + (match_operand:VF 2 "vector_merge_operand" " vu, 0, vu, 0")))] + "TARGET_VECTOR" + "vfsgnjn.vv\t%0,%3,%4%p1" + [(set_attr "type" "vfsgnj") + (set_attr "mode" "")]) + (define_insn "@pred__scalar" [(set (match_operand:VF 0 "register_operand" "=vd, vd, vr, vr") (if_then_else:VF @@ -5841,6 +5862,28 @@ (define_insn "@pred__scalar" [(set_attr "type" "vfsgnj") (set_attr "mode" "")]) +(define_insn "@pred_ncopysign_scalar" + [(set (match_operand:VF 0 "register_operand" "=vd, vd, vr, vr") + (if_then_else:VF + (unspec: + [(match_operand: 1 "vector_mask_operand" " vm, vm,Wc1,Wc1") + (match_operand 5 "vector_length_operand" " rK, rK, rK, rK") + (match_operand 6 "const_int_operand" " i, i, i, i") + (match_operand 7 "const_int_operand" " i, i, i, i") + (match_operand 8 "const_int_operand" " i, i, i, i") + (reg:SI VL_REGNUM) + (reg:SI VTYPE_REGNUM)] UNSPEC_VPREDICATE) + (neg:VF + (unspec:VF + [(match_operand:VF 3 "register_operand" " vr, vr, vr, vr") + (vec_duplicate:VF + (match_operand: 4 "register_operand" " f, f, f, f"))] UNSPEC_VCOPYSIGN)) + (match_operand:VF 2 "vector_merge_operand" " vu, 0, vu, 0")))] + "TARGET_VECTOR" + "vfsgnjn.vf\t%0,%3,%4%p1" + [(set_attr "type" "vfsgnj") + (set_attr "mode" "")]) + ;; ------------------------------------------------------------------------------- ;; ---- Predicated floating-point ternary operations ;; ------------------------------------------------------------------------------- diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-run.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-run.c new file mode 100644 index 00000000000..7a6d429c9a9 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-run.c @@ -0,0 +1,89 @@ +/* { dg-do run { target { riscv_vector } } } */ +/* { dg-additional-options "-std=c99 -fno-vect-cost-model --param=riscv-autovec-preference=fixed-vlmax -ffast-math" } */ + +#include "copysign-template.h" + +#include + +#define SZ 512 + +#define EPS 1e-6 + +#define RUN(TYPE,VAL) \ + TYPE a##TYPE[SZ]; \ + TYPE b##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a##TYPE[i] = i; \ + b##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + copysign_##TYPE (a##TYPE, a##TYPE, b##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a##TYPE[i] - ((i & 1) ? i : -i)) < EPS); \ + +#define RUN2(TYPE,VAL) \ + TYPE a2##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a2##TYPE[i] = i; \ + copysigns_##TYPE (a2##TYPE, a2##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a2##TYPE[i] + i) < EPS); \ + +#define RUN3(TYPE,VAL) \ + TYPE a3##TYPE[SZ]; \ + TYPE b3##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a3##TYPE[i] = (i & 1) ? -i : i; \ + b3##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + xorsign_##TYPE (a3##TYPE, a3##TYPE, b3##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a3##TYPE[i] + i) < EPS); \ + +#define RUN4(TYPE,VAL) \ + TYPE a4##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a4##TYPE[i] = -i; \ + xorsigns_##TYPE (a4##TYPE, a4##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a4##TYPE[i] - i) < EPS); \ + +#define RUN5(TYPE,VAL) \ + TYPE a5##TYPE[SZ]; \ + TYPE b5##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a5##TYPE[i] = i; \ + b5##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + ncopysign_##TYPE (a5##TYPE, a5##TYPE, b##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (-a5##TYPE[i] - ((i & 1) ? i : -i)) < EPS); \ + +#define RUN6(TYPE,VAL) \ + TYPE a6##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a6##TYPE[i] = i; \ + ncopysigns_##TYPE (a6##TYPE, a6##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (-a6##TYPE[i] + i) < EPS); \ + +#define RUN_ALL() \ + RUN(float, 5) \ + RUN(double, 6) \ + RUN2(float, 11) \ + RUN2(double, 12) \ + RUN3(float, 16) \ + RUN3(double, 18) \ + RUN4(float, 17) \ + RUN4(double, 19) \ + RUN5(float, 123) \ + RUN5(double, 523) \ + RUN6(float, 777) \ + RUN6(double, 877) \ + +int main () +{ + RUN_ALL() +} diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv32gcv.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv32gcv.c new file mode 100644 index 00000000000..db29e37598a --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv32gcv.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-std=c99 -O3 -fno-vect-cost-model -march=rv32gcv_zvfh -mabi=ilp32d --param=riscv-autovec-preference=fixed-vlmax -ffast-math" } */ + +#include "copysign-template.h" + +/* { dg-final { scan-assembler-times {\tvfsgnj\.vv} 6 } } */ +/* The vectorizer wraps scalar variants of copysign into vector constants which + expand cannot handle currently. Therefore only expect 3 instead of 6 + vfsgnjx.vv. */ +/* { dg-final { scan-assembler-times {\tvfsgnjx\.vv} 3 } } */ +/* { dg-final { scan-assembler-times {\tvfsgnjn\.vv} 6 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv64gcv.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv64gcv.c new file mode 100644 index 00000000000..1c2504915cc --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-rv64gcv.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-std=c99 -O3 -fno-vect-cost-model -march=rv64gcv_zvfh -mabi=lp64d --param=riscv-autovec-preference=fixed-vlmax -ffast-math" } */ + +#include "copysign-template.h" + +/* { dg-final { scan-assembler-times {\tvfsgnj\.vv} 6 } } */ +/* The vectorizer wraps scalar variants of copysign into vector constants which + expand cannot handle currently. Therefore only expect 3 instead of 6 + vfsgnjx.vv. */ +/* { dg-final { scan-assembler-times {\tvfsgnjx\.vv} 3 } } */ +/* { dg-final { scan-assembler-times {\tvfsgnjn\.vv} 6 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-template.h b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-template.h new file mode 100644 index 00000000000..df2274fd3ef --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-template.h @@ -0,0 +1,78 @@ +#include + +#define TEST_TYPE(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void copysign_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE *restrict b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = __builtin_copysign##SUFFIX (a[i], b[i]); \ + } + +#define TEST_TYPE2(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void copysigns_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = __builtin_copysign##SUFFIX (a[i], b); \ + } + +#define TEST_TYPE3(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void xorsign_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE *restrict b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = a[i] * __builtin_copysign##SUFFIX (1.0, b[i]); \ + } + +#define TEST_TYPE4(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void xorsigns_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = a[i] * __builtin_copysign##SUFFIX (1.0, b); \ + } + +#define TEST_TYPE5(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void ncopysign_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE *restrict b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = -__builtin_copysign##SUFFIX (a[i], b[i]); \ + } + +#define TEST_TYPE6(TYPE, SUFFIX) \ + __attribute__((noipa)) \ + void ncopysigns_##TYPE (TYPE *restrict dst, TYPE *restrict a, \ + TYPE b, int n) \ + { \ + for (int i = 0; i < n; i++) \ + dst[i] = -__builtin_copysign##SUFFIX (a[i], b); \ + } + + +#define TEST_ALL() \ + TEST_TYPE(_Float16,f16) \ + TEST_TYPE(float,f) \ + TEST_TYPE(double,) \ + TEST_TYPE2(_Float16,f16) \ + TEST_TYPE2(float,f) \ + TEST_TYPE2(double,) \ + TEST_TYPE3(_Float16,f16) \ + TEST_TYPE3(float,f) \ + TEST_TYPE3(double,) \ + TEST_TYPE4(_Float16,f16) \ + TEST_TYPE4(float,f) \ + TEST_TYPE4(double,) \ + TEST_TYPE5(_Float16,f16) \ + TEST_TYPE5(float,f) \ + TEST_TYPE5(double,) \ + TEST_TYPE6(_Float16,f16) \ + TEST_TYPE6(float,f) \ + TEST_TYPE6(double,) \ + +TEST_ALL() diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-zvfh-run.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-zvfh-run.c new file mode 100644 index 00000000000..7aaac9794f0 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/copysign-zvfh-run.c @@ -0,0 +1,83 @@ +/* { dg-do run { target { riscv_vector && riscv_zvfh_hw } } } */ +/* { dg-additional-options "-std=c99 -fno-vect-cost-model --param=riscv-autovec-preference=fixed-vlmax -ffast-math" } */ + +#include "copysign-template.h" + +#include + +#define SZ 512 + +#define EPS 1e-6 + +#define RUN(TYPE,VAL) \ + TYPE a##TYPE[SZ]; \ + TYPE b##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a##TYPE[i] = i; \ + b##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + copysign_##TYPE (a##TYPE, a##TYPE, b##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a##TYPE[i] - ((i & 1) ? i : -i)) < EPS); \ + +#define RUN2(TYPE,VAL) \ + TYPE a2##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a2##TYPE[i] = i; \ + copysigns_##TYPE (a2##TYPE, a2##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a2##TYPE[i] + i) < EPS); \ + +#define RUN3(TYPE,VAL) \ + TYPE a3##TYPE[SZ]; \ + TYPE b3##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a3##TYPE[i] = (i & 1) ? -i : i; \ + b3##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + xorsign_##TYPE (a3##TYPE, a3##TYPE, b3##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a3##TYPE[i] + i) < EPS); \ + +#define RUN4(TYPE,VAL) \ + TYPE a4##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a4##TYPE[i] = -i; \ + xorsigns_##TYPE (a4##TYPE, a4##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (a4##TYPE[i] - i) < EPS); \ + +#define RUN5(TYPE,VAL) \ + TYPE a5##TYPE[SZ]; \ + TYPE b5##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + { \ + a5##TYPE[i] = i; \ + b5##TYPE[i] = (i & 1) ? VAL : -VAL; \ + } \ + ncopysign_##TYPE (a5##TYPE, a5##TYPE, b##TYPE, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (-a5##TYPE[i] - ((i & 1) ? i : -i)) < EPS); \ + +#define RUN6(TYPE,VAL) \ + TYPE a6##TYPE[SZ]; \ + for (int i = 0; i < SZ; i++) \ + a6##TYPE[i] = i; \ + ncopysigns_##TYPE (a6##TYPE, a6##TYPE, -VAL, SZ); \ + for (int i = 0; i < SZ; i++) \ + assert (__builtin_fabs (-a6##TYPE[i] + i) < EPS); \ + +#define RUN_ALL() \ + RUN(_Float16, 5) \ + RUN2(_Float16, 11) \ + RUN3(_Float16, 16) \ + RUN4(_Float16, 17) \ + RUN5(_Float16, 123) \ + RUN6(_Float16, 777) \ + +int main () +{ + RUN_ALL() +}