Types.h 11 KB

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