Types.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef MANDEL_TYPES_H
  2. #define MANDEL_TYPES_H
  3. #include <cinttypes>
  4. #include <cmath>
  5. #include <string>
  6. #include "Fixed.h"
  7. #ifndef WITH_QD
  8. #define WITH_QD
  9. #endif
  10. #ifdef WITH_BOOST
  11. # include <boost/multiprecision/cpp_bin_float.hpp>
  12. # if defined(__GNUC__) || defined(__INTEL_COMPILER)
  13. //# include <boost/multiprecision/float128.hpp>
  14. # endif
  15. # include <boost/multiprecision/cpp_int.hpp>
  16. # include <boost/functional/hash.hpp>
  17. #endif
  18. #include <qd/dd_real.h>
  19. #include <qd/qd_real.h>
  20. #include "LightDoubleDouble.h"
  21. namespace mnd
  22. {
  23. #ifdef WITH_BOOST
  24. # if 0//defined(__GNUC__) || defined(__INTEL_COMPILER)
  25. using Float128 = boost::multiprecision::float128;
  26. # else
  27. using Float128 = boost::multiprecision::cpp_bin_float_quad;
  28. /*using Float128 = boost::multiprecision::number<
  29. boost::multiprecision::backends::cpp_bin_float<
  30. 112, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  31. boost::multiprecision::et_off>;*/
  32. # endif
  33. inline Float128 abs(const Float128& x) { return boost::multiprecision::abs(x); }
  34. inline Float128 floor(const Float128& x) { return boost::multiprecision::floor(x); }
  35. inline Float128 log(const Float128& x) { return boost::multiprecision::log(x); }
  36. inline Float128 log2(const Float128& x) { return boost::multiprecision::log2(x); }
  37. inline Float128 pow(const Float128& x, const Float128& y) { return boost::multiprecision::pow(x, y); }
  38. using Float256 = boost::multiprecision::number<
  39. boost::multiprecision::backends::cpp_bin_float<
  40. 240, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  41. boost::multiprecision::et_off>;
  42. //using Float256 = long double;
  43. inline Float256 abs(const Float256& x) { return boost::multiprecision::abs(x); }
  44. inline Float256 floor(const Float256& x) { return boost::multiprecision::floor(x); }
  45. inline Float256 log(const Float256& x) { return boost::multiprecision::log(x); }
  46. inline Float256 log2(const Float256& x) { return boost::multiprecision::log2(x); }
  47. inline Float256 pow(const Float256& x, const Float256& y) { return boost::multiprecision::pow(x, y); }
  48. using Float512 = boost::multiprecision::number<
  49. boost::multiprecision::backends::cpp_bin_float<
  50. 496, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  51. boost::multiprecision::et_off>;
  52. inline Float512 abs(const Float512& x) { return boost::multiprecision::abs(x); }
  53. inline Float512 floor(const Float512& x) { return boost::multiprecision::floor(x); }
  54. inline Float512 log(const Float512& x) { return boost::multiprecision::log(x); }
  55. inline Float512 log2(const Float512& x) { return boost::multiprecision::log2(x); }
  56. inline Float512 pow(const Float512& x, const Float512& y) { return boost::multiprecision::pow(x, y); }
  57. inline Float512 atan2(const Float512& y, const Float512& x) { return boost::multiprecision::atan2(y, x); }
  58. inline Float512 cos(const Float512& x) { return boost::multiprecision::cos(x); }
  59. inline Float512 sin(const Float512& x) { return boost::multiprecision::sin(x); }
  60. inline Float512 exp(const Float512& x) { return boost::multiprecision::exp(x); }
  61. using Real = Float512;
  62. using Integer = boost::multiprecision::int512_t;
  63. #else
  64. using Real = double;
  65. using Integer = int64_t;
  66. #endif
  67. using DoubleDouble = dd_real;
  68. using QuadDouble = qd_real;
  69. inline DoubleDouble abs(const DoubleDouble& x) { return ::abs(x); }
  70. inline DoubleDouble floor(const DoubleDouble& x) { return ::floor(x); }
  71. inline DoubleDouble log(const DoubleDouble& x) { return ::log(x); }
  72. inline DoubleDouble log2(const DoubleDouble& x) { return ::log(x) / ::log(DoubleDouble(2.0)); }
  73. inline DoubleDouble pow(const DoubleDouble& x, const DoubleDouble& y) { return ::pow(x, y); }
  74. inline QuadDouble abs(const QuadDouble& x) { return ::abs(x); }
  75. inline QuadDouble floor(const QuadDouble& x) { return ::floor(x); }
  76. inline QuadDouble log(const QuadDouble& x) { return ::log(x); }
  77. inline QuadDouble log2(const QuadDouble& x) { return ::log(x) / ::log(QuadDouble(2.0)); }
  78. inline QuadDouble pow(const QuadDouble& x, const QuadDouble& y) { return ::pow(x, y); }
  79. inline double abs(double x) { return ::abs(x); }
  80. inline float abs(float x) { return ::abs(x); }
  81. inline double floor(double x) { return ::floor(x); }
  82. inline float floor(float x) { return ::floorf(x); }
  83. inline double log(double x) { return ::log(x); }
  84. inline float log(float x) { return ::logf(x); }
  85. inline double log2(double x) { return ::log2(x); }
  86. inline float log2(float x) { return ::log2f(x); }
  87. inline double pow(double x, double y) { return ::pow(x, y); }
  88. inline float pow(float x, float y) { return ::powf(x, y); }
  89. template<typename T, typename U>
  90. T convert(const U& x)
  91. {
  92. return static_cast<T>(x);
  93. }
  94. #if defined(WITH_BOOST)
  95. template<>
  96. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  97. {
  98. std::string s = x.str();
  99. return DoubleDouble(s.c_str());
  100. }
  101. template<>
  102. inline LightDoubleDouble convert<LightDoubleDouble, Real>(const Real& x)
  103. {
  104. double upper = static_cast<double>(x);
  105. double lower = static_cast<double>(x - upper);
  106. return { upper, lower };
  107. }
  108. template<>
  109. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  110. {
  111. return float(x.x[0] + x.x[1]);
  112. }
  113. template<>
  114. inline float convert<float, LightDoubleDouble>(const LightDoubleDouble& x)
  115. {
  116. return float(x[0] + x[1]);
  117. }
  118. template<>
  119. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  120. {
  121. std::string s = x.str();
  122. return QuadDouble(s.c_str());
  123. }
  124. template<>
  125. inline float convert<float, QuadDouble>(const QuadDouble& x)
  126. {
  127. return float(x.x[0] + x.x[1] + x.x[2] + x.x[3]);
  128. }
  129. template<>
  130. inline float convert<float, Fixed512>(const Fixed512& x)
  131. {
  132. return float(Real(x));
  133. }
  134. template<>
  135. inline Fixed64 convert<Fixed64, Real>(const Real& x)
  136. {
  137. return Fixed64(double(x));
  138. }
  139. template<>
  140. inline Fixed128 convert<Fixed128, Real>(const Real& x)
  141. {
  142. mnd::Integer i = mnd::Integer(x * mnd::pow(Real(1LL << 32), 3));
  143. uint64_t lo = uint64_t(i & 0xFFFFFFFFFFFFFFFFULL);
  144. uint64_t hi = uint64_t((i >> 64) & 0xFFFFFFFFFFFFFFFFULL);
  145. return Fixed128(hi, lo);
  146. }
  147. #endif
  148. std::string toString(const Real& num);
  149. std::string toLegibleString(const Real& num);
  150. /*
  151. template<typename T>
  152. constexpr mnd::Real precision(void);
  153. */
  154. }
  155. #endif // MANDEL_TYPES_H