pow10.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*=============================================================================
  2. Copyright (c) 2001-2019 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_X3_POW10_DECEMBER_26_2008_1118AM)
  9. #define BOOST_SPIRIT_X3_POW10_DECEMBER_26_2008_1118AM
  10. #include <boost/config/no_tr1/cmath.hpp>
  11. #include <boost/limits.hpp>
  12. #include <boost/spirit/home/x3/support/unused.hpp>
  13. #include <boost/spirit/home/x3/support/traits/numeric_traits.hpp>
  14. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  15. # pragma warning(push)
  16. # pragma warning(disable: 4244) // conversion from 'double' to 'float', possible loss of data
  17. #endif
  18. namespace boost { namespace spirit { namespace x3 { namespace traits
  19. {
  20. namespace detail
  21. {
  22. template <typename T, typename Enable = void>
  23. struct pow10_helper
  24. {
  25. static T call(unsigned dim)
  26. {
  27. using namespace std; // allow for ADL to find the correct overload
  28. return pow(T(10), T(dim));
  29. }
  30. };
  31. template <>
  32. struct pow10_helper<unused_type>
  33. {
  34. static unused_type call(unused_type)
  35. {
  36. return unused;
  37. }
  38. };
  39. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  40. #pragma message("Spirit X3 requires full c++14 support.")
  41. #pragma message("Support for VS2015 will cease in the next Boost release.")
  42. #pragma message("This code branch will be removed.")
  43. template <>
  44. struct pow10_helper<double>
  45. {
  46. static double call(unsigned dim)
  47. {
  48. static double const exponents[] =
  49. {
  50. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  51. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  52. 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
  53. 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
  54. 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
  55. 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
  56. 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
  57. 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
  58. 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
  59. 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
  60. 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
  61. 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
  62. 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
  63. 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
  64. 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
  65. 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
  66. 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169,
  67. 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179,
  68. 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189,
  69. 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199,
  70. 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209,
  71. 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219,
  72. 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229,
  73. 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239,
  74. 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249,
  75. 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259,
  76. 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269,
  77. 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279,
  78. 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289,
  79. 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299,
  80. 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308,
  81. };
  82. BOOST_ASSERT(dim < sizeof(exponents)/sizeof(double));
  83. return exponents[dim];
  84. }
  85. };
  86. template <>
  87. struct pow10_helper<float>
  88. {
  89. static float call(unsigned dim)
  90. {
  91. return pow10_helper<double>::call(dim);
  92. }
  93. };
  94. #else
  95. template <typename T>
  96. struct pow10_table
  97. {
  98. constexpr static std::size_t size =
  99. std::numeric_limits<T>::max_exponent10;
  100. constexpr pow10_table()
  101. : exponents()
  102. {
  103. exponents[0] = T(1);
  104. for (auto i = 1; i != size; ++i)
  105. exponents[i] = exponents[i-1] * T(10);
  106. }
  107. T exponents[size];
  108. };
  109. template <typename T>
  110. struct native_pow10_helper
  111. {
  112. static T call(unsigned dim)
  113. {
  114. constexpr auto table = pow10_table<T>();
  115. return table.exponents[dim];
  116. }
  117. };
  118. template <>
  119. struct pow10_helper<float>
  120. : native_pow10_helper<float> {};
  121. template <>
  122. struct pow10_helper<double>
  123. : native_pow10_helper<double> {};
  124. template <>
  125. struct pow10_helper<long double>
  126. : native_pow10_helper<long double> {};
  127. #endif
  128. }
  129. template <typename T>
  130. inline T pow10(unsigned dim)
  131. {
  132. return detail::pow10_helper<T>::call(dim);
  133. }
  134. }}}}
  135. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  136. # pragma warning(pop)
  137. #endif
  138. #endif