diff mbox series

[v1,4/4] RISC-V: Add testcases for form 1 of scalar signed SAT_TRUNC

Message ID 20241008083325.190374-1-pan2.li@intel.com
State New
Headers show
Series [v1,1/4] Match: Support form 1 for scalar signed integer SAT_TRUNC | expand

Commit Message

Li, Pan2 Oct. 8, 2024, 8:33 a.m. UTC
From: Pan Li <pan2.li@intel.com>

Form 1:
  #define DEF_SAT_S_TRUNC_FMT_1(WT, NT, NT_MIN, NT_MAX) \
  NT __attribute__((noinline))                          \
  sat_s_trunc_##WT##_to_##NT##_fmt_1 (WT x)             \
  {                                                     \
    NT trunc = (NT)x;                                   \
    return (WT)NT_MIN <= x && x <= (WT)NT_MAX           \
      ? trunc                                           \
      : x < 0 ? NT_MIN : NT_MAX;                        \
  }

The below test are passed for this patch.
* The rv64gcv fully regression test.

It is test only patch and obvious up to a point, will commit it
directly if no comments in next 48H.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/sat_arith.h: Add test helper macros.
	* gcc.target/riscv/sat_arith_data.h: Add test data for SAT_TRUNC.
	* gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c: New test.
	* gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c: New test.
	* gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c: New test.
	* gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c: New test.
	* gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c: New test.
	* gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c: New test.
	* gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c: New test.

Signed-off-by: Pan Li <pan2.li@intel.com>
---
 gcc/testsuite/gcc.target/riscv/sat_arith.h    |  15 +++
 .../gcc.target/riscv/sat_arith_data.h         | 110 ++++++++++++++++++
 .../riscv/sat_s_trunc-1-i16-to-i8.c           |  26 +++++
 .../riscv/sat_s_trunc-1-i32-to-i16.c          |  28 +++++
 .../riscv/sat_s_trunc-1-i32-to-i8.c           |  26 +++++
 .../riscv/sat_s_trunc-1-i64-to-i16.c          |  28 +++++
 .../riscv/sat_s_trunc-1-i64-to-i32.c          |  26 +++++
 .../riscv/sat_s_trunc-1-i64-to-i8.c           |  26 +++++
 .../riscv/sat_s_trunc-run-1-i16-to-i8.c       |  16 +++
 .../riscv/sat_s_trunc-run-1-i32-to-i16.c      |  16 +++
 .../riscv/sat_s_trunc-run-1-i32-to-i8.c       |  16 +++
 .../riscv/sat_s_trunc-run-1-i64-to-i16.c      |  16 +++
 .../riscv/sat_s_trunc-run-1-i64-to-i32.c      |  16 +++
 .../riscv/sat_s_trunc-run-1-i64-to-i8.c       |  16 +++
 14 files changed, 381 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c

Comments

Kito Cheng Oct. 8, 2024, 11:06 a.m. UTC | #1
LGTM

<pan2.li@intel.com> 於 2024年10月8日 週二 16:36 寫道:

> From: Pan Li <pan2.li@intel.com>
>
> Form 1:
>   #define DEF_SAT_S_TRUNC_FMT_1(WT, NT, NT_MIN, NT_MAX) \
>   NT __attribute__((noinline))                          \
>   sat_s_trunc_##WT##_to_##NT##_fmt_1 (WT x)             \
>   {                                                     \
>     NT trunc = (NT)x;                                   \
>     return (WT)NT_MIN <= x && x <= (WT)NT_MAX           \
>       ? trunc                                           \
>       : x < 0 ? NT_MIN : NT_MAX;                        \
>   }
>
> The below test are passed for this patch.
> * The rv64gcv fully regression test.
>
> It is test only patch and obvious up to a point, will commit it
> directly if no comments in next 48H.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/sat_arith.h: Add test helper macros.
>         * gcc.target/riscv/sat_arith_data.h: Add test data for SAT_TRUNC.
>         * gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c: New test.
>         * gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c: New test.
>         * gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c: New test.
>         * gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c: New test.
>         * gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c: New test.
>         * gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c: New test.
>         * gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c: New test.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/testsuite/gcc.target/riscv/sat_arith.h    |  15 +++
>  .../gcc.target/riscv/sat_arith_data.h         | 110 ++++++++++++++++++
>  .../riscv/sat_s_trunc-1-i16-to-i8.c           |  26 +++++
>  .../riscv/sat_s_trunc-1-i32-to-i16.c          |  28 +++++
>  .../riscv/sat_s_trunc-1-i32-to-i8.c           |  26 +++++
>  .../riscv/sat_s_trunc-1-i64-to-i16.c          |  28 +++++
>  .../riscv/sat_s_trunc-1-i64-to-i32.c          |  26 +++++
>  .../riscv/sat_s_trunc-1-i64-to-i8.c           |  26 +++++
>  .../riscv/sat_s_trunc-run-1-i16-to-i8.c       |  16 +++
>  .../riscv/sat_s_trunc-run-1-i32-to-i16.c      |  16 +++
>  .../riscv/sat_s_trunc-run-1-i32-to-i8.c       |  16 +++
>  .../riscv/sat_s_trunc-run-1-i64-to-i16.c      |  16 +++
>  .../riscv/sat_s_trunc-run-1-i64-to-i32.c      |  16 +++
>  .../riscv/sat_s_trunc-run-1-i64-to-i8.c       |  16 +++
>  14 files changed, 381 insertions(+)
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
>
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_arith.h
> b/gcc/testsuite/gcc.target/riscv/sat_arith.h
> index 7c3859cc183..80d7a69e7d9 100644
> --- a/gcc/testsuite/gcc.target/riscv/sat_arith.h
> +++ b/gcc/testsuite/gcc.target/riscv/sat_arith.h
> @@ -465,4 +465,19 @@ sat_u_trunc_##WT##_to_##NT##_fmt_4 (WT x)      \
>  #define RUN_SAT_U_TRUNC_FMT_4(NT, WT, x)
> sat_u_trunc_##WT##_to_##NT##_fmt_4 (x)
>  #define RUN_SAT_U_TRUNC_FMT_4_WRAP(NT, WT, x) RUN_SAT_U_TRUNC_FMT_4(NT,
> WT, x)
>
> +#define DEF_SAT_S_TRUNC_FMT_1(NT, WT, NT_MIN, NT_MAX) \
> +NT __attribute__((noinline))                          \
> +sat_s_trunc_##WT##_to_##NT##_fmt_1 (WT x)             \
> +{                                                     \
> +  NT trunc = (NT)x;                                   \
> +  return (WT)NT_MIN <= x && x <= (WT)NT_MAX           \
> +    ? trunc                                           \
> +    : x < 0 ? NT_MIN : NT_MAX;                        \
> +}
> +#define DEF_SAT_S_TRUNC_FMT_1_WRAP(NT, WT, NT_MIN, NT_MAX) \
> +  DEF_SAT_S_TRUNC_FMT_1(NT, WT, NT_MIN, NT_MAX)
> +
> +#define RUN_SAT_S_TRUNC_FMT_1(NT, WT, x)
> sat_s_trunc_##WT##_to_##NT##_fmt_1 (x)
> +#define RUN_SAT_S_TRUNC_FMT_1_WRAP(NT, WT, x) RUN_SAT_S_TRUNC_FMT_1(NT,
> WT, x)
> +
>  #endif
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
> b/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
> index 39a1e17cd3d..9f9f7d0bcd1 100644
> --- a/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
> +++ b/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
> @@ -42,6 +42,13 @@ TEST_BINARY_STRUCT (int16_t, sssub)
>  TEST_BINARY_STRUCT (int32_t, sssub)
>  TEST_BINARY_STRUCT (int64_t, sssub)
>
> +TEST_UNARY_STRUCT (int8_t, int16_t)
> +TEST_UNARY_STRUCT (int8_t, int32_t)
> +TEST_UNARY_STRUCT (int8_t, int64_t)
> +TEST_UNARY_STRUCT (int16_t, int32_t)
> +TEST_UNARY_STRUCT (int16_t, int64_t)
> +TEST_UNARY_STRUCT (int32_t, int64_t)
> +
>  TEST_UNARY_STRUCT_DECL(uint8_t, uint16_t) \
>    TEST_UNARY_DATA(uint8_t, uint16_t)[] =
>  {
> @@ -126,6 +133,109 @@ TEST_UNARY_STRUCT_DECL(uint32_t, uint64_t) \
>    {4294967295, 18446744073709551615u},
>  };
>
> +TEST_UNARY_STRUCT_DECL(int8_t, int16_t) \
> +  TEST_UNARY_DATA(int8_t, int16_t)[] =
> +{
> +  {   0,      0},
> +  {  -2,     -2},
> +  { 127,    128},
> +  { 127,    258},
> +  {-128,   -128},
> +  {-128,   -129},
> +  {-128,   -257},
> +  { 127,  32767},
> +  {-128, -32768},
> +};
> +
> +TEST_UNARY_STRUCT_DECL(int8_t, int32_t) \
> +  TEST_UNARY_DATA(int8_t, int32_t)[] =
> +{
> +  {   0,           0},
> +  {  -2,          -2},
> +  { 127,         128},
> +  { 127,         258},
> +  {-128,        -128},
> +  {-128,        -129},
> +  {-128,        -257},
> +  { 127,       32767},
> +  {-128,      -32768},
> +  { 127,       65536},
> +  {-128,      -65537},
> +  { 127,  2147483647},
> +  {-128, -2147483648},
> +};
> +
> +TEST_UNARY_STRUCT_DECL(int8_t, int64_t) \
> +  TEST_UNARY_DATA(int8_t, int64_t)[] =
> +{
> +  {   0,                       0},
> +  {  -2,                      -2},
> +  { 127,                     128},
> +  { 127,                     258},
> +  {-128,                    -128},
> +  {-128,                    -129},
> +  {-128,                    -257},
> +  { 127,                   32767},
> +  {-128,                  -32768},
> +  { 127,                   65536},
> +  {-128,                  -65537},
> +  { 127,              2147483647},
> +  {-128,             -2147483648},
> +  { 127,            2147483648ll},
> +  {-128,           -2147483649ll},
> +  { 127,   9223372036854775807ll},
> +  {-128, -9223372036854775808ull},
> +};
> +
> +TEST_UNARY_STRUCT_DECL(int16_t, int32_t) \
> +  TEST_UNARY_DATA(int16_t, int32_t)[] =
> +{
> +  {     0,           0},
> +  {    -2,          -2},
> +  { 32767,       32768},
> +  { 32767,       65538},
> +  {-32768,      -32768},
> +  {-32768,      -32769},
> +  {-32768,      -65539},
> +  { 32767,       32767},
> +  {-32768,      -32768},
> +  { 32767,       65536},
> +  {-32768,      -65537},
> +  { 32767,  2147483647},
> +  {-32768, -2147483648},
> +};
> +
> +TEST_UNARY_STRUCT_DECL(int16_t, int64_t) \
> +  TEST_UNARY_DATA(int16_t, int64_t)[] =
> +{
> +  {     0,           0},
> +  {    -2,          -2},
> +  { 32767,       32768},
> +  { 32767,       65538},
> +  {-32768,      -32768},
> +  {-32768,      -32769},
> +  {-32768,      -65539},
> +  { 32767,       32767},
> +  {-32768,      -32768},
> +  { 32767,       65536},
> +  {-32768,      -65537},
> +  { 32767,  2147483647},
> +  {-32768, -2147483648},
> +};
> +
> +TEST_UNARY_STRUCT_DECL(int32_t, int64_t) \
> +  TEST_UNARY_DATA(int32_t, int64_t)[] =
> +{
> +  {          0,                       0},
> +  {         -2,                      -2},
> +  { 2147483647,            2147483648ll},
> +  {-2147483648,             -2147483648},
> +  { 2147483647,              2147483648},
> +  {-2147483648,           -2147483649ll},
> +  { 2147483647,   9223372036854775807ll},
> +  {-2147483648, -9223372036854775808ull},
> +};
> +
>  TEST_BINARY_STRUCT_DECL(int8_t, ssadd) TEST_BINARY_DATA(int8_t, ssadd)[] =
>  {
>    {   0,    0,    0},
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
> new file mode 100644
> index 00000000000..9c14a26a727
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
> @@ -0,0 +1,26 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int16_t_to_int8_t_fmt_1:
> +** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** li\s+[atx][0-9]+,\s*-128
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slliw\s+a0,\s*a0,\s*24
> +** sraiw\s+a0,\s*a0,\s*24
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int8_t, int16_t, INT8_MIN, INT8_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
> new file mode 100644
> index 00000000000..41c121762d9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
> @@ -0,0 +1,28 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int32_t_to_int16_t_fmt_1:
> +** li\s+[atx][0-9]+,\s*32768
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** li\s+[atx][0-9]+,\s*-32768
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slliw\s+a0,\s*a0,\s*16
> +** sraiw\s+a0,\s*a0,\s*16
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int16_t, int32_t, INT16_MIN, INT16_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
> new file mode 100644
> index 00000000000..e2f50971407
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
> @@ -0,0 +1,26 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int32_t_to_int8_t_fmt_1:
> +** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** li\s+[atx][0-9]+,\s*-128
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slliw\s+a0,\s*a0,\s*24
> +** sraiw\s+a0,\s*a0,\s*24
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int8_t, int32_t, INT8_MIN, INT8_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
> new file mode 100644
> index 00000000000..794f13406d3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
> @@ -0,0 +1,28 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int64_t_to_int16_t_fmt_1:
> +** li\s+[atx][0-9]+,\s*32768
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** li\s+[atx][0-9]+,\s*-32768
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slliw\s+a0,\s*a0,\s*16
> +** sraiw\s+a0,\s*a0,\s*16
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int16_t, int64_t, INT16_MIN, INT16_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
> new file mode 100644
> index 00000000000..742ac17ba72
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
> @@ -0,0 +1,26 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int64_t_to_int32_t_fmt_1:
> +** li\s+[atx][0-9]+,\s*-2147483648
> +** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** sext\.w\s+a0,\s*a0
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int32_t, int64_t, INT32_MIN, INT32_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
> new file mode 100644
> index 00000000000..16139ef336f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
> @@ -0,0 +1,26 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details
> -fno-schedule-insns -fno-schedule-insns2" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#include "sat_arith.h"
> +
> +/*
> +** sat_s_trunc_int64_t_to_int8_t_fmt_1:
> +** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** li\s+[atx][0-9]+,\s*-128
> +** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
> +** neg\s+[atx][0-9]+,\s*[atx][0-9]+
> +** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
> +** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
> +** slliw\s+a0,\s*a0,\s*24
> +** sraiw\s+a0,\s*a0,\s*24
> +** ret
> +*/
> +DEF_SAT_S_TRUNC_FMT_1(int8_t, int64_t, INT8_MIN, INT8_MAX)
> +
> +/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
> new file mode 100644
> index 00000000000..1f230c592a8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int8_t
> +#define T2 int16_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
> new file mode 100644
> index 00000000000..563760beff5
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int16_t
> +#define T2 int32_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT16_MIN, INT16_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
> new file mode 100644
> index 00000000000..af50d3e9990
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int8_t
> +#define T2 int32_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
> new file mode 100644
> index 00000000000..4ac7025fa94
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int16_t
> +#define T2 int64_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT16_MIN, INT16_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
> new file mode 100644
> index 00000000000..ca6d31cadfc
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int32_t
> +#define T2 int64_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT32_MIN, INT32_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
> b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
> new file mode 100644
> index 00000000000..697e1bce404
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
> @@ -0,0 +1,16 @@
> +/* { dg-do run { target { riscv_v } } } */
> +/* { dg-additional-options "-std=c99" } */
> +
> +#include "sat_arith.h"
> +#include "sat_arith_data.h"
> +
> +#define T1 int8_t
> +#define T2 int64_t
> +
> +DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
> +
> +#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
> +#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
> +#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
> +
> +#include "scalar_sat_unary.h"
> --
> 2.43.0
>
>
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.target/riscv/sat_arith.h b/gcc/testsuite/gcc.target/riscv/sat_arith.h
index 7c3859cc183..80d7a69e7d9 100644
--- a/gcc/testsuite/gcc.target/riscv/sat_arith.h
+++ b/gcc/testsuite/gcc.target/riscv/sat_arith.h
@@ -465,4 +465,19 @@  sat_u_trunc_##WT##_to_##NT##_fmt_4 (WT x)      \
 #define RUN_SAT_U_TRUNC_FMT_4(NT, WT, x) sat_u_trunc_##WT##_to_##NT##_fmt_4 (x)
 #define RUN_SAT_U_TRUNC_FMT_4_WRAP(NT, WT, x) RUN_SAT_U_TRUNC_FMT_4(NT, WT, x)
 
+#define DEF_SAT_S_TRUNC_FMT_1(NT, WT, NT_MIN, NT_MAX) \
+NT __attribute__((noinline))                          \
+sat_s_trunc_##WT##_to_##NT##_fmt_1 (WT x)             \
+{                                                     \
+  NT trunc = (NT)x;                                   \
+  return (WT)NT_MIN <= x && x <= (WT)NT_MAX           \
+    ? trunc                                           \
+    : x < 0 ? NT_MIN : NT_MAX;                        \
+}
+#define DEF_SAT_S_TRUNC_FMT_1_WRAP(NT, WT, NT_MIN, NT_MAX) \
+  DEF_SAT_S_TRUNC_FMT_1(NT, WT, NT_MIN, NT_MAX)
+
+#define RUN_SAT_S_TRUNC_FMT_1(NT, WT, x) sat_s_trunc_##WT##_to_##NT##_fmt_1 (x)
+#define RUN_SAT_S_TRUNC_FMT_1_WRAP(NT, WT, x) RUN_SAT_S_TRUNC_FMT_1(NT, WT, x)
+
 #endif
diff --git a/gcc/testsuite/gcc.target/riscv/sat_arith_data.h b/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
index 39a1e17cd3d..9f9f7d0bcd1 100644
--- a/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
+++ b/gcc/testsuite/gcc.target/riscv/sat_arith_data.h
@@ -42,6 +42,13 @@  TEST_BINARY_STRUCT (int16_t, sssub)
 TEST_BINARY_STRUCT (int32_t, sssub)
 TEST_BINARY_STRUCT (int64_t, sssub)
 
+TEST_UNARY_STRUCT (int8_t, int16_t)
+TEST_UNARY_STRUCT (int8_t, int32_t)
+TEST_UNARY_STRUCT (int8_t, int64_t)
+TEST_UNARY_STRUCT (int16_t, int32_t)
+TEST_UNARY_STRUCT (int16_t, int64_t)
+TEST_UNARY_STRUCT (int32_t, int64_t)
+
 TEST_UNARY_STRUCT_DECL(uint8_t, uint16_t) \
   TEST_UNARY_DATA(uint8_t, uint16_t)[] =
 {
@@ -126,6 +133,109 @@  TEST_UNARY_STRUCT_DECL(uint32_t, uint64_t) \
   {4294967295, 18446744073709551615u},
 };
 
+TEST_UNARY_STRUCT_DECL(int8_t, int16_t) \
+  TEST_UNARY_DATA(int8_t, int16_t)[] =
+{
+  {   0,      0},
+  {  -2,     -2},
+  { 127,    128},
+  { 127,    258},
+  {-128,   -128},
+  {-128,   -129},
+  {-128,   -257},
+  { 127,  32767},
+  {-128, -32768},
+};
+
+TEST_UNARY_STRUCT_DECL(int8_t, int32_t) \
+  TEST_UNARY_DATA(int8_t, int32_t)[] =
+{
+  {   0,           0},
+  {  -2,          -2},
+  { 127,         128},
+  { 127,         258},
+  {-128,        -128},
+  {-128,        -129},
+  {-128,        -257},
+  { 127,       32767},
+  {-128,      -32768},
+  { 127,       65536},
+  {-128,      -65537},
+  { 127,  2147483647},
+  {-128, -2147483648},
+};
+
+TEST_UNARY_STRUCT_DECL(int8_t, int64_t) \
+  TEST_UNARY_DATA(int8_t, int64_t)[] =
+{
+  {   0,                       0},
+  {  -2,                      -2},
+  { 127,                     128},
+  { 127,                     258},
+  {-128,                    -128},
+  {-128,                    -129},
+  {-128,                    -257},
+  { 127,                   32767},
+  {-128,                  -32768},
+  { 127,                   65536},
+  {-128,                  -65537},
+  { 127,              2147483647},
+  {-128,             -2147483648},
+  { 127,            2147483648ll},
+  {-128,           -2147483649ll},
+  { 127,   9223372036854775807ll},
+  {-128, -9223372036854775808ull},
+};
+
+TEST_UNARY_STRUCT_DECL(int16_t, int32_t) \
+  TEST_UNARY_DATA(int16_t, int32_t)[] =
+{
+  {     0,           0},
+  {    -2,          -2},
+  { 32767,       32768},
+  { 32767,       65538},
+  {-32768,      -32768},
+  {-32768,      -32769},
+  {-32768,      -65539},
+  { 32767,       32767},
+  {-32768,      -32768},
+  { 32767,       65536},
+  {-32768,      -65537},
+  { 32767,  2147483647},
+  {-32768, -2147483648},
+};
+
+TEST_UNARY_STRUCT_DECL(int16_t, int64_t) \
+  TEST_UNARY_DATA(int16_t, int64_t)[] =
+{
+  {     0,           0},
+  {    -2,          -2},
+  { 32767,       32768},
+  { 32767,       65538},
+  {-32768,      -32768},
+  {-32768,      -32769},
+  {-32768,      -65539},
+  { 32767,       32767},
+  {-32768,      -32768},
+  { 32767,       65536},
+  {-32768,      -65537},
+  { 32767,  2147483647},
+  {-32768, -2147483648},
+};
+
+TEST_UNARY_STRUCT_DECL(int32_t, int64_t) \
+  TEST_UNARY_DATA(int32_t, int64_t)[] =
+{
+  {          0,                       0},
+  {         -2,                      -2},
+  { 2147483647,            2147483648ll},
+  {-2147483648,             -2147483648},
+  { 2147483647,              2147483648},
+  {-2147483648,           -2147483649ll},
+  { 2147483647,   9223372036854775807ll},
+  {-2147483648, -9223372036854775808ull},
+};
+
 TEST_BINARY_STRUCT_DECL(int8_t, ssadd) TEST_BINARY_DATA(int8_t, ssadd)[] =
 {
   {   0,    0,    0},
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
new file mode 100644
index 00000000000..9c14a26a727
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i16-to-i8.c
@@ -0,0 +1,26 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int16_t_to_int8_t_fmt_1:
+** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** li\s+[atx][0-9]+,\s*-128
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slliw\s+a0,\s*a0,\s*24
+** sraiw\s+a0,\s*a0,\s*24
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int8_t, int16_t, INT8_MIN, INT8_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
new file mode 100644
index 00000000000..41c121762d9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i16.c
@@ -0,0 +1,28 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int32_t_to_int16_t_fmt_1:
+** li\s+[atx][0-9]+,\s*32768
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** li\s+[atx][0-9]+,\s*-32768
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slliw\s+a0,\s*a0,\s*16
+** sraiw\s+a0,\s*a0,\s*16
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int16_t, int32_t, INT16_MIN, INT16_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
new file mode 100644
index 00000000000..e2f50971407
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i32-to-i8.c
@@ -0,0 +1,26 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int32_t_to_int8_t_fmt_1:
+** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** li\s+[atx][0-9]+,\s*-128
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slliw\s+a0,\s*a0,\s*24
+** sraiw\s+a0,\s*a0,\s*24
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int8_t, int32_t, INT8_MIN, INT8_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
new file mode 100644
index 00000000000..794f13406d3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i16.c
@@ -0,0 +1,28 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int64_t_to_int16_t_fmt_1:
+** li\s+[atx][0-9]+,\s*32768
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** li\s+[atx][0-9]+,\s*-32768
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slliw\s+a0,\s*a0,\s*16
+** sraiw\s+a0,\s*a0,\s*16
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int16_t, int64_t, INT16_MIN, INT16_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
new file mode 100644
index 00000000000..742ac17ba72
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i32.c
@@ -0,0 +1,26 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int64_t_to_int32_t_fmt_1:
+** li\s+[atx][0-9]+,\s*-2147483648
+** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xor\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** sext\.w\s+a0,\s*a0
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int32_t, int64_t, INT32_MIN, INT32_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
new file mode 100644
index 00000000000..16139ef336f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-1-i64-to-i8.c
@@ -0,0 +1,26 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details -fno-schedule-insns -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#include "sat_arith.h"
+
+/*
+** sat_s_trunc_int64_t_to_int8_t_fmt_1:
+** slti\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** li\s+[atx][0-9]+,\s*-128
+** slt\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** addi\s+[atx][0-9]+,\s*[atx][0-9]+,\s*-1
+** neg\s+[atx][0-9]+,\s*[atx][0-9]+
+** srai\s+[atx][0-9]+,\s*[atx][0-9]+,\s*63
+** xori\s+[atx][0-9]+,\s*[atx][0-9]+,\s*127
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** and\s+[atx][0-9]+,\s*[atx][0-9]+,\s*[atx][0-9]+
+** or\s+a0,\s*[atx][0-9]+,\s*[atx][0-9]+
+** slliw\s+a0,\s*a0,\s*24
+** sraiw\s+a0,\s*a0,\s*24
+** ret
+*/
+DEF_SAT_S_TRUNC_FMT_1(int8_t, int64_t, INT8_MIN, INT8_MAX)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_TRUNC " 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
new file mode 100644
index 00000000000..1f230c592a8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i16-to-i8.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int8_t
+#define T2 int16_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
new file mode 100644
index 00000000000..563760beff5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i16.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int16_t
+#define T2 int32_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT16_MIN, INT16_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
new file mode 100644
index 00000000000..af50d3e9990
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i32-to-i8.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int8_t
+#define T2 int32_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
new file mode 100644
index 00000000000..4ac7025fa94
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i16.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int16_t
+#define T2 int64_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT16_MIN, INT16_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
new file mode 100644
index 00000000000..ca6d31cadfc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i32.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int32_t
+#define T2 int64_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT32_MIN, INT32_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"
diff --git a/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
new file mode 100644
index 00000000000..697e1bce404
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sat_s_trunc-run-1-i64-to-i8.c
@@ -0,0 +1,16 @@ 
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "sat_arith.h"
+#include "sat_arith_data.h"
+
+#define T1 int8_t
+#define T2 int64_t
+
+DEF_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, INT8_MIN, INT8_MAX)
+
+#define DATA           TEST_UNARY_DATA_WRAP(T1, T2)
+#define T              TEST_UNARY_STRUCT_DECL(T1, T2)
+#define RUN_UNARY(x)   RUN_SAT_S_TRUNC_FMT_1_WRAP(T1, T2, x)
+
+#include "scalar_sat_unary.h"