Types.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 Float512 = boost::multiprecision::number<
  46. boost::multiprecision::backends::cpp_bin_float<
  47. 496, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  48. boost::multiprecision::et_off>;
  49. inline Float512 abs(const Float512& x) { return boost::multiprecision::abs(x); }
  50. inline Float512 floor(const Float512& x) { return boost::multiprecision::floor(x); }
  51. inline Float512 log(const Float512& x) { return boost::multiprecision::log(x); }
  52. inline Float512 log2(const Float512& x) { return boost::multiprecision::log2(x); }
  53. inline Float512 pow(const Float512& x, const Float512& y) { return boost::multiprecision::pow(x, y); }
  54. using Real = Float512;
  55. using Integer = boost::multiprecision::int512_t;
  56. #else
  57. using Real = double;
  58. using Integer = int64_t;
  59. #endif
  60. using DoubleDouble = dd_real;
  61. using QuadDouble = qd_real;
  62. inline DoubleDouble abs(const DoubleDouble& x) { return ::abs(x); }
  63. inline DoubleDouble floor(const DoubleDouble& x) { return ::floor(x); }
  64. inline DoubleDouble log(const DoubleDouble& x) { return ::log(x); }
  65. inline DoubleDouble log2(const DoubleDouble& x) { return ::log(x) / ::log(DoubleDouble(2.0)); }
  66. inline DoubleDouble pow(const DoubleDouble& x, const DoubleDouble& y) { return ::pow(x, y); }
  67. inline QuadDouble abs(const QuadDouble& x) { return ::abs(x); }
  68. inline QuadDouble floor(const QuadDouble& x) { return ::floor(x); }
  69. inline QuadDouble log(const QuadDouble& x) { return ::log(x); }
  70. inline QuadDouble log2(const QuadDouble& x) { return ::log(x) / ::log(QuadDouble(2.0)); }
  71. inline QuadDouble pow(const QuadDouble& x, const QuadDouble& y) { return ::pow(x, y); }
  72. inline double abs(double x) { return ::abs(x); }
  73. inline float abs(float x) { return ::abs(x); }
  74. inline double floor(double x) { return ::floor(x); }
  75. inline float floor(float x) { return ::floorf(x); }
  76. inline double log(double x) { return ::log(x); }
  77. inline float log(float x) { return ::logf(x); }
  78. inline double log2(double x) { return ::log2(x); }
  79. inline float log2(float x) { return ::log2f(x); }
  80. inline double pow(double x, double y) { return ::pow(x, y); }
  81. inline float pow(float x, float y) { return ::powf(x, y); }
  82. template<typename T, typename U>
  83. T convert(const U& x)
  84. {
  85. return static_cast<T>(x);
  86. }
  87. #if defined(WITH_BOOST)
  88. template<>
  89. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  90. {
  91. std::string s = x.str();
  92. return DoubleDouble(s.c_str());
  93. }
  94. template<>
  95. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  96. {
  97. return float(x.x[0] + x.x[1]);
  98. }
  99. template<>
  100. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  101. {
  102. std::string s = x.str();
  103. return QuadDouble(s.c_str());
  104. }
  105. template<>
  106. inline float convert<float, QuadDouble>(const QuadDouble& x)
  107. {
  108. return float(x.x[0] + x.x[1] + x.x[2] + x.x[3]);
  109. }
  110. template<>
  111. inline float convert<float, Fixed512>(const Fixed512& x)
  112. {
  113. return float(Real(x));
  114. }
  115. template<>
  116. inline Fixed64 convert<Fixed64, Real>(const Real& x)
  117. {
  118. return Fixed64(double(x));
  119. }
  120. template<>
  121. inline Fixed128 convert<Fixed128, Real>(const Real& x)
  122. {
  123. mnd::Integer i = mnd::Integer(x * mnd::pow(Real(1LL << 32), 3));
  124. uint64_t lo = uint64_t(i & 0xFFFFFFFFFFFFFFFFULL);
  125. uint64_t hi = uint64_t((i >> 64) & 0xFFFFFFFFFFFFFFFFULL);
  126. return Fixed128(hi, lo);
  127. }
  128. #endif
  129. std::string toString(const Real& num);
  130. /*
  131. template<typename T>
  132. constexpr mnd::Real precision(void);
  133. */
  134. }
  135. #endif // MANDEL_TYPES_H