Vc  1.3.2-dev
SIMD Vector Classes for C++
math.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_MATH_H_
29 #define VC_COMMON_MATH_H_
30 
31 #define Vc_COMMON_MATH_H_INTERNAL 1
32 
33 #include "trigonometric.h"
34 
35 #include "const.h"
36 #include "macros.h"
37 
38 namespace Vc_VERSIONED_NAMESPACE
39 {
40 #ifdef Vc_IMPL_SSE
41 // for SSE, AVX, and AVX2
42 #include "logarithm.h"
43 #include "exponential.h"
44 #ifdef Vc_IMPL_AVX
46 {
47  AVX::Vector<double> x = _x;
48  typedef AVX::Vector<double> V;
49  typedef V::Mask M;
50  typedef AVX::Const<double> C;
51 
52  const M overflow = x > Vc::Detail::doubleConstant< 1, 0x0006232bdd7abcd2ull, 9>(); // max log
53  const M underflow = x < Vc::Detail::doubleConstant<-1, 0x0006232bdd7abcd2ull, 9>(); // min log
54 
55  V px = floor(C::log2_e() * x + 0.5);
56  __m128i tmp = _mm256_cvttpd_epi32(px.data());
57  const SimdArray<int, V::Size> n = SSE::int_v{tmp};
58  x -= px * C::ln2_large(); //Vc::Detail::doubleConstant<1, 0x00062e4000000000ull, -1>(); // ln2
59  x -= px * C::ln2_small(); //Vc::Detail::doubleConstant<1, 0x0007f7d1cf79abcaull, -20>(); // ln2
60 
61  const double P[] = {
62  Vc::Detail::doubleConstant<1, 0x000089cdd5e44be8ull, -13>(),
63  Vc::Detail::doubleConstant<1, 0x000f06d10cca2c7eull, -6>(),
64  Vc::Detail::doubleConstant<1, 0x0000000000000000ull, 0>()
65  };
66  const double Q[] = {
67  Vc::Detail::doubleConstant<1, 0x00092eb6bc365fa0ull, -19>(),
68  Vc::Detail::doubleConstant<1, 0x0004ae39b508b6c0ull, -9>(),
69  Vc::Detail::doubleConstant<1, 0x000d17099887e074ull, -3>(),
70  Vc::Detail::doubleConstant<1, 0x0000000000000000ull, 1>()
71  };
72  const V x2 = x * x;
73  px = x * ((P[0] * x2 + P[1]) * x2 + P[2]);
74  x = px / ((((Q[0] * x2 + Q[1]) * x2 + Q[2]) * x2 + Q[3]) - px);
75  x = V::One() + 2.0 * x;
76 
77  x = ldexp(x, n); // == x * 2ⁿ
78 
79  x(overflow) = std::numeric_limits<double>::infinity();
80  x.setZero(underflow);
81 
82  return x;
83  }
84 #endif // Vc_IMPL_AVX
85 
86 inline SSE::double_v exp(SSE::double_v::AsArg _x) {
87  SSE::Vector<double> x = _x;
88  typedef SSE::Vector<double> V;
89  typedef V::Mask M;
90  typedef SSE::Const<double> C;
91 
92  const M overflow = x > Vc::Detail::doubleConstant< 1, 0x0006232bdd7abcd2ull, 9>(); // max log
93  const M underflow = x < Vc::Detail::doubleConstant<-1, 0x0006232bdd7abcd2ull, 9>(); // min log
94 
95  V px = floor(C::log2_e() * x + 0.5);
96  SimdArray<int, V::Size> n;
97  _mm_storel_epi64(reinterpret_cast<__m128i *>(&n), _mm_cvttpd_epi32(px.data()));
98  x -= px * C::ln2_large(); //Vc::Detail::doubleConstant<1, 0x00062e4000000000ull, -1>(); // ln2
99  x -= px * C::ln2_small(); //Vc::Detail::doubleConstant<1, 0x0007f7d1cf79abcaull, -20>(); // ln2
100 
101  const double P[] = {
102  Vc::Detail::doubleConstant<1, 0x000089cdd5e44be8ull, -13>(),
103  Vc::Detail::doubleConstant<1, 0x000f06d10cca2c7eull, -6>(),
104  Vc::Detail::doubleConstant<1, 0x0000000000000000ull, 0>()
105  };
106  const double Q[] = {
107  Vc::Detail::doubleConstant<1, 0x00092eb6bc365fa0ull, -19>(),
108  Vc::Detail::doubleConstant<1, 0x0004ae39b508b6c0ull, -9>(),
109  Vc::Detail::doubleConstant<1, 0x000d17099887e074ull, -3>(),
110  Vc::Detail::doubleConstant<1, 0x0000000000000000ull, 1>()
111  };
112  const V x2 = x * x;
113  px = x * ((P[0] * x2 + P[1]) * x2 + P[2]);
114  x = px / ((((Q[0] * x2 + Q[1]) * x2 + Q[2]) * x2 + Q[3]) - px);
115  x = V::One() + 2.0 * x;
116 
117  x = ldexp(x, n); // == x * 2ⁿ
118 
119  x(overflow) = std::numeric_limits<double>::infinity();
120  x.setZero(underflow);
121 
122  return x;
123  }
124 
125 #endif
126 } // namespace Vc
127 
128 #undef Vc_COMMON_MATH_H_INTERNAL
129 
130 #endif // VC_COMMON_MATH_H_
Vc::Vector< T > exp(const Vc::Vector< T > &v)
SimdArray< T, N, V, M > floor(const SimdArray< T, N, V, M > &x)
Applies the std:: floor function component-wise and concurrently.
Definition: simdarray.h:1692
Vc::Vector< T > ldexp(Vc::Vector< T > x, Vc::SimdArray< int, size()> e)
Multiply floating-point number by integral power of 2.
Vector< int > int_v
vector of signed integers
Definition: vector.h:59
Vector< double > double_v
vector of double precision
Definition: vector.h:55
constexpr VectorSpecialInitializerOne One
The special object Vc::One can be used to construct Vector and Mask objects initialized to one/true...
Definition: types.h:89