|
| 1 | +#include <assert.h> |
| 2 | +#include <limits.h> |
| 3 | +#include <stdint.h> |
| 4 | + |
| 5 | +#define rol(type, x, d) \ |
| 6 | + ((type)(((x) << (d)) | ((x) >> ((8 * sizeof(type)) - (d))))) |
| 7 | + |
| 8 | +#define ror(type, x, d) \ |
| 9 | + ((type)(((x) >> (d)) | ((x) << ((8 * sizeof(type)) - (d))))) |
| 10 | + |
| 11 | +#ifdef __clang__ |
| 12 | +void check_left8(void) |
| 13 | +{ |
| 14 | + uint8_t op; |
| 15 | + assert(__builtin_rotateleft8(op, 1) == rol(uint8_t, op, 1)); |
| 16 | + assert(__builtin_rotateleft8(op, 2) == rol(uint8_t, op, 2)); |
| 17 | + assert(__builtin_rotateleft8(op, 3) == rol(uint8_t, op, 3)); |
| 18 | + assert(__builtin_rotateleft8(op, 4) == rol(uint8_t, op, 4)); |
| 19 | +} |
| 20 | + |
| 21 | +void check_left16(void) |
| 22 | +{ |
| 23 | + uint16_t op; |
| 24 | + assert(__builtin_rotateleft16(op, 1) == rol(uint16_t, op, 1)); |
| 25 | + assert(__builtin_rotateleft16(op, 2) == rol(uint16_t, op, 2)); |
| 26 | + assert(__builtin_rotateleft16(op, 3) == rol(uint16_t, op, 3)); |
| 27 | + assert(__builtin_rotateleft16(op, 4) == rol(uint16_t, op, 4)); |
| 28 | +} |
| 29 | + |
| 30 | +void check_left32(void) |
| 31 | +{ |
| 32 | + uint32_t op; |
| 33 | + assert(__builtin_rotateleft32(op, 1) == rol(uint32_t, op, 1)); |
| 34 | + assert(__builtin_rotateleft32(op, 2) == rol(uint32_t, op, 2)); |
| 35 | + assert(__builtin_rotateleft32(op, 3) == rol(uint32_t, op, 3)); |
| 36 | + assert(__builtin_rotateleft32(op, 4) == rol(uint32_t, op, 4)); |
| 37 | +} |
| 38 | + |
| 39 | +void check_left64(void) |
| 40 | +{ |
| 41 | + uint64_t op; |
| 42 | + assert(__builtin_rotateleft64(op, 1) == rol(uint64_t, op, 1)); |
| 43 | + assert(__builtin_rotateleft64(op, 2) == rol(uint64_t, op, 2)); |
| 44 | + assert(__builtin_rotateleft64(op, 3) == rol(uint64_t, op, 3)); |
| 45 | + assert(__builtin_rotateleft64(op, 4) == rol(uint64_t, op, 4)); |
| 46 | +} |
| 47 | + |
| 48 | +void check_right8(void) |
| 49 | +{ |
| 50 | + uint8_t op; |
| 51 | + assert(__builtin_rotateright8(op, 1) == ror(uint8_t, op, 1)); |
| 52 | + assert(__builtin_rotateright8(op, 2) == ror(uint8_t, op, 2)); |
| 53 | + assert(__builtin_rotateright8(op, 3) == ror(uint8_t, op, 3)); |
| 54 | + assert(__builtin_rotateright8(op, 4) == ror(uint8_t, op, 4)); |
| 55 | +} |
| 56 | + |
| 57 | +void check_right16(void) |
| 58 | +{ |
| 59 | + uint16_t op; |
| 60 | + assert(__builtin_rotateright16(op, 1) == ror(uint16_t, op, 1)); |
| 61 | + assert(__builtin_rotateright16(op, 2) == ror(uint16_t, op, 2)); |
| 62 | + assert(__builtin_rotateright16(op, 3) == ror(uint16_t, op, 3)); |
| 63 | + assert(__builtin_rotateright16(op, 4) == ror(uint16_t, op, 4)); |
| 64 | +} |
| 65 | + |
| 66 | +void check_right32(void) |
| 67 | +{ |
| 68 | + uint32_t op; |
| 69 | + assert(__builtin_rotateright32(op, 1) == ror(uint32_t, op, 1)); |
| 70 | + assert(__builtin_rotateright32(op, 2) == ror(uint32_t, op, 2)); |
| 71 | + assert(__builtin_rotateright32(op, 3) == ror(uint32_t, op, 3)); |
| 72 | + assert(__builtin_rotateright32(op, 4) == ror(uint32_t, op, 4)); |
| 73 | +} |
| 74 | + |
| 75 | +void check_right64(void) |
| 76 | +{ |
| 77 | + uint64_t op; |
| 78 | + assert(__builtin_rotateright64(op, 1) == ror(uint64_t, op, 1)); |
| 79 | + assert(__builtin_rotateright64(op, 2) == ror(uint64_t, op, 2)); |
| 80 | + assert(__builtin_rotateright64(op, 3) == ror(uint64_t, op, 3)); |
| 81 | + assert(__builtin_rotateright64(op, 4) == ror(uint64_t, op, 4)); |
| 82 | +} |
| 83 | +#endif |
| 84 | + |
| 85 | +int main(void) |
| 86 | +{ |
| 87 | +#ifdef __clang__ |
| 88 | + check_left8(); |
| 89 | + check_left16(); |
| 90 | + check_left32(); |
| 91 | + check_left64(); |
| 92 | + check_right8(); |
| 93 | + check_right16(); |
| 94 | + check_right32(); |
| 95 | + check_right64(); |
| 96 | +#endif |
| 97 | +} |
0 commit comments