Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_avx2_256 / impl_x86_avx2_256_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_AVX2_256_SIMD_DOUBLE_H
37 #define GMX_SIMD_IMPL_X86_AVX2_256_SIMD_DOUBLE_H
38
39 #include "config.h"
40
41 #include <immintrin.h>
42
43 #include "gromacs/math/utilities.h"
44 #include "gromacs/simd/impl_x86_avx_256/impl_x86_avx_256_simd_double.h"
45
46 namespace gmx
47 {
48
49 static inline SimdDouble gmx_simdcall fma(SimdDouble a, SimdDouble b, SimdDouble c)
50 {
51     return { _mm256_fmadd_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
52 }
53
54 static inline SimdDouble gmx_simdcall fms(SimdDouble a, SimdDouble b, SimdDouble c)
55 {
56     return { _mm256_fmsub_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
57 }
58
59 static inline SimdDouble gmx_simdcall fnma(SimdDouble a, SimdDouble b, SimdDouble c)
60 {
61     return { _mm256_fnmadd_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
62 }
63
64 static inline SimdDouble gmx_simdcall fnms(SimdDouble a, SimdDouble b, SimdDouble c)
65 {
66     return { _mm256_fnmsub_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
67 }
68
69 static inline SimdDBool gmx_simdcall testBits(SimdDouble a)
70 {
71     __m256i ia  = _mm256_castpd_si256(a.simdInternal_);
72     __m256i res = _mm256_andnot_si256(_mm256_cmpeq_epi64(ia, _mm256_setzero_si256()),
73                                       _mm256_cmpeq_epi64(ia, ia));
74
75     return { _mm256_castsi256_pd(res) };
76 }
77
78 static inline SimdDouble frexp(SimdDouble value, SimdDInt32* exponent)
79 {
80     const __m256d exponentMask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7FF0000000000000LL));
81     const __m256d mantissaMask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x800FFFFFFFFFFFFFLL));
82     const __m256i exponentBias =
83             _mm256_set1_epi64x(1022LL); // add 1 to make our definition identical to frexp()
84     const __m256d half = _mm256_set1_pd(0.5);
85     __m256i       iExponent;
86     __m128i       iExponent128;
87
88     iExponent = _mm256_castpd_si256(_mm256_and_pd(value.simdInternal_, exponentMask));
89     iExponent = _mm256_sub_epi64(_mm256_srli_epi64(iExponent, 52), exponentBias);
90     iExponent = _mm256_shuffle_epi32(iExponent, _MM_SHUFFLE(3, 1, 2, 0));
91
92     iExponent128            = _mm256_extractf128_si256(iExponent, 1);
93     exponent->simdInternal_ = _mm_unpacklo_epi64(_mm256_castsi256_si128(iExponent), iExponent128);
94
95     return { _mm256_or_pd(_mm256_and_pd(value.simdInternal_, mantissaMask), half) };
96 }
97
98 template<MathOptimization opt = MathOptimization::Safe>
99 static inline SimdDouble ldexp(SimdDouble value, SimdDInt32 exponent)
100 {
101     const __m128i exponentBias = _mm_set1_epi32(1023);
102     __m128i       iExponent    = _mm_add_epi32(exponent.simdInternal_, exponentBias);
103
104     if (opt == MathOptimization::Safe)
105     {
106         // Make sure biased argument is not negative
107         iExponent = _mm_max_epi32(iExponent, _mm_setzero_si128());
108     }
109
110     __m256i iExponent256 = _mm256_slli_epi64(_mm256_cvtepi32_epi64(iExponent), 52);
111     return { _mm256_mul_pd(value.simdInternal_, _mm256_castsi256_pd(iExponent256)) };
112 }
113
114 } // namespace gmx
115
116 #endif // GMX_SIMD_IMPL_X86_AVX2_256_SIMD_DOUBLE_H