Merge remote-tracking branch 'gerrit/release-4-6'
[alexxy/gromacs.git] / src / gromacs / legacyheaders / gmx_x86_sse4_1.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_sse4_1_h_
22 #define _gmx_x86_sse4_1_h_
23
24 #include <smmintrin.h>
25
26 #include <stdio.h>
27
28 #include "types/simple.h"
29
30 /* Create some basic definitions that are not 100% SSE2 standard and thus not
31  * available on all compilers. These should be fairly self-evident by comparing
32  * with an arbitrary emmintrin.h.
33  */
34
35
36 #define gmx_mm_extract_epi32(x, imm) _mm_cvtsi128_si32(_mm_srli_si128((x), 4 * (imm)))
37
38 #define GMX_MM_TRANSPOSE2_PD(row0, row1) {           \
39     __m128d __gmx_t1 = row0;                         \
40     row0           = _mm_unpacklo_pd(row0,row1);     \
41     row1           = _mm_unpackhi_pd(__gmx_t1,row1); \
42 }
43
44
45 #if (defined (_MSC_VER) || defined(__INTEL_COMPILER))
46 #  define gmx_mm_castsi128_ps(a) _mm_castsi128_ps(a)
47 #  define gmx_mm_castps_si128(a) _mm_castps_si128(a)
48 #  define gmx_mm_castps_ps128(a) (a)
49 #  define gmx_mm_castsi128_pd(a) _mm_castsi128_pd(a)
50 #  define gmx_mm_castpd_si128(a) _mm_castpd_si128(a)
51 #elif defined(__GNUC__)
52 #  define gmx_mm_castsi128_ps(a) ((__m128)(a))
53 #  define gmx_mm_castps_si128(a) ((__m128i)(a))
54 #  define gmx_mm_castps_ps128(a) ((__m128)(a))
55 #  define gmx_mm_castsi128_pd(a) ((__m128d)(a))
56 #  define gmx_mm_castpd_si128(a) ((__m128i)(a))
57 #else
58 static __m128  gmx_mm_castsi128_ps(__m128i a)
59 {
60     return *(__m128 *) &a;
61 }
62 static __m128i gmx_mm_castps_si128(__m128 a)
63 {
64     return *(__m128i *) &a;
65 }
66 static __m128  gmx_mm_castps_ps128(__m128 a)
67 {
68     return *(__m128 *) &a;
69 }
70 static __m128d gmx_mm_castsi128_pd(__m128i a)
71 {
72     return *(__m128d *) &a;
73 }
74 static __m128i gmx_mm_castpd_si128(__m128d a)
75 {
76     return *(__m128i *) &a;
77 }
78 #endif
79
80
81 static void
82 gmx_mm_printxmm_ps(const char *s,__m128 xmm)
83 {
84     float f[4];
85
86     _mm_storeu_ps(f,xmm);
87     printf("%s: %15.10e %15.10e %15.10e %15.10e\n",s,f[0],f[1],f[2],f[3]);
88 }
89
90
91 static void
92 gmx_mm_printxmmsum_ps(const char *s,__m128 xmm)
93 {
94     float f[4];
95
96     _mm_storeu_ps(f,xmm);
97     printf("%s (sum): %15.10g\n",s,f[0]+f[1]+f[2]+f[3]);
98 }
99
100
101 static void
102 gmx_mm_printxmm_pd(const char *s,__m128d xmm)
103 {
104     double f[2];
105
106     _mm_storeu_pd(f,xmm);
107     printf("%s: %30.20e %30.20e\n",s,f[0],f[1]);
108 }
109
110 static void
111 gmx_mm_printxmmsum_pd(const char *s,__m128d xmm)
112 {
113     double f[2];
114
115     _mm_storeu_pd(f,xmm);
116     printf("%s (sum): %15.10g\n",s,f[0]+f[1]);
117 }
118
119
120 static void
121 gmx_mm_printxmm_epi32(const char *s,__m128i xmmi)
122 {
123     int i[4];
124
125     _mm_storeu_si128((__m128i *)i,xmmi);
126     printf("%10s: %2d %2d %2d %2d\n",s,i[0],i[1],i[2],i[3]);
127 }
128
129
130
131 static int gmx_mm_check_and_reset_overflow(void)
132 {
133     int MXCSR;
134     int sse_overflow;
135
136     MXCSR = _mm_getcsr();
137     /* The overflow flag is bit 3 in the register */
138     if (MXCSR & 0x0008)
139     {
140         sse_overflow = 1;
141         /* Set the overflow flag to zero */
142         MXCSR = MXCSR & 0xFFF7;
143         _mm_setcsr(MXCSR);
144     }
145     else
146     {
147         sse_overflow = 0;
148     }
149
150     return sse_overflow;
151 }
152
153
154 #endif /* _gmx_x86_sse4_1_h_ */