260a31714795d4e9b1805d071f2c052fbecb73ba
[alexxy/gromacs.git] / include / gmx_x86_avx_128_fma.h
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  * This file is part of GROMACS.
5  * Copyright (c) 2012-  
6  *
7  * Written by the Gromacs development team under coordination of
8  * David van der Spoel, Berk Hess, and Erik Lindahl.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * To help us fund GROMACS development, we humbly ask that you cite
16  * the research papers on the package. Check out http://www.gromacs.org
17  * 
18  * And Hey:
19  * Gnomes, ROck Monsters And Chili Sauce
20  */
21 #ifndef _gmx_x86_avx_128_fma_h_
22 #define _gmx_x86_avx_128_fma_h_
23
24
25 #include <immintrin.h>
26 #ifdef HAVE_X86INTRIN_H
27 #include <x86intrin.h> /* FMA */
28 #endif
29
30 #include <stdio.h>
31
32 #include "types/simple.h"
33
34
35 #define gmx_mm_extract_epi32(x, imm) _mm_cvtsi128_si32(_mm_srli_si128((x), 4 * (imm)))
36
37
38 #define _GMX_MM_PERMUTE128D(fp1,fp0)         (((fp1) << 1) | ((fp0)))
39
40
41 #define GMX_MM_TRANSPOSE2_PD(row0, row1) {           \
42     __m128d __gmx_t1 = row0;                         \
43     row0           = _mm_unpacklo_pd(row0,row1);     \
44     row1           = _mm_unpackhi_pd(__gmx_t1,row1); \
45 }
46
47
48 #if (defined (_MSC_VER) || defined(__INTEL_COMPILER))
49 #  define gmx_mm_castsi128_ps(a) _mm_castsi128_ps(a)
50 #  define gmx_mm_castps_si128(a) _mm_castps_si128(a)
51 #  define gmx_mm_castps_ps128(a) (a)
52 #  define gmx_mm_castsi128_pd(a) _mm_castsi128_pd(a)
53 #  define gmx_mm_castpd_si128(a) _mm_castpd_si128(a)
54 #elif defined(__GNUC__)
55 #  define gmx_mm_castsi128_ps(a) ((__m128)(a))
56 #  define gmx_mm_castps_si128(a) ((__m128i)(a))
57 #  define gmx_mm_castps_ps128(a) ((__m128)(a))
58 #  define gmx_mm_castsi128_pd(a) ((__m128d)(a))
59 #  define gmx_mm_castpd_si128(a) ((__m128i)(a))
60 #else
61 static __m128  gmx_mm_castsi128_ps(__m128i a)
62 {
63     return *(__m128 *) &a;
64 }
65 static __m128i gmx_mm_castps_si128(__m128 a)
66 {
67     return *(__m128i *) &a;
68 }
69 static __m128  gmx_mm_castps_ps128(__m128 a)
70 {
71     return *(__m128 *) &a;
72 }
73 static __m128d gmx_mm_castsi128_pd(__m128i a)
74 {
75     return *(__m128d *) &a;
76 }
77 static __m128i gmx_mm_castpd_si128(__m128d a)
78 {
79     return *(__m128i *) &a;
80 }
81 #endif
82
83 #ifndef _MSC_VER
84 /* The warning directive is not supported by MSVC, and that compiler
85  * does not support overriding built-in functions anyway...
86  */
87 #if !defined(HAVE_x86INTRIN_H) || !defined(__FMA4__)
88 #warning Emulating FMA instructions - this is probably not what you want!
89 /* Wrapper routines so we can do test builds on non-FMA hardware */
90 static __m128
91 _mm_macc_ps(__m128 a, __m128 b, __m128 c)
92 {
93
94     return _mm_add_ps(c,_mm_mul_ps(a,b));
95 }
96
97 static __m128
98 _mm_nmacc_ps(__m128 a, __m128 b, __m128 c)
99 {
100
101     return _mm_sub_ps(c,_mm_mul_ps(a,b));
102 }
103
104 static __m128d
105 _mm_macc_pd(__m128d a, __m128d b, __m128d c)
106 {
107
108     return _mm_add_pd(c,_mm_mul_pd(a,b));
109 }
110
111 static __m128d
112 _mm_nmacc_pd(__m128d a, __m128d b, __m128d c)
113 {
114
115     return _mm_sub_pd(c,_mm_mul_pd(a,b));
116 }
117 #endif /* FMA4 support */
118
119 #endif /* _MSC_VER */
120
121 static void
122 gmx_mm_printxmm_ps(const char *s,__m128 xmm)
123 {
124     float f[4];
125
126     _mm_storeu_ps(f,xmm);
127     printf("%s: %15.10e %15.10e %15.10e %15.10e\n",s,f[0],f[1],f[2],f[3]);
128 }
129
130
131 static void
132 gmx_mm_printxmmsum_ps(const char *s,__m128 xmm)
133 {
134     float f[4];
135
136     _mm_storeu_ps(f,xmm);
137     printf("%s (sum): %15.10g\n",s,f[0]+f[1]+f[2]+f[3]);
138 }
139
140
141 static void
142 gmx_mm_printxmm_pd(const char *s,__m128d xmm)
143 {
144     double f[2];
145
146     _mm_storeu_pd(f,xmm);
147     printf("%s: %30.20e %30.20e\n",s,f[0],f[1]);
148 }
149
150 static void
151 gmx_mm_printxmmsum_pd(const char *s,__m128d xmm)
152 {
153     double f[2];
154
155     _mm_storeu_pd(f,xmm);
156     printf("%s (sum): %15.10g\n",s,f[0]+f[1]);
157 }
158
159
160 static void
161 gmx_mm_printxmm_epi32(const char *s,__m128i xmmi)
162 {
163     int i[4];
164
165     _mm_storeu_si128((__m128i *)i,xmmi);
166     printf("%10s: %2d %2d %2d %2d\n",s,i[0],i[1],i[2],i[3]);
167 }
168
169
170
171 static int gmx_mm_check_and_reset_overflow(void)
172 {
173     int MXCSR;
174     int sse_overflow;
175
176     MXCSR = _mm_getcsr();
177     /* The overflow flag is bit 3 in the register */
178     if (MXCSR & 0x0008)
179     {
180         sse_overflow = 1;
181         /* Set the overflow flag to zero */
182         MXCSR = MXCSR & 0xFFF7;
183         _mm_setcsr(MXCSR);
184     }
185     else
186     {
187         sse_overflow = 0;
188     }
189
190     return sse_overflow;
191 }
192
193
194
195 #endif /* _gmx_x86_avx_128_fma_h_ */