1 /* { dg-do run } */
2 /* { dg-options "-O2 -msha" } */
3 /* { dg-require-effective-target sha } */
4 
5 #include "sha-check.h"
6 #include "m128-check.h"
7 #include <x86intrin.h>
8 #include <immintrin.h>
9 
10 static int
ch(int e,int f,int g)11 ch (int e, int f, int g)
12 {
13   return (e & f) ^ (~e & g);
14 }
15 
16 static int
maj(int a,int b,int c)17 maj (int a, int b, int c)
18 {
19   return (a & b) ^ (a & c) ^ (b & c);
20 }
21 
22 static int
s0(int a)23 s0 (int a)
24 {
25   return __rord (a, 2) ^ __rord (a, 13) ^ __rord (a, 22);
26 }
27 
28 static int
s1(int e)29 s1 (int e)
30 {
31   return __rord (e, 6) ^ __rord (e, 11) ^ __rord (e, 25);
32 }
33 
34 static void
compute_sha256rnds2(int * src0,int * src1,int * src2,int * res)35 compute_sha256rnds2 (int *src0, int *src1, int *src2, int *res)
36 {
37   int wk[2] = { src0[0], src0[1] };
38   int a[3], b[3], c[3], d[3], e[3], f[3], g[3], h[3];
39 
40   a[0] = src2[3];
41   b[0] = src2[2];
42   c[0] = src1[3];
43   d[0] = src1[2];
44   e[0] = src2[1];
45   f[0] = src2[0];
46   g[0] = src1[1];
47   h[0] = src1[0];
48 
49   int i;
50   for (i = 0; i <= 1; i++)
51     {
52       a[i+1] = ch (e[i], f[i], g[i]) + s1 (e[i]) + wk[i] + h[i]
53 	       + maj (a[i], b[i], c[i]) + s0 (a[i]);
54       b[i+1] = a[i];
55       c[i+1] = b[i];
56       d[i+1] = c[i];
57       e[i+1] = ch (e[i], f[i], g[i]) + s1 (e[i]) + wk[i] + h[i] + d[i];
58       f[i+1] = e[i];
59       g[i+1] = f[i];
60       h[i+1] = g[i];
61     }
62 
63   res[0] = f[2];
64   res[1] = e[2];
65   res[2] = b[2];
66   res[3] = a[2];
67 }
68 
69 static void
sha_test(void)70 sha_test (void)
71 {
72   union128i_d s0, s1, s2, res;
73   int res_ref[4];
74 
75   s0.x = _mm_set_epi32 (0, 0, 111, 222);
76   s1.x = _mm_set_epi32 (333, 444, 555, 666);
77   s2.x = _mm_set_epi32 (777, 888, 999, 123);
78 
79   res.x = _mm_sha256rnds2_epu32 (s1.x, s2.x, s0.x);
80 
81   compute_sha256rnds2 (s0.a, s1.a, s2.a, res_ref);
82 
83   if (check_union128i_d (res, res_ref))
84     abort ();
85 }
86