2 /* { dg-options "-fno-strict-aliasing" } */
3 /* { dg-require-effective-target int32plus } */
8 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
9 #define av_le2ne32(x) (x)
11 #define av_le2ne32(x) av_bswap32(x)
14 static __attribute__((always_inline
)) inline __attribute__((const)) uint32_t av_bswap32(uint32_t x
)
16 return ((((x
) << 8 & 0xff00) | ((x
) >> 8 & 0x00ff)) << 16 | ((((x
) >> 16) << 8 & 0xff00) | (((x
) >> 16) >> 8 & 0x00ff)));
19 typedef uint32_t AVCRC
;
32 int av_crc_init(AVCRC
*ctx
, int le
, int bits
, uint32_t poly
, int ctx_size
);
39 uint32_t av_crc(const AVCRC
*ctx
, uint32_t crc
,
40 const uint8_t *buffer
, size_t length
) __attribute__((pure
));
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 },
54 static AVCRC av_crc_table
[AV_CRC_MAX
][1024];
57 int av_crc_init(AVCRC
*ctx
, int le
, int bits
, uint32_t poly
, int ctx_size
)
62 if (bits
< 8 || bits
> 32 || poly
>= (1LL << bits
))
64 if (ctx_size
!= sizeof(AVCRC
) * 257 && ctx_size
!= sizeof(AVCRC
) * 1024)
67 for (i
= 0; i
< 256; i
++) {
69 for (c
= i
, j
= 0; j
< 8; j
++)
70 c
= (c
>> 1) ^ (poly
& (-(c
& 1)));
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
);
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];
90 const AVCRC
*av_crc_get_table(AVCRCId crc_id
)
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)
100 return av_crc_table
[crc_id
];
103 uint32_t av_crc(const AVCRC
*ctx
, uint32_t crc
,
104 const uint8_t *buffer
, size_t length
)
106 const uint8_t *end
= buffer
+ length
;
110 while (((intptr_t) buffer
& 3) && buffer
< end
)
111 crc
= ctx
[((uint8_t) crc
) ^ *buffer
++] ^ (crc
>> 8);
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) )];
123 crc
= ctx
[((uint8_t) crc
) ^ *buffer
++] ^ (crc
>> 8);
131 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
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 }
144 for (i
= 0; i
< sizeof(buf
); i
++)
147 for (i
= 0; i
< 6; i
++) {
150 ctx
= av_crc_get_table (id
);
151 result
= av_crc(ctx
, 0, buf
, sizeof(buf
));
152 if (result
!= p
[i
][2])