Vc  1.1.0
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 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the names of contributing organizations nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 -------------------------------------------------------------------
28 
29 The exp implementation is derived from Cephes, which carries the
30 following Copyright notice:
31 
32 Cephes Math Library Release 2.2: June, 1992
33 Copyright 1984, 1987, 1989 by Stephen L. Moshier
34 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
35 
36 }}}*/
37 
38 #ifdef Vc_COMMON_MATH_H_INTERNAL
39 
40 constexpr float log2_e = 1.44269504088896341f;
41 constexpr float MAXLOGF = 88.72283905206835f;
42 constexpr float MINLOGF = -103.278929903431851103f; /* log(2^-149) */
43 constexpr float MAXNUMF = 3.4028234663852885981170418348451692544e38f;
44 
45 template <typename Abi, typename = enable_if<std::is_same<Abi, VectorAbi::Sse>::value ||
46  std::is_same<Abi, VectorAbi::Avx>::value>>
47 inline Vector<float, Abi> exp(Vector<float, Abi> x)
48 {
49  using V = Vector<float, Abi>;
50  typedef typename V::Mask M;
51  typedef Detail::Const<float, Abi> C;
52 
53  const M overflow = x > MAXLOGF;
54  const M underflow = x < MINLOGF;
55 
56  // log₂(eˣ) = x * log₂(e) * log₂(2)
57  // = log₂(2^(x * log₂(e)))
58  // => eˣ = 2^(x * log₂(e))
59  // => n = ⌊x * log₂(e) + ½⌋
60  // => y = x - n * ln(2) | recall that: ln(2) * log₂(e) == 1
61  // <=> eˣ = 2ⁿ * eʸ
62  V z = floor(C::log2_e() * x + 0.5f);
63  const auto n = static_cast<Vc::SimdArray<int, V::Size>>(z);
64  x -= z * C::ln2_large();
65  x -= z * C::ln2_small();
66 
67  /* Theoretical peak relative error in [-0.5, +0.5] is 4.2e-9. */
68  z = ((((( 1.9875691500E-4f * x
69  + 1.3981999507E-3f) * x
70  + 8.3334519073E-3f) * x
71  + 4.1665795894E-2f) * x
72  + 1.6666665459E-1f) * x
73  + 5.0000001201E-1f) * (x * x)
74  + x
75  + 1.0f;
76 
77  x = ldexp(z, n); // == z * 2ⁿ
78 
79  x(overflow) = std::numeric_limits<typename V::EntryType>::infinity();
80  x.setZero(underflow);
81 
82  return x;
83  }
84 
85 #endif // Vc_COMMON_MATH_H_INTERNAL
Vc::Vector< T > exp(const Vc::Vector< T > &v)
Vc::Vector< T > ldexp(Vc::Vector< T > x, Vc::SimdArray< int, size()> e)
Multiply floating-point number by integral power of 2.
Data-parallel type with (somewhat) arbitrary number of components.
Definition: simdarray.h:475