@@ -421,7 +421,8 @@ enum function_class {
function_c99_math_complex,
function_sincos,
function_c11_misc,
- function_c23_misc
+ function_c23_misc,
+ function_c2y_misc
};
/* Enumerate visibility settings. This is deliberately ordered from most
@@ -252,6 +252,7 @@ DEF_FUNCTION_TYPE_0 (BT_FN_DFLOAT128, BT
DEF_FUNCTION_TYPE_1 (BT_FN_LONG_LONG, BT_LONG, BT_LONG)
DEF_FUNCTION_TYPE_1 (BT_FN_LONGLONG_LONGLONG, BT_LONGLONG, BT_LONGLONG)
+DEF_FUNCTION_TYPE_1 (BT_FN_UINTMAX_INTMAX, BT_UINTMAX, BT_INTMAX)
DEF_FUNCTION_TYPE_1 (BT_FN_INTMAX_INTMAX, BT_INTMAX, BT_INTMAX)
DEF_FUNCTION_TYPE_1 (BT_FN_FLOAT_FLOAT, BT_FLOAT, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_DOUBLE_DOUBLE, BT_DOUBLE, BT_DOUBLE)
@@ -396,7 +397,9 @@ DEF_FUNCTION_TYPE_1 (BT_FN_UINT_CONST_PT
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_PTR, BT_ULONG, BT_PTR)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_CONST_PTR, BT_ULONG, BT_CONST_PTR)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_ULONG, BT_ULONG, BT_ULONG)
+DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_LONG, BT_ULONG, BT_LONG)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONGLONG_ULONGLONG, BT_ULONGLONG, BT_ULONGLONG)
+DEF_FUNCTION_TYPE_1 (BT_FN_ULONGLONG_LONGLONG, BT_ULONGLONG, BT_LONGLONG)
DEF_FUNCTION_TYPE_1 (BT_FN_INT8_FLOAT, BT_INT8, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_INT16_FLOAT, BT_INT16, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_UINT32_FLOAT, BT_UINT32, BT_FLOAT)
@@ -164,6 +164,14 @@ along with GCC; see the file COPYING3.
true, true, !flag_isoc23, ATTRS, \
targetm.libc_has_function (function_c23_misc, NULL_TREE), true)
+/* Like DEF_LIB_BUILTIN, except that the function is only a part of
+ the standard in C2Y or above. */
+#undef DEF_C2Y_BUILTIN
+#define DEF_C2Y_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
+ DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
+ true, true, !flag_isoc2y, ATTRS, \
+ targetm.libc_has_function (function_c2y_misc, NULL_TREE), true)
+
/* Like DEF_C99_BUILTIN, but for complex math functions. */
#undef DEF_C99_COMPL_BUILTIN
#define DEF_C99_COMPL_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
@@ -1069,6 +1077,10 @@ DEF_GCC_BUILTIN (BUILT_IN_SETJMP,
DEF_EXT_LIB_BUILTIN (BUILT_IN_STRFMON, "strfmon", BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_STRFMON_NOTHROW_3_4)
DEF_LIB_BUILTIN (BUILT_IN_STRFTIME, "strftime", BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, ATTR_FORMAT_STRFTIME_NOTHROW_3_0)
DEF_GCC_BUILTIN (BUILT_IN_TRAP, "trap", BT_FN_VOID, ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST)
+DEF_C2Y_BUILTIN (BUILT_IN_UABS, "uabs", BT_FN_UINT_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_C2Y_BUILTIN (BUILT_IN_UIMAXABS, "uimaxabs", BT_FN_UINTMAX_INTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_C2Y_BUILTIN (BUILT_IN_ULABS, "ulabs", BT_FN_ULONG_LONG, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_C2Y_BUILTIN (BUILT_IN_ULLABS, "ullabs", BT_FN_ULONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNREACHABLE_TRAP, "unreachable trap", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNREACHABLE, "unreachable", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNWIND_INIT, "unwind_init", BT_FN_VOID, ATTR_NULL)
@@ -9377,7 +9377,8 @@ fold_builtin_fabs (location_t loc, tree
return fold_build1_loc (loc, ABS_EXPR, type, arg);
}
-/* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
+/* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
+ with argument ARG. */
static tree
fold_builtin_abs (location_t loc, tree arg, tree type)
@@ -9385,6 +9386,14 @@ fold_builtin_abs (location_t loc, tree a
if (!validate_arg (arg, INTEGER_TYPE))
return NULL_TREE;
+ if (TYPE_UNSIGNED (type))
+ {
+ if (TYPE_PRECISION (TREE_TYPE (arg))
+ != TYPE_PRECISION (type)
+ || TYPE_UNSIGNED (TREE_TYPE (arg)))
+ return NULL_TREE;
+ return fold_build1_loc (loc, ABSU_EXPR, type, arg);
+ }
arg = fold_convert_loc (loc, type, arg);
return fold_build1_loc (loc, ABS_EXPR, type, arg);
}
@@ -10515,6 +10524,10 @@ fold_builtin_1 (location_t loc, tree exp
case BUILT_IN_LABS:
case BUILT_IN_LLABS:
case BUILT_IN_IMAXABS:
+ case BUILT_IN_UABS:
+ case BUILT_IN_ULABS:
+ case BUILT_IN_ULLABS:
+ case BUILT_IN_UIMAXABS:
return fold_builtin_abs (loc, arg0, type);
CASE_FLT_FN (BUILT_IN_CONJ):
@@ -262,6 +262,7 @@ int flag_isoc94;
int flag_isoc99;
int flag_isoc11;
int flag_isoc23;
+int flag_isoc2y;
/* Attribute handlers. */
@@ -7501,6 +7501,7 @@ static int flag_isoc94 = 0;
static int flag_isoc99 = 0;
static int flag_isoc11 = 0;
static int flag_isoc23 = 0;
+static int flag_isoc2y = 0;
/* Install what the common builtins.def offers plus our local additions.
@@ -7,6 +7,7 @@ extern void abort (void);
#endif
typedef __INTMAX_TYPE__ intmax_t;
+typedef unsigned __INTMAX_TYPE__ uintmax_t;
__attribute__ ((__noinline__))
int
@@ -39,3 +40,35 @@ imaxabs (intmax_t x)
ABORT_INSIDE_MAIN;
return x < 0 ? -x : x;
}
+
+__attribute__ ((__noinline__))
+unsigned int
+uabs (int x)
+{
+ ABORT_INSIDE_MAIN;
+ return x < 0 ? -(unsigned int) x : x;
+}
+
+__attribute__ ((__noinline__))
+unsigned long
+ulabs (long x)
+{
+ ABORT_INSIDE_MAIN;
+ return x < 0 ? -(unsigned long) x : x;
+}
+
+__attribute__ ((__noinline__))
+unsigned long long
+ullabs (long long x)
+{
+ ABORT_INSIDE_MAIN;
+ return x < 0 ? -(unsigned long long) x : x;
+}
+
+__attribute__ ((__noinline__))
+uintmax_t
+uimaxabs (intmax_t x)
+{
+ ABORT_INSIDE_MAIN;
+ return x < 0 ? -(uintmax_t) x : x;
+}
@@ -0,0 +1,22 @@
+/* Test for -fno-builtin-FUNCTION. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk>. */
+/* GCC normally handles uabs and ulabs as built-in functions even without
+ optimization. So test that with -fno-builtin-uabs, ulabs is so handled
+ but uabs isn't. */
+
+int abs_called = 0;
+
+extern unsigned int uabs (int);
+extern unsigned long ulabs (long);
+extern void abort (void);
+
+void
+main_test (void)
+{
+ if (ulabs (0) != 0)
+ abort ();
+ if (uabs (0) != 0)
+ abort ();
+ if (!abs_called)
+ abort ();
+}
@@ -0,0 +1,2 @@
+set additional_flags "-fno-builtin-uabs -std=c2y"
+return 0
@@ -0,0 +1,22 @@
+extern void abort (void);
+extern int abs_called;
+extern int inside_main;
+
+/* The ulabs call should have been optimized, but the uabs call
+ shouldn't have been. */
+
+unsigned int
+uabs (int x)
+{
+ if (inside_main)
+ abs_called = 1;
+ return (x < 0 ? -(unsigned int) x : x);
+}
+
+unsigned long
+ulabs (long x)
+{
+ if (inside_main)
+ abort ();
+ return (x < 0 ? -(unsigned long) x : x);
+}
@@ -0,0 +1,123 @@
+/* Test for builtin uabs, ulabs, ullabs, uimaxabs. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+
+#include <limits.h>
+typedef __INTMAX_TYPE__ intmax_t;
+typedef unsigned __INTMAX_TYPE__ uintmax_t;
+#define INTMAX_MAX __INTMAX_MAX__
+
+extern unsigned int uabs (int);
+extern unsigned long ulabs (long);
+extern unsigned long long ullabs (long long);
+extern uintmax_t uimaxabs (intmax_t);
+extern void abort (void);
+extern void link_error (void);
+
+void
+main_test (void)
+{
+ /* For each type, test both runtime and compile time (constant folding)
+ optimization. */
+ volatile int i0 = 0, i1 = 1, im1 = -1, imin = -INT_MAX, imax = INT_MAX;
+ volatile long l0 = 0L, l1 = 1L, lm1 = -1L, lmin = -LONG_MAX, lmax = LONG_MAX;
+ volatile long long ll0 = 0LL, ll1 = 1LL, llm1 = -1LL;
+ volatile long long llmin = -__LONG_LONG_MAX__, llmax = __LONG_LONG_MAX__;
+ volatile intmax_t imax0 = 0, imax1 = 1, imaxm1 = -1;
+ volatile intmax_t imaxmin = -INTMAX_MAX, imaxmax = INTMAX_MAX;
+ if (uabs (i0) != 0)
+ abort ();
+ if (uabs (0) != 0)
+ link_error ();
+ if (uabs (i1) != 1)
+ abort ();
+ if (uabs (1) != 1)
+ link_error ();
+ if (uabs (im1) != 1)
+ abort ();
+ if (uabs (-1) != 1)
+ link_error ();
+ if (uabs (imin) != INT_MAX)
+ abort ();
+ if (uabs (imin - 1) != 1U + INT_MAX)
+ abort ();
+ if (uabs (-INT_MAX) != INT_MAX)
+ link_error ();
+ if (uabs (-INT_MAX - 1) != 1U + INT_MAX)
+ link_error ();
+ if (uabs (imax) != INT_MAX)
+ abort ();
+ if (uabs (INT_MAX) != INT_MAX)
+ link_error ();
+ if (ulabs (l0) != 0L)
+ abort ();
+ if (ulabs (0L) != 0L)
+ link_error ();
+ if (ulabs (l1) != 1L)
+ abort ();
+ if (ulabs (1L) != 1L)
+ link_error ();
+ if (ulabs (lm1) != 1L)
+ abort ();
+ if (ulabs (-1L) != 1L)
+ link_error ();
+ if (ulabs (lmin) != LONG_MAX)
+ abort ();
+ if (ulabs (lmin - 1) != 1UL + LONG_MAX)
+ abort ();
+ if (ulabs (-LONG_MAX) != LONG_MAX)
+ link_error ();
+ if (ulabs (-LONG_MAX - 1) != 1UL + LONG_MAX)
+ link_error ();
+ if (ulabs (lmax) != LONG_MAX)
+ abort ();
+ if (ulabs (LONG_MAX) != LONG_MAX)
+ link_error ();
+ if (ullabs (ll0) != 0LL)
+ abort ();
+ if (ullabs (0LL) != 0LL)
+ link_error ();
+ if (ullabs (ll1) != 1LL)
+ abort ();
+ if (ullabs (1LL) != 1LL)
+ link_error ();
+ if (ullabs (llm1) != 1LL)
+ abort ();
+ if (ullabs (-1LL) != 1LL)
+ link_error ();
+ if (ullabs (llmin) != __LONG_LONG_MAX__)
+ abort ();
+ if (ullabs (llmin - 1) != 1ULL + __LONG_LONG_MAX__)
+ abort ();
+ if (ullabs (-__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
+ link_error ();
+ if (ullabs (-__LONG_LONG_MAX__ - 1) != 1ULL + __LONG_LONG_MAX__)
+ link_error ();
+ if (ullabs (llmax) != __LONG_LONG_MAX__)
+ abort ();
+ if (ullabs (__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
+ link_error ();
+ if (uimaxabs (imax0) != 0)
+ abort ();
+ if (uimaxabs (0) != 0)
+ link_error ();
+ if (uimaxabs (imax1) != 1)
+ abort ();
+ if (uimaxabs (1) != 1)
+ link_error ();
+ if (uimaxabs (imaxm1) != 1)
+ abort ();
+ if (uimaxabs (-1) != 1)
+ link_error ();
+ if (uimaxabs (imaxmin) != INTMAX_MAX)
+ abort ();
+ if (uimaxabs (imaxmin - 1) != (uintmax_t) 1 + INTMAX_MAX)
+ abort ();
+ if (uimaxabs (-INTMAX_MAX) != INTMAX_MAX)
+ link_error ();
+ if (uimaxabs (-INTMAX_MAX - 1) != (uintmax_t) 1 + INTMAX_MAX)
+ link_error ();
+ if (uimaxabs (imaxmax) != INTMAX_MAX)
+ abort ();
+ if (uimaxabs (INTMAX_MAX) != INTMAX_MAX)
+ link_error ();
+}
@@ -0,0 +1,2 @@
+set additional_flags -std=c2y
+return 0
@@ -0,0 +1 @@
+#include "lib/abs.c"
@@ -0,0 +1,119 @@
+/* Test for builtin uabs, ulabs, ullabs, uimaxabs. Test for __builtin versions. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+
+#include <limits.h>
+typedef __INTMAX_TYPE__ intmax_t;
+typedef unsigned __INTMAX_TYPE__ uintmax_t;
+#define INTMAX_MAX __INTMAX_MAX__
+
+extern void abort (void);
+extern void link_error (void);
+
+void
+main_test (void)
+{
+ /* For each type, test both runtime and compile time (constant folding)
+ optimization. */
+ volatile int i0 = 0, i1 = 1, im1 = -1, imin = -INT_MAX, imax = INT_MAX;
+ volatile long l0 = 0L, l1 = 1L, lm1 = -1L, lmin = -LONG_MAX, lmax = LONG_MAX;
+ volatile long long ll0 = 0LL, ll1 = 1LL, llm1 = -1LL;
+ volatile long long llmin = -__LONG_LONG_MAX__, llmax = __LONG_LONG_MAX__;
+ volatile intmax_t imax0 = 0, imax1 = 1, imaxm1 = -1;
+ volatile intmax_t imaxmin = -INTMAX_MAX, imaxmax = INTMAX_MAX;
+ if (__builtin_uabs (i0) != 0)
+ abort ();
+ if (__builtin_uabs (0) != 0)
+ link_error ();
+ if (__builtin_uabs (i1) != 1)
+ abort ();
+ if (__builtin_uabs (1) != 1)
+ link_error ();
+ if (__builtin_uabs (im1) != 1)
+ abort ();
+ if (__builtin_uabs (-1) != 1)
+ link_error ();
+ if (__builtin_uabs (imin) != INT_MAX)
+ abort ();
+ if (__builtin_uabs (imin - 1) != 1U + INT_MAX)
+ abort ();
+ if (__builtin_uabs (-INT_MAX) != INT_MAX)
+ link_error ();
+ if (__builtin_uabs (-INT_MAX - 1) != 1U + INT_MAX)
+ link_error ();
+ if (__builtin_uabs (imax) != INT_MAX)
+ abort ();
+ if (__builtin_uabs (INT_MAX) != INT_MAX)
+ link_error ();
+ if (__builtin_ulabs (l0) != 0L)
+ abort ();
+ if (__builtin_ulabs (0L) != 0L)
+ link_error ();
+ if (__builtin_ulabs (l1) != 1L)
+ abort ();
+ if (__builtin_ulabs (1L) != 1L)
+ link_error ();
+ if (__builtin_ulabs (lm1) != 1L)
+ abort ();
+ if (__builtin_ulabs (-1L) != 1L)
+ link_error ();
+ if (__builtin_ulabs (lmin) != LONG_MAX)
+ abort ();
+ if (__builtin_ulabs (lmin - 1) != 1UL + LONG_MAX)
+ abort ();
+ if (__builtin_ulabs (-LONG_MAX) != LONG_MAX)
+ link_error ();
+ if (__builtin_ulabs (-LONG_MAX - 1) != 1UL + LONG_MAX)
+ link_error ();
+ if (__builtin_ulabs (lmax) != LONG_MAX)
+ abort ();
+ if (__builtin_ulabs (LONG_MAX) != LONG_MAX)
+ link_error ();
+ if (__builtin_ullabs (ll0) != 0LL)
+ abort ();
+ if (__builtin_ullabs (0LL) != 0LL)
+ link_error ();
+ if (__builtin_ullabs (ll1) != 1LL)
+ abort ();
+ if (__builtin_ullabs (1LL) != 1LL)
+ link_error ();
+ if (__builtin_ullabs (llm1) != 1LL)
+ abort ();
+ if (__builtin_ullabs (-1LL) != 1LL)
+ link_error ();
+ if (__builtin_ullabs (llmin) != __LONG_LONG_MAX__)
+ abort ();
+ if (__builtin_ullabs (llmin - 1) != 1ULL + __LONG_LONG_MAX__)
+ abort ();
+ if (__builtin_ullabs (-__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
+ link_error ();
+ if (__builtin_ullabs (-__LONG_LONG_MAX__ - 1) != 1ULL + __LONG_LONG_MAX__)
+ link_error ();
+ if (__builtin_ullabs (llmax) != __LONG_LONG_MAX__)
+ abort ();
+ if (__builtin_ullabs (__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
+ link_error ();
+ if (__builtin_uimaxabs (imax0) != 0)
+ abort ();
+ if (__builtin_uimaxabs (0) != 0)
+ link_error ();
+ if (__builtin_uimaxabs (imax1) != 1)
+ abort ();
+ if (__builtin_uimaxabs (1) != 1)
+ link_error ();
+ if (__builtin_uimaxabs (imaxm1) != 1)
+ abort ();
+ if (__builtin_uimaxabs (-1) != 1)
+ link_error ();
+ if (__builtin_uimaxabs (imaxmin) != INTMAX_MAX)
+ abort ();
+ if (__builtin_uimaxabs (imaxmin - 1) != (uintmax_t) 1 + INTMAX_MAX)
+ abort ();
+ if (__builtin_uimaxabs (-INTMAX_MAX) != INTMAX_MAX)
+ link_error ();
+ if (__builtin_uimaxabs (-INTMAX_MAX - 1) != (uintmax_t) 1 + INTMAX_MAX)
+ link_error ();
+ if (__builtin_uimaxabs (imaxmax) != INTMAX_MAX)
+ abort ();
+ if (__builtin_uimaxabs (INTMAX_MAX) != INTMAX_MAX)
+ link_error ();
+}
@@ -0,0 +1,2 @@
+set additional_flags -std=c2y
+return 0
@@ -0,0 +1 @@
+#include "lib/abs.c"