diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index a14ab14ac24feeabfd1de32f4495ef41ca7c9178..eb127c346a3db2e8d50d9028451afd9256524c26 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -38,6 +38,9 @@ UNSPEC_FMAX UNSPEC_FMIN UNSPEC_FCOPYSIGN + UNSPEC_FTINT + UNSPEC_FTINTRM + UNSPEC_FTINTRP ;; Override return address for exception handling. UNSPEC_EH_RETURN @@ -374,6 +377,11 @@ (define_mode_iterator ANYF [(SF "TARGET_HARD_FLOAT") (DF "TARGET_DOUBLE_FLOAT")]) +;; Iterator for fixed-point modes which can be hold by a hardware +;; floating-point register. +(define_mode_iterator ANYFI [(SI "TARGET_HARD_FLOAT") + (DI "TARGET_DOUBLE_FLOAT")]) + ;; A mode for which moves involving FPRs may need to be split. (define_mode_iterator SPLITF [(DF "!TARGET_64BIT && TARGET_DOUBLE_FLOAT") @@ -515,6 +523,19 @@ (define_code_attr sel [(eq "masknez") (ne "maskeqz")]) (define_code_attr selinv [(eq "maskeqz") (ne "masknez")]) +;; Iterator and attributes for floating-point to fixed-point conversion +;; instructions. +(define_int_iterator LRINT [UNSPEC_FTINT UNSPEC_FTINTRM UNSPEC_FTINTRP]) +(define_int_attr lrint_pattern [(UNSPEC_FTINT "lrint") + (UNSPEC_FTINTRM "lfloor") + (UNSPEC_FTINTRP "lceil")]) +(define_int_attr lrint_submenmonic [(UNSPEC_FTINT "") + (UNSPEC_FTINTRM "rm") + (UNSPEC_FTINTRP "rp")]) +(define_int_attr lrint_allow_inexact [(UNSPEC_FTINT "1") + (UNSPEC_FTINTRM "0") + (UNSPEC_FTINTRP "0")]) + ;; ;; .................... ;; @@ -2022,6 +2043,19 @@ [(set_attr "type" "fcvt") (set_attr "mode" "<MODE>")]) +;; Convert floating-point numbers to integers +(define_insn "<lrint_pattern><ANYF:mode><ANYFI:mode>2" + [(set (match_operand:ANYFI 0 "register_operand" "=f") + (unspec:ANYFI [(match_operand:ANYF 1 "register_operand" "f")] + LRINT))] + "TARGET_HARD_FLOAT && + (<lrint_allow_inexact> + || flag_fp_int_builtin_inexact + || !flag_trapping_math)" + "ftint<lrint_submenmonic>.<ANYFI:ifmt>.<ANYF:fmt> %0,%1" + [(set_attr "type" "fcvt") + (set_attr "mode" "<ANYF:MODE>")]) + ;; Load the low word of operand 0 with operand 1. (define_insn "load_low<mode>" [(set (match_operand:SPLITF 0 "register_operand" "=f,f") diff --git a/gcc/testsuite/gcc.target/loongarch/ftint-no-inexact.c b/gcc/testsuite/gcc.target/loongarch/ftint-no-inexact.c new file mode 100644 index 0000000000000000000000000000000000000000..88b83a9c056896a8f6bbc59708d35ef00d28d481 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/ftint-no-inexact.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact" } */ +/* { dg-final { scan-assembler "ftint\\.l\\.s" } } */ +/* { dg-final { scan-assembler "ftint\\.l\\.d" } } */ +/* { dg-final { scan-assembler-not "ftintrm\\.l\\.s" } } */ +/* { dg-final { scan-assembler-not "ftintrm\\.l\\.d" } } */ +/* { dg-final { scan-assembler-not "ftintrp\\.l\\.s" } } */ +/* { dg-final { scan-assembler-not "ftintrp\\.l\\.d" } } */ + +long +my_lrint (double a) +{ + return __builtin_lrint (a); +} + +long +my_lrintf (float a) +{ + return __builtin_lrintf (a); +} + +long +my_lfloor (double a) +{ + return __builtin_lfloor (a); +} + +long +my_lfloorf (float a) +{ + return __builtin_lfloorf (a); +} + +long +my_lceil (double a) +{ + return __builtin_lceil (a); +} + +long +my_lceilf (float a) +{ + return __builtin_lceilf (a); +} diff --git a/gcc/testsuite/gcc.target/loongarch/ftint.c b/gcc/testsuite/gcc.target/loongarch/ftint.c new file mode 100644 index 0000000000000000000000000000000000000000..7a326a454d88bd3712e4c0c7522b126a0f836e99 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/ftint.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact" } */ +/* { dg-final { scan-assembler "ftint\\.l\\.s" } } */ +/* { dg-final { scan-assembler "ftint\\.l\\.d" } } */ +/* { dg-final { scan-assembler "ftintrm\\.l\\.s" } } */ +/* { dg-final { scan-assembler "ftintrm\\.l\\.d" } } */ +/* { dg-final { scan-assembler "ftintrp\\.l\\.s" } } */ +/* { dg-final { scan-assembler "ftintrp\\.l\\.d" } } */ + +long +my_lrint (double a) +{ + return __builtin_lrint (a); +} + +long +my_lrintf (float a) +{ + return __builtin_lrintf (a); +} + +long +my_lfloor (double a) +{ + return __builtin_lfloor (a); +} + +long +my_lfloorf (float a) +{ + return __builtin_lfloorf (a); +} + +long +my_lceil (double a) +{ + return __builtin_lceil (a); +} + +long +my_lceilf (float a) +{ + return __builtin_lceilf (a); +}