Types.h 12 KB

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