float128.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2013 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_MP_FLOAT128_HPP
  6. #define BOOST_MP_FLOAT128_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/scoped_array.hpp>
  9. #include <boost/functional/hash.hpp>
  10. #include <boost/multiprecision/number.hpp>
  11. #if defined(BOOST_INTEL) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  12. #if defined(BOOST_INTEL_CXX_VERSION) && (BOOST_INTEL_CXX_VERSION >= 1310) && defined(__GNUC__)
  13. #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))
  14. #define BOOST_MP_USE_FLOAT128
  15. #endif
  16. #endif
  17. #ifndef BOOST_MP_USE_FLOAT128
  18. #define BOOST_MP_USE_QUAD
  19. #endif
  20. #endif
  21. #if defined(__GNUC__) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  22. #define BOOST_MP_USE_FLOAT128
  23. #endif
  24. #if !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  25. #error "Sorry compiler is neither GCC, not Intel, don't know how to configure this header."
  26. #endif
  27. #if defined(BOOST_MP_USE_FLOAT128) && defined(BOOST_MP_USE_QUAD)
  28. #error "Oh dear, both BOOST_MP_USE_FLOAT128 and BOOST_MP_USE_QUAD are defined, which one should I be using?"
  29. #endif
  30. #if defined(BOOST_MP_USE_FLOAT128)
  31. extern "C" {
  32. #include <quadmath.h>
  33. }
  34. typedef __float128 float128_type;
  35. #elif defined(BOOST_MP_USE_QUAD)
  36. #include <boost/multiprecision/detail/float_string_cvt.hpp>
  37. typedef _Quad float128_type;
  38. extern "C" {
  39. _Quad __ldexpq(_Quad, int);
  40. _Quad __frexpq(_Quad, int*);
  41. _Quad __fabsq(_Quad);
  42. _Quad __floorq(_Quad);
  43. _Quad __ceilq(_Quad);
  44. _Quad __sqrtq(_Quad);
  45. _Quad __truncq(_Quad);
  46. _Quad __expq(_Quad);
  47. _Quad __powq(_Quad, _Quad);
  48. _Quad __logq(_Quad);
  49. _Quad __log10q(_Quad);
  50. _Quad __sinq(_Quad);
  51. _Quad __cosq(_Quad);
  52. _Quad __tanq(_Quad);
  53. _Quad __asinq(_Quad);
  54. _Quad __acosq(_Quad);
  55. _Quad __atanq(_Quad);
  56. _Quad __sinhq(_Quad);
  57. _Quad __coshq(_Quad);
  58. _Quad __tanhq(_Quad);
  59. _Quad __fmodq(_Quad, _Quad);
  60. _Quad __atan2q(_Quad, _Quad);
  61. #define ldexpq __ldexpq
  62. #define frexpq __frexpq
  63. #define fabsq __fabsq
  64. #define floorq __floorq
  65. #define ceilq __ceilq
  66. #define sqrtq __sqrtq
  67. #define truncq __truncq
  68. #define expq __expq
  69. #define powq __powq
  70. #define logq __logq
  71. #define log10q __log10q
  72. #define sinq __sinq
  73. #define cosq __cosq
  74. #define tanq __tanq
  75. #define asinq __asinq
  76. #define acosq __acosq
  77. #define atanq __atanq
  78. #define sinhq __sinhq
  79. #define coshq __coshq
  80. #define tanhq __tanhq
  81. #define fmodq __fmodq
  82. #define atan2q __atan2q
  83. }
  84. inline _Quad isnanq(_Quad v)
  85. {
  86. return v != v;
  87. }
  88. inline _Quad isinfq(_Quad v)
  89. {
  90. return __fabsq(v) > 1.18973149535723176508575932662800702e4932Q;
  91. }
  92. #endif
  93. namespace boost {
  94. namespace multiprecision {
  95. namespace backends {
  96. struct float128_backend;
  97. }
  98. using backends::float128_backend;
  99. template <>
  100. struct number_category<backends::float128_backend> : public mpl::int_<number_kind_floating_point>
  101. {};
  102. #if defined(BOOST_MP_USE_QUAD)
  103. template <>
  104. struct number_category<float128_type> : public mpl::int_<number_kind_floating_point>
  105. {};
  106. #endif
  107. typedef number<float128_backend, et_off> float128;
  108. #ifndef BOOST_NO_CXX11_CONSTEXPR
  109. namespace quad_constants {
  110. constexpr __float128 quad_min = static_cast<__float128>(1) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) / 1073741824;
  111. constexpr __float128 quad_denorm_min = static_cast<__float128>(1) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) / 5.5751862996326557854e+42;
  112. constexpr double dbl_mult = 8.9884656743115795386e+307; // This has one bit set only.
  113. constexpr __float128 quad_max = (static_cast<__float128>(1) - 9.62964972193617926527988971292463659e-35) // This now has all bits sets to 1
  114. * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * static_cast<__float128>(dbl_mult) * 65536;
  115. } // namespace quad_constants
  116. #define BOOST_MP_QUAD_MIN boost::multiprecision::quad_constants::quad_min
  117. #define BOOST_MP_QUAD_DENORM_MIN boost::multiprecision::quad_constants::quad_denorm_min
  118. #define BOOST_MP_QUAD_MAX boost::multiprecision::quad_constants::quad_max
  119. #else
  120. #define BOOST_MP_QUAD_MIN 3.36210314311209350626267781732175260e-4932Q
  121. #define BOOST_MP_QUAD_DENORM_MIN 6.475175119438025110924438958227646552e-4966Q
  122. #define BOOST_MP_QUAD_MAX 1.18973149535723176508575932662800702e4932Q
  123. #endif
  124. namespace backends {
  125. struct float128_backend
  126. {
  127. typedef mpl::list<signed char, short, int, long, boost::long_long_type> signed_types;
  128. typedef mpl::list<unsigned char, unsigned short,
  129. unsigned int, unsigned long, boost::ulong_long_type>
  130. unsigned_types;
  131. typedef mpl::list<float, double, long double> float_types;
  132. typedef int exponent_type;
  133. private:
  134. float128_type m_value;
  135. public:
  136. BOOST_CONSTEXPR float128_backend() BOOST_NOEXCEPT : m_value(0) {}
  137. BOOST_CONSTEXPR float128_backend(const float128_backend& o) BOOST_NOEXCEPT : m_value(o.m_value) {}
  138. BOOST_MP_CXX14_CONSTEXPR float128_backend& operator=(const float128_backend& o) BOOST_NOEXCEPT
  139. {
  140. m_value = o.m_value;
  141. return *this;
  142. }
  143. template <class T>
  144. BOOST_CONSTEXPR float128_backend(const T& i, const typename enable_if_c<is_convertible<T, float128_type>::value>::type* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval<float128_type&>() = std::declval<const T&>()))
  145. : m_value(i) {}
  146. template <class T>
  147. BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<is_arithmetic<T>::value || is_convertible<T, float128_type>::value, float128_backend&>::type operator=(const T& i) BOOST_NOEXCEPT_IF(noexcept(std::declval<float128_type&>() = std::declval<const T&>()))
  148. {
  149. m_value = i;
  150. return *this;
  151. }
  152. BOOST_MP_CXX14_CONSTEXPR float128_backend(long double const& f) : m_value(f)
  153. {
  154. if (::fabsl(f) > LDBL_MAX)
  155. m_value = (f < 0) ? -static_cast<__float128>(HUGE_VAL) : static_cast<__float128>(HUGE_VAL);
  156. }
  157. BOOST_MP_CXX14_CONSTEXPR float128_backend& operator=(long double const& f)
  158. {
  159. if (f > LDBL_MAX)
  160. m_value = static_cast<__float128>(HUGE_VAL);
  161. else if (-f > LDBL_MAX)
  162. m_value = -static_cast<__float128>(HUGE_VAL);
  163. else
  164. m_value = f;
  165. return *this;
  166. }
  167. float128_backend& operator=(const char* s)
  168. {
  169. #ifndef BOOST_MP_USE_QUAD
  170. char* p_end;
  171. m_value = strtoflt128(s, &p_end);
  172. if (p_end - s != (std::ptrdiff_t)std::strlen(s))
  173. {
  174. BOOST_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a floating point value"));
  175. }
  176. #else
  177. boost::multiprecision::detail::convert_from_string(*this, s);
  178. #endif
  179. return *this;
  180. }
  181. BOOST_MP_CXX14_CONSTEXPR void swap(float128_backend& o) BOOST_NOEXCEPT
  182. {
  183. // We don't call std::swap here because it's no constexpr (yet):
  184. float128_type t(o.value());
  185. o.value() = m_value;
  186. m_value = t;
  187. }
  188. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  189. {
  190. #ifndef BOOST_MP_USE_QUAD
  191. char buf[128];
  192. std::string format = "%";
  193. if (f & std::ios_base::showpos)
  194. format += "+";
  195. if (f & std::ios_base::showpoint)
  196. format += "#";
  197. format += ".*";
  198. if (digits == 0)
  199. digits = 36;
  200. format += "Q";
  201. if (f & std::ios_base::scientific)
  202. format += "e";
  203. else if (f & std::ios_base::fixed)
  204. format += "f";
  205. else
  206. format += "g";
  207. int v;
  208. if ((f & std::ios_base::scientific) && (f & std::ios_base::fixed))
  209. {
  210. v = quadmath_snprintf(buf, sizeof buf, "%Qa", m_value);
  211. }
  212. else
  213. {
  214. v = quadmath_snprintf(buf, sizeof buf, format.c_str(), digits, m_value);
  215. }
  216. if ((v < 0) || (v >= 127))
  217. {
  218. int v_max = v;
  219. boost::scoped_array<char> buf2;
  220. buf2.reset(new char[v + 3]);
  221. v = quadmath_snprintf(&buf2[0], v_max + 3, format.c_str(), digits, m_value);
  222. if (v >= v_max + 3)
  223. {
  224. BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
  225. }
  226. return &buf2[0];
  227. }
  228. return buf;
  229. #else
  230. return boost::multiprecision::detail::convert_to_string(*this, digits ? digits : 37, f);
  231. #endif
  232. }
  233. BOOST_MP_CXX14_CONSTEXPR void negate() BOOST_NOEXCEPT
  234. {
  235. m_value = -m_value;
  236. }
  237. BOOST_MP_CXX14_CONSTEXPR int compare(const float128_backend& o) const
  238. {
  239. return m_value == o.m_value ? 0 : m_value < o.m_value ? -1 : 1;
  240. }
  241. template <class T>
  242. BOOST_MP_CXX14_CONSTEXPR int compare(const T& i) const
  243. {
  244. return m_value == i ? 0 : m_value < i ? -1 : 1;
  245. }
  246. BOOST_MP_CXX14_CONSTEXPR float128_type& value()
  247. {
  248. return m_value;
  249. }
  250. BOOST_MP_CXX14_CONSTEXPR const float128_type& value() const
  251. {
  252. return m_value;
  253. }
  254. };
  255. inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a)
  256. {
  257. result.value() += a.value();
  258. }
  259. template <class A>
  260. inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const A& a)
  261. {
  262. result.value() += a;
  263. }
  264. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a)
  265. {
  266. result.value() -= a.value();
  267. }
  268. template <class A>
  269. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const A& a)
  270. {
  271. result.value() -= a;
  272. }
  273. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a)
  274. {
  275. result.value() *= a.value();
  276. }
  277. template <class A>
  278. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const A& a)
  279. {
  280. result.value() *= a;
  281. }
  282. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const float128_backend& a)
  283. {
  284. result.value() /= a.value();
  285. }
  286. template <class A>
  287. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const A& a)
  288. {
  289. result.value() /= a;
  290. }
  291. inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a, const float128_backend& b)
  292. {
  293. result.value() = a.value() + b.value();
  294. }
  295. template <class A>
  296. inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a, const A& b)
  297. {
  298. result.value() = a.value() + b;
  299. }
  300. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a, const float128_backend& b)
  301. {
  302. result.value() = a.value() - b.value();
  303. }
  304. template <class A>
  305. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a, const A& b)
  306. {
  307. result.value() = a.value() - b;
  308. }
  309. template <class A>
  310. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const A& a, const float128_backend& b)
  311. {
  312. result.value() = a - b.value();
  313. }
  314. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a, const float128_backend& b)
  315. {
  316. result.value() = a.value() * b.value();
  317. }
  318. template <class A>
  319. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a, const A& b)
  320. {
  321. result.value() = a.value() * b;
  322. }
  323. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const float128_backend& a, const float128_backend& b)
  324. {
  325. result.value() = a.value() / b.value();
  326. }
  327. template <class R>
  328. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(R* result, const float128_backend& val)
  329. {
  330. *result = static_cast<R>(val.value());
  331. }
  332. inline void eval_frexp(float128_backend& result, const float128_backend& arg, int* exp)
  333. {
  334. result.value() = frexpq(arg.value(), exp);
  335. }
  336. inline void eval_ldexp(float128_backend& result, const float128_backend& arg, int exp)
  337. {
  338. result.value() = ldexpq(arg.value(), exp);
  339. }
  340. inline void eval_floor(float128_backend& result, const float128_backend& arg)
  341. {
  342. result.value() = floorq(arg.value());
  343. }
  344. inline void eval_ceil(float128_backend& result, const float128_backend& arg)
  345. {
  346. result.value() = ceilq(arg.value());
  347. }
  348. inline void eval_sqrt(float128_backend& result, const float128_backend& arg)
  349. {
  350. result.value() = sqrtq(arg.value());
  351. }
  352. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  353. inline BOOST_MP_CXX14_CONSTEXPR
  354. #else
  355. inline
  356. #endif
  357. int eval_fpclassify(const float128_backend& arg)
  358. {
  359. float128_type v = arg.value();
  360. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  361. if (BOOST_MP_IS_CONST_EVALUATED(v))
  362. {
  363. if (v != v)
  364. return FP_NAN;
  365. if (v == 0)
  366. return FP_ZERO;
  367. float128_type t(v);
  368. if (t < 0)
  369. t = -t;
  370. if (t > BOOST_MP_QUAD_MAX)
  371. return FP_INFINITE;
  372. if (t < BOOST_MP_QUAD_MIN)
  373. return FP_SUBNORMAL;
  374. return FP_NORMAL;
  375. }
  376. else
  377. #endif
  378. {
  379. if (isnanq(v))
  380. return FP_NAN;
  381. else if (isinfq(v))
  382. return FP_INFINITE;
  383. else if (v == 0)
  384. return FP_ZERO;
  385. float128_backend t(arg);
  386. if (t.value() < 0)
  387. t.negate();
  388. if (t.value() < BOOST_MP_QUAD_MIN)
  389. return FP_SUBNORMAL;
  390. return FP_NORMAL;
  391. }
  392. }
  393. #if defined(BOOST_GCC) && (__GNUC__ == 9)
  394. // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91705
  395. inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(float128_backend& arg)
  396. {
  397. arg.value() = 1 + arg.value();
  398. }
  399. inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(float128_backend& arg)
  400. {
  401. arg.value() = arg.value() - 1;
  402. }
  403. #else
  404. inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(float128_backend& arg)
  405. {
  406. ++arg.value();
  407. }
  408. inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(float128_backend& arg)
  409. {
  410. --arg.value();
  411. }
  412. #endif
  413. /*********************************************************************
  414. *
  415. * abs/fabs:
  416. *
  417. *********************************************************************/
  418. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  419. inline BOOST_MP_CXX14_CONSTEXPR void eval_abs(float128_backend& result, const float128_backend& arg)
  420. #else
  421. inline void eval_abs(float128_backend& result, const float128_backend& arg)
  422. #endif
  423. {
  424. float128_type v(arg.value());
  425. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  426. if (BOOST_MP_IS_CONST_EVALUATED(v))
  427. {
  428. result.value() = v < 0 ? -v : v;
  429. }
  430. else
  431. #endif
  432. {
  433. result.value() = fabsq(arg.value());
  434. }
  435. }
  436. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  437. inline BOOST_MP_CXX14_CONSTEXPR void eval_fabs(float128_backend& result, const float128_backend& arg)
  438. #else
  439. inline void eval_fabs(float128_backend& result, const float128_backend& arg)
  440. #endif
  441. {
  442. float128_type v(arg.value());
  443. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  444. if (BOOST_MP_IS_CONST_EVALUATED(v))
  445. {
  446. result.value() = v < 0 ? -v : v;
  447. }
  448. else
  449. #endif
  450. {
  451. result.value() = fabsq(arg.value());
  452. }
  453. }
  454. /*********************************************************************
  455. *
  456. * Floating point functions:
  457. *
  458. *********************************************************************/
  459. inline void eval_trunc(float128_backend& result, const float128_backend& arg)
  460. {
  461. result.value() = truncq(arg.value());
  462. }
  463. /*
  464. //
  465. // This doesn't actually work... rely on our own default version instead.
  466. //
  467. inline void eval_round(float128_backend& result, const float128_backend& arg)
  468. {
  469. if(isnanq(arg.value()) || isinf(arg.value()))
  470. {
  471. result = boost::math::policies::raise_rounding_error(
  472. "boost::multiprecision::trunc<%1%>(%1%)", 0,
  473. number<float128_backend, et_off>(arg),
  474. number<float128_backend, et_off>(arg),
  475. boost::math::policies::policy<>()).backend();
  476. return;
  477. }
  478. result.value() = roundq(arg.value());
  479. }
  480. */
  481. inline void eval_exp(float128_backend& result, const float128_backend& arg)
  482. {
  483. result.value() = expq(arg.value());
  484. }
  485. inline void eval_log(float128_backend& result, const float128_backend& arg)
  486. {
  487. result.value() = logq(arg.value());
  488. }
  489. inline void eval_log10(float128_backend& result, const float128_backend& arg)
  490. {
  491. result.value() = log10q(arg.value());
  492. }
  493. inline void eval_sin(float128_backend& result, const float128_backend& arg)
  494. {
  495. result.value() = sinq(arg.value());
  496. }
  497. inline void eval_cos(float128_backend& result, const float128_backend& arg)
  498. {
  499. result.value() = cosq(arg.value());
  500. }
  501. inline void eval_tan(float128_backend& result, const float128_backend& arg)
  502. {
  503. result.value() = tanq(arg.value());
  504. }
  505. inline void eval_asin(float128_backend& result, const float128_backend& arg)
  506. {
  507. result.value() = asinq(arg.value());
  508. }
  509. inline void eval_acos(float128_backend& result, const float128_backend& arg)
  510. {
  511. result.value() = acosq(arg.value());
  512. }
  513. inline void eval_atan(float128_backend& result, const float128_backend& arg)
  514. {
  515. result.value() = atanq(arg.value());
  516. }
  517. inline void eval_sinh(float128_backend& result, const float128_backend& arg)
  518. {
  519. result.value() = sinhq(arg.value());
  520. }
  521. inline void eval_cosh(float128_backend& result, const float128_backend& arg)
  522. {
  523. result.value() = coshq(arg.value());
  524. }
  525. inline void eval_tanh(float128_backend& result, const float128_backend& arg)
  526. {
  527. result.value() = tanhq(arg.value());
  528. }
  529. inline void eval_fmod(float128_backend& result, const float128_backend& a, const float128_backend& b)
  530. {
  531. result.value() = fmodq(a.value(), b.value());
  532. }
  533. inline void eval_pow(float128_backend& result, const float128_backend& a, const float128_backend& b)
  534. {
  535. result.value() = powq(a.value(), b.value());
  536. }
  537. inline void eval_atan2(float128_backend& result, const float128_backend& a, const float128_backend& b)
  538. {
  539. result.value() = atan2q(a.value(), b.value());
  540. }
  541. #ifndef BOOST_MP_USE_QUAD
  542. inline void eval_multiply_add(float128_backend& result, const float128_backend& a, const float128_backend& b, const float128_backend& c)
  543. {
  544. result.value() = fmaq(a.value(), b.value(), c.value());
  545. }
  546. inline int eval_signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const float128_backend& arg)
  547. {
  548. return ::signbitq(arg.value());
  549. }
  550. #endif
  551. inline std::size_t hash_value(const float128_backend& val)
  552. {
  553. return boost::hash_value(static_cast<double>(val.value()));
  554. }
  555. } // namespace backends
  556. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  557. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  558. {
  559. return asinhq(arg.backend().value());
  560. }
  561. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  562. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  563. {
  564. return acoshq(arg.backend().value());
  565. }
  566. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  567. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  568. {
  569. return atanhq(arg.backend().value());
  570. }
  571. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  572. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  573. {
  574. return cbrtq(arg.backend().value());
  575. }
  576. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  577. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  578. {
  579. return erfq(arg.backend().value());
  580. }
  581. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  582. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  583. {
  584. return erfcq(arg.backend().value());
  585. }
  586. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  587. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  588. {
  589. return expm1q(arg.backend().value());
  590. }
  591. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  592. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  593. {
  594. return lgammaq(arg.backend().value());
  595. }
  596. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  597. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  598. {
  599. return tgammaq(arg.backend().value());
  600. }
  601. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  602. inline boost::multiprecision::number<float128_backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
  603. {
  604. return log1pq(arg.backend().value());
  605. }
  606. #ifndef BOOST_MP_USE_QUAD
  607. template <multiprecision::expression_template_option ExpressionTemplates>
  608. inline boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates>& a, const boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates>& b)
  609. {
  610. return ::copysignq(a.backend().value(), b.backend().value());
  611. }
  612. inline void eval_remainder(float128_backend& result, const float128_backend& a, const float128_backend& b)
  613. {
  614. result.value() = remainderq(a.value(), b.value());
  615. }
  616. inline void eval_remainder(float128_backend& result, const float128_backend& a, const float128_backend& b, int* pi)
  617. {
  618. result.value() = remquoq(a.value(), b.value(), pi);
  619. }
  620. #endif
  621. } // namespace multiprecision
  622. namespace math {
  623. using boost::multiprecision::copysign;
  624. using boost::multiprecision::signbit;
  625. } // namespace math
  626. } // namespace boost
  627. namespace boost {
  628. namespace archive {
  629. class binary_oarchive;
  630. class binary_iarchive;
  631. } // namespace archive
  632. namespace serialization {
  633. namespace float128_detail {
  634. template <class Archive>
  635. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::false_&)
  636. {
  637. // saving
  638. // non-binary
  639. std::string s(val.str(0, std::ios_base::scientific));
  640. ar& boost::make_nvp("value", s);
  641. }
  642. template <class Archive>
  643. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::false_&)
  644. {
  645. // loading
  646. // non-binary
  647. std::string s;
  648. ar& boost::make_nvp("value", s);
  649. val = s.c_str();
  650. }
  651. template <class Archive>
  652. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::true_&)
  653. {
  654. // saving
  655. // binary
  656. ar.save_binary(&val, sizeof(val));
  657. }
  658. template <class Archive>
  659. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::true_&)
  660. {
  661. // loading
  662. // binary
  663. ar.load_binary(&val, sizeof(val));
  664. }
  665. } // namespace float128_detail
  666. template <class Archive>
  667. void serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, unsigned int /*version*/)
  668. {
  669. typedef typename Archive::is_loading load_tag;
  670. typedef typename mpl::bool_<boost::is_same<Archive, boost::archive::binary_oarchive>::value || boost::is_same<Archive, boost::archive::binary_iarchive>::value> binary_tag;
  671. float128_detail::do_serialize(ar, val, load_tag(), binary_tag());
  672. }
  673. } // namespace serialization
  674. } // namespace boost
  675. namespace std {
  676. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  677. class numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >
  678. {
  679. typedef boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> number_type;
  680. public:
  681. BOOST_STATIC_CONSTEXPR bool is_specialized = true;
  682. static BOOST_MP_CXX14_CONSTEXPR number_type(min)() BOOST_NOEXCEPT { return BOOST_MP_QUAD_MIN; }
  683. static BOOST_MP_CXX14_CONSTEXPR number_type(max)() BOOST_NOEXCEPT { return BOOST_MP_QUAD_MAX; }
  684. static BOOST_MP_CXX14_CONSTEXPR number_type lowest() BOOST_NOEXCEPT { return -(max)(); }
  685. BOOST_STATIC_CONSTEXPR int digits = 113;
  686. BOOST_STATIC_CONSTEXPR int digits10 = 33;
  687. BOOST_STATIC_CONSTEXPR int max_digits10 = 36;
  688. BOOST_STATIC_CONSTEXPR bool is_signed = true;
  689. BOOST_STATIC_CONSTEXPR bool is_integer = false;
  690. BOOST_STATIC_CONSTEXPR bool is_exact = false;
  691. BOOST_STATIC_CONSTEXPR int radix = 2;
  692. static BOOST_MP_CXX14_CONSTEXPR number_type epsilon() { return 1.92592994438723585305597794258492732e-34; /* this double value has only one bit set and so is exact */ }
  693. static BOOST_MP_CXX14_CONSTEXPR number_type round_error() { return 0.5; }
  694. BOOST_STATIC_CONSTEXPR int min_exponent = -16381;
  695. BOOST_STATIC_CONSTEXPR int min_exponent10 = min_exponent * 301L / 1000L;
  696. BOOST_STATIC_CONSTEXPR int max_exponent = 16384;
  697. BOOST_STATIC_CONSTEXPR int max_exponent10 = max_exponent * 301L / 1000L;
  698. BOOST_STATIC_CONSTEXPR bool has_infinity = true;
  699. BOOST_STATIC_CONSTEXPR bool has_quiet_NaN = true;
  700. BOOST_STATIC_CONSTEXPR bool has_signaling_NaN = false;
  701. BOOST_STATIC_CONSTEXPR float_denorm_style has_denorm = denorm_present;
  702. BOOST_STATIC_CONSTEXPR bool has_denorm_loss = true;
  703. static BOOST_MP_CXX14_CONSTEXPR number_type infinity() { return HUGE_VAL; /* conversion from double infinity OK */ }
  704. static BOOST_MP_CXX14_CONSTEXPR number_type quiet_NaN() { return number_type("nan"); }
  705. static BOOST_MP_CXX14_CONSTEXPR number_type signaling_NaN() { return 0; }
  706. static BOOST_MP_CXX14_CONSTEXPR number_type denorm_min() { return BOOST_MP_QUAD_DENORM_MIN; }
  707. BOOST_STATIC_CONSTEXPR bool is_iec559 = true;
  708. BOOST_STATIC_CONSTEXPR bool is_bounded = true;
  709. BOOST_STATIC_CONSTEXPR bool is_modulo = false;
  710. BOOST_STATIC_CONSTEXPR bool traps = false;
  711. BOOST_STATIC_CONSTEXPR bool tinyness_before = false;
  712. BOOST_STATIC_CONSTEXPR float_round_style round_style = round_to_nearest;
  713. };
  714. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  715. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_specialized;
  716. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  717. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::digits;
  718. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  719. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::digits10;
  720. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  721. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_digits10;
  722. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  723. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_signed;
  724. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  725. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_integer;
  726. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  727. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_exact;
  728. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  729. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::radix;
  730. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  731. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::min_exponent;
  732. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  733. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_exponent;
  734. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  735. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::min_exponent10;
  736. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  737. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_exponent10;
  738. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  739. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_infinity;
  740. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  741. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_quiet_NaN;
  742. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  743. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_signaling_NaN;
  744. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  745. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_denorm_loss;
  746. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  747. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_iec559;
  748. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  749. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_bounded;
  750. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  751. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_modulo;
  752. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  753. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::traps;
  754. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  755. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::tinyness_before;
  756. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  757. BOOST_CONSTEXPR_OR_CONST float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::round_style;
  758. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  759. BOOST_CONSTEXPR_OR_CONST float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_denorm;
  760. } // namespace std
  761. #endif