ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
math.h
Go to the documentation of this file.
1 /* This file is part of the Vc library.
2 
3  Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
4 
5  Vc is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as
7  published by the Free Software Foundation, either version 3 of
8  the License, or (at your option) any later version.
9 
10  Vc is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with Vc. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #ifndef VC_SCALAR_MATH_H
21 #define VC_SCALAR_MATH_H
22 
23 #include "macros.h"
24 
25 namespace ROOT {
26 namespace Vc
27 {
28 namespace Scalar
29 {
30 
31 #define VC_MINMAX(V) \
32 static Vc_ALWAYS_INLINE V min(const V &x, const V &y) { return V(std::min(x.data(), y.data())); } \
33 static Vc_ALWAYS_INLINE V max(const V &x, const V &y) { return V(std::max(x.data(), y.data())); }
35 #undef VC_MINMAX
36 
37 template<typename T> static Vc_ALWAYS_INLINE Vector<T> sqrt (const Vector<T> &x)
38 {
39  return Vector<T>(std::sqrt(x.data()));
40 }
41 
42 template<typename T> static Vc_ALWAYS_INLINE Vector<T> rsqrt(const Vector<T> &x)
43 {
44  const typename Vector<T>::EntryType one = 1; return Vector<T>(one / std::sqrt(x.data()));
45 }
46 
47 template<typename T> static Vc_ALWAYS_INLINE Vector<T> abs (const Vector<T> &x)
48 {
49  return Vector<T>(std::abs(x.data()));
50 }
51 
52 template<> Vc_ALWAYS_INLINE int_v abs(const int_v &x) { return x < 0 ? -x : x; }
53 template<> Vc_ALWAYS_INLINE uint_v abs(const uint_v &x) { return x; }
54 template<> Vc_ALWAYS_INLINE short_v abs(const short_v &x) { return x < 0 ? -x : x; }
55 template<> Vc_ALWAYS_INLINE ushort_v abs(const ushort_v &x) { return x; }
56 
57 template<typename T> static Vc_ALWAYS_INLINE void sincos(const Vector<T> &x, Vector<T> *sin, Vector<T> *cos)
58 {
59 #if (defined(VC_CLANG) && VC_HAS_BUILTIN(__builtin_sincosf)) || (!defined(VC_CLANG) && defined(__GNUC__) && !defined(_WIN32))
60  __builtin_sincosf(x.data(), &sin->data(), &cos->data());
61 #elif defined(_GNU_SOURCE)
62  sincosf(x.data(), &sin->data(), &cos->data());
63 #else
64  sin->data() = std::sin(x.data());
65  cos->data() = std::cos(x.data());
66 #endif
67 }
68 
70 {
71 #if (defined(VC_CLANG) && VC_HAS_BUILTIN(__builtin_sincos)) || (!defined(VC_CLANG) && defined(__GNUC__) && !defined(_WIN32))
72  __builtin_sincos(x.data(), &sin->data(), &cos->data());
73 #elif defined(_GNU_SOURCE)
74  ::sincos(x.data(), &sin->data(), &cos->data());
75 #else
76  sin->data() = std::sin(x.data());
77  cos->data() = std::cos(x.data());
78 #endif
79 }
80 
81 template<typename T> static Vc_ALWAYS_INLINE Vector<T> sin (const Vector<T> &x)
82 {
83  return Vector<T>(std::sin(x.data()));
84 }
85 
86 template<typename T> static Vc_ALWAYS_INLINE Vector<T> asin (const Vector<T> &x)
87 {
88  return Vector<T>(std::asin(x.data()));
89 }
90 
91 template<typename T> static Vc_ALWAYS_INLINE Vector<T> cos (const Vector<T> &x)
92 {
93  return Vector<T>(std::cos(x.data()));
94 }
95 
96 template<typename T> static Vc_ALWAYS_INLINE Vector<T> log (const Vector<T> &x)
97 {
98  return Vector<T>(std::log(x.data()));
99 }
100 
101 template<typename T> static Vc_ALWAYS_INLINE Vector<T> log10(const Vector<T> &x)
102 {
103  return Vector<T>(std::log10(x.data()));
104 }
105 
106 #if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) || defined(_ISOC99_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
107 static Vc_ALWAYS_INLINE double_v log2(double_v::AsArg x) { return double_v(::log2 (x.data())); }
108 static Vc_ALWAYS_INLINE sfloat_v log2(sfloat_v::AsArg x) { return sfloat_v(::log2f(x.data())); }
109 static Vc_ALWAYS_INLINE float_v log2( float_v::AsArg x) { return float_v(::log2f(x.data())); }
110 #else
111 namespace {
112 template<typename T> static _VC_CONSTEXPR T c_ln2() { return Vc_buildFloat(1, 0x317218, -1); } // .693147182464599609375
113 template<> _VC_CONSTEXPR double c_ln2() { return Vc_buildDouble(1, 0x62E42FEFA39EFull, -1); } // .69314718055994528622676398299518041312694549560546875
114 }
115 #define VC_LOG2(V) \
116 static Vc_ALWAYS_INLINE V log2(const V &x) \
117 { \
118  return V(std::log(x.data()) / c_ln2<V::EntryType>()); \
119 }
121 #undef VC_LOG2
122 #endif
123 
124 template<typename T> static Vc_ALWAYS_INLINE Vector<T> exp (const Vector<T> &x)
125 {
126  return Vector<T>(std::exp(x.data()));
127 }
128 
129 template<typename T> static Vc_ALWAYS_INLINE Vector<T> atan (const Vector<T> &x)
130 {
131  return Vector<T>(std::atan( x.data() ));
132 }
133 
134 template<typename T> static Vc_ALWAYS_INLINE Vector<T> atan2(const Vector<T> &x, const Vector<T> &y)
135 {
136  return Vector<T>(std::atan2( x.data(), y.data() ));
137 }
138 
139 template<typename T> static Vc_ALWAYS_INLINE Vector<T> trunc(const Vector<T> &x)
140 {
141 #if __cplusplus >= 201103 /*C++11*/
142  return std::trunc(x.data());
143 #else
144  return x.data() > 0 ? std::floor(x.data()) : std::ceil(x.data());
145 #endif
146 }
147 
148 template<typename T> static Vc_ALWAYS_INLINE Vector<T> floor(const Vector<T> &x)
149 {
150  return Vector<T>(std::floor(x.data()));
151 }
152 
153 template<typename T> static Vc_ALWAYS_INLINE Vector<T> ceil(const Vector<T> &x)
154 {
155  return Vector<T>(std::ceil(x.data()));
156 }
157 
158 template<typename T> static Vc_ALWAYS_INLINE Vector<T> round(const Vector<T> &x)
159 {
160  return x;
161 }
162 
163 namespace
164 {
165  template<typename T> bool _realIsEvenHalf(T x) {
166  const T two = 2;
167  const T half = 0.5;
168  const T f = std::floor(x * half) * two;
169  return (x - f) == half;
170  }
171 } // namespace
173 {
174  return float_v(std::floor(x.data() + 0.5f) - (_realIsEvenHalf(x.data()) ? 1.f : 0.f));
175 }
176 
178 {
179  return sfloat_v(std::floor(x.data() + 0.5f) - (_realIsEvenHalf(x.data()) ? 1.f : 0.f));
180 }
181 
183 {
184  return double_v(std::floor(x.data() + 0.5 ) - (_realIsEvenHalf(x.data()) ? 1. : 0. ));
185 }
186 
187 template<typename T> static Vc_ALWAYS_INLINE Vector<T> reciprocal(const Vector<T> &x)
188 {
189  const typename Vector<T>::EntryType one = 1; return Vector<T>(one / x.data());
190 }
191 
192 #ifdef isfinite
193 #undef isfinite
194 #endif
195 #ifdef isnan
196 #undef isnan
197 #endif
198 template<typename T> static Vc_ALWAYS_INLINE typename Vector<T>::Mask isfinite(const Vector<T> &x)
199 {
200  return typename Vector<T>::Mask(
201 #ifdef _MSC_VER
202  !!_finite(x.data())
203 #elif defined(__INTEL_COMPILER)
204  ::isfinite(x.data())
205 #else
206  std::isfinite(x.data())
207 #endif
208  );
209 }
210 
211 template<typename T> static Vc_ALWAYS_INLINE typename Vector<T>::Mask isnan(const Vector<T> &x)
212 {
213  return typename Vector<T>::Mask(
214 #ifdef _MSC_VER
215  !!_isnan(x.data())
216 #elif defined(__INTEL_COMPILER)
217  ::isnan(x.data())
218 #else
219  std::isnan(x.data())
220 #endif
221  );
222 }
223 
225  return float_v(::frexpf(x.data(), &e->data()));
226 }
228  return double_v(::frexp(x.data(), &e->data()));
229 }
231  int ee;
232  const float r = ::frexpf(x.data(), &ee);
233  e->data() = ee;
234  return sfloat_v(r);
235 }
236 
238  return float_v(::ldexpf(x.data(), e.data()));
239 }
241  return double_v(::ldexp(x.data(), e.data()));
242 }
244  return sfloat_v(::ldexpf(x.data(), e.data()));
245 }
246 
247 } // namespace Scalar
248 } // namespace Vc
249 } // namespace ROOT
250 
251 #include "undomacros.h"
252 
253 #endif // VC_SCALAR_MATH_H
#define VC_ALL_VECTOR_TYPES(macro)
Definition: macros.h:360
static Vc_ALWAYS_INLINE Vector< T > log10(const Vector< T > &x)
Definition: math.h:101
Vector< float > float_v
Definition: vector.h:331
static Vc_ALWAYS_INLINE Vector< T > log(const Vector< T > &x)
Definition: math.h:96
Vc_ALWAYS_INLINE ushort_v abs(const ushort_v &x)
Definition: math.h:55
double cos(double)
TFile * f
TTree * T
double sqrt(double)
Double_t x[n]
Definition: legend1.C:17
static Vc_ALWAYS_INLINE Vector< T > floor(const Vector< T > &x)
Definition: math.h:148
Scalar::Mask< 1u > Mask
Definition: vector.h:55
double log10(double)
static Vc_ALWAYS_INLINE Vector< T >::Mask isfinite(const Vector< T > &x)
Definition: math.h:198
static Vc_ALWAYS_INLINE Vector< T > sqrt(const Vector< T > &x)
Definition: math.h:37
static Vc_ALWAYS_INLINE Vector< T > asin(const Vector< T > &x)
Definition: math.h:86
double sin(double)
static Vc_ALWAYS_INLINE Vector< T >::Mask isnan(const Vector< T > &x)
Definition: math.h:211
static Vc_ALWAYS_INLINE Vector< T > reciprocal(const Vector< T > &x)
Definition: math.h:187
int isnan(double)
#define Vc_buildDouble(sign, mantissa, exponent)
Definition: macros.h:283
ROOT::R::TRInterface & r
Definition: Object.C:4
VECTOR_NAMESPACE::double_v double_v
Definition: vector.h:80
#define VC_ALL_FLOAT_VECTOR_TYPES(macro)
Definition: macros.h:359
Vector< T > AsArg
Definition: vector.h:56
Vector< sfloat > sfloat_v
Definition: vector.h:332
#define Scalar
Definition: global.h:83
static Vc_ALWAYS_INLINE Vector< T > round(const Vector< T > &x)
Definition: math.h:158
static Vc_ALWAYS_INLINE Vector< T > cos(const Vector< T > &x)
Definition: math.h:91
double floor(double)
double asin(double)
static Vc_ALWAYS_INLINE Vector< T > exp(const Vector< T > &x)
Definition: math.h:124
static Vc_ALWAYS_INLINE Vector< T > rsqrt(const Vector< T > &x)
Definition: math.h:42
#define Vc_ALWAYS_INLINE
Definition: macros.h:130
static Vc_ALWAYS_INLINE Vector< T > sin(const Vector< T > &x)
Definition: math.h:81
Vector< double > double_v
Definition: vector.h:330
#define Vc_buildFloat(sign, mantissa, exponent)
Definition: macros.h:294
Vc_ALWAYS_INLINE Vector< float > ldexp(Vector< float > x, Vector< int > e)
Definition: math.h:237
static Vc_ALWAYS_INLINE Vector< T > ceil(const Vector< T > &x)
Definition: math.h:153
static Vc_ALWAYS_INLINE Vector< T > trunc(const Vector< T > &x)
Definition: math.h:139
double atan2(double, double)
Double_t y[n]
Definition: legend1.C:17
double atan(double)
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: math.h:47
static Vc_ALWAYS_INLINE Vector< T > atan(const Vector< T > &x)
Definition: math.h:129
static Vc_ALWAYS_INLINE void sincos(const Vector< T > &x, Vector< T > *sin, Vector< T > *cos)
Definition: math.h:57
#define _VC_CONSTEXPR
Definition: macros.h:154
Vector< T > trunc(VC_ALIGNED_PARAMETER(Vector< T >) _v)
Definition: math.h:150
Vc_ALWAYS_INLINE Vector< float > frexp(Vector< float > x, Vector< int > *e)
Definition: math.h:224
static Vc_ALWAYS_INLINE Vector< T > atan2(const Vector< T > &x, const Vector< T > &y)
Definition: math.h:134
double ceil(double)
double exp(double)
DetermineEntryType< T >::Type EntryType
Definition: vector.h:49
double log(double)
#define VC_MINMAX(V)
Definition: math.h:31
#define VC_LOG2(V)
Definition: math.h:115
Vc_ALWAYS_INLINE EntryType & data()
Definition: vector.h:58