(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
memchr.c
       1  /* PR middle-end/78257 - missing memcmp optimization with constant arrays
       2     { dg-do compile }
       3     { dg-options "-O -Wall -fdump-tree-optimized" }
       4     { dg-skip-if "test assumes structs are not packed" { default_packed } } */
       5  
       6  typedef __INT8_TYPE__  int8_t;
       7  typedef __INT16_TYPE__ int16_t;
       8  typedef __INT32_TYPE__ int32_t;
       9  typedef __SIZE_TYPE__  size_t;
      10  
      11  extern void* memchr (const void*, int, size_t);
      12  
      13  /* Verify that initializers for flexible array members are handled
      14     correctly.  */
      15  
      16  struct SX
      17  {
      18    /* offset */
      19    /*   0    */ int32_t n;
      20    /*   4    */ int8_t: 1;
      21    /*   6    */ int16_t a[];
      22  };
      23  
      24  _Static_assert (__builtin_offsetof (struct SX, a) == 6);
      25  
      26  const struct SX sx =
      27    {
      28     0x11121314, { 0x2122, 0x3132, 0x4142, 0x5152 }
      29    };
      30  
      31  const char sx_rep[] =
      32    {
      33  #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
      34     0x11, 0x12, 0x13, 0x14, 0, 0, 0x21, 0x22, 0x31, 0x32, 0x41, 0x42, 0x51, 0x52
      35  #else
      36     0x14, 0x13, 0x12, 0x11, 0, 0, 0x22, 0x21, 0x32, 0x31, 0x42, 0x41, 0x52, 0x51
      37  #endif
      38    };
      39  
      40  
      41  void test_find (void)
      42  {
      43    int n = 0, nb = (const char*)&sx.a[4] - (const char*)&sx;
      44    const char *p = (const char*)&sx, *q = sx_rep;
      45  
      46    if (nb != sizeof sx_rep)
      47      __builtin_abort ();
      48  
      49    n += p      == memchr (p, q[ 0], nb);
      50    n += p +  1 == memchr (p, q[ 1], nb);
      51    n += p +  2 == memchr (p, q[ 2], nb);
      52    n += p +  3 == memchr (p, q[ 3], nb);
      53    n += p +  4 == memchr (p, q[ 4], nb);
      54    n += p +  4 == memchr (p, q[ 5], nb);
      55    n += p +  6 == memchr (p, q[ 6], nb);
      56    n += p +  7 == memchr (p, q[ 7], nb);
      57    n += p +  8 == memchr (p, q[ 8], nb);
      58    n += p +  9 == memchr (p, q[ 9], nb);
      59    n += p + 10 == memchr (p, q[10], nb);
      60    n += p + 11 == memchr (p, q[11], nb);
      61    n += p + 12 == memchr (p, q[12], nb);
      62    n += p + 13 == memchr (p, q[13], nb);
      63  
      64    if (n != 14)
      65      __builtin_abort ();
      66  }
      67  
      68  void test_not_find (void)
      69  {
      70    int n = 0, nb = (const char*)&sx.a[4] - (const char*)&sx;
      71    const char *p = (const char*)&sx, *q = sx_rep;
      72  
      73    if (nb != sizeof sx_rep)
      74      __builtin_abort ();
      75  
      76    n += 0 == memchr (p,      0xff, nb);
      77    n += 0 == memchr (p +  1, q[ 0], nb - 1);
      78    n += 0 == memchr (p +  2, q[ 1], nb - 2);
      79    n += 0 == memchr (p +  3, q[ 2], nb - 3);
      80    n += 0 == memchr (p +  4, q[ 3], nb - 4);
      81    n += 0 == memchr (p +  6, q[ 4], nb - 6);
      82    n += 0 == memchr (p +  7, q[ 6], nb - 7);
      83    n += 0 == memchr (p +  8, q[ 7], nb - 8);
      84    n += 0 == memchr (p +  9, q[ 8], nb - 9);
      85    n += 0 == memchr (p + 10, q[ 9], nb - 10);
      86    n += 0 == memchr (p + 11, q[10], nb - 11);
      87    n += 0 == memchr (p + 12, q[11], nb - 12);
      88    n += 0 == memchr (p + 13, q[12], nb - 13);
      89    n += 0 == memchr (p + 14, q[13], nb - 14);
      90  
      91    if (n != 14)
      92      __builtin_abort ();
      93  }
      94  
      95  /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */