@@ -3269,7 +3269,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(match (unsigned_integer_sat_sub @0 @1)
(cond^ (le @1 INTEGER_CST@2) (minus INTEGER_CST@0 @1) integer_zerop)
(if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
- && types_match (type, @1))
+ && types_match (type, @1) && int_fits_type_p (@0, type))
(with
{
unsigned precision = TYPE_PRECISION (type);
@@ -267,6 +267,20 @@ sat_u_sub_imm##IMM##_##T##_fmt_4 (T x) \
#define RUN_SAT_U_SUB_IMM_FMT_4(T, x, IMM, expect) \
if (sat_u_sub_imm##IMM##_##T##_fmt_4(x) != expect) __builtin_abort ()
+#define DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1(INDEX, T, IMM) \
+T __attribute__((noinline)) \
+sat_u_sub_imm_type_check##_##INDEX##_##T##_fmt_1 (T y) \
+{ \
+ return IMM >= y ? IMM - y : 0; \
+}
+
+#define DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2(INDEX, T, IMM) \
+T __attribute__((noinline)) \
+sat_u_sub_imm_type_check##_##INDEX##_##T##_fmt_2 (T y) \
+{ \
+ return IMM > y ? IMM - y : 0; \
+}
+
/******************************************************************************/
/* Saturation Truncate (unsigned and signed) */
/******************************************************************************/
new file mode 100644
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details" } */
+
+#include "sat_arith.h"
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (0, uint8_t, -43)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (1, uint8_t, 269)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (2, uint8_t, 369u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (3, uint16_t, -4)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (4, uint16_t, 65579)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (5, uint16_t, 65679u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (6, uint32_t, -62)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (7, uint32_t, 4294967342ll)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (8, uint32_t, 4394967342ull)
+
+/* { dg-final { scan-rtl-dump-not ".SAT_ADD " "expand" } } */
new file mode 100644
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details" } */
+
+#include "sat_arith.h"
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (0, uint8_t, 123u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (1, uint8_t, 9)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (2, uint8_t, 129)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (3, uint8_t, 234u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (4, uint16_t, 32763u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (5, uint16_t, 65532u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (6, uint16_t, 52767)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (7, uint16_t, 9)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (8, uint32_t, 4294967293u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (9, uint32_t, 2147483944)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (10, uint32_t, 4294967242)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (11, uint32_t, 2147483644u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (12, uint64_t, -6232)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (13, uint64_t, 6293232)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (14, uint64_t, 576460752303483482)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (15, uint64_t, 576460752303423482u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_1 (16, uint64_t, 976460752303483482u)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_SUB " 34 "expand" } } */
new file mode 100644
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details" } */
+
+#include "sat_arith.h"
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (0, uint8_t, -43)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (1, uint8_t, 269)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (2, uint8_t, 369u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (3, uint16_t, -4)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (4, uint16_t, 65579)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (5, uint16_t, 65679u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (6, uint32_t, -62)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (7, uint32_t, 4294967342ll)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (8, uint32_t, 4394967342ull)
+
+/* { dg-final { scan-rtl-dump-not ".SAT_ADD " "expand" } } */
new file mode 100644
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O3 -fdump-rtl-expand-details" } */
+
+#include "sat_arith.h"
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (0, uint8_t, 126u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (1, uint8_t, 9)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (2, uint8_t, 129)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (3, uint8_t, 254u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (4, uint16_t, 32767u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (5, uint16_t, 65534u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (6, uint16_t, 52767)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (7, uint16_t, 9)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (8, uint32_t, 4294967293u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (9, uint32_t, 2147483944)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (10, uint32_t, 4294967242)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (11, uint32_t, 2147483644u)
+
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (12, uint64_t, -6232)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (13, uint64_t, 6293232)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (14, uint64_t, 576460752303483482)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (15, uint64_t, 576460752303423482u)
+DEF_SAT_U_SUB_IMM_TYPE_CHECK_FMT_2 (16, uint64_t, 976460752303483482u)
+
+/* { dg-final { scan-rtl-dump-times ".SAT_SUB " 34 "expand" } } */