Improve accuracy of SIMD exp for small args
[gromacs.git] / src / gromacs / simd / impl_x86_avx2_256 / impl_x86_avx2_256_simd_double.h
blob099bc40ddcad3ada9703e0f633ce17904f78da79
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2017, 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.
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.
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.
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.
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.
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.
36 #ifndef GMX_SIMD_IMPL_X86_AVX2_256_SIMD_DOUBLE_H
37 #define GMX_SIMD_IMPL_X86_AVX2_256_SIMD_DOUBLE_H
39 #include "config.h"
41 #include <immintrin.h>
43 #include "gromacs/math/utilities.h"
44 #include "gromacs/simd/impl_x86_avx_256/impl_x86_avx_256_simd_double.h"
46 namespace gmx
49 static inline SimdDouble gmx_simdcall
50 fma(SimdDouble a, SimdDouble b, SimdDouble c)
52 return {
53 _mm256_fmadd_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_)
57 static inline SimdDouble gmx_simdcall
58 fms(SimdDouble a, SimdDouble b, SimdDouble c)
60 return {
61 _mm256_fmsub_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_)
65 static inline SimdDouble gmx_simdcall
66 fnma(SimdDouble a, SimdDouble b, SimdDouble c)
68 return {
69 _mm256_fnmadd_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_)
73 static inline SimdDouble gmx_simdcall
74 fnms(SimdDouble a, SimdDouble b, SimdDouble c)
76 return {
77 _mm256_fnmsub_pd(a.simdInternal_, b.simdInternal_, c.simdInternal_)
81 static inline SimdDBool gmx_simdcall
82 testBits(SimdDouble a)
84 __m256i ia = _mm256_castpd_si256(a.simdInternal_);
85 __m256i res = _mm256_andnot_si256( _mm256_cmpeq_epi64(ia, _mm256_setzero_si256()), _mm256_cmpeq_epi64(ia, ia));
87 return {
88 _mm256_castsi256_pd(res)
92 static inline SimdDouble
93 frexp(SimdDouble value, SimdDInt32 * exponent)
95 const __m256d exponentMask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7FF0000000000000LL));
96 const __m256d mantissaMask = _mm256_castsi256_pd( _mm256_set1_epi64x(0x800FFFFFFFFFFFFFLL));
97 const __m256i exponentBias = _mm256_set1_epi64x(1022LL); // add 1 to make our definition identical to frexp()
98 const __m256d half = _mm256_set1_pd(0.5);
99 __m256i iExponent;
100 __m128i iExponent128;
102 iExponent = _mm256_castpd_si256(_mm256_and_pd(value.simdInternal_, exponentMask));
103 iExponent = _mm256_sub_epi64(_mm256_srli_epi64(iExponent, 52), exponentBias);
104 iExponent = _mm256_shuffle_epi32(iExponent, _MM_SHUFFLE(3, 1, 2, 0));
106 iExponent128 = _mm256_extractf128_si256(iExponent, 1);
107 exponent->simdInternal_ = _mm_unpacklo_epi64(_mm256_castsi256_si128(iExponent), iExponent128);
109 return {
110 _mm256_or_pd(_mm256_and_pd(value.simdInternal_, mantissaMask), half)
114 template <MathOptimization opt = MathOptimization::Safe>
115 static inline SimdDouble
116 ldexp(SimdDouble value, SimdDInt32 exponent)
118 const __m128i exponentBias = _mm_set1_epi32(1023);
119 __m128i iExponent = _mm_add_epi32(exponent.simdInternal_, exponentBias);
121 if (opt == MathOptimization::Safe)
123 // Make sure biased argument is not negative
124 iExponent = _mm_max_epi32(iExponent, _mm_setzero_si128());
127 __m256i iExponent256 = _mm256_slli_epi64(_mm256_cvtepi32_epi64(iExponent), 52);
128 return {
129 _mm256_mul_pd(value.simdInternal_, _mm256_castsi256_pd(iExponent256))
133 } // namespace gmx
135 #endif // GMX_SIMD_IMPL_X86_AVX2_256_SIMD_DOUBLE_H