Types.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include "TripleDouble.h"
  22. #include "HexDouble.h"
  23. namespace mnd
  24. {
  25. #ifdef WITH_BOOST
  26. # if 0//defined(__GNUC__) || defined(__INTEL_COMPILER)
  27. using Float128 = boost::multiprecision::float128;
  28. # else
  29. using Float128 = boost::multiprecision::cpp_bin_float_quad;
  30. /*using Float128 = boost::multiprecision::number<
  31. boost::multiprecision::backends::cpp_bin_float<
  32. 112, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  33. boost::multiprecision::et_off>;*/
  34. # endif
  35. inline Float128 abs(const Float128& x) { return boost::multiprecision::abs(x); }
  36. inline Float128 sqrt(const Float128& x) { return boost::multiprecision::sqrt(x); }
  37. inline Float128 floor(const Float128& x) { return boost::multiprecision::floor(x); }
  38. inline Float128 log(const Float128& x) { return boost::multiprecision::log(x); }
  39. inline Float128 log2(const Float128& x) { return boost::multiprecision::log2(x); }
  40. inline Float128 pow(const Float128& x, const Float128& y) { return boost::multiprecision::pow(x, y); }
  41. inline Float128 atan2(const Float128& y, const Float128& x) { return boost::multiprecision::atan2(y, x); }
  42. inline Float128 cos(const Float128& x) { return boost::multiprecision::cos(x); }
  43. inline Float128 sin(const Float128& x) { return boost::multiprecision::sin(x); }
  44. inline Float128 exp(const Float128& x) { return boost::multiprecision::exp(x); }
  45. using Float256 = boost::multiprecision::number<
  46. boost::multiprecision::backends::cpp_bin_float<
  47. 240, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  48. boost::multiprecision::et_off>;
  49. //using Float256 = long double;
  50. inline Float256 abs(const Float256& x) { return boost::multiprecision::abs(x); }
  51. inline Float256 sqrt(const Float256& x) { return boost::multiprecision::sqrt(x); }
  52. inline Float256 floor(const Float256& x) { return boost::multiprecision::floor(x); }
  53. inline Float256 log(const Float256& x) { return boost::multiprecision::log(x); }
  54. inline Float256 log2(const Float256& x) { return boost::multiprecision::log2(x); }
  55. inline Float256 pow(const Float256& x, const Float256& y) { return boost::multiprecision::pow(x, y); }
  56. inline Float256 atan2(const Float256& y, const Float256& x) { return boost::multiprecision::atan2(y, x); }
  57. inline Float256 cos(const Float256& x) { return boost::multiprecision::cos(x); }
  58. inline Float256 sin(const Float256& x) { return boost::multiprecision::sin(x); }
  59. inline Float256 exp(const Float256& x) { return boost::multiprecision::exp(x); }
  60. using Float512 = boost::multiprecision::number<
  61. boost::multiprecision::backends::cpp_bin_float<
  62. 496, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  63. boost::multiprecision::et_off>;
  64. inline Float512 abs(const Float512& x) { return boost::multiprecision::abs(x); }
  65. inline Float512 sqrt(const Float512& x) { return boost::multiprecision::sqrt(x); }
  66. inline Float512 floor(const Float512& x) { return boost::multiprecision::floor(x); }
  67. inline Float512 log(const Float512& x) { return boost::multiprecision::log(x); }
  68. inline Float512 log2(const Float512& x) { return boost::multiprecision::log2(x); }
  69. inline Float512 pow(const Float512& x, const Float512& y) { return boost::multiprecision::pow(x, y); }
  70. inline Float512 atan2(const Float512& y, const Float512& x) { return boost::multiprecision::atan2(y, x); }
  71. inline Float512 cos(const Float512& x) { return boost::multiprecision::cos(x); }
  72. inline Float512 sin(const Float512& x) { return boost::multiprecision::sin(x); }
  73. inline Float512 exp(const Float512& x) { return boost::multiprecision::exp(x); }
  74. using Real = Float512;
  75. using Integer = boost::multiprecision::int512_t;
  76. /*boost::multiprecision::number<
  77. boost::multiprecision::backends::cpp_bin_float<
  78. 1500, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  79. boost::multiprecision::et_off>;
  80. inline Real abs(const Real& x) { return boost::multiprecision::abs(x); }
  81. inline Real sqrt(const Real& x) { return boost::multiprecision::sqrt(x); }
  82. inline Real floor(const Real& x) { return boost::multiprecision::floor(x); }
  83. inline Real log(const Real& x) { return boost::multiprecision::log(x); }
  84. inline Real log2(const Real& x) { return boost::multiprecision::log2(x); }
  85. inline Real pow(const Real& x, const Real& y) { return boost::multiprecision::pow(x, y); }
  86. inline Real atan2(const Real& y, const Real& x) { return boost::multiprecision::atan2(y, x); }
  87. inline Real cos(const Real& x) { return boost::multiprecision::cos(x); }
  88. inline Real sin(const Real& x) { return boost::multiprecision::sin(x); }
  89. inline Real exp(const Real& x) { return boost::multiprecision::exp(x); }
  90. */
  91. #else
  92. using Real = double;
  93. using Integer = int64_t;
  94. #endif
  95. using DoubleDouble = dd_real;
  96. using QuadDouble = qd_real;
  97. inline DoubleDouble abs(const DoubleDouble& x) { return ::abs(x); }
  98. inline DoubleDouble sqrt(const DoubleDouble& x) { return ::sqrt(x); }
  99. inline DoubleDouble floor(const DoubleDouble& x) { return ::floor(x); }
  100. inline DoubleDouble log(const DoubleDouble& x) { return ::log(x); }
  101. inline DoubleDouble log2(const DoubleDouble& x) { return ::log(x) / ::log(DoubleDouble(2.0)); }
  102. inline DoubleDouble pow(const DoubleDouble& x, const DoubleDouble& y) { return ::pow(x, y); }
  103. inline DoubleDouble atan2(const DoubleDouble& y, const DoubleDouble& x) { return ::atan2(y, x); }
  104. inline DoubleDouble cos(const DoubleDouble& x) { return ::cos(x); }
  105. inline DoubleDouble sin(const DoubleDouble& x) { return ::sin(x); }
  106. inline DoubleDouble exp(const DoubleDouble& x) { return ::exp(x); }
  107. inline QuadDouble abs(const QuadDouble& x) { return ::abs(x); }
  108. inline QuadDouble sqrt(const QuadDouble& x) { return ::sqrt(x); }
  109. inline QuadDouble floor(const QuadDouble& x) { return ::floor(x); }
  110. inline QuadDouble log(const QuadDouble& x) { return ::log(x); }
  111. inline QuadDouble log2(const QuadDouble& x) { return ::log(x) / ::log(QuadDouble(2.0)); }
  112. inline QuadDouble pow(const QuadDouble& x, const QuadDouble& y) { return ::pow(x, y); }
  113. inline QuadDouble atan2(const QuadDouble& y, const QuadDouble& x) { return ::atan2(y, x); }
  114. inline QuadDouble cos(const QuadDouble& x) { return ::cos(x); }
  115. inline QuadDouble sin(const QuadDouble& x) { return ::sin(x); }
  116. inline QuadDouble exp(const QuadDouble& x) { return ::exp(x); }
  117. inline double abs(double x) { return ::abs(x); }
  118. inline float abs(float x) { return ::abs(x); }
  119. inline double sqrt(double x) { return ::sqrt(x); }
  120. inline float sqrt(float x) { return ::sqrtf(x); }
  121. inline double floor(double x) { return ::floor(x); }
  122. inline float floor(float x) { return ::floorf(x); }
  123. inline double log(double x) { return ::log(x); }
  124. inline float log(float x) { return ::logf(x); }
  125. inline double log2(double x) { return ::log2(x); }
  126. inline float log2(float x) { return ::log2f(x); }
  127. inline double atan2(double x, double y) { return ::atan2(x, y); }
  128. inline float atan2(float x, float y) { return ::atan2(x, y); }
  129. inline double pow(double x, double y) { return ::pow(x, y); }
  130. inline float pow(float x, float y) { return ::powf(x, y); }
  131. inline double cos(double x) { return ::cos(x); }
  132. inline float cos(float x) { return ::cos(x); }
  133. inline double sin(double x) { return ::sin(x); }
  134. inline float sin(float x) { return ::sin(x); }
  135. inline double exp(double x) { return ::exp(x); }
  136. inline float exp(float x) { return ::exp(x); }
  137. template<typename T, typename U>
  138. T convert(const U& x)
  139. {
  140. return static_cast<T>(x);
  141. }
  142. #if defined(WITH_BOOST)
  143. template<>
  144. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  145. {
  146. double s = static_cast<double>(x);
  147. double e = static_cast<double>(x - s);
  148. return DoubleDouble{ s, e };
  149. }
  150. template<>
  151. inline LightDoubleDouble convert<LightDoubleDouble, Real>(const Real& x)
  152. {
  153. double s = static_cast<double>(x);
  154. double e = static_cast<double>(x - s);
  155. return LightDoubleDouble{ s, e };
  156. }
  157. template<>
  158. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  159. {
  160. return float(x.x[0] + x.x[1]);
  161. }
  162. template<>
  163. inline float convert<float, LightDoubleDouble>(const LightDoubleDouble& x)
  164. {
  165. return float(x[0] + x[1]);
  166. }
  167. template<>
  168. inline Real convert<Real, LightDoubleDouble>(const LightDoubleDouble& x)
  169. {
  170. return Real{ x[0] } + x[1];
  171. }
  172. template<>
  173. inline TripleDouble convert<TripleDouble, Real>(const Real& x)
  174. {
  175. double s = static_cast<double>(x);
  176. Real tmp = x - s;
  177. double e1 = static_cast<double>(tmp);
  178. tmp = tmp - e1;
  179. double e2 = static_cast<double>(tmp);
  180. return TripleDouble { s, e1, e2 };
  181. }
  182. template<>
  183. inline Real convert<Real, TripleDouble>(const TripleDouble& x)
  184. {
  185. return Real{ x[0] } + Real{ x[1] } + Real{ x[2] };
  186. }
  187. template<>
  188. inline float convert<float, TripleDouble>(const TripleDouble& x)
  189. {
  190. return float(x.x[0] + x.x[1] + x.x[2]);
  191. }
  192. template<>
  193. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  194. {
  195. double s = static_cast<double>(x);
  196. Real tmp = x - s;
  197. double e1 = static_cast<double>(tmp);
  198. tmp = tmp - e1;
  199. double e2 = static_cast<double>(tmp);
  200. tmp = tmp - e2;
  201. double e3 = static_cast<double>(tmp);
  202. return QuadDouble{ s, e1, e2, e3 };
  203. }
  204. template<>
  205. inline float convert<float, QuadDouble>(const QuadDouble& x)
  206. {
  207. return float(x.x[0] + x.x[1]);
  208. }
  209. template<>
  210. inline Real convert<Real, HexDouble>(const HexDouble& x)
  211. {
  212. return Real{ x[0] } + x[1] + x[2] + x[3] + x[4] + x[5];
  213. }
  214. template<>
  215. inline HexDouble convert<HexDouble, Real>(const Real& x)
  216. {
  217. double s = static_cast<double>(x);
  218. Real tmp = x - s;
  219. double e1 = static_cast<double>(tmp);
  220. tmp = tmp - e1;
  221. double e2 = static_cast<double>(tmp);
  222. tmp = tmp - e2;
  223. double e3 = static_cast<double>(tmp);
  224. tmp = tmp - e3;
  225. double e4 = static_cast<double>(tmp);
  226. tmp = tmp - e4;
  227. double e5 = static_cast<double>(tmp);
  228. return HexDouble { s, e1, e2, e3, e4, e5 };
  229. }
  230. template<>
  231. inline float convert<float, HexDouble>(const HexDouble& x)
  232. {
  233. return float(x.x[0] + x.x[1]);
  234. }
  235. template<>
  236. inline float convert<float, Fixed512>(const Fixed512& x)
  237. {
  238. return float(Real(x));
  239. }
  240. template<>
  241. inline Fixed64 convert<Fixed64, Real>(const Real& x)
  242. {
  243. return Fixed64{ static_cast<int64_t>(x * 0xFFFFFFFFFFFFLL), true };
  244. }
  245. template<>
  246. inline Fixed128 convert<Fixed128, Real>(const Real& x)
  247. {
  248. mnd::Integer i = mnd::Integer(x * mnd::pow(Real(1LL << 32), 3));
  249. uint64_t lo = uint64_t(i & 0xFFFFFFFFFFFFFFFFULL);
  250. uint64_t hi = uint64_t((i >> 64) & 0xFFFFFFFFFFFFFFFFULL);
  251. return Fixed128(hi, lo);
  252. }
  253. #endif
  254. std::string toString(const Real& num);
  255. std::string toLegibleString(const Real& num);
  256. /*
  257. template<typename T>
  258. constexpr mnd::Real precision(void);
  259. */
  260. }
  261. #endif // MANDEL_TYPES_H