1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3 * Martin Renou *
4 * Copyright (c) QuantStack *
5 * Copyright (c) Serge Guelton *
6 * *
7 * Distributed under the terms of the BSD 3-Clause License. *
8 * *
9 * The full license is in the file LICENSE, distributed with this software. *
10 ****************************************************************************/
11
12#ifndef XSIMD_NUMERICAL_CONSTANT_HPP
13#define XSIMD_NUMERICAL_CONSTANT_HPP
14
15#include <limits>
16
17#include "../types/xsimd_utils.hpp"
18
19namespace xsimd
20{
21
22 namespace constants
23 {
24
25#define XSIMD_DEFINE_CONSTANT(NAME, SINGLE, DOUBLE) \
26 template <class T> \
27 inline T NAME() noexcept \
28 { \
29 return T(NAME<typename T::value_type>()); \
30 } \
31 template <> \
32 inline float NAME<float>() noexcept \
33 { \
34 return SINGLE; \
35 } \
36 template <> \
37 inline double NAME<double>() noexcept \
38 { \
39 return DOUBLE; \
40 }
41
42#define XSIMD_DEFINE_CONSTANT_HEX(NAME, SINGLE, DOUBLE) \
43 template <class T> \
44 inline T NAME() noexcept \
45 { \
46 return T(NAME<typename T::value_type>()); \
47 } \
48 template <> \
49 inline float NAME<float>() noexcept \
50 { \
51 return bit_cast<float>((uint32_t)SINGLE); \
52 } \
53 template <> \
54 inline double NAME<double>() noexcept \
55 { \
56 return bit_cast<double>((uint64_t)DOUBLE); \
57 }
58
59 XSIMD_DEFINE_CONSTANT(infinity, (std::numeric_limits<float>::infinity()), (std::numeric_limits<double>::infinity()))
60 XSIMD_DEFINE_CONSTANT(invlog_2, 1.442695040888963407359924681001892137426645954152986f, 1.442695040888963407359924681001892137426645954152986)
61 XSIMD_DEFINE_CONSTANT_HEX(invlog_2hi, 0x3fb8b000, 0x3ff7154765200000)
62 XSIMD_DEFINE_CONSTANT_HEX(invlog_2lo, 0xb9389ad4, 0x3de705fc2eefa200)
63 XSIMD_DEFINE_CONSTANT(invlog10_2, 3.32192809488736234787031942949f, 3.32192809488736234787031942949)
64 XSIMD_DEFINE_CONSTANT_HEX(invpi, 0x3ea2f983, 0x3fd45f306dc9c883)
65 XSIMD_DEFINE_CONSTANT(log_2, 0.6931471805599453094172321214581765680755001343602553f, 0.6931471805599453094172321214581765680755001343602553)
66 XSIMD_DEFINE_CONSTANT_HEX(log_2hi, 0x3f318000, 0x3fe62e42fee00000)
67 XSIMD_DEFINE_CONSTANT_HEX(log_2lo, 0xb95e8083, 0x3dea39ef35793c76)
68 XSIMD_DEFINE_CONSTANT_HEX(log10_2hi, 0x3e9a0000, 0x3fd3440000000000)
69 XSIMD_DEFINE_CONSTANT_HEX(log10_2lo, 0x39826a14, 0x3ed3509f79fef312)
70 XSIMD_DEFINE_CONSTANT_HEX(logeps, 0xc17f1402, 0xc04205966f2b4f12)
71 XSIMD_DEFINE_CONSTANT_HEX(logpi, 0x3f928682, 0x3ff250d048e7a1bd)
72 XSIMD_DEFINE_CONSTANT_HEX(logsqrt2pi, 0x3f6b3f8e, 0x3fed67f1c864beb5)
73 XSIMD_DEFINE_CONSTANT(maxflint, 16777216.0f, 9007199254740992.0)
74 XSIMD_DEFINE_CONSTANT(maxlog, 88.3762626647949f, 709.78271289338400)
75 XSIMD_DEFINE_CONSTANT(maxlog2, 127.0f, 1023.)
76 XSIMD_DEFINE_CONSTANT(maxlog10, 38.23080825805664f, 308.2547155599167)
77 XSIMD_DEFINE_CONSTANT_HEX(mediumpi, 0x43490fdb, 0x412921fb54442d18)
78 XSIMD_DEFINE_CONSTANT(minlog, -88.3762626647949f, -708.3964185322641)
79 XSIMD_DEFINE_CONSTANT(minlog2, -127.0f, -1023.)
80 XSIMD_DEFINE_CONSTANT(minlog10, -37.89999771118164f, -308.2547155599167)
81 XSIMD_DEFINE_CONSTANT(minusinfinity, (-infinity<float>()), (-infinity<double>()))
82 XSIMD_DEFINE_CONSTANT(minuszero, -0.0f, -0.0)
83 XSIMD_DEFINE_CONSTANT_HEX(nan, 0xffffffff, 0xffffffffffffffff)
84 XSIMD_DEFINE_CONSTANT_HEX(oneosqrteps, 0x453504f3, 0x4190000000000000)
85 XSIMD_DEFINE_CONSTANT_HEX(oneotwoeps, 0x4a800000, 0x4320000000000000)
86 XSIMD_DEFINE_CONSTANT_HEX(pi, 0x40490fdb, 0x400921fb54442d18)
87 XSIMD_DEFINE_CONSTANT_HEX(pio_2lo, 0xb33bbd2e, 0x3c91a62633145c07)
88 XSIMD_DEFINE_CONSTANT_HEX(pio_4lo, 0xb2bbbd2e, 0x3c81a62633145c07)
89 XSIMD_DEFINE_CONSTANT_HEX(pio2, 0x3fc90fdb, 0x3ff921fb54442d18)
90 XSIMD_DEFINE_CONSTANT_HEX(pio2_1, 0x3fc90f80, 0x3ff921fb54400000)
91 XSIMD_DEFINE_CONSTANT_HEX(pio2_1t, 0x37354443, 0x3dd0b4611a626331)
92 XSIMD_DEFINE_CONSTANT_HEX(pio2_2, 0x37354400, 0x3dd0b4611a600000)
93 XSIMD_DEFINE_CONSTANT_HEX(pio2_2t, 0x2e85a308, 0x3ba3198a2e037073)
94 XSIMD_DEFINE_CONSTANT_HEX(pio2_3, 0x2e85a300, 0x3ba3198a2e000000)
95 XSIMD_DEFINE_CONSTANT_HEX(pio2_3t, 0x248d3132, 0x397b839a252049c1)
96 XSIMD_DEFINE_CONSTANT_HEX(pio4, 0x3f490fdb, 0x3fe921fb54442d18)
97 XSIMD_DEFINE_CONSTANT_HEX(signmask, 0x80000000, 0x8000000000000000)
98 XSIMD_DEFINE_CONSTANT(smallestposval, std::numeric_limits<float>::min(), std::numeric_limits<double>::min())
99 XSIMD_DEFINE_CONSTANT_HEX(sqrt_2pi, 0x40206c99, 0x40040d931ff62704)
100 XSIMD_DEFINE_CONSTANT_HEX(sqrteps, 0x39b504f3, 0x3e50000000000000)
101 XSIMD_DEFINE_CONSTANT_HEX(tanpio8, 0x3ed413cd, 0x3fda827999fcef31)
102 XSIMD_DEFINE_CONSTANT_HEX(tan3pio8, 0x401a827a, 0x4003504f333f9de6)
103 XSIMD_DEFINE_CONSTANT_HEX(twentypi, 0x427b53d1, 0x404f6a7a2955385e)
104 XSIMD_DEFINE_CONSTANT_HEX(twoopi, 0x3f22f983, 0x3fe45f306dc9c883)
105 XSIMD_DEFINE_CONSTANT(twotonmb, 8388608.0f, 4503599627370496.0)
106 XSIMD_DEFINE_CONSTANT_HEX(twotonmbo3, 0x3ba14518, 0x3ed428a2f98d7286)
107
108#undef XSIMD_DEFINE_CONSTANT
109#undef XSIMD_DEFINE_CONSTANT_HEX
110
111 template <class T>
112 constexpr T allbits() noexcept;
113
114 template <class T>
115 constexpr as_integer_t<T> mask1frexp() noexcept;
116
117 template <class T>
118 constexpr as_integer_t<T> mask2frexp() noexcept;
119
120 template <class T>
121 constexpr as_integer_t<T> maxexponent() noexcept;
122
123 template <class T>
124 constexpr as_integer_t<T> maxexponentm1() noexcept;
125
126 template <class T>
127 constexpr int32_t nmb() noexcept;
128
129 template <class T>
130 constexpr T zero() noexcept;
131
132 template <class T>
133 constexpr T minvalue() noexcept;
134
135 template <class T>
136 constexpr T maxvalue() noexcept;
137
138 /**************************
139 * allbits implementation *
140 **************************/
141
142 namespace detail
143 {
144 template <class T, bool = std::is_integral<T>::value>
145 struct allbits_impl
146 {
147 static constexpr T get_value() noexcept
148 {
149 return T(~0);
150 }
151 };
152
153 template <class T>
154 struct allbits_impl<T, false>
155 {
156 static constexpr T get_value() noexcept
157 {
158 return nan<T>();
159 }
160 };
161 }
162
163 template <class T>
164 inline constexpr T allbits() noexcept
165 {
166 return T(detail::allbits_impl<typename T::value_type>::get_value());
167 }
168
169 /*****************************
170 * mask1frexp implementation *
171 *****************************/
172
173 template <class T>
174 inline constexpr as_integer_t<T> mask1frexp() noexcept
175 {
176 return as_integer_t<T>(mask1frexp<typename T::value_type>());
177 }
178
179 template <>
180 inline constexpr int32_t mask1frexp<float>() noexcept
181 {
182 return 0x7f800000;
183 }
184
185 template <>
186 inline constexpr int64_t mask1frexp<double>() noexcept
187 {
188 return 0x7ff0000000000000;
189 }
190
191 /*****************************
192 * mask2frexp implementation *
193 *****************************/
194
195 template <class T>
196 inline constexpr as_integer_t<T> mask2frexp() noexcept
197 {
198 return as_integer_t<T>(mask2frexp<typename T::value_type>());
199 }
200
201 template <>
202 inline constexpr int32_t mask2frexp<float>() noexcept
203 {
204 return 0x3f000000;
205 }
206
207 template <>
208 inline constexpr int64_t mask2frexp<double>() noexcept
209 {
210 return 0x3fe0000000000000;
211 }
212
213 /******************************
214 * maxexponent implementation *
215 ******************************/
216
217 template <class T>
218 inline constexpr as_integer_t<T> maxexponent() noexcept
219 {
220 return as_integer_t<T>(maxexponent<typename T::value_type>());
221 }
222
223 template <>
224 inline constexpr int32_t maxexponent<float>() noexcept
225 {
226 return 127;
227 }
228
229 template <>
230 inline constexpr int64_t maxexponent<double>() noexcept
231 {
232 return 1023;
233 }
234
235 /******************************
236 * maxexponent implementation *
237 ******************************/
238
239 template <class T>
240 inline constexpr as_integer_t<T> maxexponentm1() noexcept
241 {
242 return as_integer_t<T>(maxexponentm1<typename T::value_type>());
243 }
244
245 template <>
246 inline constexpr int32_t maxexponentm1<float>() noexcept
247 {
248 return 126;
249 }
250
251 template <>
252 inline constexpr int64_t maxexponentm1<double>() noexcept
253 {
254 return 1022;
255 }
256
257 /**********************
258 * nmb implementation *
259 **********************/
260
261 template <class T>
262 inline constexpr int32_t nmb() noexcept
263 {
264 return nmb<typename T::value_type>();
265 }
266
267 template <>
268 inline constexpr int32_t nmb<float>() noexcept
269 {
270 return 23;
271 }
272
273 template <>
274 inline constexpr int32_t nmb<double>() noexcept
275 {
276 return 52;
277 }
278
279 /***********************
280 * zero implementation *
281 ***********************/
282
283 template <class T>
284 inline constexpr T zero() noexcept
285 {
286 return T(typename T::value_type(0));
287 }
288
289 /***************************
290 * minvalue implementation *
291 ***************************/
292
293 namespace detail
294 {
295 template <class T>
296 struct minvalue_impl
297 {
298 static constexpr T get_value() noexcept
299 {
300 return std::numeric_limits<typename T::value_type>::min();
301 }
302 };
303
304 template <class T>
305 struct minvalue_common
306 {
307 static constexpr T get_value() noexcept
308 {
309 return std::numeric_limits<T>::min();
310 }
311 };
312
313 template <>
314 struct minvalue_impl<int8_t> : minvalue_common<int8_t>
315 {
316 };
317 template <>
318 struct minvalue_impl<uint8_t> : minvalue_common<uint8_t>
319 {
320 };
321 template <>
322 struct minvalue_impl<int16_t> : minvalue_common<int16_t>
323 {
324 };
325 template <>
326 struct minvalue_impl<uint16_t> : minvalue_common<uint16_t>
327 {
328 };
329 template <>
330 struct minvalue_impl<int32_t> : minvalue_common<int32_t>
331 {
332 };
333 template <>
334 struct minvalue_impl<uint32_t> : minvalue_common<uint32_t>
335 {
336 };
337 template <>
338 struct minvalue_impl<int64_t> : minvalue_common<int64_t>
339 {
340 };
341 template <>
342 struct minvalue_impl<uint64_t> : minvalue_common<uint64_t>
343 {
344 };
345
346 template <>
347 struct minvalue_impl<float>
348 {
349 static float get_value() noexcept
350 {
351 return bit_cast<float>(val: (uint32_t)0xff7fffff);
352 }
353 };
354
355 template <>
356 struct minvalue_impl<double>
357 {
358 static double get_value() noexcept
359 {
360 return bit_cast<double>(val: (uint64_t)0xffefffffffffffff);
361 }
362 };
363 }
364
365 template <class T>
366 inline constexpr T minvalue() noexcept
367 {
368 return T(detail::minvalue_impl<typename T::value_type>::get_value());
369 }
370
371 /***************************
372 * maxvalue implementation *
373 ***************************/
374
375 template <class T>
376 inline constexpr T maxvalue() noexcept
377 {
378 return T(std::numeric_limits<typename T::value_type>::max());
379 }
380 }
381
382}
383
384#endif
385