Types.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef MANDEL_TYPES_H
  2. #define MANDEL_TYPES_H
  3. #include <cinttypes>
  4. #include <cmath>
  5. #include <string>
  6. #include "Fixed.h"
  7. #define WITH_QD
  8. #ifdef WITH_BOOST
  9. # include <boost/multiprecision/cpp_bin_float.hpp>
  10. # if defined(__GNUC__) || defined(__INTEL_COMPILER)
  11. //# include <boost/multiprecision/float128.hpp>
  12. # endif
  13. # include <boost/multiprecision/cpp_int.hpp>
  14. # include <boost/functional/hash.hpp>
  15. #endif
  16. #include <qd/dd_real.h>
  17. #include <qd/qd_real.h>
  18. namespace mnd
  19. {
  20. #ifdef WITH_BOOST
  21. # if 0//defined(__GNUC__) || defined(__INTEL_COMPILER)
  22. using Float128 = boost::multiprecision::float128;
  23. # else
  24. using Float128 = boost::multiprecision::cpp_bin_float_quad;
  25. /*using Float128 = boost::multiprecision::number<
  26. boost::multiprecision::backends::cpp_bin_float<
  27. 112, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  28. boost::multiprecision::et_off>;*/
  29. # endif
  30. inline Float128 abs(const Float128& x) { return boost::multiprecision::abs(x); }
  31. inline Float128 floor(const Float128& x) { return boost::multiprecision::floor(x); }
  32. inline Float128 log(const Float128& x) { return boost::multiprecision::log(x); }
  33. inline Float128 log2(const Float128& x) { return boost::multiprecision::log2(x); }
  34. inline Float128 pow(const Float128& x, const Float128& y) { return boost::multiprecision::pow(x, y); }
  35. using Float256 = boost::multiprecision::number<
  36. boost::multiprecision::backends::cpp_bin_float<
  37. 240, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  38. boost::multiprecision::et_off>;
  39. //using Float256 = long double;
  40. inline Float256 abs(const Float256& x) { return boost::multiprecision::abs(x); }
  41. inline Float256 floor(const Float256& x) { return boost::multiprecision::floor(x); }
  42. inline Float256 log(const Float256& x) { return boost::multiprecision::log(x); }
  43. inline Float256 log2(const Float256& x) { return boost::multiprecision::log2(x); }
  44. inline Float256 pow(const Float256& x, const Float256& y) { return boost::multiprecision::pow(x, y); }
  45. using Real = Float256;
  46. using Integer = boost::multiprecision::int256_t;
  47. #else
  48. using Real = double;
  49. using Integer = int64_t;
  50. #endif
  51. using DoubleDouble = dd_real;
  52. using QuadDouble = qd_real;
  53. inline DoubleDouble abs(const DoubleDouble& x) { return ::abs(x); }
  54. inline DoubleDouble floor(const DoubleDouble& x) { return ::floor(x); }
  55. inline DoubleDouble log(const DoubleDouble& x) { return ::log(x); }
  56. inline DoubleDouble log2(const DoubleDouble& x) { return ::log(x) / ::log(DoubleDouble(2.0)); }
  57. inline DoubleDouble pow(const DoubleDouble& x, const DoubleDouble& y) { return ::pow(x, y); }
  58. inline QuadDouble abs(const QuadDouble& x) { return ::abs(x); }
  59. inline QuadDouble floor(const QuadDouble& x) { return ::floor(x); }
  60. inline QuadDouble log(const QuadDouble& x) { return ::log(x); }
  61. inline QuadDouble log2(const QuadDouble& x) { return ::log(x) / ::log(QuadDouble(2.0)); }
  62. inline QuadDouble pow(const QuadDouble& x, const QuadDouble& y) { return ::pow(x, y); }
  63. inline double abs(double x) { return ::abs(x); }
  64. inline float abs(float x) { return ::abs(x); }
  65. inline double floor(double x) { return ::floor(x); }
  66. inline float floor(float x) { return ::floorf(x); }
  67. inline double log(double x) { return ::log(x); }
  68. inline float log(float x) { return ::logf(x); }
  69. inline double log2(double x) { return ::log2(x); }
  70. inline float log2(float x) { return ::log2f(x); }
  71. inline double pow(double x, double y) { return ::pow(x, y); }
  72. inline float pow(float x, float y) { return ::powf(x, y); }
  73. template<typename T, typename U>
  74. T convert(const U& x)
  75. {
  76. return static_cast<T>(x);
  77. }
  78. #if defined(WITH_BOOST) && defined(WITH_QD)
  79. template<>
  80. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  81. {
  82. std::string s = x.str();
  83. return DoubleDouble(s.c_str());
  84. }
  85. template<>
  86. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  87. {
  88. return float(x.x[0] + x.x[1]);
  89. }
  90. template<>
  91. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  92. {
  93. std::string s = x.str();
  94. return QuadDouble(s.c_str());
  95. }
  96. template<>
  97. inline float convert<float, QuadDouble>(const QuadDouble& x)
  98. {
  99. return float(x.x[0] + x.x[1] + x.x[2] + x.x[3]);
  100. }
  101. template<>
  102. inline float convert<float, Fixed512>(const Fixed512& x)
  103. {
  104. return float(Real(x));
  105. }
  106. #endif
  107. std::string toString(const Real& num);
  108. /*
  109. template<typename T>
  110. constexpr mnd::Real precision(void);
  111. */
  112. }
  113. #endif // MANDEL_TYPES_H