summaryrefslogtreecommitdiff
path: root/cipher/cipher-gcm-intel-pclmul.c
blob: 391cbe6f2278cbae96b45560536e26c286638da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
/* cipher-gcm-intel-pclmul.c  -  Intel PCLMUL accelerated Galois Counter Mode
 *                               implementation
 * Copyright (C) 2013-2014,2019,2022 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser general Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "g10lib.h"
#include "cipher.h"
#include "bufhelp.h"
#include "./cipher-internal.h"


#ifdef GCM_USE_INTEL_PCLMUL


#if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
/* Prevent compiler from issuing SSE instructions between asm blocks. */
#  pragma GCC target("no-sse")
#endif
#if __clang__
#  pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
#endif


#define ALWAYS_INLINE inline __attribute__((always_inline))
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))

#define ASM_FUNC_ATTR        NO_INSTRUMENT_FUNCTION
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE


#define GCM_INTEL_USE_VPCLMUL_AVX2         (1 << 0)
#define GCM_INTEL_AGGR8_TABLE_INITIALIZED  (1 << 1)
#define GCM_INTEL_AGGR16_TABLE_INITIALIZED (1 << 2)
#define GCM_INTEL_USE_VPCLMUL_AVX512       (1 << 3)
#define GCM_INTEL_AGGR32_TABLE_INITIALIZED (1 << 4)


/*
 Intel PCLMUL ghash based on white paper:
  "Intel® Carry-Less Multiplication Instruction and its Usage for Computing the
   GCM Mode - Rev 2.01"; Shay Gueron, Michael E. Kounavis.
 */
static ASM_FUNC_ATTR_INLINE
void reduction(void)
{
  /* input: <xmm1:xmm3> */

  asm volatile (/* first phase of the reduction */
                "movdqa %%xmm3, %%xmm6\n\t"
                "movdqa %%xmm3, %%xmm5\n\t"
                "psllq $1, %%xmm6\n\t"  /* packed right shifting << 63 */
                "pxor %%xmm3, %%xmm6\n\t"
                "psllq $57, %%xmm5\n\t"  /* packed right shifting << 57 */
                "psllq $62, %%xmm6\n\t"  /* packed right shifting << 62 */
                "pxor %%xmm5, %%xmm6\n\t" /* xor the shifted versions */
                "pshufd $0x6a, %%xmm6, %%xmm5\n\t"
                "pshufd $0xae, %%xmm6, %%xmm6\n\t"
                "pxor %%xmm5, %%xmm3\n\t" /* first phase of the reduction
                                             complete */

                /* second phase of the reduction */
                "pxor %%xmm3, %%xmm1\n\t" /* xor the shifted versions */
                "psrlq $1, %%xmm3\n\t"    /* packed left shifting >> 1 */
                "pxor %%xmm3, %%xmm6\n\t"
                "psrlq $1, %%xmm3\n\t"    /* packed left shifting >> 2 */
                "pxor %%xmm3, %%xmm1\n\t"
                "psrlq $5, %%xmm3\n\t"    /* packed left shifting >> 7 */
                "pxor %%xmm3, %%xmm6\n\t"
                "pxor %%xmm6, %%xmm1\n\t" /* the result is in xmm1 */
                ::: "memory" );
}

static ASM_FUNC_ATTR_INLINE
void gfmul_pclmul(void)
{
  /* Input: XMM0 and XMM1, Output: XMM1. Input XMM0 stays unmodified.
     Input must be converted to little-endian.
   */
  asm volatile (/* gfmul, xmm0 has operator a and xmm1 has operator b. */
                "pshufd $78, %%xmm0, %%xmm2\n\t"
                "pshufd $78, %%xmm1, %%xmm4\n\t"
                "pxor %%xmm0, %%xmm2\n\t" /* xmm2 holds a0+a1 */
                "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds b0+b1 */

                "movdqa %%xmm0, %%xmm3\n\t"
                "pclmulqdq $0, %%xmm1, %%xmm3\n\t"  /* xmm3 holds a0*b0 */
                "pclmulqdq $17, %%xmm0, %%xmm1\n\t" /* xmm6 holds a1*b1 */
                "movdqa %%xmm3, %%xmm5\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm4\n\t"  /* xmm4 holds (a0+a1)*(b0+b1) */

                "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */
                "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
                "movdqa %%xmm4, %%xmm5\n\t"
                "psrldq $8, %%xmm4\n\t"
                "pslldq $8, %%xmm5\n\t"
                "pxor %%xmm5, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the
                                             carry-less multiplication of xmm0
                                             by xmm1 */
                ::: "memory" );

  reduction();
}

#define GFMUL_AGGR4_ASM_1(be_to_le)                                            \
    /* perform clmul and merge results... */                                   \
    "movdqu 2*16(%[h_table]), %%xmm2\n\t" /* Load H4 */                        \
    "movdqu 0*16(%[buf]), %%xmm5\n\t"                                          \
    be_to_le("pshufb %[be_mask], %%xmm5\n\t") /* be => le */                   \
    "pxor %%xmm5, %%xmm1\n\t"                                                  \
                                                                               \
    "pshufd $78, %%xmm2, %%xmm5\n\t"                                           \
    "pshufd $78, %%xmm1, %%xmm4\n\t"                                           \
    "pxor %%xmm2, %%xmm5\n\t" /* xmm5 holds 4:a0+a1 */                         \
    "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds 4:b0+b1 */                         \
    "movdqa %%xmm2, %%xmm3\n\t"                                                \
    "pclmulqdq $0, %%xmm1, %%xmm3\n\t"   /* xmm3 holds 4:a0*b0 */              \
    "pclmulqdq $17, %%xmm2, %%xmm1\n\t"  /* xmm1 holds 4:a1*b1 */              \
    "pclmulqdq $0, %%xmm5, %%xmm4\n\t"   /* xmm4 holds 4:(a0+a1)*(b0+b1) */    \
                                                                               \
    "movdqu 1*16(%[h_table]), %%xmm5\n\t" /* Load H3 */                        \
    "movdqu 1*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %[be_mask], %%xmm2\n\t") /* be => le */                   \
                                                                               \
    "pshufd $78, %%xmm5, %%xmm0\n\t"                                           \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 3:a0+a1 */                         \
    "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 3:b0+b1 */                         \
    "movdqa %%xmm5, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 3:a0*b0 */               \
    "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */               \
    "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */      \
                                                                               \
    "movdqu 2*16(%[buf]), %%xmm5\n\t"                                          \
    be_to_le("pshufb %[be_mask], %%xmm5\n\t") /* be => le */                   \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4:a0*b0 */                       \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4:a1*b1 */                       \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4:(a0+a1)*(b0+b1) */             \
                                                                               \
    "movdqu 0*16(%[h_table]), %%xmm2\n\t" /* Load H2 */                        \
                                                                               \
    "pshufd $78, %%xmm2, %%xmm0\n\t"                                           \
    "pshufd $78, %%xmm5, %%xmm7\n\t"                                           \
    "pxor %%xmm2, %%xmm0\n\t" /* xmm0 holds 2:a0+a1 */                         \
    "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 2:b0+b1 */                         \
    "movdqa %%xmm2, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 2:a0*b0 */               \
    "pclmulqdq $17, %%xmm2, %%xmm5\n\t" /* xmm5 holds 2:a1*b1 */               \
    "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 2:(a0+a1)*(b0+b1) */      \
                                                                               \
    "movdqu 3*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %[be_mask], %%xmm2\n\t") /* be => le */                   \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4:a0*b0 */                     \
    "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4:a1*b1 */                     \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4:(a0+a1)*(b0+b1) */

#define GFMUL_AGGR4_ASM_2()                                                    \
    "movdqu %[h_1], %%xmm5\n\t" /* Load H1 */                                  \
                                                                               \
    "pshufd $78, %%xmm5, %%xmm0\n\t"                                           \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 1:a0+a1 */                         \
    "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 1:b0+b1 */                         \
    "movdqa %%xmm5, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 1:a0*b0 */               \
    "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 1:a1*b1 */               \
    "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 1:(a0+a1)*(b0+b1) */      \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+4:a0*b0 */                   \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+4:a1*b1 */                   \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 1+2+3+4:(a0+a1)*(b0+b1) */         \
                                                                               \
    /* aggregated reduction... */                                              \
    "movdqa %%xmm3, %%xmm5\n\t"                                                \
    "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */                     \
    "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */     \
    "movdqa %%xmm4, %%xmm5\n\t"                                                \
    "psrldq $8, %%xmm4\n\t"                                                    \
    "pslldq $8, %%xmm5\n\t"                                                    \
    "pxor %%xmm5, %%xmm3\n\t"                                                  \
    "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the           \
                                  carry-less multiplication of xmm0            \
                                  by xmm1 */

#define be_to_le(...) __VA_ARGS__
#define le_to_le(...) /*_*/

static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr4(const void *buf, const void *h_1, const void *h_table,
		   const unsigned char *be_mask)
{
  /* Input:
      Hash: XMM1
     Output:
      Hash: XMM1
   */
  asm volatile (GFMUL_AGGR4_ASM_1(be_to_le)
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table),
                  [be_mask] "m" (*be_mask)
                : "memory" );

  asm volatile (GFMUL_AGGR4_ASM_2()
                :
                : [h_1] "m" (*(const unsigned char *)h_1)
                : "memory" );

  reduction();
}

static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr4_le(const void *buf, const void *h_1, const void *h_table)
{
  /* Input:
      Hash: XMM1
     Output:
      Hash: XMM1
   */
  asm volatile (GFMUL_AGGR4_ASM_1(le_to_le)
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table)
                : "memory" );

  asm volatile (GFMUL_AGGR4_ASM_2()
                :
                : [h_1] "m" (*(const unsigned char *)h_1)
                : "memory" );

  reduction();
}

#ifdef __x86_64__

#define GFMUL_AGGR8_ASM(be_to_le)                                              \
    /* Load H6, H7, H8. */                                                     \
    "movdqu 6*16(%[h_table]), %%xmm10\n\t"                                     \
    "movdqu 5*16(%[h_table]), %%xmm9\n\t"                                      \
    "movdqu 4*16(%[h_table]), %%xmm8\n\t"                                      \
                                                                               \
    /* perform clmul and merge results... */                                   \
    "movdqu 0*16(%[buf]), %%xmm5\n\t"                                          \
    "movdqu 1*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */                      \
    be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */                      \
    "pxor %%xmm5, %%xmm1\n\t"                                                  \
                                                                               \
    "pshufd $78, %%xmm10, %%xmm5\n\t"                                          \
    "pshufd $78, %%xmm1, %%xmm4\n\t"                                           \
    "pxor %%xmm10, %%xmm5\n\t" /* xmm5 holds 8:a0+a1 */                        \
    "pxor %%xmm1, %%xmm4\n\t"  /* xmm4 holds 8:b0+b1 */                        \
    "movdqa %%xmm10, %%xmm3\n\t"                                               \
    "pclmulqdq $0, %%xmm1, %%xmm3\n\t"   /* xmm3 holds 8:a0*b0 */              \
    "pclmulqdq $17, %%xmm10, %%xmm1\n\t" /* xmm1 holds 8:a1*b1 */              \
    "pclmulqdq $0, %%xmm5, %%xmm4\n\t"   /* xmm4 holds 8:(a0+a1)*(b0+b1) */    \
                                                                               \
    "pshufd $78, %%xmm9, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 7:a0+a1 */                       \
    "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 7:b0+b1 */                        \
    "movdqa %%xmm9, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 7:a0*b0 */               \
    "pclmulqdq $17, %%xmm9, %%xmm2\n\t" /* xmm2 holds 7:a1*b1 */               \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 7:(a0+a1)*(b0+b1) */     \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 7+8:a0*b0 */                       \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 7+8:a1*b1 */                       \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 7+8:(a0+a1)*(b0+b1) */             \
                                                                               \
    "movdqu 2*16(%[buf]), %%xmm5\n\t"                                          \
    "movdqu 3*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */                      \
    be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */                      \
                                                                               \
    "pshufd $78, %%xmm8, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm5, %%xmm7\n\t"                                           \
    "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 6:a0+a1 */                       \
    "pxor %%xmm5, %%xmm7\n\t"  /* xmm7 holds 6:b0+b1 */                        \
    "movdqa %%xmm8, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 6:a0*b0 */               \
    "pclmulqdq $17, %%xmm8, %%xmm5\n\t" /* xmm5 holds 6:a1*b1 */               \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 6:(a0+a1)*(b0+b1) */     \
                                                                               \
    /* Load H3, H4, H5. */                                                     \
    "movdqu 3*16(%[h_table]), %%xmm10\n\t"                                     \
    "movdqu 2*16(%[h_table]), %%xmm9\n\t"                                      \
    "movdqu 1*16(%[h_table]), %%xmm8\n\t"                                      \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 6+7+8:a0*b0 */                     \
    "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 6+7+8:a1*b1 */                     \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 6+7+8:(a0+a1)*(b0+b1) */           \
                                                                               \
    "pshufd $78, %%xmm10, %%xmm11\n\t"                                         \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm10, %%xmm11\n\t" /* xmm11 holds 5:a0+a1 */                      \
    "pxor %%xmm2, %%xmm7\n\t"   /* xmm7 holds 5:b0+b1 */                       \
    "movdqa %%xmm10, %%xmm6\n\t"                                               \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"   /* xmm6 holds 5:a0*b0 */              \
    "pclmulqdq $17, %%xmm10, %%xmm2\n\t" /* xmm2 holds 5:a1*b1 */              \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t"  /* xmm7 holds 5:(a0+a1)*(b0+b1) */    \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 5+6+7+8:a0*b0 */                   \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 5+6+7+8:a1*b1 */                   \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 5+6+7+8:(a0+a1)*(b0+b1) */         \
                                                                               \
    "movdqu 4*16(%[buf]), %%xmm5\n\t"                                          \
    "movdqu 5*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */                      \
    be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */                      \
                                                                               \
    "pshufd $78, %%xmm9, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm5, %%xmm7\n\t"                                           \
    "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 4:a0+a1 */                       \
    "pxor %%xmm5, %%xmm7\n\t"  /* xmm7 holds 4:b0+b1 */                        \
    "movdqa %%xmm9, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 4:a0*b0 */               \
    "pclmulqdq $17, %%xmm9, %%xmm5\n\t" /* xmm5 holds 4:a1*b1 */               \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 4:(a0+a1)*(b0+b1) */     \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 4+5+6+7+8:a0*b0 */                 \
    "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 4+5+6+7+8:a1*b1 */                 \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 4+5+6+7+8:(a0+a1)*(b0+b1) */       \
                                                                               \
    "pshufd $78, %%xmm8, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 3:a0+a1 */                       \
    "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 3:b0+b1 */                        \
    "movdqa %%xmm8, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 3:a0*b0 */               \
    "pclmulqdq $17, %%xmm8, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */               \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */     \
                                                                               \
    "movdqu 0*16(%[h_table]), %%xmm8\n\t" /* Load H2 */                        \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4+5+6+7+8:a0*b0 */               \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4+5+6+7+8:a1*b1 */               \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4+5+6+7+8:(a0+a1)*(b0+b1) */     \
                                                                               \
    "movdqu 6*16(%[buf]), %%xmm5\n\t"                                          \
    "movdqu 7*16(%[buf]), %%xmm2\n\t"                                          \
    be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */                      \
    be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */                      \
                                                                               \
    "pshufd $78, %%xmm8, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm5, %%xmm7\n\t"                                           \
    "pxor %%xmm8, %%xmm11\n\t"  /* xmm11 holds 2:a0+a1 */                      \
    "pxor %%xmm5, %%xmm7\n\t"   /* xmm7 holds 2:b0+b1 */                       \
    "movdqa %%xmm8, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm5, %%xmm6\n\t"   /* xmm6 holds 2:a0*b0 */              \
    "pclmulqdq $17, %%xmm8, %%xmm5\n\t"  /* xmm5 holds 2:a1*b1 */              \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t"  /* xmm7 holds 2:(a0+a1)*(b0+b1) */    \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4+5+6+7+8:a0*b0 */             \
    "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4+5+6+7+8:a1*b1 */             \
    "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4+5+6+7+8:(a0+a1)*(b0+b1) */   \
                                                                               \
    "pshufd $78, %%xmm0, %%xmm11\n\t"                                          \
    "pshufd $78, %%xmm2, %%xmm7\n\t"                                           \
    "pxor %%xmm0, %%xmm11\n\t" /* xmm11 holds 1:a0+a1 */                       \
    "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 1:b0+b1 */                        \
    "movdqa %%xmm0, %%xmm6\n\t"                                                \
    "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 1:a0*b0 */               \
    "pclmulqdq $17, %%xmm0, %%xmm2\n\t" /* xmm2 holds 1:a1*b1 */               \
    "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 1:(a0+a1)*(b0+b1) */     \
                                                                               \
    "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+4+5+6+7+8:a0*b0 */           \
    "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+4+5+6+7+8:a1*b1 */           \
    "pxor %%xmm7, %%xmm4\n\t"/* xmm4 holds 1+2+3+4+5+6+7+8:(a0+a1)*(b0+b1) */  \
                                                                               \
    /* aggregated reduction... */                                              \
    "movdqa %%xmm3, %%xmm5\n\t"                                                \
    "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */                     \
    "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */     \
    "movdqa %%xmm4, %%xmm5\n\t"                                                \
    "psrldq $8, %%xmm4\n\t"                                                    \
    "pslldq $8, %%xmm5\n\t"                                                    \
    "pxor %%xmm5, %%xmm3\n\t"                                                  \
    "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the           \
                                  carry-less multiplication of xmm0            \
                                  by xmm1 */

static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr8(const void *buf, const void *h_table)
{
  /* Input:
      H¹: XMM0
      bemask: XMM15
      Hash: XMM1
     Output:
      Hash: XMM1
     Inputs XMM0 and XMM15 stays unmodified.
   */
  asm volatile (GFMUL_AGGR8_ASM(be_to_le)
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table)
                : "memory" );

  reduction();
}

static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr8_le(const void *buf, const void *h_table)
{
  /* Input:
      H¹: XMM0
      Hash: XMM1
     Output:
      Hash: XMM1
     Inputs XMM0 and XMM15 stays unmodified.
   */
  asm volatile (GFMUL_AGGR8_ASM(le_to_le)
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table)
                : "memory" );

  reduction();
}

#ifdef GCM_USE_INTEL_VPCLMUL_AVX2

#define GFMUL_AGGR16_ASM_VPCMUL_AVX2(be_to_le)                                          \
    /* perform clmul and merge results... */                                            \
    "vmovdqu 0*16(%[buf]), %%ymm5\n\t"                                                  \
    "vmovdqu 2*16(%[buf]), %%ymm2\n\t"                                                  \
    be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */                      \
    be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */                      \
    "vpxor %%ymm5, %%ymm1, %%ymm1\n\t"                                                  \
                                                                                        \
    "vpshufd $78, %%ymm0, %%ymm5\n\t"                                                   \
    "vpshufd $78, %%ymm1, %%ymm4\n\t"                                                   \
    "vpxor %%ymm0, %%ymm5, %%ymm5\n\t" /* ymm5 holds 15|16:a0+a1 */                     \
    "vpxor %%ymm1, %%ymm4, %%ymm4\n\t" /* ymm4 holds 15|16:b0+b1 */                     \
    "vpclmulqdq $0, %%ymm1, %%ymm0, %%ymm3\n\t"  /* ymm3 holds 15|16:a0*b0 */           \
    "vpclmulqdq $17, %%ymm0, %%ymm1, %%ymm1\n\t" /* ymm1 holds 15|16:a1*b1 */           \
    "vpclmulqdq $0, %%ymm5, %%ymm4, %%ymm4\n\t"  /* ymm4 holds 15|16:(a0+a1)*(b0+b1) */ \
                                                                                        \
    "vmovdqu %[h1_h2], %%ymm0\n\t"                                                      \
                                                                                        \
    "vpshufd $78, %%ymm13, %%ymm14\n\t"                                                 \
    "vpshufd $78, %%ymm2, %%ymm7\n\t"                                                   \
    "vpxor %%ymm13, %%ymm14, %%ymm14\n\t" /* ymm14 holds 13|14:a0+a1 */                 \
    "vpxor %%ymm2, %%ymm7, %%ymm7\n\t"    /* ymm7 holds 13|14:b0+b1 */                  \
    "vpclmulqdq $0, %%ymm2, %%ymm13, %%ymm6\n\t"  /* ymm6 holds 13|14:a0*b0 */          \
    "vpclmulqdq $17, %%ymm13, %%ymm2, %%ymm2\n\t" /* ymm2 holds 13|14:a1*b1 */          \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t"  /* ymm7 holds 13|14:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 13+15|14+16:a0*b0 */               \
    "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 13+15|14+16:a1*b1 */               \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 13+15|14+16:(a0+a1)*(b0+b1) */     \
                                                                                        \
    "vmovdqu 4*16(%[buf]), %%ymm5\n\t"                                                  \
    "vmovdqu 6*16(%[buf]), %%ymm2\n\t"                                                  \
    be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */                      \
    be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */                      \
                                                                                        \
    "vpshufd $78, %%ymm12, %%ymm14\n\t"                                                 \
    "vpshufd $78, %%ymm5, %%ymm7\n\t"                                                   \
    "vpxor %%ymm12, %%ymm14, %%ymm14\n\t" /* ymm14 holds 11|12:a0+a1 */                 \
    "vpxor %%ymm5, %%ymm7, %%ymm7\n\t"    /* ymm7 holds 11|12:b0+b1 */                  \
    "vpclmulqdq $0, %%ymm5, %%ymm12, %%ymm6\n\t"  /* ymm6 holds 11|12:a0*b0 */          \
    "vpclmulqdq $17, %%ymm12, %%ymm5, %%ymm5\n\t" /* ymm5 holds 11|12:a1*b1 */          \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t"  /* ymm7 holds 11|12:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 11+13+15|12+14+16:a0*b0 */         \
    "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 11+13+15|12+14+16:a1*b1 */         \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 11+13+15|12+14+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vpshufd $78, %%ymm11, %%ymm14\n\t"                                                 \
    "vpshufd $78, %%ymm2, %%ymm7\n\t"                                                   \
    "vpxor %%ymm11, %%ymm14, %%ymm14\n\t" /* ymm14 holds 9|10:a0+a1 */                  \
    "vpxor %%ymm2, %%ymm7, %%ymm7\n\t"    /* ymm7 holds 9|10:b0+b1 */                   \
    "vpclmulqdq $0, %%ymm2, %%ymm11, %%ymm6\n\t"  /* ymm6 holds 9|10:a0*b0 */           \
    "vpclmulqdq $17, %%ymm11, %%ymm2, %%ymm2\n\t" /* ymm2 holds 9|10:a1*b1 */           \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 9|10:(a0+a1)*(b0+b1) */  \
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 9+11+…+15|10+12+…+16:a0*b0 */      \
    "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 9+11+…+15|10+12+…+16:a1*b1 */      \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 9+11+…+15|10+12+…+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vmovdqu 8*16(%[buf]), %%ymm5\n\t"                                                  \
    "vmovdqu 10*16(%[buf]), %%ymm2\n\t"                                                 \
    be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */                      \
    be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */                      \
                                                                                        \
    "vpshufd $78, %%ymm10, %%ymm14\n\t"                                                 \
    "vpshufd $78, %%ymm5, %%ymm7\n\t"                                                   \
    "vpxor %%ymm10, %%ymm14, %%ymm14\n\t" /* ymm14 holds 7|8:a0+a1 */                   \
    "vpxor %%ymm5, %%ymm7, %%ymm7\n\t"    /* ymm7 holds 7|8:b0+b1 */                    \
    "vpclmulqdq $0, %%ymm5, %%ymm10, %%ymm6\n\t"  /* ymm6 holds 7|8:a0*b0 */            \
    "vpclmulqdq $17, %%ymm10, %%ymm5, %%ymm5\n\t" /* ymm5 holds 7|8:a1*b1 */            \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 7|8:(a0+a1)*(b0+b1) */   \
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 7+9+…+15|8+10+…+16:a0*b0 */        \
    "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 7+9+…+15|8+10+…+16:a1*b1 */        \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 7+9+…+15|8+10+…+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vpshufd $78, %%ymm9, %%ymm14\n\t"                                                  \
    "vpshufd $78, %%ymm2, %%ymm7\n\t"                                                   \
    "vpxor %%ymm9, %%ymm14, %%ymm14\n\t" /* ymm14 holds 5|6:a0+a1 */                    \
    "vpxor %%ymm2, %%ymm7, %%ymm7\n\t"   /* ymm7 holds 5|6:b0+b1 */                     \
    "vpclmulqdq $0, %%ymm2, %%ymm9, %%ymm6\n\t"  /* ymm6 holds 5|6:a0*b0 */             \
    "vpclmulqdq $17, %%ymm9, %%ymm2, %%ymm2\n\t" /* ymm2 holds 5|6:a1*b1 */             \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 5|6:(a0+a1)*(b0+b1) */   \
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 5+7+…+15|6+8+…+16:a0*b0 */         \
    "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 5+7+…+15|6+8+…+16:a1*b1 */         \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 5+7+…+15|6+8+…+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vmovdqu 12*16(%[buf]), %%ymm5\n\t"                                                 \
    "vmovdqu 14*16(%[buf]), %%ymm2\n\t"                                                 \
    be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */                      \
    be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */                      \
                                                                                        \
    "vpshufd $78, %%ymm8, %%ymm14\n\t"                                                  \
    "vpshufd $78, %%ymm5, %%ymm7\n\t"                                                   \
    "vpxor %%ymm8, %%ymm14, %%ymm14\n\t" /* ymm14 holds 3|4:a0+a1 */                    \
    "vpxor %%ymm5, %%ymm7, %%ymm7\n\t"   /* ymm7 holds 3|4:b0+b1 */                     \
    "vpclmulqdq $0, %%ymm5, %%ymm8, %%ymm6\n\t"  /* ymm6 holds 3|4:a0*b0 */             \
    "vpclmulqdq $17, %%ymm8, %%ymm5, %%ymm5\n\t" /* ymm5 holds 3|4:a1*b1 */             \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 3|4:(a0+a1)*(b0+b1) */   \
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 3+5+…+15|4+6+…+16:a0*b0 */         \
    "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 3+5+…+15|4+6+…+16:a1*b1 */         \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 3+5+…+15|4+6+…+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    "vpshufd $78, %%ymm0, %%ymm14\n\t"                                                  \
    "vpshufd $78, %%ymm2, %%ymm7\n\t"                                                   \
    "vpxor %%ymm0, %%ymm14, %%ymm14\n\t" /* ymm14 holds 1|2:a0+a1 */                    \
    "vpxor %%ymm2, %%ymm7, %%ymm7\n\t"   /* ymm7 holds 1|2:b0+b1 */                     \
    "vpclmulqdq $0, %%ymm2, %%ymm0, %%ymm6\n\t"  /* ymm6 holds 1|2:a0*b0 */             \
    "vpclmulqdq $17, %%ymm0, %%ymm2, %%ymm2\n\t" /* ymm2 holds 1|2:a1*b1 */             \
    "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 1|2:(a0+a1)*(b0+b1) */   \
                                                                                        \
    "vmovdqu %[h15_h16], %%ymm0\n\t"                                                    \
                                                                                        \
    "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 1+3+…+15|2+4+…+16:a0*b0 */         \
    "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 1+3+…+15|2+4+…+16:a1*b1 */         \
    "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 1+3+…+15|2+4+…+16:(a0+a1)*(b0+b1) */\
                                                                                        \
    /* aggregated reduction... */                                                       \
    "vpxor %%ymm1, %%ymm3, %%ymm5\n\t" /* ymm5 holds a0*b0+a1*b1 */                     \
    "vpxor %%ymm5, %%ymm4, %%ymm4\n\t" /* ymm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */     \
    "vpslldq $8, %%ymm4, %%ymm5\n\t"                                                    \
    "vpsrldq $8, %%ymm4, %%ymm4\n\t"                                                    \
    "vpxor %%ymm5, %%ymm3, %%ymm3\n\t"                                                  \
    "vpxor %%ymm4, %%ymm1, %%ymm1\n\t" /* <ymm1:xmm3> holds the result of the           \
                                          carry-less multiplication of ymm0             \
                                          by ymm1 */                                    \
                                                                                        \
    /* first phase of the reduction */                                                  \
    "vpsllq $1, %%ymm3, %%ymm6\n\t"  /* packed right shifting << 63 */                  \
    "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"                                                  \
    "vpsllq $57, %%ymm3, %%ymm5\n\t"  /* packed right shifting << 57 */                 \
    "vpsllq $62, %%ymm6, %%ymm6\n\t"  /* packed right shifting << 62 */                 \
    "vpxor %%ymm5, %%ymm6, %%ymm6\n\t" /* xor the shifted versions */                   \
    "vpshufd $0x6a, %%ymm6, %%ymm5\n\t"                                                 \
    "vpshufd $0xae, %%ymm6, %%ymm6\n\t"                                                 \
    "vpxor %%ymm5, %%ymm3, %%ymm3\n\t" /* first phase of the reduction complete */      \
                                                                                        \
    /* second phase of the reduction */                                                 \
    "vpxor %%ymm3, %%ymm1, %%ymm1\n\t" /* xor the shifted versions */                   \
    "vpsrlq $1, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 1 */                  \
    "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"                                                  \
    "vpsrlq $1, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 2 */                  \
    "vpxor %%ymm3, %%ymm1, %%ymm1\n\t"                                                  \
    "vpsrlq $5, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 7 */                  \
    "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"                                                  \
    "vpxor %%ymm6, %%ymm1, %%ymm1\n\t" /* the result is in ymm1 */                      \
                                                                                        \
    /* merge 128-bit halves */                                                          \
    "vextracti128 $1, %%ymm1, %%xmm2\n\t"                                               \
    "vpxor %%xmm2, %%xmm1, %%xmm1\n\t"

static ASM_FUNC_ATTR_INLINE void
gfmul_vpclmul_avx2_aggr16(const void *buf, const void *h_table,
			  const u64 *h1_h2_h15_h16)
{
  /* Input:
      Hx: YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13
      bemask: YMM15
      Hash: XMM1
    Output:
      Hash: XMM1
    Inputs YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13 and YMM15 stay
    unmodified.
  */
  asm volatile (GFMUL_AGGR16_ASM_VPCMUL_AVX2(be_to_le)
		:
		: [buf] "r" (buf),
		  [h_table] "r" (h_table),
		  [h1_h2] "m" (h1_h2_h15_h16[0]),
		  [h15_h16] "m" (h1_h2_h15_h16[4])
		: "memory" );
}

static ASM_FUNC_ATTR_INLINE void
gfmul_vpclmul_avx2_aggr16_le(const void *buf, const void *h_table,
			     const u64 *h1_h2_h15_h16)
{
  /* Input:
      Hx: YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13
      bemask: YMM15
      Hash: XMM1
    Output:
      Hash: XMM1
    Inputs YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13 and YMM15 stay
    unmodified.
  */
  asm volatile (GFMUL_AGGR16_ASM_VPCMUL_AVX2(le_to_le)
		:
		: [buf] "r" (buf),
		  [h_table] "r" (h_table),
		  [h1_h2] "m" (h1_h2_h15_h16[0]),
		  [h15_h16] "m" (h1_h2_h15_h16[4])
		: "memory" );
}

static ASM_FUNC_ATTR_INLINE
void gfmul_pclmul_avx2(void)
{
  /* Input: YMM0 and YMM1, Output: YMM1. Input YMM0 stays unmodified.
     Input must be converted to little-endian.
   */
  asm volatile (/* gfmul, ymm0 has operator a and ymm1 has operator b. */
		"vpshufd $78, %%ymm0, %%ymm2\n\t"
		"vpshufd $78, %%ymm1, %%ymm4\n\t"
		"vpxor %%ymm0, %%ymm2, %%ymm2\n\t" /* ymm2 holds a0+a1 */
		"vpxor %%ymm1, %%ymm4, %%ymm4\n\t" /* ymm4 holds b0+b1 */

		"vpclmulqdq $0, %%ymm1, %%ymm0, %%ymm3\n\t"  /* ymm3 holds a0*b0 */
		"vpclmulqdq $17, %%ymm0, %%ymm1, %%ymm1\n\t" /* ymm6 holds a1*b1 */
		"vpclmulqdq $0, %%ymm2, %%ymm4, %%ymm4\n\t"  /* ymm4 holds (a0+a1)*(b0+b1) */

		"vpxor %%ymm1, %%ymm3, %%ymm5\n\t" /* ymm5 holds a0*b0+a1*b1 */
		"vpxor %%ymm5, %%ymm4, %%ymm4\n\t" /* ymm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
		"vpslldq $8, %%ymm4, %%ymm5\n\t"
		"vpsrldq $8, %%ymm4, %%ymm4\n\t"
		"vpxor %%ymm5, %%ymm3, %%ymm3\n\t"
		"vpxor %%ymm4, %%ymm1, %%ymm1\n\t" /* <ymm1:ymm3> holds the result of the
						      carry-less multiplication of ymm0
						      by ymm1 */

		/* first phase of the reduction */
		"vpsllq $1, %%ymm3, %%ymm6\n\t"  /* packed right shifting << 63 */
		"vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
		"vpsllq $57, %%ymm3, %%ymm5\n\t"  /* packed right shifting << 57 */
		"vpsllq $62, %%ymm6, %%ymm6\n\t"  /* packed right shifting << 62 */
		"vpxor %%ymm5, %%ymm6, %%ymm6\n\t" /* xor the shifted versions */
		"vpshufd $0x6a, %%ymm6, %%ymm5\n\t"
		"vpshufd $0xae, %%ymm6, %%ymm6\n\t"
		"vpxor %%ymm5, %%ymm3, %%ymm3\n\t" /* first phase of the reduction complete */

		/* second phase of the reduction */
		"vpxor %%ymm3, %%ymm1, %%ymm1\n\t" /* xor the shifted versions */
		"vpsrlq $1, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 1 */
		"vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
		"vpsrlq $1, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 2 */
		"vpxor %%ymm3, %%ymm1, %%ymm1\n\t"
		"vpsrlq $5, %%ymm3, %%ymm3\n\t"    /* packed left shifting >> 7 */
		"vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
		"vpxor %%ymm6, %%ymm1, %%ymm1\n\t" /* the result is in ymm1 */
                ::: "memory" );
}

static ASM_FUNC_ATTR_INLINE void
gcm_lsh_avx2(void *h, unsigned int hoffs)
{
  static const u64 pconst[4] __attribute__ ((aligned (32))) =
    {
      U64_C(0x0000000000000001), U64_C(0xc200000000000000),
      U64_C(0x0000000000000001), U64_C(0xc200000000000000)
    };

  asm volatile ("vmovdqu %[h], %%ymm2\n\t"
                "vpshufd $0xff, %%ymm2, %%ymm3\n\t"
                "vpsrad $31, %%ymm3, %%ymm3\n\t"
                "vpslldq $8, %%ymm2, %%ymm4\n\t"
                "vpand %[pconst], %%ymm3, %%ymm3\n\t"
                "vpaddq %%ymm2, %%ymm2, %%ymm2\n\t"
                "vpsrlq $63, %%ymm4, %%ymm4\n\t"
                "vpxor %%ymm3, %%ymm2, %%ymm2\n\t"
                "vpxor %%ymm4, %%ymm2, %%ymm2\n\t"
                "vmovdqu %%ymm2, %[h]\n\t"
                : [h] "+m" (*((byte *)h + hoffs))
                : [pconst] "m" (*pconst)
                : "memory" );
}

static ASM_FUNC_ATTR_INLINE void
load_h1h2_to_ymm1(gcry_cipher_hd_t c)
{
  unsigned int key_pos =
    offsetof(struct gcry_cipher_handle, u_mode.gcm.u_ghash_key.key);
  unsigned int table_pos =
    offsetof(struct gcry_cipher_handle, u_mode.gcm.gcm_table);

  if (key_pos + 16 == table_pos)
    {
      /* Optimization: Table follows immediately after key. */
      asm volatile ("vmovdqu %[key], %%ymm1\n\t"
		    :
		    : [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory");
    }
  else
    {
      asm volatile ("vmovdqa %[key], %%xmm1\n\t"
		    "vinserti128 $1, 0*16(%[h_table]), %%ymm1, %%ymm1\n\t"
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table),
		      [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory");
    }
}

static ASM_FUNC_ATTR void
ghash_setup_aggr8_avx2(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;

  asm volatile (/* load H⁴ */
		"vbroadcasti128 3*16(%[h_table]), %%ymm0\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");
  /* load H <<< 1, H² <<< 1 */
  load_h1h2_to_ymm1 (c);

  gfmul_pclmul_avx2 (); /* H<<<1•H⁴ => H⁵, H²<<<1•H⁴ => H⁶ */

  asm volatile ("vmovdqu %%ymm1, 3*16(%[h_table])\n\t"
		/* load H³ <<< 1, H⁴ <<< 1 */
		"vmovdqu 1*16(%[h_table]), %%ymm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx2 (); /* H³<<<1•H⁴ => H⁷, H⁴<<<1•H⁴ => H⁸ */

  asm volatile ("vmovdqu %%ymm1, 6*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
		"vmovdqu %%ymm1, 5*16(%[h_table])\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1, H⁶ <<< 1 */
  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 5 * 16); /* H⁷ <<< 1, H⁸ <<< 1 */
}

static ASM_FUNC_ATTR void
ghash_setup_aggr16_avx2(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR16_TABLE_INITIALIZED;

  asm volatile (/* load H⁸ */
		"vbroadcasti128 7*16(%[h_table]), %%ymm0\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");
  /* load H <<< 1, H² <<< 1 */
  load_h1h2_to_ymm1 (c);

  gfmul_pclmul_avx2 (); /* H<<<1•H⁸ => H⁹, H²<<<1•H⁸ => H¹⁰ */

  asm volatile ("vmovdqu %%ymm1, 7*16(%[h_table])\n\t"
		/* load H³ <<< 1, H⁴ <<< 1 */
		"vmovdqu 1*16(%[h_table]), %%ymm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx2 (); /* H³<<<1•H⁸ => H¹¹, H⁴<<<1•H⁸ => H¹² */

  asm volatile ("vmovdqu %%ymm1, 9*16(%[h_table])\n\t"
		/* load H⁵ <<< 1, H⁶ <<< 1 */
		"vmovdqu 3*16(%[h_table]), %%ymm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx2 (); /* H⁵<<<1•H⁸ => H¹³, H⁶<<<1•H⁸ => H¹⁴ */

  asm volatile ("vmovdqu %%ymm1, 11*16(%[h_table])\n\t"
		/* load H⁷ <<< 1, H⁸ <<< 1 */
		"vmovdqu 5*16(%[h_table]), %%ymm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx2 (); /* H⁷<<<1•H⁸ => H¹⁵, H⁸<<<1•H⁸ => H¹⁶ */

  asm volatile ("vmovdqu %%ymm1, 14*16(%[h_table])\n\t" /* store H¹⁶ for aggr32 setup */
                "vmovdqu %%ymm1, 13*16(%[h_table])\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 7 * 16); /* H⁹ <<< 1, H¹⁰ <<< 1 */
  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 9 * 16); /* H¹¹ <<< 1, H¹² <<< 1 */
  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 11 * 16); /* H¹³ <<< 1, H¹⁴ <<< 1 */
  gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 13 * 16); /* H¹⁵ <<< 1, H¹⁶ <<< 1 */
}

#endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */

#ifdef GCM_USE_INTEL_VPCLMUL_AVX512

#define GFMUL_AGGR32_ASM_VPCMUL_AVX512(be_to_le)                                          \
    /* perform clmul and merge results... */                                              \
    "vmovdqu64 0*16(%[buf]), %%zmm5\n\t"                                                  \
    "vmovdqu64 4*16(%[buf]), %%zmm2\n\t"                                                  \
    be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */                        \
    be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */                        \
    "vpxorq %%zmm5, %%zmm1, %%zmm1\n\t"                                                   \
                                                                                          \
    "vpshufd $78, %%zmm0, %%zmm5\n\t"                                                     \
    "vpshufd $78, %%zmm1, %%zmm4\n\t"                                                     \
    "vpxorq %%zmm0, %%zmm5, %%zmm5\n\t" /* zmm5 holds 29|…|32:a0+a1 */                    \
    "vpxorq %%zmm1, %%zmm4, %%zmm4\n\t" /* zmm4 holds 29|…|32:b0+b1 */                    \
    "vpclmulqdq $0, %%zmm1, %%zmm0, %%zmm3\n\t"  /* zmm3 holds 29|…|32:a0*b0 */           \
    "vpclmulqdq $17, %%zmm0, %%zmm1, %%zmm1\n\t" /* zmm1 holds 29|…|32:a1*b1 */           \
    "vpclmulqdq $0, %%zmm5, %%zmm4, %%zmm4\n\t"  /* zmm4 holds 29|…|32:(a0+a1)*(b0+b1) */ \
                                                                                          \
    "vpshufd $78, %%zmm13, %%zmm14\n\t"                                                   \
    "vpshufd $78, %%zmm2, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm13, %%zmm14, %%zmm14\n\t" /* zmm14 holds 25|…|28:a0+a1 */                \
    "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t"    /* zmm7 holds 25|…|28:b0+b1 */                 \
    "vpclmulqdq $0, %%zmm2, %%zmm13, %%zmm17\n\t"  /* zmm17 holds 25|…|28:a0*b0 */        \
    "vpclmulqdq $17, %%zmm13, %%zmm2, %%zmm18\n\t" /* zmm18 holds 25|…|28:a1*b1 */        \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t"  /* zmm19 holds 25|…|28:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vmovdqu64 8*16(%[buf]), %%zmm5\n\t"                                                  \
    "vmovdqu64 12*16(%[buf]), %%zmm2\n\t"                                                 \
    be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */                        \
    be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */                        \
                                                                                          \
    "vpshufd $78, %%zmm12, %%zmm14\n\t"                                                   \
    "vpshufd $78, %%zmm5, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm12, %%zmm14, %%zmm14\n\t" /* zmm14 holds 21|…|24:a0+a1 */                \
    "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t"    /* zmm7 holds 21|…|24:b0+b1 */                 \
    "vpclmulqdq $0, %%zmm5, %%zmm12, %%zmm6\n\t"  /* zmm6 holds 21|…|24:a0*b0 */          \
    "vpclmulqdq $17, %%zmm12, %%zmm5, %%zmm5\n\t" /* zmm5 holds 21|…|24:a1*b1 */          \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t"  /* zmm7 holds 21|…|24:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 21+…|…|…+32:a0*b0 */    \
    "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 21+…|…|…+32:a1*b1 */    \
    "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 21+…|…|…+32:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vpshufd $78, %%zmm11, %%zmm14\n\t"                                                   \
    "vpshufd $78, %%zmm2, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm11, %%zmm14, %%zmm14\n\t" /* zmm14 holds 17|…|20:a0+a1 */                \
    "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t"    /* zmm7 holds 17|…|20:b0+b1 */                 \
    "vpclmulqdq $0, %%zmm2, %%zmm11, %%zmm17\n\t"  /* zmm17 holds 17|…|20:a0*b0 */        \
    "vpclmulqdq $17, %%zmm11, %%zmm2, %%zmm18\n\t" /* zmm18 holds 17|…|20:a1*b1 */        \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t" /* zmm19 holds 17|…|20:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vmovdqu64 16*16(%[buf]), %%zmm5\n\t"                                                 \
    "vmovdqu64 20*16(%[buf]), %%zmm2\n\t"                                                 \
    be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */                        \
    be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */                        \
                                                                                          \
    "vpshufd $78, %%zmm10, %%zmm14\n\t"                                                   \
    "vpshufd $78, %%zmm5, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm10, %%zmm14, %%zmm14\n\t" /* zmm14 holds 13|…|16:a0+a1 */                \
    "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t"    /* zmm7 holds 13|…|16:b0+b1 */                 \
    "vpclmulqdq $0, %%zmm5, %%zmm10, %%zmm6\n\t"  /* zmm6 holds 13|…|16:a0*b0 */          \
    "vpclmulqdq $17, %%zmm10, %%zmm5, %%zmm5\n\t" /* zmm5 holds 13|…|16:a1*b1 */          \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 13|…|16:(a0+a1)*(b0+b1) */ \
                                                                                          \
    "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 13+…|…|…+32:a0*b0 */    \
    "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 13+…|…|…+32:a1*b1 */    \
    "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 13+…|…|…+32:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vpshufd $78, %%zmm9, %%zmm14\n\t"                                                    \
    "vpshufd $78, %%zmm2, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm9, %%zmm14, %%zmm14\n\t" /* zmm14 holds 9|…|12:a0+a1 */                  \
    "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t"   /* zmm7 holds 9|…|12:b0+b1 */                   \
    "vpclmulqdq $0, %%zmm2, %%zmm9, %%zmm17\n\t"  /* zmm17 holds 9|…|12:a0*b0 */          \
    "vpclmulqdq $17, %%zmm9, %%zmm2, %%zmm18\n\t" /* zmm18 holds 9|…|12:a1*b1 */          \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t" /* zmm19 holds 9|…|12:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vmovdqu64 24*16(%[buf]), %%zmm5\n\t"                                                 \
    "vmovdqu64 28*16(%[buf]), %%zmm2\n\t"                                                 \
    be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */                        \
    be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */                        \
                                                                                          \
    "vpshufd $78, %%zmm8, %%zmm14\n\t"                                                    \
    "vpshufd $78, %%zmm5, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm8, %%zmm14, %%zmm14\n\t" /* zmm14 holds 5|…|8:a0+a1 */                   \
    "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t"   /* zmm7 holds 5|…|8:b0+b1 */                    \
    "vpclmulqdq $0, %%zmm5, %%zmm8, %%zmm6\n\t"  /* zmm6 holds 5|…|8:a0*b0 */             \
    "vpclmulqdq $17, %%zmm8, %%zmm5, %%zmm5\n\t" /* zmm5 holds 5|…|8:a1*b1 */             \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 5|…|8:(a0+a1)*(b0+b1) */   \
                                                                                          \
    "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 5+…|…|…+32:a0*b0 */     \
    "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 5+…|…|…+32:a1*b1 */     \
    "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 5+…|…|…+32:(a0+a1)*(b0+b1) */\
                                                                                          \
    "vpshufd $78, %%zmm16, %%zmm14\n\t"                                                   \
    "vpshufd $78, %%zmm2, %%zmm7\n\t"                                                     \
    "vpxorq %%zmm16, %%zmm14, %%zmm14\n\t" /* zmm14 holds 1|…|4:a0+a1 */                  \
    "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t"   /* zmm7 holds 1|2:b0+b1 */                      \
    "vpclmulqdq $0, %%zmm2, %%zmm16, %%zmm6\n\t"  /* zmm6 holds 1|2:a0*b0 */              \
    "vpclmulqdq $17, %%zmm16, %%zmm2, %%zmm2\n\t" /* zmm2 holds 1|2:a1*b1 */              \
    "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 1|2:(a0+a1)*(b0+b1) */     \
                                                                                          \
    "vpxorq %%zmm6, %%zmm3, %%zmm3\n\t" /* zmm3 holds 1+3+…+15|2+4+…+16:a0*b0 */          \
    "vpxorq %%zmm2, %%zmm1, %%zmm1\n\t" /* zmm1 holds 1+3+…+15|2+4+…+16:a1*b1 */          \
    "vpxorq %%zmm7, %%zmm4, %%zmm4\n\t" /* zmm4 holds 1+3+…+15|2+4+…+16:(a0+a1)*(b0+b1) */\
                                                                                          \
    /* aggregated reduction... */                                                         \
    "vpternlogq $0x96, %%zmm1, %%zmm3, %%zmm4\n\t" /* zmm4 holds                          \
                                                    * a0*b0+a1*b1+(a0+a1)*(b0+b1) */      \
    "vpslldq $8, %%zmm4, %%zmm5\n\t"                                                      \
    "vpsrldq $8, %%zmm4, %%zmm4\n\t"                                                      \
    "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t"                                                   \
    "vpxorq %%zmm4, %%zmm1, %%zmm1\n\t" /* <zmm1:zmm3> holds the result of the            \
                                          carry-less multiplication of zmm0               \
                                          by zmm1 */                                      \
                                                                                          \
    /* first phase of the reduction */                                                    \
    "vpsllq $1, %%zmm3, %%zmm6\n\t"  /* packed right shifting << 63 */                    \
    "vpxorq %%zmm3, %%zmm6, %%zmm6\n\t"                                                   \
    "vpsllq $57, %%zmm3, %%zmm5\n\t"  /* packed right shifting << 57 */                   \
    "vpsllq $62, %%zmm6, %%zmm6\n\t"  /* packed right shifting << 62 */                   \
    "vpxorq %%zmm5, %%zmm6, %%zmm6\n\t" /* xor the shifted versions */                    \
    "vpshufd $0x6a, %%zmm6, %%zmm5\n\t"                                                   \
    "vpshufd $0xae, %%zmm6, %%zmm6\n\t"                                                   \
    "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t" /* first phase of the reduction complete */       \
                                                                                          \
    /* second phase of the reduction */                                                   \
    "vpsrlq $1, %%zmm3, %%zmm2\n\t"    /* packed left shifting >> 1 */                    \
    "vpsrlq $2, %%zmm3, %%zmm4\n\t"    /* packed left shifting >> 2 */                    \
    "vpsrlq $7, %%zmm3, %%zmm5\n\t"    /* packed left shifting >> 7 */                    \
    "vpternlogq $0x96, %%zmm3, %%zmm2, %%zmm1\n\t" /* xor the shifted versions */         \
    "vpternlogq $0x96, %%zmm4, %%zmm5, %%zmm6\n\t"                                        \
    "vpxorq %%zmm6, %%zmm1, %%zmm1\n\t" /* the result is in zmm1 */                       \
                                                                                          \
    /* merge 256-bit halves */                                                            \
    "vextracti64x4 $1, %%zmm1, %%ymm2\n\t"                                                \
    "vpxor %%ymm2, %%ymm1, %%ymm1\n\t"                                                    \
    /* merge 128-bit halves */                                                            \
    "vextracti128 $1, %%ymm1, %%xmm2\n\t"                                                 \
    "vpxor %%xmm2, %%xmm1, %%xmm1\n\t"

static ASM_FUNC_ATTR_INLINE void
gfmul_vpclmul_avx512_aggr32(const void *buf, const void *h_table)
{
  /* Input:
      Hx: ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16
      bemask: ZMM15
      Hash: XMM1
    Output:
      Hash: XMM1
    Inputs ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16 and YMM15 stay
    unmodified.
  */
  asm volatile (GFMUL_AGGR32_ASM_VPCMUL_AVX512(be_to_le)
		:
		: [buf] "r" (buf),
		  [h_table] "r" (h_table)
		: "memory" );
}

static ASM_FUNC_ATTR_INLINE void
gfmul_vpclmul_avx512_aggr32_le(const void *buf, const void *h_table)
{
  /* Input:
      Hx: ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16
      bemask: ZMM15
      Hash: XMM1
    Output:
      Hash: XMM1
    Inputs ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16 and YMM15 stay
    unmodified.
  */
  asm volatile (GFMUL_AGGR32_ASM_VPCMUL_AVX512(le_to_le)
		:
		: [buf] "r" (buf),
		  [h_table] "r" (h_table)
		: "memory" );
}

static ASM_FUNC_ATTR_INLINE
void gfmul_pclmul_avx512(void)
{
  /* Input: ZMM0 and ZMM1, Output: ZMM1. Input ZMM0 stays unmodified.
     Input must be converted to little-endian.
   */
  asm volatile (/* gfmul, zmm0 has operator a and zmm1 has operator b. */
		"vpshufd $78, %%zmm0, %%zmm2\n\t"
		"vpshufd $78, %%zmm1, %%zmm4\n\t"
		"vpxorq %%zmm0, %%zmm2, %%zmm2\n\t" /* zmm2 holds a0+a1 */
		"vpxorq %%zmm1, %%zmm4, %%zmm4\n\t" /* zmm4 holds b0+b1 */

		"vpclmulqdq $0, %%zmm1, %%zmm0, %%zmm3\n\t"  /* zmm3 holds a0*b0 */
		"vpclmulqdq $17, %%zmm0, %%zmm1, %%zmm1\n\t" /* zmm6 holds a1*b1 */
		"vpclmulqdq $0, %%zmm2, %%zmm4, %%zmm4\n\t"  /* zmm4 holds (a0+a1)*(b0+b1) */

		"vpternlogq $0x96, %%zmm1, %%zmm3, %%zmm4\n\t" /* zmm4 holds
								* a0*b0+a1*b1+(a0+a1)*(b0+b1) */
		"vpslldq $8, %%zmm4, %%zmm5\n\t"
		"vpsrldq $8, %%zmm4, %%zmm4\n\t"
		"vpxorq %%zmm5, %%zmm3, %%zmm3\n\t"
		"vpxorq %%zmm4, %%zmm1, %%zmm1\n\t" /* <zmm1:zmm3> holds the result of the
						      carry-less multiplication of zmm0
						      by zmm1 */

		/* first phase of the reduction */
		"vpsllq $1, %%zmm3, %%zmm6\n\t"  /* packed right shifting << 63 */
		"vpxorq %%zmm3, %%zmm6, %%zmm6\n\t"
		"vpsllq $57, %%zmm3, %%zmm5\n\t"  /* packed right shifting << 57 */
		"vpsllq $62, %%zmm6, %%zmm6\n\t"  /* packed right shifting << 62 */
		"vpxorq %%zmm5, %%zmm6, %%zmm6\n\t" /* xor the shifted versions */
		"vpshufd $0x6a, %%zmm6, %%zmm5\n\t"
		"vpshufd $0xae, %%zmm6, %%zmm6\n\t"
		"vpxorq %%zmm5, %%zmm3, %%zmm3\n\t" /* first phase of the reduction complete */

		/* second phase of the reduction */
		"vpsrlq $1, %%zmm3, %%zmm2\n\t"    /* packed left shifting >> 1 */
		"vpsrlq $2, %%zmm3, %%zmm4\n\t"    /* packed left shifting >> 2 */
		"vpsrlq $7, %%zmm3, %%zmm5\n\t"    /* packed left shifting >> 7 */
		"vpternlogq $0x96, %%zmm3, %%zmm2, %%zmm1\n\t" /* xor the shifted versions */
		"vpternlogq $0x96, %%zmm4, %%zmm5, %%zmm6\n\t"
		"vpxorq %%zmm6, %%zmm1, %%zmm1\n\t" /* the result is in zmm1 */
                ::: "memory" );
}

static ASM_FUNC_ATTR_INLINE void
gcm_lsh_avx512(void *h, unsigned int hoffs)
{
  static const u64 pconst[8] __attribute__ ((aligned (64))) =
    {
      U64_C(0x0000000000000001), U64_C(0xc200000000000000),
      U64_C(0x0000000000000001), U64_C(0xc200000000000000),
      U64_C(0x0000000000000001), U64_C(0xc200000000000000),
      U64_C(0x0000000000000001), U64_C(0xc200000000000000)
    };

  asm volatile ("vmovdqu64 %[h], %%zmm2\n\t"
                "vpshufd $0xff, %%zmm2, %%zmm3\n\t"
                "vpsrad $31, %%zmm3, %%zmm3\n\t"
                "vpslldq $8, %%zmm2, %%zmm4\n\t"
                "vpandq %[pconst], %%zmm3, %%zmm3\n\t"
                "vpaddq %%zmm2, %%zmm2, %%zmm2\n\t"
                "vpsrlq $63, %%zmm4, %%zmm4\n\t"
                "vpternlogq $0x96, %%zmm4, %%zmm3, %%zmm2\n\t"
                "vmovdqu64 %%zmm2, %[h]\n\t"
                : [h] "+m" (*((byte *)h + hoffs))
                : [pconst] "m" (*pconst)
                : "memory" );
}

static ASM_FUNC_ATTR_INLINE void
load_h1h4_to_zmm1(gcry_cipher_hd_t c)
{
  unsigned int key_pos =
    offsetof(struct gcry_cipher_handle, u_mode.gcm.u_ghash_key.key);
  unsigned int table_pos =
    offsetof(struct gcry_cipher_handle, u_mode.gcm.gcm_table);

  if (key_pos + 16 == table_pos)
    {
      /* Optimization: Table follows immediately after key. */
      asm volatile ("vmovdqu64 %[key], %%zmm1\n\t"
		    :
		    : [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory");
    }
  else
    {
      asm volatile ("vmovdqu64 -1*16(%[h_table]), %%zmm1\n\t"
		    "vinserti64x2 $0, %[key], %%zmm1, %%zmm1\n\t"
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table),
		      [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory");
    }
}

static ASM_FUNC_ATTR void
ghash_setup_aggr8_avx512(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;

  asm volatile (/* load H⁴ */
		"vbroadcasti64x2 3*16(%[h_table]), %%zmm0\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");
  /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁴ <<< 1 */
  load_h1h4_to_zmm1 (c);

  gfmul_pclmul_avx512 (); /* H<<<1•H⁴ => H⁵, …, H⁴<<<1•H⁴ => H⁸ */

  asm volatile ("vmovdqu64 %%zmm1, 4*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
		"vmovdqu64 %%zmm1, 3*16(%[h_table])\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1, …, H⁸ <<< 1 */
}

static ASM_FUNC_ATTR void
ghash_setup_aggr16_avx512(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR16_TABLE_INITIALIZED;

  asm volatile (/* load H⁸ */
		"vbroadcasti64x2 7*16(%[h_table]), %%zmm0\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");
  /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁴ <<< 1 */
  load_h1h4_to_zmm1 (c);

  gfmul_pclmul_avx512 (); /* H<<<1•H⁸ => H⁹, … , H⁴<<<1•H⁸ => H¹² */

  asm volatile ("vmovdqu64 %%zmm1, 7*16(%[h_table])\n\t"
		/* load H⁵ <<< 1, …, H⁸ <<< 1 */
		"vmovdqu64 3*16(%[h_table]), %%zmm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx512 (); /* H⁵<<<1•H⁸ => H¹¹, … , H⁸<<<1•H⁸ => H¹⁶ */

  asm volatile ("vmovdqu64 %%zmm1, 12*16(%[h_table])\n\t" /* store H¹⁶ for aggr32 setup */
                "vmovdqu64 %%zmm1, 11*16(%[h_table])\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 7 * 16); /* H⁹ <<< 1, …, H¹² <<< 1 */
  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 11 * 16); /* H¹³ <<< 1, …, H¹⁶ <<< 1 */
}

static ASM_FUNC_ATTR void
ghash_setup_aggr32_avx512(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR32_TABLE_INITIALIZED;

  asm volatile (/* load H¹⁶ */
		"vbroadcasti64x2 15*16(%[h_table]), %%zmm0\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");
  /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁴ <<< 1 */
  load_h1h4_to_zmm1 (c);

  gfmul_pclmul_avx512 (); /* H<<<1•H¹⁶ => H¹⁷, …, H⁴<<<1•H¹⁶ => H²⁰ */

  asm volatile ("vmovdqu64 %%zmm1, 15*16(%[h_table])\n\t"
		/* load H⁵ <<< 1, …, H⁸ <<< 1 */
		"vmovdqu64 3*16(%[h_table]), %%zmm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx512 (); /* H⁵<<<1•H¹⁶ => H²¹, …, H⁹<<<1•H¹⁶ => H²⁴ */

  asm volatile ("vmovdqu64 %%zmm1, 19*16(%[h_table])\n\t"
		/* load H⁹ <<< 1, …, H¹² <<< 1 */
		"vmovdqu64 7*16(%[h_table]), %%zmm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx512 (); /* H⁹<<<1•H¹⁶ => H²⁵, …, H¹²<<<1•H¹⁶ => H²⁸ */

  asm volatile ("vmovdqu64 %%zmm1, 23*16(%[h_table])\n\t"
		/* load H¹³ <<< 1, …, H¹⁶ <<< 1 */
		"vmovdqu64 11*16(%[h_table]), %%zmm1\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul_avx512 (); /* H¹³<<<1•H¹⁶ => H²⁹, …, H¹⁶<<<1•H¹⁶ => H³² */

  asm volatile ("vmovdqu64 %%zmm1, 27*16(%[h_table])\n\t"
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 15 * 16);
  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 19 * 16);
  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 23 * 16);
  gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 27 * 16);
}

static const u64 swap128b_perm[8] __attribute__ ((aligned (64))) =
  {
    /* For swapping order of 128bit lanes in 512bit register using vpermq. */
    6, 7, 4, 5, 2, 3, 0, 1
  };

#endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */
#endif /* __x86_64__ */

static unsigned int ASM_FUNC_ATTR
_gcry_ghash_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
			  size_t nblocks);

static unsigned int ASM_FUNC_ATTR
_gcry_polyval_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
			    size_t nblocks);

static ASM_FUNC_ATTR_INLINE void
gcm_lsh(void *h, unsigned int hoffs)
{
  static const u64 pconst[2] __attribute__ ((aligned (16))) =
    { U64_C(0x0000000000000001), U64_C(0xc200000000000000) };

  asm volatile ("movdqu %[h], %%xmm2\n\t"
                "pshufd $0xff, %%xmm2, %%xmm3\n\t"
                "movdqa %%xmm2, %%xmm4\n\t"
                "psrad $31, %%xmm3\n\t"
                "pslldq $8, %%xmm4\n\t"
                "pand %[pconst], %%xmm3\n\t"
                "paddq %%xmm2, %%xmm2\n\t"
                "psrlq $63, %%xmm4\n\t"
                "pxor %%xmm3, %%xmm2\n\t"
                "pxor %%xmm4, %%xmm2\n\t"
                "movdqu %%xmm2, %[h]\n\t"
                : [h] "+m" (*((byte *)h + hoffs))
                : [pconst] "m" (*pconst)
                : "memory" );
}

void ASM_FUNC_ATTR
_gcry_ghash_setup_intel_pclmul (gcry_cipher_hd_t c, unsigned int hw_features)
{
  static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
#if defined(__x86_64__) && defined(__WIN64__)
  char win64tmp[10 * 16];

  /* XMM6-XMM15 need to be restored after use. */
  asm volatile ("movdqu %%xmm6,  0*16(%0)\n\t"
                "movdqu %%xmm7,  1*16(%0)\n\t"
                "movdqu %%xmm8,  2*16(%0)\n\t"
                "movdqu %%xmm9,  3*16(%0)\n\t"
                "movdqu %%xmm10, 4*16(%0)\n\t"
                "movdqu %%xmm11, 5*16(%0)\n\t"
                "movdqu %%xmm12, 6*16(%0)\n\t"
                "movdqu %%xmm13, 7*16(%0)\n\t"
                "movdqu %%xmm14, 8*16(%0)\n\t"
                "movdqu %%xmm15, 9*16(%0)\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#endif

  (void)hw_features;

  c->u_mode.gcm.hw_impl_flags = 0;
  c->u_mode.gcm.ghash_fn = _gcry_ghash_intel_pclmul;
  c->u_mode.gcm.polyval_fn = _gcry_polyval_intel_pclmul;

  /* Swap endianness of hsub. */
  asm volatile ("movdqu (%[key]), %%xmm0\n\t"
                "pshufb %[be_mask], %%xmm0\n\t"
                "movdqu %%xmm0, (%[key])\n\t"
                :
                : [key] "r" (c->u_mode.gcm.u_ghash_key.key),
                  [be_mask] "m" (*be_mask)
                : "memory");

  gcm_lsh (c->u_mode.gcm.u_ghash_key.key, 0); /* H <<< 1 */

  asm volatile ("movdqa %%xmm0, %%xmm1\n\t"
                "movdqu (%[key]), %%xmm0\n\t" /* load H <<< 1 */
                :
                : [key] "r" (c->u_mode.gcm.u_ghash_key.key)
                : "memory");

  gfmul_pclmul (); /* H<<<1•H => H² */

  asm volatile ("movdqu %%xmm1, 0*16(%[h_table])\n\t"
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gcm_lsh (c->u_mode.gcm.gcm_table, 0 * 16); /* H² <<< 1 */

  if (0)
    { }
#ifdef GCM_USE_INTEL_VPCLMUL_AVX2
  else if ((hw_features & HWF_INTEL_VAES_VPCLMUL)
           && (hw_features & HWF_INTEL_AVX2))
    {
      c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_USE_VPCLMUL_AVX2;

#ifdef GCM_USE_INTEL_VPCLMUL_AVX512
      if (hw_features & HWF_INTEL_AVX512)
	c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_USE_VPCLMUL_AVX512;
#endif

      asm volatile (/* H² */
		    "vinserti128 $1, %%xmm1, %%ymm1, %%ymm1\n\t"
		    /* load H <<< 1, H² <<< 1 */
		    "vinserti128 $1, 0*16(%[h_table]), %%ymm0, %%ymm0\n\t"
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table)
		    : "memory");

      gfmul_pclmul_avx2 (); /* H<<<1•H² => H³, H²<<<1•H² => H⁴ */

      asm volatile ("vmovdqu %%ymm1, 2*16(%[h_table])\n\t" /* store H⁴ for aggr8 setup */
		    "vmovdqu %%ymm1, 1*16(%[h_table])\n\t"
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table)
		    : "memory");

      gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 1 * 16); /* H³ <<< 1, H⁴ <<< 1 */

      asm volatile ("vzeroupper\n\t"
		    ::: "memory" );
    }
#endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
  else
    {
      asm volatile ("movdqa %%xmm1, %%xmm7\n\t"
		    ::: "memory");

      gfmul_pclmul (); /* H<<<1•H² => H³ */

      asm volatile ("movdqa %%xmm7, %%xmm0\n\t"
		    "movdqu %%xmm1, 1*16(%[h_table])\n\t"
		    "movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table)
		    : "memory");

      gfmul_pclmul (); /* H²<<<1•H² => H⁴ */

      asm volatile ("movdqu %%xmm1, 3*16(%[h_table])\n\t" /* store H⁴ for aggr8 setup */
		    "movdqu %%xmm1, 2*16(%[h_table])\n\t"
		    :
		    : [h_table] "r" (c->u_mode.gcm.gcm_table)
		    : "memory");

      gcm_lsh (c->u_mode.gcm.gcm_table, 1 * 16); /* H³ <<< 1 */
      gcm_lsh (c->u_mode.gcm.gcm_table, 2 * 16); /* H⁴ <<< 1 */
    }

  /* Clear/restore used registers. */
  asm volatile ("pxor %%xmm0, %%xmm0\n\t"
		"pxor %%xmm1, %%xmm1\n\t"
		"pxor %%xmm2, %%xmm2\n\t"
		"pxor %%xmm3, %%xmm3\n\t"
		"pxor %%xmm4, %%xmm4\n\t"
		"pxor %%xmm5, %%xmm5\n\t"
		"pxor %%xmm6, %%xmm6\n\t"
		"pxor %%xmm7, %%xmm7\n\t"
		::: "memory" );
#ifdef __x86_64__
#ifdef __WIN64__
  asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
                "movdqu 1*16(%0), %%xmm7\n\t"
                "movdqu 2*16(%0), %%xmm8\n\t"
                "movdqu 3*16(%0), %%xmm9\n\t"
                "movdqu 4*16(%0), %%xmm10\n\t"
                "movdqu 5*16(%0), %%xmm11\n\t"
                "movdqu 6*16(%0), %%xmm12\n\t"
                "movdqu 7*16(%0), %%xmm13\n\t"
                "movdqu 8*16(%0), %%xmm14\n\t"
                "movdqu 9*16(%0), %%xmm15\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#else
  asm volatile ("pxor %%xmm8, %%xmm8\n\t"
                "pxor %%xmm9, %%xmm9\n\t"
                "pxor %%xmm10, %%xmm10\n\t"
                "pxor %%xmm11, %%xmm11\n\t"
                "pxor %%xmm12, %%xmm12\n\t"
                "pxor %%xmm13, %%xmm13\n\t"
                "pxor %%xmm14, %%xmm14\n\t"
                "pxor %%xmm15, %%xmm15\n\t"
                ::: "memory" );
#endif /* __WIN64__ */
#endif /* __x86_64__ */
}


#ifdef __x86_64__
static ASM_FUNC_ATTR void
ghash_setup_aggr8(gcry_cipher_hd_t c)
{
  c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;

  asm volatile ("movdqa 3*16(%[h_table]), %%xmm0\n\t" /* load H⁴ */
		"movdqu %[key], %%xmm1\n\t" /* load H <<< 1 */
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table),
		  [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
		: "memory");

  gfmul_pclmul (); /* H<<<1•H⁴ => H⁵ */

  asm volatile ("movdqu %%xmm1, 3*16(%[h_table])\n\t"
		"movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul (); /* H²<<<1•H⁴ => H⁶ */

  asm volatile ("movdqu %%xmm1, 4*16(%[h_table])\n\t"
		"movdqu 1*16(%[h_table]), %%xmm1\n\t" /* load H³ <<< 1 */
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul (); /* H³<<<1•H⁴ => H⁷ */

  asm volatile ("movdqu %%xmm1, 5*16(%[h_table])\n\t"
		"movdqu 2*16(%[h_table]), %%xmm1\n\t" /* load H⁴ <<< 1 */
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gfmul_pclmul (); /* H⁴<<<1•H⁴ => H⁸ */

  asm volatile ("movdqu %%xmm1, 6*16(%[h_table])\n\t"
		"movdqu %%xmm1, 7*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
		:
		: [h_table] "r" (c->u_mode.gcm.gcm_table)
		: "memory");

  gcm_lsh (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1 */
  gcm_lsh (c->u_mode.gcm.gcm_table, 4 * 16); /* H⁶ <<< 1 */
  gcm_lsh (c->u_mode.gcm.gcm_table, 5 * 16); /* H⁷ <<< 1 */
  gcm_lsh (c->u_mode.gcm.gcm_table, 6 * 16); /* H⁸ <<< 1 */
}
#endif /* __x86_64__ */


unsigned int ASM_FUNC_ATTR
_gcry_ghash_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
			  size_t nblocks)
{
  static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
  const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
#if defined(__x86_64__) && defined(__WIN64__)
  char win64tmp[10 * 16];
#endif

  if (nblocks == 0)
    return 0;

#if defined(__x86_64__) && defined(__WIN64__)
  /* XMM6-XMM15 need to be restored after use. */
  asm volatile ("movdqu %%xmm6,  0*16(%0)\n\t"
                "movdqu %%xmm7,  1*16(%0)\n\t"
                "movdqu %%xmm8,  2*16(%0)\n\t"
                "movdqu %%xmm9,  3*16(%0)\n\t"
                "movdqu %%xmm10, 4*16(%0)\n\t"
                "movdqu %%xmm11, 5*16(%0)\n\t"
                "movdqu %%xmm12, 6*16(%0)\n\t"
                "movdqu %%xmm13, 7*16(%0)\n\t"
                "movdqu %%xmm14, 8*16(%0)\n\t"
                "movdqu %%xmm15, 9*16(%0)\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#endif

  /* Preload hash. */
  asm volatile ("movdqa %[be_mask], %%xmm7\n\t"
                "movdqu %[hash], %%xmm1\n\t"
                "pshufb %%xmm7, %%xmm1\n\t" /* be => le */
                :
                : [hash] "m" (*result),
                  [be_mask] "m" (*be_mask)
                : "memory" );

#if defined(GCM_USE_INTEL_VPCLMUL_AVX2)
  if (nblocks >= 16
      && ((c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX2)
          || (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512)))
    {
#if defined(GCM_USE_INTEL_VPCLMUL_AVX512)
      if (nblocks >= 32
	  && (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512))
	{
	  asm volatile ("vpopcntb %%xmm7, %%xmm16\n\t" /* spec stop for old AVX512 CPUs */
			"vshufi64x2 $0, %%zmm7, %%zmm7, %%zmm15\n\t"
			"vmovdqa %%xmm1, %%xmm8\n\t"
			"vmovdqu64 %[swapperm], %%zmm14\n\t"
			:
			: [swapperm] "m" (swap128b_perm),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR32_TABLE_INITIALIZED))
	    {
	      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
		{
		  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
		    ghash_setup_aggr8_avx512 (c); /* Clobbers registers XMM0-XMM7. */

		  ghash_setup_aggr16_avx512 (c); /* Clobbers registers XMM0-XMM7. */
		}

	      ghash_setup_aggr32_avx512 (c); /* Clobbers registers XMM0-XMM7. */
	    }

	  /* Preload H1-H32. */
	  load_h1h4_to_zmm1 (c);
	  asm volatile ("vpermq %%zmm1, %%zmm14, %%zmm16\n\t" /* H1|H2|H3|H4 */
			"vmovdqa %%xmm8, %%xmm1\n\t"
			"vpermq 27*16(%[h_table]), %%zmm14, %%zmm0\n\t"  /* H28|H29|H31|H32 */
			"vpermq 23*16(%[h_table]), %%zmm14, %%zmm13\n\t" /* H25|H26|H27|H28 */
			"vpermq 19*16(%[h_table]), %%zmm14, %%zmm12\n\t" /* H21|H22|H23|H24 */
			"vpermq 15*16(%[h_table]), %%zmm14, %%zmm11\n\t" /* H17|H18|H19|H20 */
			"vpermq 11*16(%[h_table]), %%zmm14, %%zmm10\n\t" /* H13|H14|H15|H16 */
			"vpermq 7*16(%[h_table]), %%zmm14, %%zmm9\n\t"   /* H9|H10|H11|H12 */
			"vpermq 3*16(%[h_table]), %%zmm14, %%zmm8\n\t"   /* H4|H6|H7|H8 */
			:
			: [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  while (nblocks >= 32)
	    {
	      gfmul_vpclmul_avx512_aggr32 (buf, c->u_mode.gcm.gcm_table);

	      buf += 32 * blocksize;
	      nblocks -= 32;
	    }

	  asm volatile ("vmovdqa %%xmm15, %%xmm7\n\t"
			"vpxorq %%ymm16, %%ymm16, %%ymm16\n\t"
			"vpxorq %%ymm17, %%ymm17, %%ymm17\n\t"
			"vpxorq %%ymm18, %%ymm18, %%ymm18\n\t"
			"vpxorq %%ymm19, %%ymm19, %%ymm19\n\t"
			:
			:
			: "memory" );
	}
#endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */

      if (nblocks >= 16)
	{
	  u64 h1_h2_h15_h16[4*2];

	  asm volatile ("vinserti128 $1, %%xmm7, %%ymm7, %%ymm15\n\t"
			"vmovdqa %%xmm1, %%xmm8\n\t"
			::: "memory" );

	  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
	    {
	      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
		ghash_setup_aggr8_avx2 (c); /* Clobbers registers XMM0-XMM7. */

	      ghash_setup_aggr16_avx2 (c); /* Clobbers registers XMM0-XMM7. */
	    }

	  /* Preload H1-H16. */
	  load_h1h2_to_ymm1 (c);
	  asm volatile ("vperm2i128 $0x23, %%ymm1, %%ymm1, %%ymm7\n\t" /* H1|H2 */
			"vmovdqa %%xmm8, %%xmm1\n\t"
			"vpxor %%xmm8, %%xmm8, %%xmm8\n\t"
			"vperm2i128 $0x23, 13*16(%[h_table]), %%ymm8, %%ymm0\n\t"  /* H15|H16 */
			"vperm2i128 $0x23, 11*16(%[h_table]), %%ymm8, %%ymm13\n\t" /* H13|H14 */
			"vperm2i128 $0x23, 9*16(%[h_table]), %%ymm8, %%ymm12\n\t"  /* H11|H12 */
			"vperm2i128 $0x23, 7*16(%[h_table]), %%ymm8, %%ymm11\n\t"  /* H9|H10 */
			"vperm2i128 $0x23, 5*16(%[h_table]), %%ymm8, %%ymm10\n\t"  /* H7|H8 */
			"vperm2i128 $0x23, 3*16(%[h_table]), %%ymm8, %%ymm9\n\t"   /* H5|H6 */
			"vperm2i128 $0x23, 1*16(%[h_table]), %%ymm8, %%ymm8\n\t"   /* H3|H4 */
			"vmovdqu %%ymm0, %[h15_h16]\n\t"
			"vmovdqu %%ymm7, %[h1_h2]\n\t"
			: [h1_h2] "=m" (h1_h2_h15_h16[0]),
			  [h15_h16] "=m" (h1_h2_h15_h16[4])
			: [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  while (nblocks >= 16)
	    {
	      gfmul_vpclmul_avx2_aggr16 (buf, c->u_mode.gcm.gcm_table,
					h1_h2_h15_h16);

	      buf += 16 * blocksize;
	      nblocks -= 16;
	    }

	  asm volatile ("vmovdqu %%ymm15, %[h15_h16]\n\t"
			"vmovdqu %%ymm15, %[h1_h2]\n\t"
			"vmovdqa %%xmm15, %%xmm7\n\t"
			:
			  [h1_h2] "=m" (h1_h2_h15_h16[0]),
			  [h15_h16] "=m" (h1_h2_h15_h16[4])
			:
			: "memory" );
	}

      asm volatile ("vzeroupper\n\t" ::: "memory" );
    }
#endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */

#ifdef __x86_64__
  if (nblocks >= 8)
    {
      asm volatile ("movdqa %%xmm7, %%xmm15\n\t"
		    "movdqa %%xmm1, %%xmm8\n\t"
		    ::: "memory" );

      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
	ghash_setup_aggr8 (c); /* Clobbers registers XMM0-XMM7. */

      /* Preload H1. */
      asm volatile ("movdqa %%xmm8, %%xmm1\n\t"
		    "movdqa %[h_1], %%xmm0\n\t"
		    :
		    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory" );

      while (nblocks >= 8)
        {
          gfmul_pclmul_aggr8 (buf, c->u_mode.gcm.gcm_table);

          buf += 8 * blocksize;
          nblocks -= 8;
        }
    }
#endif /* __x86_64__ */

  while (nblocks >= 4)
    {
      gfmul_pclmul_aggr4 (buf, c->u_mode.gcm.u_ghash_key.key,
                          c->u_mode.gcm.gcm_table, be_mask);

      buf += 4 * blocksize;
      nblocks -= 4;
    }

  if (nblocks)
    {
      /* Preload H1. */
      asm volatile ("movdqa %[h_1], %%xmm0\n\t"
                    :
                    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
                    : "memory" );

      while (nblocks)
        {
          asm volatile ("movdqu %[buf], %%xmm2\n\t"
                        "pshufb %[be_mask], %%xmm2\n\t" /* be => le */
                        "pxor %%xmm2, %%xmm1\n\t"
                        :
                        : [buf] "m" (*buf), [be_mask] "m" (*be_mask)
                        : "memory" );

          gfmul_pclmul ();

          buf += blocksize;
          nblocks--;
        }
    }

  /* Store hash. */
  asm volatile ("pshufb %[be_mask], %%xmm1\n\t" /* be => le */
                "movdqu %%xmm1, %[hash]\n\t"
                : [hash] "=m" (*result)
                : [be_mask] "m" (*be_mask)
                : "memory" );

  /* Clear/restore used registers. */
  asm volatile ("pxor %%xmm0, %%xmm0\n\t"
		"pxor %%xmm1, %%xmm1\n\t"
		"pxor %%xmm2, %%xmm2\n\t"
		"pxor %%xmm3, %%xmm3\n\t"
		"pxor %%xmm4, %%xmm4\n\t"
		"pxor %%xmm5, %%xmm5\n\t"
		"pxor %%xmm6, %%xmm6\n\t"
		"pxor %%xmm7, %%xmm7\n\t"
		:
		:
		: "memory" );
#ifdef __x86_64__
#ifdef __WIN64__
  asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
		"movdqu 1*16(%0), %%xmm7\n\t"
		"movdqu 2*16(%0), %%xmm8\n\t"
		"movdqu 3*16(%0), %%xmm9\n\t"
		"movdqu 4*16(%0), %%xmm10\n\t"
		"movdqu 5*16(%0), %%xmm11\n\t"
		"movdqu 6*16(%0), %%xmm12\n\t"
		"movdqu 7*16(%0), %%xmm13\n\t"
		"movdqu 8*16(%0), %%xmm14\n\t"
		"movdqu 9*16(%0), %%xmm15\n\t"
		:
		: "r" (win64tmp)
		: "memory" );
#else
  /* Clear used registers. */
  asm volatile (
		"pxor %%xmm8, %%xmm8\n\t"
		"pxor %%xmm9, %%xmm9\n\t"
		"pxor %%xmm10, %%xmm10\n\t"
		"pxor %%xmm11, %%xmm11\n\t"
		"pxor %%xmm12, %%xmm12\n\t"
		"pxor %%xmm13, %%xmm13\n\t"
		"pxor %%xmm14, %%xmm14\n\t"
		"pxor %%xmm15, %%xmm15\n\t"
		:
		:
		: "memory" );
#endif /* __WIN64__ */
#endif /* __x86_64__ */

  return 0;
}

unsigned int ASM_FUNC_ATTR
_gcry_polyval_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
			    size_t nblocks)
{
  static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
  const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
#if defined(__x86_64__) && defined(__WIN64__)
  char win64tmp[10 * 16];
#endif

  if (nblocks == 0)
    return 0;

#if defined(__x86_64__) && defined(__WIN64__)
  /* XMM6-XMM15 need to be restored after use. */
  asm volatile ("movdqu %%xmm6,  0*16(%0)\n\t"
                "movdqu %%xmm7,  1*16(%0)\n\t"
                "movdqu %%xmm8,  2*16(%0)\n\t"
                "movdqu %%xmm9,  3*16(%0)\n\t"
                "movdqu %%xmm10, 4*16(%0)\n\t"
                "movdqu %%xmm11, 5*16(%0)\n\t"
                "movdqu %%xmm12, 6*16(%0)\n\t"
                "movdqu %%xmm13, 7*16(%0)\n\t"
                "movdqu %%xmm14, 8*16(%0)\n\t"
                "movdqu %%xmm15, 9*16(%0)\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#endif

  /* Preload hash. */
  asm volatile ("pxor %%xmm7, %%xmm7\n\t"
                "movdqu %[hash], %%xmm1\n\t"
                "pshufb %[be_mask], %%xmm1\n\t" /* be => le */
                :
                : [hash] "m" (*result),
                  [be_mask] "m" (*be_mask)
                : "memory" );

#if defined(GCM_USE_INTEL_VPCLMUL_AVX2)
  if (nblocks >= 16
      && ((c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX2)
          || (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512)))
    {
#if defined(GCM_USE_INTEL_VPCLMUL_AVX512)
      if (nblocks >= 32
	  && (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512))
	{
	  asm volatile ("vpopcntb %%xmm1, %%xmm16\n\t" /* spec stop for old AVX512 CPUs */
			"vmovdqa %%xmm1, %%xmm8\n\t"
			"vmovdqu64 %[swapperm], %%zmm14\n\t"
			:
			: [swapperm] "m" (swap128b_perm),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR32_TABLE_INITIALIZED))
	    {
	      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
		{
		  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
		    ghash_setup_aggr8_avx512 (c); /* Clobbers registers XMM0-XMM7. */

		  ghash_setup_aggr16_avx512 (c); /* Clobbers registers XMM0-XMM7. */
		}

	      ghash_setup_aggr32_avx512 (c); /* Clobbers registers XMM0-XMM7. */
	    }

	  /* Preload H1-H32. */
	  load_h1h4_to_zmm1 (c);
	  asm volatile ("vpermq %%zmm1, %%zmm14, %%zmm16\n\t" /* H1|H2|H3|H4 */
			"vmovdqa %%xmm8, %%xmm1\n\t"
			"vpermq 27*16(%[h_table]), %%zmm14, %%zmm0\n\t"  /* H28|H29|H31|H32 */
			"vpermq 23*16(%[h_table]), %%zmm14, %%zmm13\n\t" /* H25|H26|H27|H28 */
			"vpermq 19*16(%[h_table]), %%zmm14, %%zmm12\n\t" /* H21|H22|H23|H24 */
			"vpermq 15*16(%[h_table]), %%zmm14, %%zmm11\n\t" /* H17|H18|H19|H20 */
			"vpermq 11*16(%[h_table]), %%zmm14, %%zmm10\n\t" /* H13|H14|H15|H16 */
			"vpermq 7*16(%[h_table]), %%zmm14, %%zmm9\n\t"   /* H9|H10|H11|H12 */
			"vpermq 3*16(%[h_table]), %%zmm14, %%zmm8\n\t"   /* H4|H6|H7|H8 */
			:
			: [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  while (nblocks >= 32)
	    {
	      gfmul_vpclmul_avx512_aggr32_le (buf, c->u_mode.gcm.gcm_table);

	      buf += 32 * blocksize;
	      nblocks -= 32;
	    }

	  asm volatile ("vpxor %%xmm7, %%xmm7, %%xmm7\n\t"
			"vpxorq %%ymm16, %%ymm16, %%ymm16\n\t"
			"vpxorq %%ymm17, %%ymm17, %%ymm17\n\t"
			"vpxorq %%ymm18, %%ymm18, %%ymm18\n\t"
			"vpxorq %%ymm19, %%ymm19, %%ymm19\n\t"
			:
			:
			: "memory" );
	}
#endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */

      if (nblocks >= 16)
	{
	  u64 h1_h2_h15_h16[4*2];

	  asm volatile ("vmovdqa %%xmm1, %%xmm8\n\t"
			::: "memory" );

	  if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
	    {
	      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
		ghash_setup_aggr8_avx2 (c); /* Clobbers registers XMM0-XMM7. */

	      ghash_setup_aggr16_avx2 (c); /* Clobbers registers XMM0-XMM7. */
	    }

	  /* Preload H1-H16. */
	  load_h1h2_to_ymm1 (c);
	  asm volatile ("vperm2i128 $0x23, %%ymm1, %%ymm1, %%ymm7\n\t" /* H1|H2 */
			"vmovdqa %%xmm8, %%xmm1\n\t"
			"vpxor %%xmm8, %%xmm8, %%xmm8\n\t"
			"vperm2i128 $0x23, 13*16(%[h_table]), %%ymm8, %%ymm0\n\t"  /* H15|H16 */
			"vperm2i128 $0x23, 11*16(%[h_table]), %%ymm8, %%ymm13\n\t" /* H13|H14 */
			"vperm2i128 $0x23, 9*16(%[h_table]), %%ymm8, %%ymm12\n\t"  /* H11|H12 */
			"vperm2i128 $0x23, 7*16(%[h_table]), %%ymm8, %%ymm11\n\t"  /* H9|H10 */
			"vperm2i128 $0x23, 5*16(%[h_table]), %%ymm8, %%ymm10\n\t"  /* H7|H8 */
			"vperm2i128 $0x23, 3*16(%[h_table]), %%ymm8, %%ymm9\n\t"   /* H5|H6 */
			"vperm2i128 $0x23, 1*16(%[h_table]), %%ymm8, %%ymm8\n\t"   /* H3|H4 */
			"vmovdqu %%ymm0, %[h15_h16]\n\t"
			"vmovdqu %%ymm7, %[h1_h2]\n\t"
			: [h1_h2] "=m" (h1_h2_h15_h16[0]),
			  [h15_h16] "=m" (h1_h2_h15_h16[4])
			: [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
			  [h_table] "r" (c->u_mode.gcm.gcm_table)
			: "memory" );

	  while (nblocks >= 16)
	    {
	      gfmul_vpclmul_avx2_aggr16_le (buf, c->u_mode.gcm.gcm_table,
					    h1_h2_h15_h16);

	      buf += 16 * blocksize;
	      nblocks -= 16;
	    }

	  asm volatile ("vpxor %%xmm7, %%xmm7, %%xmm7\n\t"
			"vmovdqu %%ymm7, %[h15_h16]\n\t"
			"vmovdqu %%ymm7, %[h1_h2]\n\t"
			: [h1_h2] "=m" (h1_h2_h15_h16[0]),
			  [h15_h16] "=m" (h1_h2_h15_h16[4])
			:
			: "memory" );
	}

      asm volatile ("vzeroupper\n\t" ::: "memory" );
    }
#endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */

#ifdef __x86_64__
  if (nblocks >= 8)
    {
      asm volatile ("movdqa %%xmm1, %%xmm8\n\t"
		    ::: "memory" );

      if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
	ghash_setup_aggr8 (c); /* Clobbers registers XMM0-XMM7. */

      /* Preload H1. */
      asm volatile ("movdqa %%xmm8, %%xmm1\n\t"
		    "pxor %%xmm15, %%xmm15\n\t"
		    "movdqa %[h_1], %%xmm0\n\t"
		    :
		    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
		    : "memory" );

      while (nblocks >= 8)
        {
          gfmul_pclmul_aggr8_le (buf, c->u_mode.gcm.gcm_table);

          buf += 8 * blocksize;
          nblocks -= 8;
        }
    }
#endif

  while (nblocks >= 4)
    {
      gfmul_pclmul_aggr4_le (buf, c->u_mode.gcm.u_ghash_key.key,
                             c->u_mode.gcm.gcm_table);

      buf += 4 * blocksize;
      nblocks -= 4;
    }

  if (nblocks)
    {
      /* Preload H1. */
      asm volatile ("movdqa %[h_1], %%xmm0\n\t"
                    :
                    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
                    : "memory" );

      while (nblocks)
        {
          asm volatile ("movdqu %[buf], %%xmm2\n\t"
                        "pxor %%xmm2, %%xmm1\n\t"
                        :
                        : [buf] "m" (*buf)
                        : "memory" );

          gfmul_pclmul ();

          buf += blocksize;
          nblocks--;
        }
    }

  /* Store hash. */
  asm volatile ("pshufb %[be_mask], %%xmm1\n\t" /* be => le */
                "movdqu %%xmm1, %[hash]\n\t"
                : [hash] "=m" (*result)
                : [be_mask] "m" (*be_mask)
                : "memory" );

  /* Clear/restore used registers. */
  asm volatile ("pxor %%xmm0, %%xmm0\n\t"
		"pxor %%xmm1, %%xmm1\n\t"
		"pxor %%xmm2, %%xmm2\n\t"
		"pxor %%xmm3, %%xmm3\n\t"
		"pxor %%xmm4, %%xmm4\n\t"
		"pxor %%xmm5, %%xmm5\n\t"
		"pxor %%xmm6, %%xmm6\n\t"
		"pxor %%xmm7, %%xmm7\n\t"
		:
		:
		: "memory" );
#ifdef __x86_64__
#ifdef __WIN64__
  asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
		"movdqu 1*16(%0), %%xmm7\n\t"
		"movdqu 2*16(%0), %%xmm8\n\t"
		"movdqu 3*16(%0), %%xmm9\n\t"
		"movdqu 4*16(%0), %%xmm10\n\t"
		"movdqu 5*16(%0), %%xmm11\n\t"
		"movdqu 6*16(%0), %%xmm12\n\t"
		"movdqu 7*16(%0), %%xmm13\n\t"
		"movdqu 8*16(%0), %%xmm14\n\t"
		"movdqu 9*16(%0), %%xmm15\n\t"
		:
		: "r" (win64tmp)
		: "memory" );
#else
  /* Clear used registers. */
  asm volatile (
		"pxor %%xmm8, %%xmm8\n\t"
		"pxor %%xmm9, %%xmm9\n\t"
		"pxor %%xmm10, %%xmm10\n\t"
		"pxor %%xmm11, %%xmm11\n\t"
		"pxor %%xmm12, %%xmm12\n\t"
		"pxor %%xmm13, %%xmm13\n\t"
		"pxor %%xmm14, %%xmm14\n\t"
		"pxor %%xmm15, %%xmm15\n\t"
		:
		:
		: "memory" );
#endif /* __WIN64__ */
#endif /* __x86_64__ */

  return 0;
}

#if __clang__
#  pragma clang attribute pop
#endif

#endif /* GCM_USE_INTEL_PCLMUL */