1 /* This file is part of the gf2x library.
2 
3    Copyright 2010, 2013, 2015
4    Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann
5 
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of either:
8     - If the archive contains a file named toom-gpl.c (not a trivial
9     placeholder), the GNU General Public License as published by the Free
10     Software Foundation; either version 3 of the License, or (at your
11     option) any later version.
12     - If the archive contains a file named toom-gpl.c which is a trivial
13     placeholder, the GNU Lesser General Public License as published by
14     the Free Software Foundation; either version 2.1 of the License, or
15     (at your option) any later version.
16 
17    This program is distributed in the hope that it will be useful, but WITHOUT
18    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19    FITNESS FOR A PARTICULAR PURPOSE.  See the license text for more details.
20 
21    You should have received a copy of the GNU General Public License as
22    well as the GNU Lesser General Public License along with this program;
23    see the files COPYING and COPYING.LIB.  If not, write to the Free
24    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25    02110-1301, USA.
26 */
27 
28 /* Implements 128x128 -> 256 bit product using pclmulqdq instruction. */
29 
30 #ifndef GF2X_MUL2_H_
31 #define GF2X_MUL2_H_
32 
33 #include "gf2x.h"
34 /* All gf2x source files for lowlevel functions must include gf2x-small.h
35  * This is mandatory for the tuning mechanism. */
36 #include "gf2x/gf2x-small.h"
37 
38 #if GF2X_WORDSIZE != 64
39 #error "This code is for 64-bit only"
40 #endif
41 
42 #ifndef GF2X_HAVE_PCLMUL_SUPPORT
43 #error "This code needs pclmul support"
44 #endif
45 
46 /* Karatsuba with 3 multiplications */
47 GF2X_STORAGE_CLASS_mul2
gf2x_mul2(unsigned long * t,unsigned long const * s1,unsigned long const * s2)48 void gf2x_mul2(unsigned long * t, unsigned long const * s1,
49         unsigned long const * s2)
50 {
51 #define PXOR(lop, rop) _mm_xor_si128((lop), (rop))
52     __m128i ss1 = _mm_loadu_si128((__m128i *)s1);
53     __m128i ss2 = _mm_loadu_si128((__m128i *)s2);
54 
55 
56     __m128i t00 = _mm_clmulepi64_si128(ss1, ss2, 0);
57     __m128i t11 = _mm_clmulepi64_si128(ss1, ss2, 0x11);
58 
59     ss1 = PXOR(ss1, _mm_shuffle_epi32(ss1, _MM_SHUFFLE(1,0,3,2)));
60     ss2 = PXOR(ss2, _mm_shuffle_epi32(ss2, _MM_SHUFFLE(1,0,3,2)));
61 
62     __m128i tk = PXOR(t00, PXOR(t11, _mm_clmulepi64_si128(ss1, ss2, 0)));
63 
64     /* mul2cl.c is essentially identical, just replaces srli and srli by
65      * unpacklo and unpackhi */
66     _mm_storeu_si128((__m128i *)t,     PXOR(t00, _mm_slli_si128(tk, 8)));
67     _mm_storeu_si128((__m128i *)(t+2), PXOR(t11, _mm_srli_si128(tk, 8)));
68 #undef PXOR
69 }
70 #endif  /* GF2X_MUL2_H_ */
71