Types.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. inline double abs(double x) { return ::abs(x); }
  64. inline float abs(float x) { return ::abs(x); }
  65. inline double sqrt(double x) { return ::sqrt(x); }
  66. inline float sqrt(float x) { return ::sqrtf(x); }
  67. inline double floor(double x) { return ::floor(x); }
  68. inline float floor(float x) { return ::floorf(x); }
  69. inline double log(double x) { return ::log(x); }
  70. inline float log(float x) { return ::logf(x); }
  71. inline double log2(double x) { return ::log2(x); }
  72. inline float log2(float x) { return ::log2f(x); }
  73. inline double atan2(double x, double y) { return ::atan2(x, y); }
  74. inline float atan2(float x, float y) { return ::atan2(x, y); }
  75. inline double pow(double x, double y) { return ::pow(x, y); }
  76. inline float pow(float x, float y) { return ::powf(x, y); }
  77. inline double cos(double x) { return ::cos(x); }
  78. inline float cos(float x) { return ::cos(x); }
  79. inline double sin(double x) { return ::sin(x); }
  80. inline float sin(float x) { return ::sin(x); }
  81. inline double exp(double x) { return ::exp(x); }
  82. inline float exp(float x) { return ::exp(x); }
  83. template<typename T, typename U>
  84. T convert(const U& x)
  85. {
  86. return static_cast<T>(x);
  87. }
  88. #if defined(WITH_BOOST)
  89. template<>
  90. inline TripleFloat convert<TripleFloat, Real>(const Real& x)
  91. {
  92. float s = static_cast<float>(x);
  93. Real t = x - s;
  94. float e1 = static_cast<float>(t);
  95. float e2 = static_cast<float>(t - e1);
  96. return TripleFloat{ s, e1, e2 };
  97. }
  98. template<>
  99. inline Real convert<Real, TripleFloat>(const TripleFloat& x)
  100. {
  101. return Real{ x[0] } + x[1] + x[2];
  102. }
  103. /*template<>
  104. inline DoubleDouble convert<DoubleDouble, Real>(const Real& x)
  105. {
  106. double s = static_cast<double>(x);
  107. double e = static_cast<double>(x - s);
  108. return DoubleDouble{ s, e };
  109. }*/
  110. template<>
  111. inline LightDoubleDouble convert<LightDoubleDouble, Real>(const Real& x)
  112. {
  113. double s = static_cast<double>(x);
  114. double e = static_cast<double>(x - s);
  115. return LightDoubleDouble{ s, e };
  116. }
  117. /*template<>
  118. inline float convert<float, DoubleDouble>(const DoubleDouble& x)
  119. {
  120. return float(x.x[0] + x.x[1]);
  121. }*/
  122. template<>
  123. inline float convert<float, LightDoubleDouble>(const LightDoubleDouble& x)
  124. {
  125. return float(x[0] + x[1]);
  126. }
  127. template<>
  128. inline Real convert<Real, LightDoubleDouble>(const LightDoubleDouble& x)
  129. {
  130. return Real{ x[0] } + x[1];
  131. }
  132. template<>
  133. inline TripleDouble convert<TripleDouble, Real>(const Real& x)
  134. {
  135. double s = static_cast<double>(x);
  136. Real tmp = x - s;
  137. double e1 = static_cast<double>(tmp);
  138. tmp = tmp - e1;
  139. double e2 = static_cast<double>(tmp);
  140. return TripleDouble { s, e1, e2 };
  141. }
  142. template<>
  143. inline Real convert<Real, TripleDouble>(const TripleDouble& x)
  144. {
  145. return Real{ x[0] } + Real{ x[1] } + Real{ x[2] };
  146. }
  147. template<>
  148. inline float convert<float, TripleDouble>(const TripleDouble& x)
  149. {
  150. return float(x.x[0] + x.x[1] + x.x[2]);
  151. }
  152. template<>
  153. inline QuadDouble convert<QuadDouble, Real>(const Real& x)
  154. {
  155. double s = static_cast<double>(x);
  156. Real tmp = x - s;
  157. double e1 = static_cast<double>(tmp);
  158. tmp = tmp - e1;
  159. double e2 = static_cast<double>(tmp);
  160. tmp = tmp - e2;
  161. double e3 = static_cast<double>(tmp);
  162. return QuadDouble{ s, e1, e2, e3 };
  163. }
  164. template<>
  165. inline float convert<float, QuadDouble>(const QuadDouble& x)
  166. {
  167. return float(x.x[0] + x.x[1]);
  168. }
  169. template<>
  170. inline Real convert<Real, HexDouble>(const HexDouble& x)
  171. {
  172. return Real{ x[0] } + x[1] + x[2] + x[3] + x[4] + x[5];
  173. }
  174. template<>
  175. inline HexDouble convert<HexDouble, Real>(const Real& x)
  176. {
  177. double s = static_cast<double>(x);
  178. Real tmp = x - s;
  179. double e1 = static_cast<double>(tmp);
  180. tmp = tmp - e1;
  181. double e2 = static_cast<double>(tmp);
  182. tmp = tmp - e2;
  183. double e3 = static_cast<double>(tmp);
  184. tmp = tmp - e3;
  185. double e4 = static_cast<double>(tmp);
  186. tmp = tmp - e4;
  187. double e5 = static_cast<double>(tmp);
  188. return HexDouble { s, e1, e2, e3, e4, e5 };
  189. }
  190. template<>
  191. inline float convert<float, HexDouble>(const HexDouble& x)
  192. {
  193. return float(x.x[0] + x.x[1]);
  194. }
  195. template<>
  196. inline Real convert<Real, OctaDouble>(const OctaDouble& x)
  197. {
  198. return Real{ x[0] } + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7];
  199. }
  200. template<>
  201. inline OctaDouble convert<OctaDouble, Real>(const Real& x)
  202. {
  203. double s = static_cast<double>(x);
  204. Real tmp = x - s;
  205. double e1 = static_cast<double>(tmp);
  206. tmp = tmp - e1;
  207. double e2 = static_cast<double>(tmp);
  208. tmp = tmp - e2;
  209. double e3 = static_cast<double>(tmp);
  210. tmp = tmp - e3;
  211. double e4 = static_cast<double>(tmp);
  212. tmp = tmp - e4;
  213. double e5 = static_cast<double>(tmp);
  214. tmp = tmp - e5;
  215. double e6 = static_cast<double>(tmp);
  216. tmp = tmp - e6;
  217. double e7 = static_cast<double>(tmp);
  218. return OctaDouble{ s, e1, e2, e3, e4, e5, e6, e7 };
  219. }
  220. template<>
  221. inline float convert<float, OctaDouble>(const OctaDouble& x)
  222. {
  223. return float(x.x[0] + x.x[1]);
  224. }
  225. template<>
  226. inline float convert<float, Fixed512>(const Fixed512& x)
  227. {
  228. return float(Real(x));
  229. }
  230. template<>
  231. inline Fixed64 convert<Fixed64, Real>(const Real& x)
  232. {
  233. return Fixed64{ static_cast<int64_t>(x * 0xFFFFFFFFFFFFLL), true };
  234. }
  235. template<>
  236. inline Fixed128 convert<Fixed128, Real>(const Real& x)
  237. {
  238. mnd::Integer i = mnd::Integer(x * mnd::pow(Real(1LL << 32), 3));
  239. uint64_t lo = uint64_t(i & 0xFFFFFFFFFFFFFFFFULL);
  240. uint64_t hi = uint64_t((i >> 64) & 0xFFFFFFFFFFFFFFFFULL);
  241. return Fixed128(hi, lo);
  242. }
  243. #endif
  244. std::string toString(const Real& num);
  245. std::string toLegibleString(const Real& num);
  246. /*
  247. template<typename T>
  248. constexpr mnd::Real precision(void);
  249. */
  250. }
  251. #endif // MANDEL_TYPES_H