1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-09-11 12:10:50 +03:00
Files
glibc/sysdeps/loongarch/fpu/s_fmaximum_template.c
Xi Ruoyao 0195552e15 LoongArch: Optimize f{max,min}imum{,f}
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>
2025-02-28 11:44:19 +08:00

40 lines
1.3 KiB
C

/* 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