(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
ubsan/
overflow-vec-2.c
       1  /* { dg-do run } */
       2  /* { dg-options "-Wno-psabi -fsanitize=signed-integer-overflow -Wno-unused-variable -fsanitize-recover=signed-integer-overflow" } */
       3  
       4  #define SCHAR_MAX __SCHAR_MAX__
       5  #define SCHAR_MIN (-__SCHAR_MAX__ - 1)
       6  #define SHRT_MAX __SHRT_MAX__
       7  #define SHRT_MIN (-__SHRT_MAX__ - 1)
       8  #define INT_MAX __INT_MAX__
       9  #define INT_MIN (-__INT_MAX__ - 1)
      10  
      11  typedef signed char VC __attribute__((vector_size (16)));
      12  typedef short VS __attribute__((vector_size (8 * sizeof (short))));
      13  typedef int VI __attribute__((vector_size (4 * sizeof (int))));
      14  typedef int VI2 __attribute__((vector_size (16 * sizeof (int))));
      15  
      16  void __attribute__((noinline,noclone))
      17  checkvc (VC i, VC j)
      18  {
      19    if (__builtin_memcmp (&i, &j, sizeof (VC)))
      20      __builtin_abort ();
      21  }
      22  
      23  void __attribute__((noinline,noclone))
      24  checkvs (VS i, VS j)
      25  {
      26    if (__builtin_memcmp (&i, &j, sizeof (VS)))
      27      __builtin_abort ();
      28  }
      29  
      30  void __attribute__((noinline,noclone))
      31  checkvi (VI i, VI j)
      32  {
      33    if (__builtin_memcmp (&i, &j, sizeof (VI)))
      34      __builtin_abort ();
      35  }
      36  
      37  void __attribute__((noinline,noclone))
      38  checkvi2 (VI2 i, VI2 j)
      39  {
      40    if (__builtin_memcmp (&i, &j, sizeof (VI2)))
      41      __builtin_abort ();
      42  }
      43  
      44  VI __attribute__((noinline,noclone))
      45  foo (VI i)
      46  {
      47    return -i;
      48  }
      49  
      50  VS __attribute__((noinline,noclone))
      51  bar (VS i, VS j)
      52  {
      53    return i + j;
      54  }
      55  
      56  int
      57  main (void)
      58  {
      59    /* Check that for a vector operation, only the first element with UB is reported.  */
      60    volatile VC a = (VC) { 0, SCHAR_MAX - 2, SCHAR_MAX - 2, 3, 2, 3, 4, 5,  0, 7,  1,  2,  3, 4,  SCHAR_MAX - 13, SCHAR_MAX };
      61    volatile VC b = (VC) { 5, 2,		   3,		  5, 0, 1, 2, 7,  8, 9,  10, 11, 6, -2, 13,	        1 };
      62    volatile VC k = b + a;
      63    checkvc (k,	  (VC) { 5, SCHAR_MAX,     SCHAR_MIN,     8, 2, 4, 6, 12, 8, 16, 11, 13, 9, 2,  SCHAR_MAX,      SCHAR_MIN });
      64    k = a + b;
      65    checkvc (k,     (VC) { 5, SCHAR_MAX,     SCHAR_MIN,     8, 2, 4, 6, 12, 8, 16, 11, 13, 9, 2,  SCHAR_MAX,      SCHAR_MIN });
      66  
      67    volatile VS c = (VS) { 0, SHRT_MAX - 2, SHRT_MAX - 2, 3, 3, 4,  SHRT_MAX - 13, SHRT_MAX };
      68    volatile VS d = (VS) { 5, 2,		  3,		5, 6, -2, 13,	         1 };
      69    volatile VS l = d + c;
      70    checkvs (l,	  (VS) { 5, SHRT_MAX,     SHRT_MIN,     8, 9, 2,  SHRT_MAX,      SHRT_MIN });
      71    l = bar (c, d);
      72    checkvs (l,     (VS) { 5, SHRT_MAX,     SHRT_MIN,     8, 9, 2,  SHRT_MAX,      SHRT_MIN });
      73  
      74    volatile VI e = (VI) { INT_MAX - 4, INT_MAX - 5, INT_MAX - 13, INT_MAX };
      75    volatile VI f = (VI) { 4,	      6,	   13,	         1 };
      76    volatile VI m = f + e;
      77    checkvi (m,	  (VI) { INT_MAX,     INT_MIN,     INT_MAX,      INT_MIN });
      78    m = e + f;
      79    checkvi (m,     (VI) { INT_MAX,     INT_MIN,     INT_MAX,      INT_MIN });
      80  
      81    volatile VI2 g = (VI2) { 0, INT_MAX - 2, INT_MAX - 2, 3, 3, 4,  INT_MAX - 13, INT_MAX };
      82    volatile VI2 h = (VI2) { 5, 2,	   3,		5, 6, -2, 13,	        1 };
      83    volatile VI2 n = h + g;
      84    checkvi2 (n,	   (VI2) { 5, INT_MAX,     INT_MIN,     8, 9, 2,  INT_MAX,      INT_MIN });
      85    n = g + h;
      86    checkvi2 (n,     (VI2) { 5, INT_MAX,     INT_MIN,     8, 9, 2,  INT_MAX,      INT_MIN });
      87  
      88    volatile VC a2 = k - b;
      89    checkvc (a2, a);
      90    volatile VC b2 = k - a;
      91    checkvc (b2, b);
      92  
      93    volatile VS c2 = l - d;
      94    checkvs (c2, c);
      95    volatile VS d2 = l - c;
      96    checkvs (d2, d);
      97  
      98    volatile VI e2 = m - f;
      99    checkvi (e2, e);
     100    volatile VI f2 = m - e;
     101    checkvi (f2, f);
     102  
     103    volatile VI2 g2 = n - h;
     104    checkvi2 (g2, g);
     105    volatile VI2 h2 = n - g;
     106    checkvi2 (h2, h);
     107  
     108    a	    = (VC) { 0,         SCHAR_MAX / 4, SCHAR_MAX / 4, 3, 2, 3, 4, 5, 0, 7, 1, 2, 3, 4, SCHAR_MAX - 13, SCHAR_MAX };
     109    b	    = (VC) { SCHAR_MAX, 4,	       5,	      2, 3, 4, 5, 2, 9, 2, 9, 1, 0, 8, 1,	       2 };
     110    k = a * b;
     111    checkvc (k, (VC) { 0,		124,	       -101,	      6, 6,12,20,10, 0,14, 9, 2, 0,32, SCHAR_MAX - 13, -2 });
     112  
     113    c	    = (VS) { 0,		SHRT_MAX / 8, SHRT_MAX / 7, 5, 8, 9, SHRT_MAX - 10, SHRT_MAX };
     114    d	    = (VS) { SHRT_MAX,  8,	      17,	    2, 3, 4, 1,		    3 };
     115    l = c * d;
     116    checkvs (l, (VS) { 0,		32760,	      14041,	   10,24,36, SHRT_MAX - 10, 32765 });
     117  
     118    e	    = (VI) { INT_MAX,	INT_MAX / 5,  INT_MAX / 6, INT_MAX };
     119    f	    = (VI) { 0,		5,	      7,	   2 };
     120    m = e * f;
     121    checkvi (m, (VI) { 0,		2147483645,   -1789569709, -2 });
     122  
     123    g	    = (VI2) { INT_MAX,	INT_MAX / 9,  INT_MAX / 8, 5, 6, 7, 8, INT_MAX };
     124    h	    = (VI2) { 0,	10,	      8,	   2, 3, 4, 5, 1 };
     125    n = g * h;
     126    checkvi2 (n,(VI2) { 0,	-1908874356,  2147483640, 10,18,28,40, INT_MAX });
     127  
     128    a	    = (VC) { 5, 7, 8, 9, SCHAR_MAX, SCHAR_MIN, 24, 32, 0, 1, 2, 3, 4, 5, SCHAR_MAX, SCHAR_MIN };
     129    k = -a;
     130    checkvc (k, (VC) {-5,-7,-8,-9,-SCHAR_MAX, SCHAR_MIN,-24,-32, 0,-1,-2,-3,-4,-5,-SCHAR_MAX, SCHAR_MIN });
     131  
     132    c	    = (VS) { 0, 7, 23, SHRT_MIN, SHRT_MIN, SHRT_MAX, 2, 5 };
     133    l = -c;
     134    checkvs (l, (VS) { 0,-7,-23, SHRT_MIN, SHRT_MIN,-SHRT_MAX,-2,-5 });
     135  
     136    e	    = (VI) { 5, INT_MAX, INT_MIN, INT_MIN };
     137    m = foo (e);
     138    checkvi (m, (VI) {-5,-INT_MAX, INT_MIN, INT_MIN });
     139  
     140    g	     = (VI2) { 10, 11, 0, INT_MAX - 2, 1, INT_MIN + 1, 5, INT_MIN };
     141    n = -g;
     142    checkvi2 (n, (VI2) {-10,-11, 0,-INT_MAX + 2,-1, INT_MAX,    -5, INT_MIN });
     143    return 0;
     144  }
     145  
     146  /* { dg-output "signed integer overflow: 3 \\+ 125 cannot be represented in type 'signed char'\[^\n\r]*(\n|\r\n|\r)" } */
     147  /* { dg-output "\[^\n\r]*signed integer overflow: 125 \\+ 3 cannot be represented in type 'signed char'\[^\n\r]*(\n|\r\n|\r)" } */
     148  /* { dg-output "\[^\n\r]*signed integer overflow: 3 \\+ 32765 cannot be represented in type 'short int'\[^\n\r]*(\n|\r\n|\r)" } */
     149  /* { dg-output "\[^\n\r]*signed integer overflow: 32765 \\+ 3 cannot be represented in type 'short int'\[^\n\r]*(\n|\r\n|\r)" } */
     150  /* { dg-output "\[^\n\r]*signed integer overflow: 6 \\+ 2147483642 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     151  /* { dg-output "\[^\n\r]*signed integer overflow: 2147483642 \\+ 6 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     152  /* { dg-output "\[^\n\r]*signed integer overflow: 3 \\+ 2147483645 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     153  /* { dg-output "\[^\n\r]*signed integer overflow: 2147483645 \\+ 3 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     154  /* { dg-output "\[^\n\r]*signed integer overflow: -128 - 3 cannot be represented in type 'signed char'\[^\n\r]*(\n|\r\n|\r)" } */
     155  /* { dg-output "\[^\n\r]*signed integer overflow: -128 - 125 cannot be represented in type 'signed char'\[^\n\r]*(\n|\r\n|\r)" } */
     156  /* { dg-output "\[^\n\r]*signed integer overflow: -32768 - 3 cannot be represented in type 'short int'\[^\n\r]*(\n|\r\n|\r)" } */
     157  /* { dg-output "\[^\n\r]*signed integer overflow: -32768 - 32765 cannot be represented in type 'short int'\[^\n\r]*(\n|\r\n|\r)" } */
     158  /* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 6 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     159  /* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 2147483642 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     160  /* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 3 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     161  /* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 2147483645 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     162  /* { dg-output "\[^\n\r]*signed integer overflow: 31 \\* 5 cannot be represented in type 'signed char'\[^\n\r]*(\n|\r\n|\r)" } */
     163  /* { dg-output "\[^\n\r]*signed integer overflow: 4681 \\* 17 cannot be represented in type 'short int'\[^\n\r]*(\n|\r\n|\r)" } */
     164  /* { dg-output "\[^\n\r]*signed integer overflow: 357913941 \\* 7 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     165  /* { dg-output "\[^\n\r]*signed integer overflow: 238609294 \\* 10 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
     166  /* { dg-output "\[^\n\r]*negation of -128 cannot be represented in type 'signed char'; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
     167  /* { dg-output "\[^\n\r]*negation of -32768 cannot be represented in type 'short int'; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
     168  /* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
     169  /* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself" } */