Types.h 11 KB

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