Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 09:11:32

0001 /***************************************************************************
0002  * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
0003  * Martin Renou                                                             *
0004  * Copyright (c) QuantStack                                                 *
0005  * Copyright (c) Serge Guelton                                              *
0006  *                                                                          *
0007  * Distributed under the terms of the BSD 3-Clause License.                 *
0008  *                                                                          *
0009  * The full license is in the file LICENSE, distributed with this software. *
0010  ****************************************************************************/
0011 
0012 #ifndef XSIMD_NUMERICAL_CONSTANT_HPP
0013 #define XSIMD_NUMERICAL_CONSTANT_HPP
0014 
0015 #include <limits>
0016 
0017 #include "../types/xsimd_utils.hpp"
0018 
0019 namespace xsimd
0020 {
0021 
0022     namespace constants
0023     {
0024 
0025 #define XSIMD_DEFINE_CONSTANT(NAME, SINGLE, DOUBLE) \
0026     template <class T>                              \
0027     XSIMD_INLINE T NAME() noexcept                  \
0028     {                                               \
0029         return T(NAME<typename T::value_type>());   \
0030     }                                               \
0031     template <>                                     \
0032     XSIMD_INLINE float NAME<float>() noexcept       \
0033     {                                               \
0034         return SINGLE;                              \
0035     }                                               \
0036     template <>                                     \
0037     XSIMD_INLINE double NAME<double>() noexcept     \
0038     {                                               \
0039         return DOUBLE;                              \
0040     }
0041 
0042 #define XSIMD_DEFINE_CONSTANT_HEX(NAME, SINGLE, DOUBLE) \
0043     template <class T>                                  \
0044     XSIMD_INLINE T NAME() noexcept                      \
0045     {                                                   \
0046         return T(NAME<typename T::value_type>());       \
0047     }                                                   \
0048     template <>                                         \
0049     XSIMD_INLINE float NAME<float>() noexcept           \
0050     {                                                   \
0051         return bit_cast<float>((uint32_t)SINGLE);       \
0052     }                                                   \
0053     template <>                                         \
0054     XSIMD_INLINE double NAME<double>() noexcept         \
0055     {                                                   \
0056         return bit_cast<double>((uint64_t)DOUBLE);      \
0057     }
0058 
0059 // Under fast-math, GCC might replace signmask (minus zero) by zero
0060 #if defined(__FAST_MATH__) && defined(__GNUC__) && !defined(__clang__)
0061 #pragma GCC push_options
0062 #pragma GCC optimize("signed-zeros")
0063 #endif
0064         XSIMD_DEFINE_CONSTANT(infinity, (std::numeric_limits<float>::infinity()), (std::numeric_limits<double>::infinity()))
0065         XSIMD_DEFINE_CONSTANT(invlog_2, 1.442695040888963407359924681001892137426645954152986f, 1.442695040888963407359924681001892137426645954152986)
0066         XSIMD_DEFINE_CONSTANT_HEX(invlog_2hi, 0x3fb8b000, 0x3ff7154765200000)
0067         XSIMD_DEFINE_CONSTANT_HEX(invlog_2lo, 0xb9389ad4, 0x3de705fc2eefa200)
0068         XSIMD_DEFINE_CONSTANT(invlog10_2, 3.32192809488736234787031942949f, 3.32192809488736234787031942949)
0069         XSIMD_DEFINE_CONSTANT_HEX(invpi, 0x3ea2f983, 0x3fd45f306dc9c883)
0070         XSIMD_DEFINE_CONSTANT(log_2, 0.6931471805599453094172321214581765680755001343602553f, 0.6931471805599453094172321214581765680755001343602553)
0071         XSIMD_DEFINE_CONSTANT_HEX(log_2hi, 0x3f318000, 0x3fe62e42fee00000)
0072         XSIMD_DEFINE_CONSTANT_HEX(log_2lo, 0xb95e8083, 0x3dea39ef35793c76)
0073         XSIMD_DEFINE_CONSTANT_HEX(log10_2hi, 0x3e9a0000, 0x3fd3440000000000)
0074         XSIMD_DEFINE_CONSTANT_HEX(log10_2lo, 0x39826a14, 0x3ed3509f79fef312)
0075         XSIMD_DEFINE_CONSTANT_HEX(logeps, 0xc17f1402, 0xc04205966f2b4f12)
0076         XSIMD_DEFINE_CONSTANT_HEX(logpi, 0x3f928682, 0x3ff250d048e7a1bd)
0077         XSIMD_DEFINE_CONSTANT_HEX(logsqrt2pi, 0x3f6b3f8e, 0x3fed67f1c864beb5)
0078         XSIMD_DEFINE_CONSTANT(maxflint, 16777216.0f, 9007199254740992.0)
0079         XSIMD_DEFINE_CONSTANT(maxlog, 88.3762626647949f, 709.78271289338400)
0080         XSIMD_DEFINE_CONSTANT(maxlog2, 127.0f, 1023.)
0081         XSIMD_DEFINE_CONSTANT(maxlog10, 38.23080825805664f, 308.2547155599167)
0082         XSIMD_DEFINE_CONSTANT_HEX(mediumpi, 0x43490fdb, 0x412921fb54442d18)
0083         XSIMD_DEFINE_CONSTANT(minlog, -88.3762626647949f, -708.3964185322641)
0084         XSIMD_DEFINE_CONSTANT(minlog2, -127.0f, -1023.)
0085         XSIMD_DEFINE_CONSTANT(minlog10, -37.89999771118164f, -308.2547155599167)
0086         XSIMD_DEFINE_CONSTANT(minusinfinity, (-infinity<float>()), (-infinity<double>()))
0087         XSIMD_DEFINE_CONSTANT_HEX(nan, 0xffffffff, 0xffffffffffffffff)
0088         XSIMD_DEFINE_CONSTANT_HEX(oneosqrteps, 0x453504f3, 0x4190000000000000)
0089         XSIMD_DEFINE_CONSTANT_HEX(oneotwoeps, 0x4a800000, 0x4320000000000000)
0090         XSIMD_DEFINE_CONSTANT_HEX(pi, 0x40490fdb, 0x400921fb54442d18)
0091         XSIMD_DEFINE_CONSTANT_HEX(pio_2lo, 0xb33bbd2e, 0x3c91a62633145c07)
0092         XSIMD_DEFINE_CONSTANT_HEX(pio_4lo, 0xb2bbbd2e, 0x3c81a62633145c07)
0093         XSIMD_DEFINE_CONSTANT_HEX(pio2, 0x3fc90fdb, 0x3ff921fb54442d18)
0094         XSIMD_DEFINE_CONSTANT_HEX(pio2_1, 0x3fc90f80, 0x3ff921fb54400000)
0095         XSIMD_DEFINE_CONSTANT_HEX(pio2_1t, 0x37354443, 0x3dd0b4611a626331)
0096         XSIMD_DEFINE_CONSTANT_HEX(pio2_2, 0x37354400, 0x3dd0b4611a600000)
0097         XSIMD_DEFINE_CONSTANT_HEX(pio2_2t, 0x2e85a308, 0x3ba3198a2e037073)
0098         XSIMD_DEFINE_CONSTANT_HEX(pio2_3, 0x2e85a300, 0x3ba3198a2e000000)
0099         XSIMD_DEFINE_CONSTANT_HEX(pio2_3t, 0x248d3132, 0x397b839a252049c1)
0100         XSIMD_DEFINE_CONSTANT_HEX(pio4, 0x3f490fdb, 0x3fe921fb54442d18)
0101         XSIMD_DEFINE_CONSTANT_HEX(signmask, 0x80000000, 0x8000000000000000)
0102         XSIMD_DEFINE_CONSTANT(smallestposval, std::numeric_limits<float>::min(), std::numeric_limits<double>::min())
0103         XSIMD_DEFINE_CONSTANT_HEX(sqrt_2pi, 0x40206c99, 0x40040d931ff62704)
0104         XSIMD_DEFINE_CONSTANT_HEX(sqrteps, 0x39b504f3, 0x3e50000000000000)
0105         XSIMD_DEFINE_CONSTANT_HEX(tanpio8, 0x3ed413cd, 0x3fda827999fcef31)
0106         XSIMD_DEFINE_CONSTANT_HEX(tan3pio8, 0x401a827a, 0x4003504f333f9de6)
0107         XSIMD_DEFINE_CONSTANT_HEX(twentypi, 0x427b53d1, 0x404f6a7a2955385e)
0108         XSIMD_DEFINE_CONSTANT_HEX(twoopi, 0x3f22f983, 0x3fe45f306dc9c883)
0109         XSIMD_DEFINE_CONSTANT(twotonmb, 8388608.0f, 4503599627370496.0)
0110         XSIMD_DEFINE_CONSTANT_HEX(twotonmbo3, 0x3ba14518, 0x3ed428a2f98d7286)
0111 #if defined(__FAST_MATH__) && defined(__GNUC__) && !defined(__clang__)
0112 #pragma GCC pop_options
0113 #endif
0114 
0115 #undef XSIMD_DEFINE_CONSTANT
0116 #undef XSIMD_DEFINE_CONSTANT_HEX
0117 
0118         template <class T>
0119         constexpr T allbits() noexcept;
0120 
0121         template <class T>
0122         constexpr as_integer_t<T> mask1frexp() noexcept;
0123 
0124         template <class T>
0125         constexpr as_integer_t<T> mask2frexp() noexcept;
0126 
0127         template <class T>
0128         constexpr as_integer_t<T> maxexponent() noexcept;
0129 
0130         template <class T>
0131         constexpr as_integer_t<T> maxexponentm1() noexcept;
0132 
0133         template <class T>
0134         constexpr int32_t nmb() noexcept;
0135 
0136         template <class T>
0137         constexpr T zero() noexcept;
0138 
0139         template <class T>
0140         constexpr T minvalue() noexcept;
0141 
0142         template <class T>
0143         constexpr T maxvalue() noexcept;
0144 
0145         /**************************
0146          * allbits implementation *
0147          **************************/
0148 
0149         namespace detail
0150         {
0151             template <class T, bool = std::is_integral<T>::value>
0152             struct allbits_impl
0153             {
0154                 static constexpr T get_value() noexcept
0155                 {
0156                     return T(~0);
0157                 }
0158             };
0159 
0160             template <class T>
0161             struct allbits_impl<T, false>
0162             {
0163                 static constexpr T get_value() noexcept
0164                 {
0165                     return nan<T>();
0166                 }
0167             };
0168         }
0169 
0170         template <class T>
0171         XSIMD_INLINE constexpr T allbits() noexcept
0172         {
0173             return T(detail::allbits_impl<typename T::value_type>::get_value());
0174         }
0175 
0176         /*****************************
0177          * mask1frexp implementation *
0178          *****************************/
0179 
0180         template <class T>
0181         XSIMD_INLINE constexpr as_integer_t<T> mask1frexp() noexcept
0182         {
0183             return as_integer_t<T>(mask1frexp<typename T::value_type>());
0184         }
0185 
0186         template <>
0187         XSIMD_INLINE constexpr int32_t mask1frexp<float>() noexcept
0188         {
0189             return 0x7f800000;
0190         }
0191 
0192         template <>
0193         XSIMD_INLINE constexpr int64_t mask1frexp<double>() noexcept
0194         {
0195             return 0x7ff0000000000000;
0196         }
0197 
0198         /*****************************
0199          * mask2frexp implementation *
0200          *****************************/
0201 
0202         template <class T>
0203         XSIMD_INLINE constexpr as_integer_t<T> mask2frexp() noexcept
0204         {
0205             return as_integer_t<T>(mask2frexp<typename T::value_type>());
0206         }
0207 
0208         template <>
0209         XSIMD_INLINE constexpr int32_t mask2frexp<float>() noexcept
0210         {
0211             return 0x3f000000;
0212         }
0213 
0214         template <>
0215         XSIMD_INLINE constexpr int64_t mask2frexp<double>() noexcept
0216         {
0217             return 0x3fe0000000000000;
0218         }
0219 
0220         /******************************
0221          * maxexponent implementation *
0222          ******************************/
0223 
0224         template <class T>
0225         XSIMD_INLINE constexpr as_integer_t<T> maxexponent() noexcept
0226         {
0227             return as_integer_t<T>(maxexponent<typename T::value_type>());
0228         }
0229 
0230         template <>
0231         XSIMD_INLINE constexpr int32_t maxexponent<float>() noexcept
0232         {
0233             return 127;
0234         }
0235 
0236         template <>
0237         XSIMD_INLINE constexpr int64_t maxexponent<double>() noexcept
0238         {
0239             return 1023;
0240         }
0241 
0242         /******************************
0243          * maxexponent implementation *
0244          ******************************/
0245 
0246         template <class T>
0247         XSIMD_INLINE constexpr as_integer_t<T> maxexponentm1() noexcept
0248         {
0249             return as_integer_t<T>(maxexponentm1<typename T::value_type>());
0250         }
0251 
0252         template <>
0253         XSIMD_INLINE constexpr int32_t maxexponentm1<float>() noexcept
0254         {
0255             return 126;
0256         }
0257 
0258         template <>
0259         XSIMD_INLINE constexpr int64_t maxexponentm1<double>() noexcept
0260         {
0261             return 1022;
0262         }
0263 
0264         /**********************
0265          * nmb implementation *
0266          **********************/
0267 
0268         template <class T>
0269         XSIMD_INLINE constexpr int32_t nmb() noexcept
0270         {
0271             return nmb<typename T::value_type>();
0272         }
0273 
0274         template <>
0275         XSIMD_INLINE constexpr int32_t nmb<float>() noexcept
0276         {
0277             return 23;
0278         }
0279 
0280         template <>
0281         XSIMD_INLINE constexpr int32_t nmb<double>() noexcept
0282         {
0283             return 52;
0284         }
0285 
0286         /***********************
0287          * zero implementation *
0288          ***********************/
0289 
0290         template <class T>
0291         XSIMD_INLINE constexpr T zero() noexcept
0292         {
0293             return T(typename T::value_type(0));
0294         }
0295 
0296         /***************************
0297          * minvalue implementation *
0298          ***************************/
0299 
0300         namespace detail
0301         {
0302             template <class T>
0303             struct minvalue_impl
0304             {
0305                 static constexpr T get_value() noexcept
0306                 {
0307                     return std::numeric_limits<typename T::value_type>::min();
0308                 }
0309             };
0310 
0311             template <class T>
0312             struct minvalue_common
0313             {
0314                 static constexpr T get_value() noexcept
0315                 {
0316                     return std::numeric_limits<T>::min();
0317                 }
0318             };
0319 
0320             template <>
0321             struct minvalue_impl<int8_t> : minvalue_common<int8_t>
0322             {
0323             };
0324             template <>
0325             struct minvalue_impl<uint8_t> : minvalue_common<uint8_t>
0326             {
0327             };
0328             template <>
0329             struct minvalue_impl<int16_t> : minvalue_common<int16_t>
0330             {
0331             };
0332             template <>
0333             struct minvalue_impl<uint16_t> : minvalue_common<uint16_t>
0334             {
0335             };
0336             template <>
0337             struct minvalue_impl<int32_t> : minvalue_common<int32_t>
0338             {
0339             };
0340             template <>
0341             struct minvalue_impl<uint32_t> : minvalue_common<uint32_t>
0342             {
0343             };
0344             template <>
0345             struct minvalue_impl<int64_t> : minvalue_common<int64_t>
0346             {
0347             };
0348             template <>
0349             struct minvalue_impl<uint64_t> : minvalue_common<uint64_t>
0350             {
0351             };
0352 
0353             template <>
0354             struct minvalue_impl<float>
0355             {
0356                 XSIMD_INLINE static float get_value() noexcept
0357                 {
0358                     return bit_cast<float>((uint32_t)0xff7fffff);
0359                 }
0360             };
0361 
0362             template <>
0363             struct minvalue_impl<double>
0364             {
0365                 XSIMD_INLINE static double get_value() noexcept
0366                 {
0367                     return bit_cast<double>((uint64_t)0xffefffffffffffff);
0368                 }
0369             };
0370         }
0371 
0372         template <class T>
0373         constexpr T minvalue() noexcept
0374         {
0375             return T(detail::minvalue_impl<typename T::value_type>::get_value());
0376         }
0377 
0378         /***************************
0379          * maxvalue implementation *
0380          ***************************/
0381 
0382         template <class T>
0383         constexpr T maxvalue() noexcept
0384         {
0385             return T(std::numeric_limits<typename T::value_type>::max());
0386         }
0387     }
0388 
0389 }
0390 
0391 #endif