diff options
Diffstat (limited to 'gcc/testsuite/c-c++-common')
22 files changed, 690 insertions, 0 deletions
diff --git a/gcc/testsuite/c-c++-common/builtin-arith-overflow-1.c b/gcc/testsuite/c-c++-common/builtin-arith-overflow-1.c new file mode 100644 index 00000000000..69b508386d5 --- /dev/null +++ b/gcc/testsuite/c-c++-common/builtin-arith-overflow-1.c @@ -0,0 +1,49 @@ +/* { dg-do compile } */ + +int +f1 (void) +{ + int x = __builtin_add_overflow (); /* { dg-error "not enough arguments to function" } */ + x += __builtin_sub_overflow (); /* { dg-error "not enough arguments to function" } */ + x += __builtin_mul_overflow (); /* { dg-error "not enough arguments to function" } */ + return x; +} + +int +f2 (int a, int b, int *c, int d) +{ + int x = __builtin_add_overflow (a, b, c, d); /* { dg-error "too many arguments to function" } */ + x += __builtin_sub_overflow (a, b, c, d, d, d); /* { dg-error "too many arguments to function" } */ + x += __builtin_mul_overflow (a, b, c, d); /* { dg-error "too many arguments to function" } */ + return x; +} + +enum E { e0 = 0, e1 = 1 }; + +#ifndef __cplusplus +#define bool _Bool +#endif + +int +f3 (float fa, int a, _Complex long int ca, double fb, void *pb, int b, enum E eb, bool bb, int *c) +{ + int x = __builtin_add_overflow (fa, b, c); /* { dg-error "argument 1 in call to function\[^\n\r]*does not have integral type" } */ + x += __builtin_sub_overflow (ca, b, c); /* { dg-error "argument 1 in call to function\[^\n\r]*does not have integral type" } */ + x += __builtin_mul_overflow (a, fb, c); /* { dg-error "argument 2 in call to function\[^\n\r]*does not have integral type" } */ + x += __builtin_add_overflow (a, pb, c); /* { dg-error "argument 2 in call to function\[^\n\r]*does not have integral type" } */ + x += __builtin_sub_overflow (a, eb, c); + x += __builtin_mul_overflow (a, bb, c); + return x; +} + +int +f4 (float *fp, double *dp, _Complex int *cp, enum E *ep, bool *bp, long long int *llp) +{ + int x = __builtin_add_overflow (1, 2, fp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ + x += __builtin_sub_overflow (1, 2, dp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ + x += __builtin_mul_overflow (1, 2, cp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ + x += __builtin_add_overflow (1, 2, ep); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ + x += __builtin_sub_overflow (1, 2, bp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ + x += __builtin_mul_overflow (1, 2, llp); + return x; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.c new file mode 100644 index 00000000000..4d90ba5e1db --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.c @@ -0,0 +1,24 @@ +/* Test __builtin_{add,sub,mul,{s,u}add,{s,u}sub,{s,u}mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define U(s, op) s##op +TESTS (int, INT_MIN, INT_MAX) +#undef U +#define U(s, op) op +TESTS (int, INT_MIN, INT_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (int, INT_MIN, INT_MAX) +#undef U +#define U(s, op) s##op + TESTS (int, INT_MIN, INT_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.h b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.h new file mode 100644 index 00000000000..b7ae82ea85b --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-1.h @@ -0,0 +1,37 @@ +#include "builtin-arith-overflow.h" + +#define TESTS(type, min, max) \ +ST (100, signed type, 2, 3, 5, U(s, add), 0) \ +ST (101, signed type, max, -1, max - 1, U(s, add), 0) \ +ST (102, signed type, max, 0, max, U(s, add), 0) \ +ST (103, signed type, 1, max, min, U(s, add), 1) \ +ST (104, signed type, 0, min, min, U(s, sub), 1) \ +ST (110, signed type, 2, 3, -1, U(s, sub), 0) \ +ST (111, signed type, max, -1, min, U(s, sub), 1) \ +ST (112, signed type, max, 0, max, U(s, sub), 0) \ +ST (113, signed type, 1, max, min + 2, U(s, sub), 0) \ +ST (114, signed type, max, -1, min, U(s, sub), 1) \ +ST (120, signed type, 2, 3, 6, U(s, mul), 0) \ +ST (122, signed type, min, -1, min, U(s, mul), 1) \ +ST (123, signed type, max, 0, 0, U(s, mul), 0) \ +ST (124, signed type, 1, max, max, U(s, mul), 0) \ +ST (125, signed type, max, 2, -2, U(s, mul), 1) \ +ST (126, signed type, max / 25, 25, max / 25 * 25, U(s, mul), 0) \ +ST (127, signed type, max / 25 + 1, 25, max / 25 * 25 + (unsigned type) 25, U(s, mul), 1) \ +ST (150, unsigned type, 2, 3, 5, U(u, add), 0) \ +ST (151, unsigned type, -1, -1, -2, U(u, add), 1) \ +ST (152, unsigned type, -1, 0, -1, U(u, add), 0) \ +ST (153, unsigned type, 1, -1, 0, U(u, add), 1) \ +ST (154, unsigned type, 0, min, min, U(u, sub), 1) \ +ST (160, unsigned type, 2, 3, -1, U(u, sub), 1) \ +ST (161, unsigned type, -1, -1, 0, U(u, sub), 0) \ +ST (162, unsigned type, -1, 0, -1, U(u, sub), 0) \ +ST (163, unsigned type, 1, -1, 2, U(u, sub), 1) \ +ST (164, unsigned type, 15, 14, 1, U(u, sub), 0) \ +ST (170, unsigned type, 2, 3, 6, U(u, mul), 0) \ +ST (171, unsigned type, max, 3, 3 * (unsigned type) max, U(u, mul), 1) \ +ST (172, unsigned type, -1, 0, 0, U(u, mul), 0) \ +ST (173, unsigned type, 1, -1, -1, U(u, mul), 0) \ +ST (174, unsigned type, -1, 2, -2, U(u, mul), 1) \ +ST (175, unsigned type, ((unsigned type) -1) / 25, 25, ((unsigned type) -1) / 25 * 25, U(u, mul), 0) \ +ST (176, unsigned type, ((unsigned type) -1) / 25 + 1, 25, ((unsigned type) -1) / 25 * 25 + (unsigned type) 25, U(u, mul), 1) diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-10.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-10.c new file mode 100644 index 00000000000..6b1c427c91c --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-10.c @@ -0,0 +1,19 @@ +/* Test __builtin_{add,sub}_overflow on {,un}signed long int. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +typedef signed long int S; +typedef unsigned long int U; +#define COND 1 +#define SHIFT ((__SIZEOF_LONG__ - 1) * __CHAR_BIT__) +#define S_MAX __LONG_MAX__ +#define S_MIN (-__LONG_MAX__ - 1) +#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__ +typedef long long int W; +#elif __SIZEOF_INT128__ > __SIZEOF_LONG__ +typedef __int128 W; +#else +#undef COND +#define COND 0 +#endif +#include "builtin-arith-overflow-7.c" diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-11.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-11.c new file mode 100644 index 00000000000..1e7d4ae3424 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-11.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub}_overflow on {,un}signed long long int. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +typedef signed long long int S; +typedef unsigned long long int U; +#define COND 1 +#define SHIFT ((__SIZEOF_LONG_LONG__ - 1) * __CHAR_BIT__) +#define S_MAX __LONG_LONG_MAX__ +#define S_MIN (-__LONG_LONG_MAX__ - 1) +#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__ +typedef __int128 W; +#else +#undef COND +#define COND 0 +#endif +#include "builtin-arith-overflow-7.c" diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.c new file mode 100644 index 00000000000..a37d817205b --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub,mul_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +TESTS (int, INT_MIN, INT_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (int, INT_MIN, INT_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.h b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.h new file mode 100644 index 00000000000..1e48456dd64 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-12.h @@ -0,0 +1,80 @@ +#include "builtin-arith-overflow.h" + +#define TESTS(type, min, max) \ +T (100, signed type, unsigned type, unsigned type, -1, 0, 0, mul, 0) \ +T (101, signed type, unsigned type, unsigned type, -1, 1, (unsigned type) -1, mul, 1) \ +T (102, unsigned type, signed type, unsigned type, 12, -3, (unsigned type) -36, mul, 1) \ +T (103, signed type, unsigned type, unsigned type, 3, 4, 12, mul, 0) \ +T (104, unsigned type, signed type, unsigned type, (unsigned type) -1 / 12, 12, (unsigned type) -1 / 12 * 12, mul, 0) \ +T (105, unsigned type, signed type, unsigned type, (unsigned type) -1 / 12, 13, (unsigned type) -1 / 12 * 13, mul, 1) \ +T (106, unsigned type, unsigned type, signed type, 0, 0, 0, mul, 0) \ +T (107, unsigned type, unsigned type, signed type, max / 31, 31, (signed type) ((unsigned type) max / 31 * 31), mul, 0) \ +T (108, unsigned type, unsigned type, signed type, max / 31, 32, (signed type) ((unsigned type) max / 31 * 32), mul, 1) \ +T (109, unsigned type, unsigned type, signed type, max / 31, 65, (signed type) ((unsigned type) max / 31 * 65), mul, 1) \ +T (110, signed type, unsigned type, signed type, -1, 7, -7, mul, 0) \ +T (111, unsigned type, signed type, signed type, 2, min / 2, min, mul, 0) \ +T (112, signed type, unsigned type, signed type, max / 12, 13, (signed type) ((unsigned type) max / 12 * 13), mul, 1) \ +T (113, unsigned type, signed type, signed type, (unsigned type) max + 19, 0, 0, mul, 0) \ +T (114, signed type, unsigned type, signed type, 0, (unsigned type) max + 1, 0, mul, 0) \ +T (115, unsigned type, signed type, signed type, (unsigned type) max + 1, -1, min, mul, 0) \ +T (116, signed type, unsigned type, signed type, -1, (unsigned type) max + 2, max, mul, 1) \ +T (117, signed type, signed type, unsigned type, min / 64, -64, (unsigned type) min, mul, 0) \ +T (118, signed type, signed type, unsigned type, min / 32, -33, ((unsigned type) max + 1) / 32 * 33, mul, 0) \ +T (119, signed type, signed type, unsigned type, min / 32, -65, ((unsigned type) max + 1) / 32 * 65, mul, 1) \ +T (120, signed type, signed type, unsigned type, -1, -1, 1, mul, 0) \ +T (121, signed type, signed type, unsigned type, 0, 0, 0, mul, 0) \ +T (122, signed type, signed type, unsigned type, 0, -6, 0, mul, 0) \ +T (123, signed type, signed type, unsigned type, -15, 0, 0, mul, 0) \ +T (124, signed type, signed type, unsigned type, -1, 1, ~(unsigned type) 0, mul, 1) \ +T (125, signed type, signed type, unsigned type, -17, 5, (unsigned type) -17 * 5, mul, 1) \ +T (126, signed type, signed type, unsigned type, 7, max / 7, max / 7 * 7, mul, 0) \ +T (127, signed type, signed type, unsigned type, max / 7, 8, (unsigned type) max / 7 * 8, mul, 0) \ +T (128, signed type, signed type, unsigned type, 15, max / 7, (unsigned type) max / 7 * 15, mul, 1) \ +T (129, signed type, unsigned type, signed type, min, 5, min + 5, add, 0) \ +T (130, unsigned type, signed type, signed type, ~(unsigned type) 0, min, max, add, 0) \ +T (131, signed type, unsigned type, signed type, max, 1, min, add, 1) \ +T (132, unsigned type, signed type, signed type, max / 2, max / 2 + 1, max, add, 0) \ +T (133, signed type, unsigned type, signed type, max / 2 + 1, max / 2 + 1, min, add, 1) \ +T (134, signed type, unsigned type, unsigned type, min, ~(unsigned type) 0, max, add, 0) \ +T (135, unsigned type, signed type, unsigned type, ~(unsigned type) 0, min + 1, (unsigned type) max + 1, add, 0) \ +T (136, signed type, unsigned type, unsigned type, 1, ~(unsigned type) 0, 0, add, 1) \ +T (137, unsigned type, signed type, unsigned type, 2, -3, ~(unsigned type) 0, add, 1) \ +T (138, signed type, unsigned type, signed type, min, 1, max, sub, 1) \ +T (139, signed type, unsigned type, signed type, min + 1, 1, min, sub, 0) \ +T (140, signed type, unsigned type, signed type, max, (unsigned type) max + 1, -1, sub, 0) \ +T (141, signed type, unsigned type, signed type, max, ~(unsigned type) 0, min, sub, 0) \ +T (142, signed type, unsigned type, signed type, max - 1, ~(unsigned type) 0, max, sub, 1) \ +T (143, signed type, unsigned type, unsigned type, -1, 0, ~(unsigned type) 0, sub, 1) \ +T (144, signed type, unsigned type, unsigned type, -1, ~(unsigned type) 0, 0, sub, 1) \ +T (145, signed type, unsigned type, unsigned type, min, 0, min, sub, 1) \ +T (146, signed type, unsigned type, unsigned type, max, max, 0, sub, 0) \ +T (147, signed type, unsigned type, unsigned type, max, (unsigned type) max + 1, -1, sub, 1) \ +T (148, signed type, unsigned type, unsigned type, max - 1, max, -1, sub, 1) \ +T (149, unsigned type, signed type, signed type, 0, max, -max, sub, 0) \ +T (150, unsigned type, signed type, signed type, (unsigned type) max + 1, 0, min, sub, 1) \ +T (151, unsigned type, signed type, signed type, (unsigned type) max + 1, 1, max, sub, 0) \ +T (152, unsigned type, unsigned type, signed type, 0, (unsigned type) max + 1, min, add, 1) \ +T (153, signed type, signed type, unsigned type, -1, 0, -1, add, 1) \ +T (154, unsigned type, signed type, unsigned type, 5, 6, -1, sub, 1) \ +T (155, unsigned type, signed type, unsigned type, ~(unsigned type) 0, max, (unsigned type) max + 1, sub, 0) \ +T (156, unsigned type, signed type, unsigned type, (unsigned type) max + 1, min, 0, sub, 1) \ +T (157, signed type, signed type, unsigned type, 3, -2, 1, add, 0) \ +T (158, signed type, signed type, unsigned type, 3, -4, -1, add, 1) \ +T (159, signed type, signed type, unsigned type, -3, -4, -7, add, 1) \ +T (160, signed type, signed type, unsigned type, -5, 4, -1, add, 1) \ +T (161, signed type, signed type, unsigned type, -5, 5, 0, add, 0) \ +T (162, signed type, signed type, unsigned type, min, 1, min + 1, add, 1) \ +T (163, unsigned type, unsigned type, signed type, max, 1, min, add, 1) \ +T (164, unsigned type, unsigned type, signed type, max - 1, 1, max, add, 0) \ +T (165, unsigned type, unsigned type, signed type, ~(unsigned type) 0, ~(unsigned type) 0, ~(unsigned type) 0 - 1, add, 1) \ +T (166, unsigned type, unsigned type, signed type, (unsigned type) max + 3, 2, min, sub, 1) \ +T (167, unsigned type, unsigned type, signed type, (unsigned type) max + 2, 2, max, sub, 0) \ +T (168, unsigned type, unsigned type, signed type, (unsigned type) max + 2, (unsigned type) max + 3, -1, sub, 0) \ +T (169, unsigned type, unsigned type, signed type, 0, (unsigned type) max + 1, min, sub, 0) \ +T (170, unsigned type, unsigned type, signed type, 0, (unsigned type) max + 2, max, sub, 1) \ +T (171, signed type, signed type, unsigned type, 3, 2, 1, sub, 0) \ +T (172, signed type, signed type, unsigned type, 3, 4, -1, sub, 1) \ +T (173, signed type, signed type, unsigned type, -3, 4, -7, sub, 1) \ +T (174, signed type, signed type, unsigned type, -5, -4, -1, sub, 1) \ +T (175, signed type, signed type, unsigned type, -5, -5, 0, sub, 0) \ +T (176, signed type, signed type, unsigned type, min, -1, min + 1, sub, 1) diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-13.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-13.c new file mode 100644 index 00000000000..730b6d964af --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-13.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +TESTS (long, LONG_MIN, LONG_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (long, LONG_MIN, LONG_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-14.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-14.c new file mode 100644 index 00000000000..6f03cf71949 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-14.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +TESTS (long long, LLONG_MIN, LLONG_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (long long, LLONG_MIN, LLONG_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-15.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-15.c new file mode 100644 index 00000000000..3d78994d7dd --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-15.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +TESTS (char, SCHAR_MIN, SCHAR_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (char, SCHAR_MIN, SCHAR_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-16.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-16.c new file mode 100644 index 00000000000..d148be6507e --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-16.c @@ -0,0 +1,17 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +TESTS (short, SHRT_MIN, SHRT_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (short, SHRT_MIN, SHRT_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-17.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-17.c new file mode 100644 index 00000000000..3b33e49220c --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-17.c @@ -0,0 +1,20 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run { target int128 } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-12.h" + +#define INT128_MAX ((signed __int128) (((unsigned __int128) 1 << (__SIZEOF_INT128__ * __CHAR_BIT__ - 1)) - 1)) +#define INT128_MIN (-INT128_MAX - 1) + +TESTS (__int128, INT128_MIN, INT128_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (__int128, INT128_MIN, INT128_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-18.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-18.c new file mode 100644 index 00000000000..cce5b11b7cf --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-18.c @@ -0,0 +1,36 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow.h" + +#ifdef __SIZEOF_INT128__ +#define WTYPE __int128 +#else +#define WTYPE long long int +#endif + +#define TESTS \ +T (100, signed char, signed char, unsigned WTYPE, -1, 0, -1, add, 1) \ +T (101, unsigned char, unsigned char, unsigned WTYPE, 5, 5, 10, add, 0) \ +T (102, signed char, unsigned short, unsigned WTYPE, 5, 5, 0, sub, 0) \ +T (103, signed char, unsigned short, unsigned WTYPE, 5, 6, -1, sub, 1) \ +T (104, signed char, signed char, unsigned WTYPE, -1, -1, 1, mul, 0) \ +T (105, unsigned char, signed char, unsigned WTYPE, 17, -2, -34, mul, 1) \ +T (106, unsigned WTYPE, signed WTYPE, signed char, 5, -2, -10, mul, 0) \ +T (107, long long int, long long int, unsigned char, -3, 5, 2, add, 0) \ +T (108, long long int, int, unsigned char, -5, 3, -2, add, 1) \ +T (109, int, WTYPE, unsigned char, -3, 5, 2, add, 0) \ +T (110, unsigned char, unsigned char, unsigned WTYPE, SCHAR_MAX - 1, (unsigned char) SCHAR_MAX + 4, -5, sub, 1) + +TESTS + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-2.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-2.c new file mode 100644 index 00000000000..4b51d9eaa04 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-2.c @@ -0,0 +1,24 @@ +/* Test __builtin_{add,sub,mul,{s,u}addl,{s,u}subl,{s,u}mull}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define U(s, op) s##op##l +TESTS (long, LONG_MIN, LONG_MAX) +#undef U +#define U(s, op) op +TESTS (long, LONG_MIN, LONG_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (long, LONG_MIN, LONG_MAX) +#undef U +#define U(s, op) s##op##l + TESTS (long, LONG_MIN, LONG_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-3.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-3.c new file mode 100644 index 00000000000..cc1a44117ad --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-3.c @@ -0,0 +1,24 @@ +/* Test __builtin_{add,sub,mul,{s,u}addll,{s,u}subll,{s,u}mulll}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define U(s, op) s##op##ll +TESTS (long long, LLONG_MIN, LLONG_MAX) +#undef U +#define U(s, op) op +TESTS (long long, LLONG_MIN, LLONG_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (long long, LLONG_MIN, LLONG_MAX) +#undef U +#define U(s, op) s##op##ll + TESTS (long long, LLONG_MIN, LLONG_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-4.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-4.c new file mode 100644 index 00000000000..6d35aa1ae15 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-4.c @@ -0,0 +1,18 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define U(s, op) op +TESTS (char, SCHAR_MIN, SCHAR_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (char, SCHAR_MIN, SCHAR_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-5.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-5.c new file mode 100644 index 00000000000..fd5c7c981c4 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-5.c @@ -0,0 +1,18 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define U(s, op) op +TESTS (short, SHRT_MIN, SHRT_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (short, SHRT_MIN, SHRT_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-6.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-6.c new file mode 100644 index 00000000000..e2661e77149 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-6.c @@ -0,0 +1,21 @@ +/* Test __builtin_{add,sub,mul}_overflow. */ +/* { dg-do run { target int128 } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#include "builtin-arith-overflow-1.h" + +#define INT128_MAX ((signed __int128) (((unsigned __int128) 1 << (__SIZEOF_INT128__ * __CHAR_BIT__ - 1)) - 1)) +#define INT128_MIN (-INT128_MAX - 1) + +#define U(s, op) op +TESTS (__int128, INT128_MIN, INT128_MAX) + +#undef T +#define T(n, t1, t2, tr, v1, v2, vr, b, o) t##n##b (); + +int +main () +{ + TESTS (__int128, INT128_MIN, INT128_MAX) + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-7.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-7.c new file mode 100644 index 00000000000..6f61d7c553c --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-7.c @@ -0,0 +1,80 @@ +/* Test __builtin_{add,sub}_overflow on {,un}signed char. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +#define UCHAR_MAX ((unsigned char) ~0) +#ifndef SHIFT +typedef signed char S; +typedef unsigned char U; +typedef int W; +#define SHIFT 0 +#define S_MAX __SCHAR_MAX__ +#define S_MIN (-__SCHAR_MAX__ - 1) +#define COND (__SIZEOF_INT__ > 1) +#endif + +#define F(n, t1, t2, tr, b) \ +__attribute__((noinline, noclone)) tr \ +n (t1 x, t2 y, int *ovf) \ +{ \ + tr res; \ + *ovf = __builtin_##b##_overflow (x, y, &res); \ + return res; \ +} + +F (spses, S, S, S, add) +F (upueu, U, U, U, add) +F (spseu, S, S, U, add) +F (upues, U, U, S, add) +F (spues, S, U, S, add) +F (upses, U, S, S, add) +F (spueu, S, U, U, add) +F (upseu, U, S, U, add) +F (ssses, S, S, S, sub) +F (usueu, U, U, U, sub) +F (ssseu, S, S, U, sub) +F (usues, U, U, S, sub) +F (ssues, S, U, S, sub) +F (usses, U, S, S, sub) +F (ssueu, S, U, U, sub) +F (usseu, U, S, U, sub) + +int +main () +{ +#if COND + int i, j; + for (i = 0; i < UCHAR_MAX; i++) + for (j = 0; j < UCHAR_MAX; j++) + { + S s1 = ((W) i << SHIFT) + S_MIN; + U u1 = ((W) i << SHIFT); + S s2 = ((W) j << SHIFT) + S_MIN; + U u2 = ((W) j << SHIFT); + W w; + int ovf; +#define T(n, t1, t2, tr, op) \ + w = ((W) t1##1) op ((W) t2##2); \ + if (n (t1##1, t2##2, &ovf) != (tr) w \ + || ovf != (w != (tr) w)) \ + __builtin_abort (); + T (spses, s, s, S, +) + T (upueu, u, u, U, +) + T (spseu, s, s, U, +) + T (upues, u, u, S, +) + T (spues, s, u, S, +) + T (upses, u, s, S, +) + T (spueu, s, u, U, +) + T (upseu, u, s, U, +) + T (ssses, s, s, S, -) + T (usueu, u, u, U, -) + T (ssseu, s, s, U, -) + T (usues, u, u, S, -) + T (ssues, s, u, S, -) + T (usses, u, s, S, -) + T (ssueu, s, u, U, -) + T (usseu, u, s, U, -) + } +#endif + return 0; +} diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-8.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-8.c new file mode 100644 index 00000000000..9e1754fed30 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-8.c @@ -0,0 +1,23 @@ +/* Test __builtin_{add,sub}_overflow on {,un}signed short. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +typedef signed short int S; +typedef unsigned short int U; +#define COND 1 +#define SHIFT ((__SIZEOF_SHORT__ - 1) * __CHAR_BIT__) +#define S_MAX __SHRT_MAX__ +#define S_MIN (-__SHRT_MAX__ - 1) +#if __SIZEOF_INT__ > __SIZEOF_SHORT__ +typedef int W; +#elif __SIZEOF_LONG__ > __SIZEOF_SHORT__ +typedef long int W; +#elif __SIZEOF_LONG_LONG__ > __SIZEOF_SHORT__ +typedef long long int W; +#elif __SIZEOF_INT128__ > __SIZEOF_SHORT__ +typedef __int128 W; +#else +#undef COND +#define COND 0 +#endif +#include "builtin-arith-overflow-7.c" diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-9.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-9.c new file mode 100644 index 00000000000..80549069718 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-9.c @@ -0,0 +1,21 @@ +/* Test __builtin_{add,sub}_overflow on {,un}signed int. */ +/* { dg-do run } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ + +typedef signed int S; +typedef unsigned int U; +#define COND 1 +#define SHIFT ((__SIZEOF_INT__ - 1) * __CHAR_BIT__) +#define S_MAX __INT_MAX__ +#define S_MIN (-__INT_MAX__ - 1) +#if __SIZEOF_LONG__ > __SIZEOF_INT__ +typedef long int W; +#elif __SIZEOF_LONG_LONG__ > __SIZEOF_INT__ +typedef long long int W; +#elif __SIZEOF_INT128__ > __SIZEOF_INT__ +typedef __int128 W; +#else +#undef COND +#define COND 0 +#endif +#include "builtin-arith-overflow-7.c" diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow.h b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow.h new file mode 100644 index 00000000000..9bf3ecf1f21 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow.h @@ -0,0 +1,94 @@ +#define SCHAR_MAX __SCHAR_MAX__ +#define SCHAR_MIN (-__SCHAR_MAX__ - 1) +#define SHRT_MAX __SHRT_MAX__ +#define SHRT_MIN (-__SHRT_MAX__ - 1) +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MAX __LONG_MAX__ +#define LONG_MIN (-__LONG_MAX__ - 1) +#define LLONG_MAX __LONG_LONG_MAX__ +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1) + +int v; + +__attribute__((noinline, noclone)) void +bar (void) +{ + v++; +} + +#define T(n, t1, t2, tr, v1, v2, vr, b, o) \ +__attribute__((noinline, noclone)) tr \ +t##n##_1##b (t1 x, t2 y) \ +{ \ + tr r; \ + if (__builtin_##b##_overflow (x, y, &r)) \ + bar (); \ + return r; \ +} \ + \ +__attribute__((noinline, noclone)) tr \ +t##n##_2##b (t2 y) \ +{ \ + t1 x = (v1); \ + tr r; \ + if (__builtin_##b##_overflow (x, y, &r)) \ + bar (); \ + return r; \ +} \ + \ +__attribute__((noinline, noclone)) tr \ +t##n##_3##b (t2 y) \ +{ \ + tr r; \ + if (__builtin_##b##_overflow ((t1) (v1), y, \ + &r)) \ + bar (); \ + return r; \ +} \ + \ +__attribute__((noinline, noclone)) tr \ +t##n##_4##b (t1 x) \ +{ \ + t2 y = (v2); \ + tr r; \ + if (__builtin_##b##_overflow (x, y, &r)) \ + bar (); \ + return r; \ +} \ + \ +__attribute__((noinline, noclone)) tr \ +t##n##_5##b (t1 x) \ +{ \ + tr r; \ + if (__builtin_##b##_overflow (x, (t2) (v2), \ + &r)) \ + bar (); \ + return r; \ +} \ + \ +__attribute__((noinline, noclone)) void \ +t##n##b (void) \ +{ \ + t1 x = (v1); \ + t2 y = (v2); \ + tr r1, r2; \ + v = 0; \ + if (t##n##_1##b (x, y) != (tr) (vr) \ + || t##n##_2##b (y) != (tr) (vr) \ + || t##n##_3##b (y) != (tr) (vr) \ + || t##n##_4##b (x) != (tr) (vr) \ + || t##n##_5##b (x) != (tr) (vr)) \ + __builtin_abort (); \ + if (__builtin_##b##_overflow (x, y, &r1)) \ + bar (); \ + if (r1 != (tr) (vr)) \ + __builtin_abort (); \ + if (__builtin_##b##_overflow ((t1) (v1), \ + (t2) (v2), &r2))\ + bar (); \ + if (r2 != (tr) (vr) || v != 7 * o) \ + __builtin_abort (); \ +} +#define ST(n, t, v1, v2, vr, b, o) \ +T (n, t, t, t, v1, v2, vr, b, o) |