(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
torture/
pr69714.c
       1  /* { dg-do run } */
       2  /* { dg-options "-fno-strict-aliasing" } */
       3  /* { dg-require-effective-target int32plus } */
       4  
       5  #include <stdint.h>
       6  #include <stdio.h>
       7  
       8  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
       9  #define av_le2ne32(x) (x)
      10  #else
      11  #define av_le2ne32(x) av_bswap32(x)
      12  #endif
      13  
      14  static __attribute__((always_inline)) inline __attribute__((const)) uint32_t av_bswap32(uint32_t x)
      15  {
      16      return ((((x) << 8 & 0xff00) | ((x) >> 8 & 0x00ff)) << 16 | ((((x) >> 16) << 8 & 0xff00) | (((x) >> 16) >> 8 & 0x00ff)));
      17  }
      18  
      19  typedef uint32_t AVCRC;
      20  
      21  typedef enum {
      22      AV_CRC_8_ATM,
      23      AV_CRC_16_ANSI,
      24      AV_CRC_16_CCITT,
      25      AV_CRC_32_IEEE,
      26      AV_CRC_32_IEEE_LE,
      27      AV_CRC_16_ANSI_LE,
      28      AV_CRC_24_IEEE = 12,
      29      AV_CRC_MAX,
      30  } AVCRCId;
      31  
      32  int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size);
      33  
      34  
      35  
      36  
      37  
      38  
      39  uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
      40                  const uint8_t *buffer, size_t length) __attribute__((pure));
      41  static struct {
      42      uint8_t le;
      43      uint8_t bits;
      44      uint32_t poly;
      45  } av_crc_table_params[AV_CRC_MAX] = {
      46      [AV_CRC_8_ATM] = { 0, 8, 0x07 },
      47      [AV_CRC_16_ANSI] = { 0, 16, 0x8005 },
      48      [AV_CRC_16_CCITT] = { 0, 16, 0x1021 },
      49      [AV_CRC_24_IEEE] = { 0, 24, 0x864CFB },
      50      [AV_CRC_32_IEEE] = { 0, 32, 0x04C11DB7 },
      51      [AV_CRC_32_IEEE_LE] = { 1, 32, 0xEDB88320 },
      52      [AV_CRC_16_ANSI_LE] = { 1, 16, 0xA001 },
      53  };
      54  static AVCRC av_crc_table[AV_CRC_MAX][1024];
      55  
      56  
      57  int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
      58  {
      59      unsigned i, j;
      60      uint32_t c;
      61  
      62      if (bits < 8 || bits > 32 || poly >= (1LL << bits))
      63          return -1;
      64      if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) * 1024)
      65          return -1;
      66  
      67      for (i = 0; i < 256; i++) {
      68          if (le) {
      69              for (c = i, j = 0; j < 8; j++)
      70                  c = (c >> 1) ^ (poly & (-(c & 1)));
      71              ctx[i] = c;
      72          } else {
      73              for (c = i << 24, j = 0; j < 8; j++)
      74                  c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >> 31));
      75              ctx[i] = av_bswap32(c);
      76          }
      77      }
      78      ctx[256] = 1;
      79  
      80      if (ctx_size >= sizeof(AVCRC) * 1024)
      81          for (i = 0; i < 256; i++)
      82              for (j = 0; j < 3; j++)
      83                  ctx[256 *(j + 1) + i] =
      84                      (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
      85  
      86  
      87      return 0;
      88  }
      89  
      90  const AVCRC *av_crc_get_table(AVCRCId crc_id)
      91  {
      92      if (!av_crc_table[crc_id][(sizeof(av_crc_table[crc_id]) / sizeof((av_crc_table[crc_id])[0])) - 1])
      93          if (av_crc_init(av_crc_table[crc_id],
      94                          av_crc_table_params[crc_id].le,
      95                          av_crc_table_params[crc_id].bits,
      96                          av_crc_table_params[crc_id].poly,
      97                          sizeof(av_crc_table[crc_id])) < 0)
      98              return ((void *)0);
      99  
     100      return av_crc_table[crc_id];
     101  }
     102  
     103  uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
     104                  const uint8_t *buffer, size_t length)
     105  {
     106      const uint8_t *end = buffer + length;
     107  
     108  
     109      if (!ctx[256]) {
     110          while (((intptr_t) buffer & 3) && buffer < end)
     111              crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
     112  
     113          while (buffer < end - 3) {
     114              crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
     115              crc = ctx[3 * 256 + ( crc & 0xFF)] ^
     116                    ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
     117                    ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
     118                    ctx[0 * 256 + ((crc >> 24) )];
     119          }
     120      }
     121  
     122      while (buffer < end)
     123          crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
     124  
     125      return crc;
     126  }
     127  
     128  
     129  int main(void)
     130  {
     131  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
     132      uint8_t buf[1999];
     133      int i;
     134      unsigned
     135        p[6][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
     136  		  { AV_CRC_32_IEEE , 0x04C11DB7, 0xE0BAF5C0 },
     137  		  { AV_CRC_24_IEEE , 0x864CFB , 0x326039 },
     138  		  { AV_CRC_16_ANSI_LE, 0xA001 , 0xBFD8 },
     139  		  { AV_CRC_16_ANSI , 0x8005 , 0xBB1F },
     140  		  { AV_CRC_8_ATM , 0x07 , 0xE3 }
     141      };
     142      const AVCRC *ctx;
     143  
     144      for (i = 0; i < sizeof(buf); i++)
     145          buf[i] = i + i * i;
     146  
     147      for (i = 0; i < 6; i++) {
     148          int id = p[i][0];
     149  	uint32_t result;
     150          ctx = av_crc_get_table (id);
     151  	result = av_crc(ctx, 0, buf, sizeof(buf));
     152  	if (result != p[i][2])
     153  	  __builtin_abort ();
     154      }
     155  #endif
     156      return 0;
     157  }