From patchwork Fri Sep 1 03:22:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: chenxiaolong X-Patchwork-Id: 137365 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp644461vqu; Thu, 31 Aug 2023 20:24:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFPKs1p+7hdqqbvoukbWytg9aiCSFkfqT/MMNVHLohb0g6hWHQhSmIO4rTThwmNhBZGFuKH X-Received: by 2002:a2e:7015:0:b0:2b5:9d78:213e with SMTP id l21-20020a2e7015000000b002b59d78213emr790682ljc.22.1693538656860; Thu, 31 Aug 2023 20:24:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693538656; cv=none; d=google.com; s=arc-20160816; b=Fu6xUd/MMd30v/y4mtE1pJaE0AwZ11WGowqS7E3E/kTipIlhhITXNqfntBE4FQDEri b5kR8nAfqVZr0kI4M75oNR4IuFUtFXDaK5g/cDQ/vzg3XlDHJ2kAuQLAJcqJ/qVhN7T0 WT1bmmnLlDMxwVV8YhPkRilyIy2i9z7/oDXjf5ORWPNz7vhpj+3VPOpP9wDCK0mnXLT7 Z+aJj2be2/sz+IcYE1XlgT/JqZ3uXpjmOxo6zOME1SlEQx5oTt+2CC/SMjFwRePOVufD nhSgqrXstKdU5VE//ZdKvTk2JCW/OQ+Jt+dEC5GHTXn1Z28s/HzvMZRL56ONFJRDSXPe zfMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dmarc-filter :delivered-to; bh=Y46EDWscwmG7fX6gt5XR8HB14c+vBcVTXUU7kLTZLsA=; fh=b4bHiRhwmyngBnK1A0teGZR99sACoeEAslk5F3Bw8oE=; b=HUEFagkpAqz6/bR+RJ4MyQ5aMgQGOWpO9bHV1MqJzHO+SmyI/Enp8KPqr0+FB+QScg mN5ukyP0NU8J4IHailOi49XKT7JhWZzB6X+NMrkF+olwpX4CuZfqAKhRNvMJEQa+CebT FquS8eVQzrvyfZ7YexY4vBB4265X+RnnmpFyxYxIOKocBbNEcAC8r+XKGCH/acJv21L2 c4duRT45H4KFdz9qXqmCucxCFEfrvThMPUUs+fORAt+vi2uI0JyC2+JkilYUbWKsAN4n yATDLWsPi6TPEQVW/Ggm4tCO0JH8D+AHO2PZl8ceHtc4Iy/9C1r9OE8mLKWINxbcubZ+ U1UA== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g12-20020a1709063b0c00b009a19698e288si1845199ejf.543.2023.08.31.20.24.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 20:24:16 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB049385701D for ; Fri, 1 Sep 2023 03:23:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id B3FC83857725 for ; Fri, 1 Sep 2023 03:23:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B3FC83857725 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=loongson.cn Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbukO-0004pR-5H for gcc-patches@gcc.gnu.org; Thu, 31 Aug 2023 23:23:05 -0400 Received: from loongson.cn (unknown [10.10.130.252]) by gateway (Coremail) with SMTP id _____8BxY_AIWfFko6wdAA--.60652S3; Fri, 01 Sep 2023 11:22:48 +0800 (CST) Received: from slurm-master.loongson.cn (unknown [10.10.130.252]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxxswEWfFk75BoAA--.33179S4; Fri, 01 Sep 2023 11:22:44 +0800 (CST) From: chenxiaolong To: gcc-patches@gcc.gnu.org Subject: [PATCH v6] LoongArch:Implement 128-bit floating point functions in gcc. Date: Fri, 1 Sep 2023 11:22:42 +0800 Message-Id: <20230901032242.57839-1-chenxiaolong@loongson.cn> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxxswEWfFk75BoAA--.33179S4 X-CM-SenderInfo: hfkh05xldrz0tqj6z05rqj20fqof0/1tbiAQAABWTwEzQDiQAJs0 X-Coremail-Antispam: 1Uk129KBj93XoW3Gr1DXry5KF1xWw4UWF15GFX_yoWfXrW7pF W7Cr1YyrZ7JFs3Zr1fJ3Z5ArnxAr47Gw4xZr9xKryUuw4UXr92qF15trW7tF15J3yrWr1S v390qay2y3W8A3gCm3ZEXasCq-sJn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVWxJr0_GcWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0cIa020Ex4CE44I27w Aqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_JrI_JrylYx0Ex4A2jsIE 14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x 0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E 7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcV C0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8j-e5UUUUU== Received-SPF: pass client-ip=114.242.206.163; envelope-from=chenxiaolong@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_FAIL, SPF_HELO_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: 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: , Cc: chenxiaolong , xuchenghua@loongson.cn, chenglulu@loongson.cn, i@xen0n.name Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775803990781118220 X-GMAIL-MSGID: 1775803990781118220 Brief version history of patch set: v1 -> v2: According to the GNU code specification, adjust the format of the function implementation with "q" as the suffix function. v2 - >v3: 1.On the LoongArch architecture, refer to the functionality of 64-bit functions and modify the underlying implementation of __builtin_{nanq, nansq} functions in libgcc. 2.Modify the function's instruction template to use some instructions such as "bstrins.d" to implement the 128-bit __builtin_{fabsq, copysignq} function instead of calling libgcc library support, so as to better play the machine's performance. v3 -> v4: 1.The above v1,v2, and v3 all implement 128-bit floating-point functions with "q" as the suffix, but it is an older implementation. The v4 version completely abandoned the old implementation by associating the 128-bit floating-point function with the "q" suffix with the "f128" function that already existed in GCC. 2.Modify the code so that both "__float128" and "_Float128" function types can be supported in compiler gcc. 3.Associating a function with the suffix "q" to the "f128" function allows two different forms of the function to produce the same effect, For example, __builtin_{huge_{valq/valf128},{infq/inff128},{nanq/nanf128},{nansq/nansf128}, {fabsq/fabsf128}}. 4.For the _builtin_copysignq function, do not call the new "f128" implementation, but use the "bstrins" and other instructions in the machine description file to implement the function function, the result is that the number of assembly instructions can be reduced and the function optimization to achieve the optimal effect. v4 -> v5: Removed the v4 implementation of the __builtin_fabsf128() function added to LoongArch.md. v5 -> v6: 1.Modify the test cases in the math-float-128.c file. 2.Removed the v5 implementation of the __builtin_copysignf128() function added to LoongArch.md. During implementation, float128_type_node is bound with the type "__float128" so that the compiler can correctly identify the type of the function. The "q" suffix is associated with the "f128" function, which makes GCC more flexible to support different user input cases, implementing functions such as __builtin_{huge_valq, infq, fabsq, copysignq, nanq, nansq}. gcc/ChangeLog: * config/loongarch/loongarch-builtins.cc (loongarch_init_builtins): Associate the __float128 type to float128_type_node so that it can be recognized by the compiler. * config/loongarch/loongarch-c.cc (loongarch_cpu_cpp_builtins): Add the flag "FLOAT128_TYPE" to gcc and associate a function with the suffix "q" to "f128". * doc/extend.texi:Added support for 128-bit floating-point functions on the LoongArch architecture. gcc/testsuite/ChangeLog: * gcc.target/loongarch/math-float-128.c: New test. --- gcc/config/loongarch/loongarch-builtins.cc | 5 ++ gcc/config/loongarch/loongarch-c.cc | 11 +++ gcc/doc/extend.texi | 20 ++++- .../gcc.target/loongarch/math-float-128.c | 81 +++++++++++++++++++ 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.target/loongarch/math-float-128.c diff --git a/gcc/config/loongarch/loongarch-builtins.cc b/gcc/config/loongarch/loongarch-builtins.cc index b929f224dfa..58b612bf445 100644 --- a/gcc/config/loongarch/loongarch-builtins.cc +++ b/gcc/config/loongarch/loongarch-builtins.cc @@ -256,6 +256,11 @@ loongarch_init_builtins (void) unsigned int i; tree type; + /* Register the type float128_type_node as a built-in type and + give it an alias "__float128". */ + (*lang_hooks.types.register_builtin_type) (float128_type_node, + "__float128"); + /* Iterate through all of the bdesc arrays, initializing all of the builtin functions. */ for (i = 0; i < ARRAY_SIZE (loongarch_builtins); i++) diff --git a/gcc/config/loongarch/loongarch-c.cc b/gcc/config/loongarch/loongarch-c.cc index 67911b78f28..6ffbf748316 100644 --- a/gcc/config/loongarch/loongarch-c.cc +++ b/gcc/config/loongarch/loongarch-c.cc @@ -99,6 +99,17 @@ loongarch_cpu_cpp_builtins (cpp_reader *pfile) else builtin_define ("__loongarch_frlen=0"); + /* Add support for FLOAT128_TYPE on the LoongArch architecture. */ + builtin_define ("__FLOAT128_TYPE__"); + + /* Map the old _Float128 'q' builtins into the new 'f128' builtins. */ + builtin_define ("__builtin_fabsq=__builtin_fabsf128"); + builtin_define ("__builtin_copysignq=__builtin_copysignf128"); + builtin_define ("__builtin_nanq=__builtin_nanf128"); + builtin_define ("__builtin_nansq=__builtin_nansf128"); + builtin_define ("__builtin_infq=__builtin_inff128"); + builtin_define ("__builtin_huge_valq=__builtin_huge_valf128"); + /* Native Data Sizes. */ builtin_define_with_int_value ("_LOONGARCH_SZINT", INT_TYPE_SIZE); builtin_define_with_int_value ("_LOONGARCH_SZLONG", LONG_TYPE_SIZE); diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 80dd2a84b0b..947c05babc9 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1093,10 +1093,10 @@ types. As an extension, GNU C and GNU C++ support additional floating types, which are not supported by all targets. @itemize @bullet -@item @code{__float128} is available on i386, x86_64, IA-64, and -hppa HP-UX, as well as on PowerPC GNU/Linux targets that enable +@item @code{__float128} is available on i386, x86_64, IA-64, LoongArch +and hppa HP-UX, as well as on PowerPC GNU/Linux targets that enable the vector scalar (VSX) instruction set. @code{__float128} supports -the 128-bit floating type. On i386, x86_64, PowerPC, and IA-64 +the 128-bit floating type. On i386, x86_64, PowerPC, LoongArch and IA-64, other than HP-UX, @code{__float128} is an alias for @code{_Float128}. On hppa and IA-64 HP-UX, @code{__float128} is an alias for @code{long double}. @@ -16657,6 +16657,20 @@ function you need to include @code{larchintrin.h}. void __break (imm0_32767) @end smallexample +Additional built-in functions are available for LoongArch family +processors to efficiently use 128-bit floating-point (__float128) +values. + +The following are the basic built-in functions supported. +@smallexample +__float128 __builtin_fabsq (__float128); +__float128 __builtin_copysignq (__float128, __float128); +__float128 __builtin_infq (void); +__float128 __builtin_huge_valq (void); +__float128 __builtin_nanq (void); +__float128 __builtin_nansq (void); +@end smallexample + @node MIPS DSP Built-in Functions @subsection MIPS DSP Built-in Functions diff --git a/gcc/testsuite/gcc.target/loongarch/math-float-128.c b/gcc/testsuite/gcc.target/loongarch/math-float-128.c new file mode 100644 index 00000000000..387566a57c9 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/math-float-128.c @@ -0,0 +1,81 @@ +/* { dg-do compile } */ +/* { dg-options " -march=loongarch64 -O2 " } */ +/* { dg-final { scan-assembler-not "my_fabsq2:.*\\bl\t%plt\\(__builtin_fabsq\\).*my_fabsq2" } } */ +/* { dg-final { scan-assembler-not "my_copysignq2:.*\\bl\t%plt\\(__builtin_copysignq\\).*my_copysignq2" } } */ +/* { dg-final { scan-assembler-not "my_infq2:.*\\bl\t%plt\\(__builtin_infq\\).*my_infq2" } } */ +/* { dg-final { scan-assembler-not "my_huge_valq2:.*\\bl\t%plt\\(__builtin_huge_valq\\).*my_huge_valq2" } } */ +/* { dg-final { scan-assembler-not "my_nanq2:.*\\bl\t%plt\\(__builtin_nanq\\).*my_nanq2" } } */ +/* { dg-final { scan-assembler-not "my_nansq2:.*\\bl\t%plt\\(__builtin_nansq\\).*my_nansq2" } } */ + +__float128 +my_fabsq1 (__float128 a) +{ + return __builtin_fabsq (a); +} + +_Float128 +my_fabsq2 (_Float128 a) +{ + return __builtin_fabsq (a); +} + +__float128 +my_copysignq1 (__float128 a, __float128 b) +{ + return __builtin_copysignq (a, b); +} + +_Float128 +my_copysignq2 (_Float128 a, _Float128 b) +{ + return __builtin_copysignq (a, b); +} + +__float128 +my_infq1 (void) +{ + return __builtin_infq (); +} + +_Float128 +my_infq2 (void) +{ + return __builtin_infq (); +} + +__float128 +my_huge_valq1 (void) +{ + return __builtin_huge_valq (); +} + +_Float128 +my_huge_valq2 (void) +{ + return __builtin_huge_valq (); +} + +__float128 +my_nanq1 (void) +{ + return __builtin_nanq (""); +} + +_Float128 +my_nanq2 (void) +{ + return __builtin_nanq (""); +} + +__float128 +my_nansq1 (void) +{ + return __builtin_nansq (""); +} + +_Float128 +my_nansq2 (void) +{ + return __builtin_nansq (""); +} +