summaryrefslogtreecommitdiffstats
path: root/third_party/xsimd/include/xsimd/arch/xsimd_constants.hpp
blob: 22dd5d3e3030df9e4b25890ffcbb457b59a72bd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
 * Martin Renou                                                             *
 * Copyright (c) QuantStack                                                 *
 * Copyright (c) Serge Guelton                                              *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#ifndef XSIMD_NUMERICAL_CONSTANT_HPP
#define XSIMD_NUMERICAL_CONSTANT_HPP

#include <limits>

#include "../types/xsimd_utils.hpp"

namespace xsimd
{

    namespace constants
    {

#define XSIMD_DEFINE_CONSTANT(NAME, SINGLE, DOUBLE) \
    template <class T>                              \
    inline T NAME() noexcept                        \
    {                                               \
        return T(NAME<typename T::value_type>());   \
    }                                               \
    template <>                                     \
    inline float NAME<float>() noexcept             \
    {                                               \
        return SINGLE;                              \
    }                                               \
    template <>                                     \
    inline double NAME<double>() noexcept           \
    {                                               \
        return DOUBLE;                              \
    }

#define XSIMD_DEFINE_CONSTANT_HEX(NAME, SINGLE, DOUBLE) \
    template <class T>                                  \
    inline T NAME() noexcept                            \
    {                                                   \
        return T(NAME<typename T::value_type>());       \
    }                                                   \
    template <>                                         \
    inline float NAME<float>() noexcept                 \
    {                                                   \
        return bit_cast<float>((uint32_t)SINGLE);       \
    }                                                   \
    template <>                                         \
    inline double NAME<double>() noexcept               \
    {                                                   \
        return bit_cast<double>((uint64_t)DOUBLE);      \
    }

// Under fast-math, GCC might replace signmask (minus zero) by zero
#if defined(__FAST_MATH__) && defined(__GNUC__) && !defined(__clang__)
#pragma GCC push_options
#pragma GCC optimize("signed-zeros")
#endif
        XSIMD_DEFINE_CONSTANT(infinity, (std::numeric_limits<float>::infinity()), (std::numeric_limits<double>::infinity()))
        XSIMD_DEFINE_CONSTANT(invlog_2, 1.442695040888963407359924681001892137426645954152986f, 1.442695040888963407359924681001892137426645954152986)
        XSIMD_DEFINE_CONSTANT_HEX(invlog_2hi, 0x3fb8b000, 0x3ff7154765200000)
        XSIMD_DEFINE_CONSTANT_HEX(invlog_2lo, 0xb9389ad4, 0x3de705fc2eefa200)
        XSIMD_DEFINE_CONSTANT(invlog10_2, 3.32192809488736234787031942949f, 3.32192809488736234787031942949)
        XSIMD_DEFINE_CONSTANT_HEX(invpi, 0x3ea2f983, 0x3fd45f306dc9c883)
        XSIMD_DEFINE_CONSTANT(log_2, 0.6931471805599453094172321214581765680755001343602553f, 0.6931471805599453094172321214581765680755001343602553)
        XSIMD_DEFINE_CONSTANT_HEX(log_2hi, 0x3f318000, 0x3fe62e42fee00000)
        XSIMD_DEFINE_CONSTANT_HEX(log_2lo, 0xb95e8083, 0x3dea39ef35793c76)
        XSIMD_DEFINE_CONSTANT_HEX(log10_2hi, 0x3e9a0000, 0x3fd3440000000000)
        XSIMD_DEFINE_CONSTANT_HEX(log10_2lo, 0x39826a14, 0x3ed3509f79fef312)
        XSIMD_DEFINE_CONSTANT_HEX(logeps, 0xc17f1402, 0xc04205966f2b4f12)
        XSIMD_DEFINE_CONSTANT_HEX(logpi, 0x3f928682, 0x3ff250d048e7a1bd)
        XSIMD_DEFINE_CONSTANT_HEX(logsqrt2pi, 0x3f6b3f8e, 0x3fed67f1c864beb5)
        XSIMD_DEFINE_CONSTANT(maxflint, 16777216.0f, 9007199254740992.0)
        XSIMD_DEFINE_CONSTANT(maxlog, 88.3762626647949f, 709.78271289338400)
        XSIMD_DEFINE_CONSTANT(maxlog2, 127.0f, 1023.)
        XSIMD_DEFINE_CONSTANT(maxlog10, 38.23080825805664f, 308.2547155599167)
        XSIMD_DEFINE_CONSTANT_HEX(mediumpi, 0x43490fdb, 0x412921fb54442d18)
        XSIMD_DEFINE_CONSTANT(minlog, -88.3762626647949f, -708.3964185322641)
        XSIMD_DEFINE_CONSTANT(minlog2, -127.0f, -1023.)
        XSIMD_DEFINE_CONSTANT(minlog10, -37.89999771118164f, -308.2547155599167)
        XSIMD_DEFINE_CONSTANT(minusinfinity, (-infinity<float>()), (-infinity<double>()))
        XSIMD_DEFINE_CONSTANT_HEX(nan, 0xffffffff, 0xffffffffffffffff)
        XSIMD_DEFINE_CONSTANT_HEX(oneosqrteps, 0x453504f3, 0x4190000000000000)
        XSIMD_DEFINE_CONSTANT_HEX(oneotwoeps, 0x4a800000, 0x4320000000000000)
        XSIMD_DEFINE_CONSTANT_HEX(pi, 0x40490fdb, 0x400921fb54442d18)
        XSIMD_DEFINE_CONSTANT_HEX(pio_2lo, 0xb33bbd2e, 0x3c91a62633145c07)
        XSIMD_DEFINE_CONSTANT_HEX(pio_4lo, 0xb2bbbd2e, 0x3c81a62633145c07)
        XSIMD_DEFINE_CONSTANT_HEX(pio2, 0x3fc90fdb, 0x3ff921fb54442d18)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_1, 0x3fc90f80, 0x3ff921fb54400000)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_1t, 0x37354443, 0x3dd0b4611a626331)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_2, 0x37354400, 0x3dd0b4611a600000)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_2t, 0x2e85a308, 0x3ba3198a2e037073)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_3, 0x2e85a300, 0x3ba3198a2e000000)
        XSIMD_DEFINE_CONSTANT_HEX(pio2_3t, 0x248d3132, 0x397b839a252049c1)
        XSIMD_DEFINE_CONSTANT_HEX(pio4, 0x3f490fdb, 0x3fe921fb54442d18)
        XSIMD_DEFINE_CONSTANT_HEX(signmask, 0x80000000, 0x8000000000000000)
        XSIMD_DEFINE_CONSTANT(smallestposval, std::numeric_limits<float>::min(), std::numeric_limits<double>::min())
        XSIMD_DEFINE_CONSTANT_HEX(sqrt_2pi, 0x40206c99, 0x40040d931ff62704)
        XSIMD_DEFINE_CONSTANT_HEX(sqrteps, 0x39b504f3, 0x3e50000000000000)
        XSIMD_DEFINE_CONSTANT_HEX(tanpio8, 0x3ed413cd, 0x3fda827999fcef31)
        XSIMD_DEFINE_CONSTANT_HEX(tan3pio8, 0x401a827a, 0x4003504f333f9de6)
        XSIMD_DEFINE_CONSTANT_HEX(twentypi, 0x427b53d1, 0x404f6a7a2955385e)
        XSIMD_DEFINE_CONSTANT_HEX(twoopi, 0x3f22f983, 0x3fe45f306dc9c883)
        XSIMD_DEFINE_CONSTANT(twotonmb, 8388608.0f, 4503599627370496.0)
        XSIMD_DEFINE_CONSTANT_HEX(twotonmbo3, 0x3ba14518, 0x3ed428a2f98d7286)
#if defined(__FAST_MATH__) && defined(__GNUC__) && !defined(__clang__)
#pragma GCC pop_options
#endif

#undef XSIMD_DEFINE_CONSTANT
#undef XSIMD_DEFINE_CONSTANT_HEX

        template <class T>
        constexpr T allbits() noexcept;

        template <class T>
        constexpr as_integer_t<T> mask1frexp() noexcept;

        template <class T>
        constexpr as_integer_t<T> mask2frexp() noexcept;

        template <class T>
        constexpr as_integer_t<T> maxexponent() noexcept;

        template <class T>
        constexpr as_integer_t<T> maxexponentm1() noexcept;

        template <class T>
        constexpr int32_t nmb() noexcept;

        template <class T>
        constexpr T zero() noexcept;

        template <class T>
        constexpr T minvalue() noexcept;

        template <class T>
        constexpr T maxvalue() noexcept;

        /**************************
         * allbits implementation *
         **************************/

        namespace detail
        {
            template <class T, bool = std::is_integral<T>::value>
            struct allbits_impl
            {
                static constexpr T get_value() noexcept
                {
                    return T(~0);
                }
            };

            template <class T>
            struct allbits_impl<T, false>
            {
                static constexpr T get_value() noexcept
                {
                    return nan<T>();
                }
            };
        }

        template <class T>
        inline constexpr T allbits() noexcept
        {
            return T(detail::allbits_impl<typename T::value_type>::get_value());
        }

        /*****************************
         * mask1frexp implementation *
         *****************************/

        template <class T>
        inline constexpr as_integer_t<T> mask1frexp() noexcept
        {
            return as_integer_t<T>(mask1frexp<typename T::value_type>());
        }

        template <>
        inline constexpr int32_t mask1frexp<float>() noexcept
        {
            return 0x7f800000;
        }

        template <>
        inline constexpr int64_t mask1frexp<double>() noexcept
        {
            return 0x7ff0000000000000;
        }

        /*****************************
         * mask2frexp implementation *
         *****************************/

        template <class T>
        inline constexpr as_integer_t<T> mask2frexp() noexcept
        {
            return as_integer_t<T>(mask2frexp<typename T::value_type>());
        }

        template <>
        inline constexpr int32_t mask2frexp<float>() noexcept
        {
            return 0x3f000000;
        }

        template <>
        inline constexpr int64_t mask2frexp<double>() noexcept
        {
            return 0x3fe0000000000000;
        }

        /******************************
         * maxexponent implementation *
         ******************************/

        template <class T>
        inline constexpr as_integer_t<T> maxexponent() noexcept
        {
            return as_integer_t<T>(maxexponent<typename T::value_type>());
        }

        template <>
        inline constexpr int32_t maxexponent<float>() noexcept
        {
            return 127;
        }

        template <>
        inline constexpr int64_t maxexponent<double>() noexcept
        {
            return 1023;
        }

        /******************************
         * maxexponent implementation *
         ******************************/

        template <class T>
        inline constexpr as_integer_t<T> maxexponentm1() noexcept
        {
            return as_integer_t<T>(maxexponentm1<typename T::value_type>());
        }

        template <>
        inline constexpr int32_t maxexponentm1<float>() noexcept
        {
            return 126;
        }

        template <>
        inline constexpr int64_t maxexponentm1<double>() noexcept
        {
            return 1022;
        }

        /**********************
         * nmb implementation *
         **********************/

        template <class T>
        inline constexpr int32_t nmb() noexcept
        {
            return nmb<typename T::value_type>();
        }

        template <>
        inline constexpr int32_t nmb<float>() noexcept
        {
            return 23;
        }

        template <>
        inline constexpr int32_t nmb<double>() noexcept
        {
            return 52;
        }

        /***********************
         * zero implementation *
         ***********************/

        template <class T>
        inline constexpr T zero() noexcept
        {
            return T(typename T::value_type(0));
        }

        /***************************
         * minvalue implementation *
         ***************************/

        namespace detail
        {
            template <class T>
            struct minvalue_impl
            {
                static constexpr T get_value() noexcept
                {
                    return std::numeric_limits<typename T::value_type>::min();
                }
            };

            template <class T>
            struct minvalue_common
            {
                static constexpr T get_value() noexcept
                {
                    return std::numeric_limits<T>::min();
                }
            };

            template <>
            struct minvalue_impl<int8_t> : minvalue_common<int8_t>
            {
            };
            template <>
            struct minvalue_impl<uint8_t> : minvalue_common<uint8_t>
            {
            };
            template <>
            struct minvalue_impl<int16_t> : minvalue_common<int16_t>
            {
            };
            template <>
            struct minvalue_impl<uint16_t> : minvalue_common<uint16_t>
            {
            };
            template <>
            struct minvalue_impl<int32_t> : minvalue_common<int32_t>
            {
            };
            template <>
            struct minvalue_impl<uint32_t> : minvalue_common<uint32_t>
            {
            };
            template <>
            struct minvalue_impl<int64_t> : minvalue_common<int64_t>
            {
            };
            template <>
            struct minvalue_impl<uint64_t> : minvalue_common<uint64_t>
            {
            };

            template <>
            struct minvalue_impl<float>
            {
                inline static float get_value() noexcept
                {
                    return bit_cast<float>((uint32_t)0xff7fffff);
                }
            };

            template <>
            struct minvalue_impl<double>
            {
                inline static double get_value() noexcept
                {
                    return bit_cast<double>((uint64_t)0xffefffffffffffff);
                }
            };
        }

        template <class T>
        constexpr T minvalue() noexcept
        {
            return T(detail::minvalue_impl<typename T::value_type>::get_value());
        }

        /***************************
         * maxvalue implementation *
         ***************************/

        template <class T>
        constexpr T maxvalue() noexcept
        {
            return T(std::numeric_limits<typename T::value_type>::max());
        }
    }

}

#endif