Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
exponential.h
1/* This file is part of the Vc library. {{{
2Copyright © 2012-2015 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, 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
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON 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
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26-------------------------------------------------------------------
27
28The exp implementation is derived from Cephes, which carries the
29following Copyright notice:
30
31Cephes Math Library Release 2.2: June, 1992
32Copyright 1984, 1987, 1989 by Stephen L. Moshier
33Direct inquiries to 30 Frost Street, Cambridge, MA 02140
34
35}}}*/
36
37#ifdef Vc_COMMON_MATH_H_INTERNAL
38
39constexpr float log2_e = 1.44269504088896341f;
40
41// These constants are adjusted to account for single-precision floating point.
42// The original are for double precision:
43//
44// constexpr float MAXLOGF = 88.72283905206835f;
45// constexpr float MINLOGF = -103.278929903431851103f; /* log(2^-149) */
46
47constexpr float MAXLOGF = 88.722831726074219f; /* log(2^127.99998474121094f) */
48constexpr float MINLOGF = -88.029685974121094f; /* log(2^-126.99999237060547f) */
49constexpr float MAXNUMF = 3.4028234663852885981170418348451692544e38f;
50
51template <typename Abi, typename = enable_if<std::is_same<Abi, VectorAbi::Sse>::value ||
52 std::is_same<Abi, VectorAbi::Avx>::value>>
53inline Vector<float, detail::not_fixed_size_abi<Abi>> exp(Vector<float, Abi> x)
54{
55 using V = Vector<float, Abi>;
56 typedef typename V::Mask M;
57 typedef Detail::Const<float, Abi> C;
58
59 const M overflow = x > MAXLOGF;
60 const M underflow = x < MINLOGF;
61
62 // log₂(eˣ) = x * log₂(e) * log₂(2)
63 // = log₂(2^(x * log₂(e)))
64 // => eˣ = 2^(x * log₂(e))
65 // => n = ⌊x * log₂(e) + ½⌋
66 // => y = x - n * ln(2) | recall that: ln(2) * log₂(e) == 1
67 // <=> eˣ = 2ⁿ * eʸ
68 V z = floor(C::log2_e() * x + 0.5f);
69 const auto n = static_cast<Vc::SimdArray<int, V::Size>>(z);
70 x -= z * C::ln2_large();
71 x -= z * C::ln2_small();
72
73 /* Theoretical peak relative error in [-0.5, +0.5] is 4.2e-9. */
74 z = ((((( 1.9875691500E-4f * x
75 + 1.3981999507E-3f) * x
76 + 8.3334519073E-3f) * x
77 + 4.1665795894E-2f) * x
78 + 1.6666665459E-1f) * x
79 + 5.0000001201E-1f) * (x * x)
80 + x
81 + 1.0f;
82
83 x = ldexp(z, n); // == z * 2ⁿ
84
85 x(overflow) = std::numeric_limits<typename V::EntryType>::infinity();
86 x.setZero(underflow);
87
88 return x;
89 }
90
91#endif // Vc_COMMON_MATH_H_INTERNAL
Data-parallel arithmetic type with user-defined number of elements.
Definition simdarray.h:617