Vc  1.3.2-dev
SIMD Vector Classes for C++
exponential.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2012-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 The exp implementation is derived from Cephes, which carries the
29 following Copyright notice:
30 
31 Cephes Math Library Release 2.2: June, 1992
32 Copyright 1984, 1987, 1989 by Stephen L. Moshier
33 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
34 
35 }}}*/
36 
37 #ifdef Vc_COMMON_MATH_H_INTERNAL
38 
39 constexpr float log2_e = 1.44269504088896341f;
40 constexpr float MAXLOGF = 88.72283905206835f;
41 constexpr float MINLOGF = -103.278929903431851103f; /* log(2^-149) */
42 constexpr float MAXNUMF = 3.4028234663852885981170418348451692544e38f;
43 
44 template <typename Abi, typename = enable_if<std::is_same<Abi, VectorAbi::Sse>::value ||
45  std::is_same<Abi, VectorAbi::Avx>::value>>
46 inline Vector<float, Abi> exp(Vector<float, Abi> x)
47 {
48  using V = Vector<float, Abi>;
49  typedef typename V::Mask M;
50  typedef Detail::Const<float, Abi> C;
51 
52  const M overflow = x > MAXLOGF;
53  const M underflow = x < MINLOGF;
54 
55  // log₂(eˣ) = x * log₂(e) * log₂(2)
56  // = log₂(2^(x * log₂(e)))
57  // => eˣ = 2^(x * log₂(e))
58  // => n = ⌊x * log₂(e) + ½⌋
59  // => y = x - n * ln(2) | recall that: ln(2) * log₂(e) == 1
60  // <=> eˣ = 2ⁿ * eʸ
61  V z = floor(C::log2_e() * x + 0.5f);
62  const auto n = static_cast<Vc::SimdArray<int, V::Size>>(z);
63  x -= z * C::ln2_large();
64  x -= z * C::ln2_small();
65 
66  /* Theoretical peak relative error in [-0.5, +0.5] is 4.2e-9. */
67  z = ((((( 1.9875691500E-4f * x
68  + 1.3981999507E-3f) * x
69  + 8.3334519073E-3f) * x
70  + 4.1665795894E-2f) * x
71  + 1.6666665459E-1f) * x
72  + 5.0000001201E-1f) * (x * x)
73  + x
74  + 1.0f;
75 
76  x = ldexp(z, n); // == z * 2ⁿ
77 
78  x(overflow) = std::numeric_limits<typename V::EntryType>::infinity();
79  x.setZero(underflow);
80 
81  return x;
82  }
83 
84 #endif // Vc_COMMON_MATH_H_INTERNAL
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.
Data-parallel arithmetic type with user-defined number of elements.
Definition: simdarray.h:565