[PATCH v2 1/4] LoongArch: Optimize f{max,min}imum{,f}

caiyinyu caiyinyu@loongson.cn
Thu Feb 27 10:59:41 GMT 2025


LGTM

在 2025/2/24 下午8:49, Xi Ruoyao 写道:
> The code now looks like:
>
> 	fclass.s        $fa2, $fa0
> 	movfr2gr.s      $t0, $fa2
> 	slli.w          $t0, $t0, 0x0
> 	fclass.s        $fa2, $fa1
> 	movfr2gr.s      $t1, $fa2
> 	or              $t0, $t0, $t1
> 	andi            $t0, $t0, 0x3
> 	bnez            $t0, 1f
> 	fmin.s          $fa0, $fa0, $fa1
> 	ret
> 	1:
> 	fmul.s		$fa0, $fa0, $fa1
> 	ret
>
> This looks really bad, with expensive movfr2gr instructions, redundant
> sign-extensions and masking (arguably it's a compiler
> missed-optimzation), and a branch.  Rewrite it with inline assembly:
>
> 	fcmp.cor.s      $fcc0, $fa0, $fa0
> 	fcmp.cor.s      $fcc1, $fa1, $fa1
> 	fsel            $fa2, $fa0, $fa1, $fcc0
> 	fsel            $fa0, $fa1, $fa0, $fcc1
> 	fmax.s          $fa0, $fa2, $fa0
> 	ret
>
> Note that we cannot make it more readable with
> "double a = __builtin_isnanf (x) ? y : x" because this C statement only
> happens to produce what we want with https://gcc.gnu.org/PR66462, if
> this bug is fixed in the future the generated code may change.
>
> Signed-off-by: Xi Ruoyao <xry111@xry111.site>
> ---
>   .../loongarch/fpu/math-type-macros-double.h   | 21 ++++++++++
>   .../loongarch/fpu/math-type-macros-float.h    | 21 ++++++++++
>   sysdeps/loongarch/fpu/s_fmaximum.c            | 40 -------------------
>   sysdeps/loongarch/fpu/s_fmaximum_template.c   | 39 ++++++++++++++++++
>   sysdeps/loongarch/fpu/s_fmaximumf.c           | 40 -------------------
>   sysdeps/loongarch/fpu/s_fminimum.c            | 40 -------------------
>   sysdeps/loongarch/fpu/s_fminimum_template.c   | 39 ++++++++++++++++++
>   sysdeps/loongarch/fpu/s_fminimumf.c           | 40 -------------------
>   8 files changed, 120 insertions(+), 160 deletions(-)
>   create mode 100644 sysdeps/loongarch/fpu/math-type-macros-double.h
>   create mode 100644 sysdeps/loongarch/fpu/math-type-macros-float.h
>   delete mode 100644 sysdeps/loongarch/fpu/s_fmaximum.c
>   create mode 100644 sysdeps/loongarch/fpu/s_fmaximum_template.c
>   delete mode 100644 sysdeps/loongarch/fpu/s_fmaximumf.c
>   delete mode 100644 sysdeps/loongarch/fpu/s_fminimum.c
>   create mode 100644 sysdeps/loongarch/fpu/s_fminimum_template.c
>   delete mode 100644 sysdeps/loongarch/fpu/s_fminimumf.c
>
> diff --git a/sysdeps/loongarch/fpu/math-type-macros-double.h b/sysdeps/loongarch/fpu/math-type-macros-double.h
> new file mode 100644
> index 0000000000..dd53885144
> --- /dev/null
> +++ b/sysdeps/loongarch/fpu/math-type-macros-double.h
> @@ -0,0 +1,21 @@
> +/* Helper macros for double variants of type generic functions of libm,
> +   LoongArch version.
> +   Copyright (C) 2025 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/>.  */
> +
> +#include_next <math-type-macros-double.h>
> +#define INSN_FMT "d"
> diff --git a/sysdeps/loongarch/fpu/math-type-macros-float.h b/sysdeps/loongarch/fpu/math-type-macros-float.h
> new file mode 100644
> index 0000000000..ee5f7847c8
> --- /dev/null
> +++ b/sysdeps/loongarch/fpu/math-type-macros-float.h
> @@ -0,0 +1,21 @@
> +/* Helper macros for float variants of type generic functions of libm,
> +   LoongArch version.
> +   Copyright (C) 2025 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/>.  */
> +
> +#include_next <math-type-macros-float.h>
> +#define INSN_FMT "s"
> diff --git a/sysdeps/loongarch/fpu/s_fmaximum.c b/sysdeps/loongarch/fpu/s_fmaximum.c
> deleted file mode 100644
> index 07fc7214a1..0000000000
> --- a/sysdeps/loongarch/fpu/s_fmaximum.c
> +++ /dev/null
> @@ -1,40 +0,0 @@
> -/* fmaximum().  LoongArch version.
> -   Copyright (C) 2022-2025 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>
> -#include <fpu_control.h>
> -
> -double
> -__fmaximum (double x, double y)
> -{
> -  int x_cond;
> -  int y_cond;
> -  asm volatile ("fclass.d \t%0, %1" : "=f" (x_cond) : "f" (x));
> -  asm volatile ("fclass.d \t%0, %1" : "=f" (y_cond) : "f" (y));
> -
> -  if (__glibc_unlikely((x_cond | y_cond) & _FCLASS_NAN))
> -      return x * y;
> -  else
> -    {
> -      asm volatile ("fmax.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
> -      return x;
> -    }
> -}
> -libm_alias_double (__fmaximum, fmaximum)
> diff --git a/sysdeps/loongarch/fpu/s_fmaximum_template.c b/sysdeps/loongarch/fpu/s_fmaximum_template.c
> new file mode 100644
> index 0000000000..701850ea73
> --- /dev/null
> +++ b/sysdeps/loongarch/fpu/s_fmaximum_template.c
> @@ -0,0 +1,39 @@
> +/* Return maximum of X and Y.  LoongArch version.
> +   Copyright (C) 2025 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/>.  */
> +
> +#ifndef INSN_FMT
> +#include_next <s_fmaximum_template.c>
> +#else
> +
> +#include <math.h>
> +
> +FLOAT
> +M_DECL_FUNC (__fmaximum) (FLOAT x, FLOAT y)
> +{
> +  FLOAT a, b;
> +  asm("fcmp.cor." INSN_FMT "\t$fcc0, %2, %2\n\t"
> +      "fcmp.cor." INSN_FMT "\t$fcc1, %3, %3\n\t"
> +      "fsel"		   "\t%0, %2, %3, $fcc0\n\t"
> +      "fsel"		   "\t%1, %3, %2, $fcc1\n\t"
> +      "fmax."	  INSN_FMT "\t%1, %0, %1"
> +      : "=&f" (a), "=f" (b) : "f" (x), "f" (y) : "fcc0", "fcc1");
> +  return b;
> +}
> +declare_mgen_alias (__fmaximum, fmaximum);
> +
> +#endif
> diff --git a/sysdeps/loongarch/fpu/s_fmaximumf.c b/sysdeps/loongarch/fpu/s_fmaximumf.c
> deleted file mode 100644
> index a518ccf348..0000000000
> --- a/sysdeps/loongarch/fpu/s_fmaximumf.c
> +++ /dev/null
> @@ -1,40 +0,0 @@
> -/* fmaximumf().  LoongArch version.
> -   Copyright (C) 2022-2025 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>
> -#include <fpu_control.h>
> -
> -float
> -__fmaximumf (float x, float y)
> -{
> -  int x_cond;
> -  int y_cond;
> -  asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x));
> -  asm volatile ("fclass.s \t%0, %1" : "=f" (y_cond) : "f" (y));
> -
> -  if (__glibc_unlikely((x_cond | y_cond) & _FCLASS_NAN))
> -      return x * y;
> -  else
> -    {
> -      asm volatile ("fmax.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
> -      return x;
> -    }
> -}
> -libm_alias_float (__fmaximum, fmaximum)
> diff --git a/sysdeps/loongarch/fpu/s_fminimum.c b/sysdeps/loongarch/fpu/s_fminimum.c
> deleted file mode 100644
> index a63c357957..0000000000
> --- a/sysdeps/loongarch/fpu/s_fminimum.c
> +++ /dev/null
> @@ -1,40 +0,0 @@
> -/* fminimum().  LoongArch version.
> -   Copyright (C) 2022-2025 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>
> -#include <fpu_control.h>
> -
> -double
> -__fminimum (double x, double y)
> -{
> -  int x_cond;
> -  int y_cond;
> -  asm volatile ("fclass.d \t%0, %1" : "=f" (x_cond) : "f" (x));
> -  asm volatile ("fclass.d \t%0, %1" : "=f" (y_cond) : "f" (y));
> -
> -  if (__glibc_unlikely((x_cond | y_cond) & _FCLASS_NAN))
> -      return x * y;
> -  else
> -    {
> -      asm volatile ("fmin.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
> -      return x;
> -    }
> -}
> -libm_alias_double (__fminimum, fminimum)
> diff --git a/sysdeps/loongarch/fpu/s_fminimum_template.c b/sysdeps/loongarch/fpu/s_fminimum_template.c
> new file mode 100644
> index 0000000000..80db72ee3c
> --- /dev/null
> +++ b/sysdeps/loongarch/fpu/s_fminimum_template.c
> @@ -0,0 +1,39 @@
> +/* Return minimum of X and Y.  LoongArch version.
> +   Copyright (C) 2025 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/>.  */
> +
> +#ifndef INSN_FMT
> +#include_next <s_fminimum_template.c>
> +#else
> +
> +#include <math.h>
> +
> +FLOAT
> +M_DECL_FUNC (__fminimum) (FLOAT x, FLOAT y)
> +{
> +  FLOAT a, b;
> +  asm("fcmp.cor." INSN_FMT "\t$fcc0, %2, %2\n\t"
> +      "fcmp.cor." INSN_FMT "\t$fcc1, %3, %3\n\t"
> +      "fsel"		   "\t%0, %2, %3, $fcc0\n\t"
> +      "fsel"		   "\t%1, %3, %2, $fcc1\n\t"
> +      "fmin."	  INSN_FMT "\t%1, %0, %1"
> +      : "=&f" (a), "=f" (b) : "f" (x), "f" (y) : "fcc0", "fcc1");
> +  return b;
> +}
> +declare_mgen_alias (__fminimum, fminimum);
> +
> +#endif
> diff --git a/sysdeps/loongarch/fpu/s_fminimumf.c b/sysdeps/loongarch/fpu/s_fminimumf.c
> deleted file mode 100644
> index 973a9f75d1..0000000000
> --- a/sysdeps/loongarch/fpu/s_fminimumf.c
> +++ /dev/null
> @@ -1,40 +0,0 @@
> -/* fminimumf().  LoongArch version.
> -   Copyright (C) 2022-2025 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>
> -#include <fpu_control.h>
> -
> -float
> -__fminimumf (float x, float y)
> -{
> -  int x_cond;
> -  int y_cond;
> -  asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x));
> -  asm volatile ("fclass.s \t%0, %1" : "=f" (y_cond) : "f" (y));
> -
> -  if (__glibc_unlikely((x_cond | y_cond) & _FCLASS_NAN))
> -      return x * y;
> -  else
> -    {
> -      asm volatile ("fmin.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
> -      return x;
> -    }
> -}
> -libm_alias_float (__fminimum, fminimum)



More information about the Libc-alpha mailing list