| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-options "-O2 -msm3" } */ |
| 3 |
/* { dg-require-effective-target sm3 } */ |
| 4 |
|
| 5 |
#include "sm3-check.h" |
| 6 |
#include <x86intrin.h> |
| 7 |
#include <immintrin.h> |
| 8 |
|
| 9 |
static unsigned |
| 10 |
p0 (unsigned w) |
| 11 |
{ |
| 12 |
return (w ^ rol32 (w, 9) ^ rol32 (w, 17)); |
| 13 |
} |
| 14 |
|
| 15 |
static unsigned |
| 16 |
ff (unsigned x, unsigned y, unsigned z, int round) |
| 17 |
{ |
| 18 |
if (round < 16) |
| 19 |
return (x ^ y ^ z); |
| 20 |
else |
| 21 |
return ((x & y) | (x & z) | (y & z)); |
| 22 |
} |
| 23 |
|
| 24 |
static unsigned |
| 25 |
gg (unsigned x, unsigned y, unsigned z, int round) |
| 26 |
{ |
| 27 |
if (round < 16) |
| 28 |
return (x ^ y ^ z); |
| 29 |
else |
| 30 |
return ((x & y) | ((~x) & z)); |
| 31 |
} |
| 32 |
|
| 33 |
static void |
| 34 |
compute_sm3rnds2 (int *src0, int *src1, int *src2, int imm, int *res) |
| 35 |
{ |
| 36 |
unsigned s1, s2, t1, t2, co; |
| 37 |
unsigned w[6], a[3], b[3], c[3], d[3], e[3], f[3], g[3], h[3]; |
| 38 |
int round, i; |
| 39 |
|
| 40 |
a[0] = src1[3]; |
| 41 |
b[0] = src1[2]; |
| 42 |
c[0] = src0[3]; |
| 43 |
d[0] = src0[2]; |
| 44 |
e[0] = src1[1]; |
| 45 |
f[0] = src1[0]; |
| 46 |
g[0] = src0[1]; |
| 47 |
h[0] = src0[0]; |
| 48 |
w[0] = src2[0]; |
| 49 |
w[1] = src2[1]; |
| 50 |
w[4] = src2[2]; |
| 51 |
w[5] = src2[3]; |
| 52 |
|
| 53 |
c[0] = rol32 (c[0], 9); |
| 54 |
d[0] = rol32 (d[0], 9); |
| 55 |
g[0] = rol32 (g[0], 19); |
| 56 |
h[0] = rol32 (h[0], 19); |
| 57 |
|
| 58 |
round = imm & 0x3e; |
| 59 |
if (round < 16) |
| 60 |
co = 0x79cc4519; |
| 61 |
else |
| 62 |
co = 0x7a879d8a; |
| 63 |
co = rol32 (co, round); |
| 64 |
|
| 65 |
for (i = 0; i < 2; i++) |
| 66 |
{ |
| 67 |
s1 = rol32 ((rol32 (a[i], 12) + e[i] + co), 7); |
| 68 |
s2 = s1 ^ rol32 (a[i], 12); |
| 69 |
t1 = ff (a[i], b[i], c[i], round) + d[i] + s2 + (w[i] ^ w[i + 4]); |
| 70 |
t2 = gg (e[i], f[i], g[i], round) + h[i] + s1 + w[i]; |
| 71 |
d[i + 1] = c[i]; |
| 72 |
c[i + 1] = rol32 (b[i], 9); |
| 73 |
b[i + 1] = a[i]; |
| 74 |
a[i + 1] = t1; |
| 75 |
h[i + 1] = g[i]; |
| 76 |
g[i + 1] = rol32 (f[i], 19); |
| 77 |
f[i + 1] = e[i]; |
| 78 |
e[i + 1] = p0 (t2); |
| 79 |
co = rol32 (co, 1); |
| 80 |
} |
| 81 |
|
| 82 |
res[3] = a[2]; |
| 83 |
res[2] = b[2]; |
| 84 |
res[1] = e[2]; |
| 85 |
res[0] = f[2]; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
sm3_test (void) |
| 90 |
{ |
| 91 |
union128i_d s1, s2, s3, res; |
| 92 |
int res_ref[4]; |
| 93 |
|
| 94 |
s1.x = _mm_set_epi32 (111, 222, 333, 444); |
| 95 |
s2.x = _mm_set_epi32 (555, 666, 777, 888); |
| 96 |
s3.x = _mm_set_epi32 (999, 123, 456, 789); |
| 97 |
|
| 98 |
res.x = _mm_sm3rnds2_epi32 (s1.x, s2.x, s3.x, 22); |
| 99 |
|
| 100 |
compute_sm3rnds2 (s1.a, s2.a, s3.a, 22, res_ref); |
| 101 |
|
| 102 |
if (check_union128i_d (res, res_ref)) |
| 103 |
abort (); |
| 104 |
} |