| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-fdump-tree-crc-details" } */ |
| 3 |
/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Os" "-flto" } } */ |
| 4 |
|
| 5 |
#include <stddef.h> |
| 6 |
typedef unsigned char u8; |
| 7 |
typedef unsigned short u16; |
| 8 |
typedef unsigned char __u8; |
| 9 |
typedef unsigned short __u16; |
| 10 |
struct i2c_msg { |
| 11 |
__u16 addr; |
| 12 |
__u16 flags; |
| 13 |
#define I2C_M_RD 0x0001 /* guaranteed to be 0x0001! */ |
| 14 |
/* ... */ |
| 15 |
__u16 len; |
| 16 |
__u8 *buf; |
| 17 |
}; |
| 18 |
|
| 19 |
#define POLY (0x1070U << 3) |
| 20 |
static u8 crc8(u16 data) |
| 21 |
{ |
| 22 |
int i; |
| 23 |
|
| 24 |
for (i = 0; i < 8; i++) { |
| 25 |
if (data & 0x8000) |
| 26 |
data = data ^ POLY; |
| 27 |
data = data << 1; |
| 28 |
} |
| 29 |
return (u8)(data >> 8); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* i2c_smbus_pec - Incremental CRC8 over the given input data array |
| 34 |
* @crc: previous return crc8 value |
| 35 |
* @p: pointer to data buffer. |
| 36 |
* @count: number of bytes in data buffer. |
| 37 |
* |
| 38 |
* Incremental CRC8 over count bytes in the array pointed to by p |
| 39 |
*/ |
| 40 |
u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
| 41 |
{ |
| 42 |
int i; |
| 43 |
|
| 44 |
for (i = 0; i < count; i++) |
| 45 |
crc = crc8((crc ^ p[i]) << 8); |
| 46 |
return crc; |
| 47 |
} |
| 48 |
static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg) |
| 49 |
{ |
| 50 |
return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/* Assume a 7-bit address, which is reasonable for SMBus */ |
| 55 |
u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
| 56 |
{ |
| 57 |
/* The address will be sent first */ |
| 58 |
u8 addr = i2c_8bit_addr_from_msg(msg); |
| 59 |
pec = i2c_smbus_pec(pec, &addr, 1); |
| 60 |
|
| 61 |
/* The data buffer follows */ |
| 62 |
return i2c_smbus_pec(pec, msg->buf, msg->len); |
| 63 |
} |
| 64 |
|
| 65 |
/* { dg-final { scan-tree-dump "calculates CRC!" "crc" } } */ |