Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_sse4_1 / impl_x86_sse4_1_simd_double.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 #ifndef GMX_SIMD_IMPL_X86_SSE4_1_SIMD_DOUBLE_H
37 #define GMX_SIMD_IMPL_X86_SSE4_1_SIMD_DOUBLE_H
38
39 #include "config.h"
40
41 #include <smmintrin.h>
42
43 #include "gromacs/simd/impl_x86_sse2/impl_x86_sse2_simd_double.h"
44
45 namespace gmx
46 {
47
48 template<int index>
49 static inline std::int32_t gmx_simdcall extract(SimdDInt32 a)
50 {
51     return _mm_extract_epi32(a.simdInternal_, index);
52 }
53
54 static inline SimdDouble maskzRsqrt(SimdDouble x, SimdDBool m)
55 {
56 #ifndef NDEBUG
57     x.simdInternal_ = _mm_blendv_pd(_mm_set1_pd(1.0), x.simdInternal_, m.simdInternal_);
58 #endif
59     return { _mm_and_pd(_mm_cvtps_pd(_mm_rsqrt_ps(_mm_cvtpd_ps(x.simdInternal_))), m.simdInternal_) };
60 }
61
62 static inline SimdDouble maskzRcp(SimdDouble x, SimdDBool m)
63 {
64 #ifndef NDEBUG
65     x.simdInternal_ = _mm_blendv_pd(_mm_set1_pd(1.0), x.simdInternal_, m.simdInternal_);
66 #endif
67     return { _mm_and_pd(_mm_cvtps_pd(_mm_rcp_ps(_mm_cvtpd_ps(x.simdInternal_))), m.simdInternal_) };
68 }
69
70 static inline SimdDouble gmx_simdcall round(SimdDouble x)
71 {
72     return { _mm_round_pd(x.simdInternal_, _MM_FROUND_NINT) };
73 }
74
75 static inline SimdDouble gmx_simdcall trunc(SimdDouble x)
76 {
77     return { _mm_round_pd(x.simdInternal_, _MM_FROUND_TRUNC) };
78 }
79
80 static inline SimdDBool gmx_simdcall testBits(SimdDouble a)
81 {
82     __m128i ia = _mm_castpd_si128(a.simdInternal_);
83     __m128i res = _mm_andnot_si128(_mm_cmpeq_epi64(ia, _mm_setzero_si128()), _mm_cmpeq_epi64(ia, ia));
84
85     return { _mm_castsi128_pd(res) };
86 }
87
88 static inline SimdDouble gmx_simdcall blend(SimdDouble a, SimdDouble b, SimdDBool sel)
89 {
90     return { _mm_blendv_pd(a.simdInternal_, b.simdInternal_, sel.simdInternal_) };
91 }
92
93 static inline SimdDInt32 gmx_simdcall operator*(SimdDInt32 a, SimdDInt32 b)
94 {
95     return { _mm_mullo_epi32(a.simdInternal_, b.simdInternal_) };
96 }
97
98 static inline SimdDInt32 gmx_simdcall blend(SimdDInt32 a, SimdDInt32 b, SimdDIBool sel)
99 {
100     return { _mm_blendv_epi8(a.simdInternal_, b.simdInternal_, sel.simdInternal_) };
101 }
102
103 template<MathOptimization opt = MathOptimization::Safe>
104 static inline SimdDouble ldexp(SimdDouble value, SimdDInt32 exponent)
105 {
106     const __m128i exponentBias = _mm_set1_epi32(1023);
107     __m128i       iExponent    = _mm_add_epi32(exponent.simdInternal_, exponentBias);
108
109     if (opt == MathOptimization::Safe)
110     {
111         // Make sure biased argument is not negative
112         iExponent = _mm_max_epi32(iExponent, _mm_setzero_si128());
113     }
114
115     // After conversion integers will be in slot 0,1. Move them to 0,2 so
116     // we can do a 64-bit shift and get them to the dp exponents.
117     iExponent = _mm_shuffle_epi32(iExponent, _MM_SHUFFLE(3, 1, 2, 0));
118     iExponent = _mm_slli_epi64(iExponent, 52);
119
120     return { _mm_mul_pd(value.simdInternal_, _mm_castsi128_pd(iExponent)) };
121 }
122
123 } // namespace gmx
124
125 #endif // GMX_SIMD_IMPL_X86_SSE4_1_SIMD_DOUBLE_H