[v2,01/10] LoongArch: Hard Float Support for functions {ll/l/ }rint{f/ }.

Message ID 20221107140523.896735-2-tangxiaolin@loongson.cn
State Superseded
Headers
Series LoongArch: Hard Float Support Of Math Functions. |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Xiaolin Tang Nov. 7, 2022, 2:05 p.m. UTC
  Use hardware Floating-point instruction frint.{s/d} to implement
functions {ll/l/ }rint{f/ }.

       *  sysdeps/loongarch/fpu/s_llrint.c: New file.
       *  sysdeps/loongarch/fpu/s_llrintf.c: Likewise.
       *  sysdeps/loongarch/fpu/s_lrint.c: Likewise.
       *  sysdeps/loongarch/fpu/s_lrintf.c: Likewise.
       *  sysdeps/loongarch/fpu/s_rint.c: Likewise.
       *  sysdeps/loongarch/fpu/s_rintf.c: Likewise.
---
 sysdeps/loongarch/fpu/s_llrint.c  | 29 +++++++++++++++++++++++++++++
 sysdeps/loongarch/fpu/s_llrintf.c | 29 +++++++++++++++++++++++++++++
 sysdeps/loongarch/fpu/s_lrint.c   | 29 +++++++++++++++++++++++++++++
 sysdeps/loongarch/fpu/s_lrintf.c  | 29 +++++++++++++++++++++++++++++
 sysdeps/loongarch/fpu/s_rint.c    | 29 +++++++++++++++++++++++++++++
 sysdeps/loongarch/fpu/s_rintf.c   | 29 +++++++++++++++++++++++++++++
 6 files changed, 174 insertions(+)
 create mode 100644 sysdeps/loongarch/fpu/s_llrint.c
 create mode 100644 sysdeps/loongarch/fpu/s_llrintf.c
 create mode 100644 sysdeps/loongarch/fpu/s_lrint.c
 create mode 100644 sysdeps/loongarch/fpu/s_lrintf.c
 create mode 100644 sysdeps/loongarch/fpu/s_rint.c
 create mode 100644 sysdeps/loongarch/fpu/s_rintf.c
  

Comments

Xi Ruoyao Nov. 7, 2022, 2:38 p.m. UTC | #1
On Mon, 2022-11-07 at 22:05 +0800, Xiaolin Tang wrote:

> +long long int
> +__llrint (double x)
> +{
> +  asm volatile ("frint.d \t%0, %1" : "=f" (x) : "f" (x));
> +  return x;
> +}

GCC trunk generates:

__llrint:
	frint.d 	$f0, $f0
	ftintrz.l.d $f0,$f0
	movfr2gr.d	$r4,$f0
	jr	$r1

It's not optimal.  The optimal way is:

__llrint:
	ftint.l.d 	$f0, $f0
	movfr2gr.d	$r4,$f0
	jr	$r1

I have a GCC patch to expand __builtin_llrint to ftint.l.d and
movgr2fr.d (with -fno-math-errno, which is enabled by Glibc building
system).

But I need some time to test my patch correctly: unfortunately I'm now
hitting some "random crashes" running Expect on LoongArch so I can't
complete GCC regression test.
  
Adhemerval Zanella Nov. 7, 2022, 4:36 p.m. UTC | #2
On 07/11/22 11:38, Xi Ruoyao wrote:
> On Mon, 2022-11-07 at 22:05 +0800, Xiaolin Tang wrote:
> 
>> +long long int
>> +__llrint (double x)
>> +{
>> +  asm volatile ("frint.d \t%0, %1" : "=f" (x) : "f" (x));
>> +  return x;
>> +}
> 
> GCC trunk generates:
> 
> __llrint:
> 	frint.d 	$f0, $f0
> 	ftintrz.l.d $f0,$f0
> 	movfr2gr.d	$r4,$f0
> 	jr	$r1
> 
> It's not optimal.  The optimal way is:
> 
> __llrint:
> 	ftint.l.d 	$f0, $f0
> 	movfr2gr.d	$r4,$f0
> 	jr	$r1
> 
> I have a GCC patch to expand __builtin_llrint to ftint.l.d and
> movgr2fr.d (with -fno-math-errno, which is enabled by Glibc building
> system).
> 
> But I need some time to test my patch correctly: unfortunately I'm now
> hitting some "random crashes" running Expect on LoongArch so I can't
> complete GCC regression test.

A compiler builtin would indeed more desirable, since it would allows to
move it on generic code and use a math-use-builtins-llrint.h instead of
arch-specific code.
  

Patch

diff --git a/sysdeps/loongarch/fpu/s_llrint.c b/sysdeps/loongarch/fpu/s_llrint.c
new file mode 100644
index 0000000000..cf787f50ed
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_llrint.c
@@ -0,0 +1,29 @@ 
+/* llrint().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-double.h>
+
+long long int
+__llrint (double x)
+{
+  asm volatile ("frint.d \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_double (__llrint, llrint)
diff --git a/sysdeps/loongarch/fpu/s_llrintf.c b/sysdeps/loongarch/fpu/s_llrintf.c
new file mode 100644
index 0000000000..a258e635ce
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_llrintf.c
@@ -0,0 +1,29 @@ 
+/* llrintf().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-float.h>
+
+long long int
+__llrintf (float x)
+{
+  asm volatile ("frint.s \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_float (__llrint, llrint)
diff --git a/sysdeps/loongarch/fpu/s_lrint.c b/sysdeps/loongarch/fpu/s_lrint.c
new file mode 100644
index 0000000000..10d43218f8
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_lrint.c
@@ -0,0 +1,29 @@ 
+/* lrint().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-double.h>
+
+long int
+__lrint (double x)
+{
+  asm volatile ("frint.d \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_double (__lrint, lrint)
diff --git a/sysdeps/loongarch/fpu/s_lrintf.c b/sysdeps/loongarch/fpu/s_lrintf.c
new file mode 100644
index 0000000000..7168eda7cb
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_lrintf.c
@@ -0,0 +1,29 @@ 
+/* lrintf().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-float.h>
+
+long int
+__lrintf (float x)
+{
+  asm volatile ("frint.s \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_float (__lrint, lrint)
diff --git a/sysdeps/loongarch/fpu/s_rint.c b/sysdeps/loongarch/fpu/s_rint.c
new file mode 100644
index 0000000000..429d5d1176
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_rint.c
@@ -0,0 +1,29 @@ 
+/* rint().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-double.h>
+
+double
+__rint (double x)
+{
+  asm volatile ("frint.d \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_double (__rint, rint)
diff --git a/sysdeps/loongarch/fpu/s_rintf.c b/sysdeps/loongarch/fpu/s_rintf.c
new file mode 100644
index 0000000000..b3faba2027
--- /dev/null
+++ b/sysdeps/loongarch/fpu/s_rintf.c
@@ -0,0 +1,29 @@ 
+/* rintf().  LoongArch version.
+   Copyright (C) 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
+   <https://www.gnu.org/licenses/>.  */
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <libm-alias-float.h>
+
+float
+__rintf (float x)
+{
+  asm volatile ("frint.s \t%0, %1" : "=f" (x) : "f" (x));
+  return x;
+}
+libm_alias_float (__rint, rint)