Vc  1.0.0-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 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>
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)
Vc::Vector< T > ldexp(Vc::Vector< T > x, Vc::SimdArray< int, size()> e)
Multiply floating-point number by integral power of 2.