Types.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 sqrt(const Float128& x) { return boost::multiprecision::sqrt(x); }
  35. inline Float128 floor(const Float128& x) { return boost::multiprecision::floor(x); }
  36. inline Float128 log(const Float128& x) { return boost::multiprecision::log(x); }
  37. inline Float128 log2(const Float128& x) { return boost::multiprecision::log2(x); }
  38. inline Float128 pow(const Float128& x, const Float128& y) { return boost::multiprecision::pow(x, y); }
  39. using Float256 = boost::multiprecision::number<
  40. boost::multiprecision::backends::cpp_bin_float<
  41. 240, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  42. boost::multiprecision::et_off>;
  43. //using Float256 = long double;
  44. inline Float256 abs(const Float256& x) { return boost::multiprecision::abs(x); }
  45. inline Float256 sqrt(const Float256& x) { return boost::multiprecision::sqrt(x); }
  46. inline Float256 floor(const Float256& x) { return boost::multiprecision::floor(x); }
  47. inline Float256 log(const Float256& x) { return boost::multiprecision::log(x); }
  48. inline Float256 log2(const Float256& x) { return boost::multiprecision::log2(x); }
  49. inline Float256 pow(const Float256& x, const Float256& y) { return boost::multiprecision::pow(x, y); }
  50. using Float512 = boost::multiprecision::number<
  51. boost::multiprecision::backends::cpp_bin_float<
  52. 496, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  53. boost::multiprecision::et_off>;
  54. inline Float512 abs(const Float512& x) { return boost::multiprecision::abs(x); }
  55. inline Float512 sqrt(const Float512& x) { return boost::multiprecision::sqrt(x); }
  56. inline Float512 floor(const Float512& x) { return boost::multiprecision::floor(x); }
  57. inline Float512 log(const Float512& x) { return boost::multiprecision::log(x); }
  58. inline Float512 log2(const Float512& x) { return boost::multiprecision::log2(x); }
  59. inline Float512 pow(const Float512& x, const Float512& y) { return boost::multiprecision::pow(x, y); }
  60. inline Float512 atan2(const Float512& y, const Float512& x) { return boost::multiprecision::atan2(y, x); }
  61. inline Float512 cos(const Float512& x) { return boost::multiprecision::cos(x); }
  62. inline Float512 sin(const Float512& x) { return boost::multiprecision::sin(x); }
  63. inline Float512 exp(const Float512& x) { return boost::multiprecision::exp(x); }
  64. using Real = Float512;
  65. using Integer = boost::multiprecision::int512_t;
  66. #else
  67. using Real = double;
  68. using Integer = int64_t;
  69. #endif
  70. using DoubleDouble = dd_real;
  71. using QuadDouble = qd_real;
  72. inline DoubleDouble abs(const DoubleDouble& x) { return ::abs(x); }
  73. inline DoubleDouble sqrt(const DoubleDouble& x) { return ::sqrt(x); }
  74. inline DoubleDouble floor(const DoubleDouble& x) { return ::floor(x); }
  75. inline DoubleDouble log(const DoubleDouble& x) { return ::log(x); }
  76. inline DoubleDouble log2(const DoubleDouble& x) { return ::log(x) / ::log(DoubleDouble(2.0)); }
  77. inline DoubleDouble pow(const DoubleDouble& x, const DoubleDouble& y) { return ::pow(x, y); }
  78. inline QuadDouble abs(const QuadDouble& x) { return ::abs(x); }
  79. inline QuadDouble sqrt(const QuadDouble& x) { return ::sqrt(x); }
  80. inline QuadDouble floor(const QuadDouble& x) { return ::floor(x); }
  81. inline QuadDouble log(const QuadDouble& x) { return ::log(x); }
  82. inline QuadDouble log2(const QuadDouble& x) { return ::log(x) / ::log(QuadDouble(2.0)); }
  83. inline QuadDouble pow(const QuadDouble& x, const QuadDouble& y) { return ::pow(x, y); }
  84. inline double abs(double x) { return ::abs(x); }
  85. inline float abs(float x) { return ::abs(x); }
  86. inline double sqrt(double x) { return ::sqrt(x); }
  87. inline float sqrt(float x) { return ::sqrtf(x); }
  88. inline double floor(double x) { return ::floor(x); }
  89. inline float floor(float x) { return ::floorf(x); }
  90. inline double log(double x) { return ::log(x); }
  91. inline float log(float x) { return ::logf(x); }
  92. inline double log2(double x) { return ::log2(x); }
  93. inline float log2(float x) { return ::log2f(x); }
  94. inline double pow(double x, double y) { return ::pow(x, y); }
  95. inline float pow(float x, float y) { return ::powf(x, y); }
  96. template<typename T, typename U>
  97. T convert(const U& x)
  98. {
  99. return static_cast<T>(x);
  100. }
  101. #if defined(WITH_BOOST)
  102. template<>
  103. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  104. {
  105. std::string s = x.str();
  106. return DoubleDouble(s.c_str());
  107. }
  108. template<>
  109. inline LightDoubleDouble convert<LightDoubleDouble, Real>(const Real& x)
  110. {
  111. double upper = static_cast<double>(x);
  112. double lower = static_cast<double>(x - upper);
  113. return { upper, lower };
  114. }
  115. template<>
  116. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  117. {
  118. return float(x.x[0] + x.x[1]);
  119. }
  120. template<>
  121. inline float convert<float, LightDoubleDouble>(const LightDoubleDouble& x)
  122. {
  123. return float(x[0] + x[1]);
  124. }
  125. template<>
  126. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  127. {
  128. std::string s = x.str();
  129. return QuadDouble(s.c_str());
  130. }
  131. template<>
  132. inline float convert<float, QuadDouble>(const QuadDouble& x)
  133. {
  134. return float(x.x[0] + x.x[1] + x.x[2] + x.x[3]);
  135. }
  136. template<>
  137. inline float convert<float, Fixed512>(const Fixed512& x)
  138. {
  139. return float(Real(x));
  140. }
  141. template<>
  142. inline Fixed64 convert<Fixed64, Real>(const Real& x)
  143. {
  144. return Fixed64(double(x));
  145. }
  146. template<>
  147. inline Fixed128 convert<Fixed128, Real>(const Real& x)
  148. {
  149. mnd::Integer i = mnd::Integer(x * mnd::pow(Real(1LL << 32), 3));
  150. uint64_t lo = uint64_t(i & 0xFFFFFFFFFFFFFFFFULL);
  151. uint64_t hi = uint64_t((i >> 64) & 0xFFFFFFFFFFFFFFFFULL);
  152. return Fixed128(hi, lo);
  153. }
  154. #endif
  155. std::string toString(const Real& num);
  156. std::string toLegibleString(const Real& num);
  157. /*
  158. template<typename T>
  159. constexpr mnd::Real precision(void);
  160. */
  161. }
  162. #endif // MANDEL_TYPES_H