float128.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 http://www.boost.org/LICENSE_1_
  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/multiprecision/number.hpp>
  10. #if defined(BOOST_INTEL) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  11. # if defined(BOOST_INTEL_CXX_VERSION) && (BOOST_INTEL_CXX_VERSION >= 1310) && defined(__GNUC__)
  12. # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))
  13. # define BOOST_MP_USE_FLOAT128
  14. # endif
  15. # endif
  16. # ifndef BOOST_MP_USE_FLOAT128
  17. # define BOOST_MP_USE_QUAD
  18. # endif
  19. #endif
  20. #if defined(__GNUC__) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  21. # define BOOST_MP_USE_FLOAT128
  22. #endif
  23. #if !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
  24. # error "Sorry compiler is neither GCC, not Intel, don't know how to configure this header."
  25. #endif
  26. #if defined(BOOST_MP_USE_FLOAT128) && defined(BOOST_MP_USE_QUAD)
  27. # error "Oh dear, both BOOST_MP_USE_FLOAT128 and BOOST_MP_USE_QUAD are defined, which one should I be using?"
  28. #endif
  29. #if defined(BOOST_MP_USE_FLOAT128)
  30. extern "C" {
  31. #include <quadmath.h>
  32. }
  33. typedef __float128 float128_type;
  34. #elif defined(BOOST_MP_USE_QUAD)
  35. #include <boost/multiprecision/detail/float_string_cvt.hpp>
  36. typedef _Quad float128_type;
  37. extern "C" {
  38. _Quad __ldexpq(_Quad, int);
  39. _Quad __frexpq(_Quad, int*);
  40. _Quad __fabsq(_Quad);
  41. _Quad __floorq(_Quad);
  42. _Quad __ceilq(_Quad);
  43. _Quad __sqrtq(_Quad);
  44. _Quad __truncq(_Quad);
  45. _Quad __expq(_Quad);
  46. _Quad __powq(_Quad, _Quad);
  47. _Quad __logq(_Quad);
  48. _Quad __log10q(_Quad);
  49. _Quad __sinq(_Quad);
  50. _Quad __cosq(_Quad);
  51. _Quad __tanq(_Quad);
  52. _Quad __asinq(_Quad);
  53. _Quad __acosq(_Quad);
  54. _Quad __atanq(_Quad);
  55. _Quad __sinhq(_Quad);
  56. _Quad __coshq(_Quad);
  57. _Quad __tanhq(_Quad);
  58. _Quad __fmodq(_Quad, _Quad);
  59. _Quad __atan2q(_Quad, _Quad);
  60. #define ldexpq __ldexpq
  61. #define frexpq __frexpq
  62. #define fabsq __fabsq
  63. #define floorq __floorq
  64. #define ceilq __ceilq
  65. #define sqrtq __sqrtq
  66. #define truncq __truncq
  67. #define expq __expq
  68. #define powq __powq
  69. #define logq __logq
  70. #define log10q __log10q
  71. #define sinq __sinq
  72. #define cosq __cosq
  73. #define tanq __tanq
  74. #define asinq __asinq
  75. #define acosq __acosq
  76. #define atanq __atanq
  77. #define sinhq __sinhq
  78. #define coshq __coshq
  79. #define tanhq __tanhq
  80. #define fmodq __fmodq
  81. #define atan2q __atan2q
  82. }
  83. inline _Quad isnanq(_Quad v)
  84. {
  85. return v != v;
  86. }
  87. inline _Quad isinfq(_Quad v)
  88. {
  89. return __fabsq(v) > 1.18973149535723176508575932662800702e4932Q;
  90. }
  91. #endif
  92. namespace boost{
  93. namespace multiprecision{
  94. namespace backends{
  95. struct float128_backend;
  96. }
  97. using backends::float128_backend;
  98. template<>
  99. struct number_category<backends::float128_backend> : public mpl::int_<number_kind_floating_point> {};
  100. template<>
  101. struct number_category<float128_type> : public mpl::int_<number_kind_floating_point> {};
  102. typedef number<float128_backend, et_off> float128;
  103. namespace backends{
  104. struct float128_backend
  105. {
  106. typedef mpl::list<signed char, short, int, long, long long> signed_types;
  107. typedef mpl::list<unsigned char, unsigned short,
  108. unsigned int, unsigned long, unsigned long long> unsigned_types;
  109. typedef mpl::list<float, double, long double> float_types;
  110. typedef int exponent_type;
  111. private:
  112. float128_type m_value;
  113. public:
  114. BOOST_CONSTEXPR float128_backend() : m_value(0) {}
  115. BOOST_CONSTEXPR float128_backend(const float128_backend& o) : m_value(o.m_value) {}
  116. float128_backend& operator = (const float128_backend& o)
  117. {
  118. m_value = o.m_value;
  119. return *this;
  120. }
  121. template <class T>
  122. BOOST_CONSTEXPR float128_backend(const T& i, const typename enable_if_c<is_convertible<T, float128_type>::value>::type* = 0)
  123. : m_value(i) {}
  124. template <class T>
  125. typename enable_if_c<is_arithmetic<T>::value || is_convertible<T, float128_type>::value, float128_backend&>::type operator = (const T& i)
  126. {
  127. m_value = i;
  128. return *this;
  129. }
  130. float128_backend& operator = (const char* s)
  131. {
  132. #ifndef BOOST_MP_USE_QUAD
  133. char* p_end;
  134. m_value = strtoflt128(s, &p_end);
  135. if(p_end - s != (std::ptrdiff_t)std::strlen(s))
  136. {
  137. BOOST_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a floating point value"));
  138. }
  139. #else
  140. detail::convert_from_string(*this, s);
  141. #endif
  142. return *this;
  143. }
  144. void swap(float128_backend& o)
  145. {
  146. std::swap(m_value, o.value());
  147. }
  148. std::string str(std::streamsize digits, std::ios_base::fmtflags f)const
  149. {
  150. #ifndef BOOST_MP_USE_QUAD
  151. char buf[100];
  152. boost::scoped_array<char> buf2;
  153. std::string format = "%";
  154. if(f & std::ios_base::showpos)
  155. format += "+";
  156. if(f & std::ios_base::showpoint)
  157. format += "#";
  158. format += ".*";
  159. if(digits == 0)
  160. digits = 36;
  161. format += "Q";
  162. if(f & std::ios_base::scientific)
  163. format += "e";
  164. else if(f & std::ios_base::fixed)
  165. format += "f";
  166. else
  167. format += "g";
  168. int v = quadmath_snprintf (buf, 100, format.c_str(), digits, m_value);
  169. if((v < 0) || (v >= 99))
  170. {
  171. int v_max = v;
  172. buf2.reset(new char[v+3]);
  173. v = quadmath_snprintf (&buf2[0], v_max + 3, format.c_str(), digits, m_value);
  174. if(v >= v_max + 3)
  175. {
  176. BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
  177. }
  178. return &buf2[0];
  179. }
  180. return buf;
  181. #else
  182. return detail::convert_to_string(*this, digits ? digits : 37, f);
  183. #endif
  184. }
  185. void negate()
  186. {
  187. m_value = -m_value;
  188. }
  189. int compare(const float128_backend& o)const
  190. {
  191. return m_value == o.m_value ? 0 : m_value < o.m_value ? -1 : 1;
  192. }
  193. template <class T>
  194. int compare(const T& i)const
  195. {
  196. return m_value == i ? 0 : m_value < i ? -1 : 1;
  197. }
  198. float128_type& value()
  199. {
  200. return m_value;
  201. }
  202. const float128_type& value()const
  203. {
  204. return m_value;
  205. }
  206. };
  207. inline void eval_add(float128_backend& result, const float128_backend& a)
  208. {
  209. result.value() += a.value();
  210. }
  211. template <class A>
  212. inline void eval_add(float128_backend& result, const A& a)
  213. {
  214. result.value() += a;
  215. }
  216. inline void eval_subtract(float128_backend& result, const float128_backend& a)
  217. {
  218. result.value() -= a.value();
  219. }
  220. template <class A>
  221. inline void eval_subtract(float128_backend& result, const A& a)
  222. {
  223. result.value() -= a;
  224. }
  225. inline void eval_multiply(float128_backend& result, const float128_backend& a)
  226. {
  227. result.value() *= a.value();
  228. }
  229. template <class A>
  230. inline void eval_multiply(float128_backend& result, const A& a)
  231. {
  232. result.value() *= a;
  233. }
  234. inline void eval_divide(float128_backend& result, const float128_backend& a)
  235. {
  236. result.value() /= a.value();
  237. }
  238. template <class A>
  239. inline void eval_divide(float128_backend& result, const A& a)
  240. {
  241. result.value() /= a;
  242. }
  243. inline void eval_add(float128_backend& result, const float128_backend& a, const float128_backend& b)
  244. {
  245. result.value() = a.value() + b.value();
  246. }
  247. template <class A>
  248. inline void eval_add(float128_backend& result, const float128_backend& a, const A& b)
  249. {
  250. result.value() = a.value() + b;
  251. }
  252. inline void eval_subtract(float128_backend& result, const float128_backend& a, const float128_backend& b)
  253. {
  254. result.value() = a.value() - b.value();
  255. }
  256. template <class A>
  257. inline void eval_subtract(float128_backend& result, const float128_backend& a, const A& b)
  258. {
  259. result.value() = a.value() - b;
  260. }
  261. template <class A>
  262. inline void eval_subtract(float128_backend& result, const A& a, const float128_backend& b)
  263. {
  264. result.value() = a - b.value();
  265. }
  266. inline void eval_multiply(float128_backend& result, const float128_backend& a, const float128_backend& b)
  267. {
  268. result.value() = a.value() * b.value();
  269. }
  270. template <class A>
  271. inline void eval_multiply(float128_backend& result, const float128_backend& a, const A& b)
  272. {
  273. result.value() = a.value() * b;
  274. }
  275. inline void eval_divide(float128_backend& result, const float128_backend& a, const float128_backend& b)
  276. {
  277. result.value() = a.value() / b.value();
  278. }
  279. template <class R>
  280. inline void eval_convert_to(R* result, const float128_backend& val)
  281. {
  282. *result = static_cast<R>(val.value());
  283. }
  284. inline void eval_frexp(float128_backend& result, const float128_backend& arg, int* exp)
  285. {
  286. result.value() = frexpq(arg.value(), exp);
  287. }
  288. inline void eval_ldexp(float128_backend& result, const float128_backend& arg, int exp)
  289. {
  290. result.value() = ldexpq(arg.value(), exp);
  291. }
  292. inline void eval_floor(float128_backend& result, const float128_backend& arg)
  293. {
  294. result.value() = floorq(arg.value());
  295. }
  296. inline void eval_ceil(float128_backend& result, const float128_backend& arg)
  297. {
  298. result.value() = ceilq(arg.value());
  299. }
  300. inline void eval_sqrt(float128_backend& result, const float128_backend& arg)
  301. {
  302. result.value() = sqrtq(arg.value());
  303. }
  304. inline int eval_fpclassify(const float128_backend& arg)
  305. {
  306. return isnanq(arg.value()) ? FP_NAN : isinfq(arg.value()) ? FP_INFINITE : arg.value() == 0 ? FP_ZERO : FP_NORMAL;
  307. }
  308. inline void eval_increment(float128_backend& arg)
  309. {
  310. ++arg.value();
  311. }
  312. inline void eval_decrement(float128_backend& arg)
  313. {
  314. --arg.value();
  315. }
  316. /*********************************************************************
  317. *
  318. * abs/fabs:
  319. *
  320. *********************************************************************/
  321. inline void eval_abs(float128_backend& result, const float128_backend& arg)
  322. {
  323. result.value() = fabsq(arg.value());
  324. }
  325. inline void eval_fabs(float128_backend& result, const float128_backend& arg)
  326. {
  327. result.value() = fabsq(arg.value());
  328. }
  329. /*********************************************************************
  330. *
  331. * Floating point functions:
  332. *
  333. *********************************************************************/
  334. inline void eval_trunc(float128_backend& result, const float128_backend& arg)
  335. {
  336. if(isnanq(arg.value()) || isinfq(arg.value()))
  337. {
  338. result = boost::math::policies::raise_rounding_error(
  339. "boost::multiprecision::trunc<%1%>(%1%)", 0,
  340. number<float128_backend, et_off>(arg),
  341. number<float128_backend, et_off>(arg),
  342. boost::math::policies::policy<>()).backend();
  343. return;
  344. }
  345. result.value() = truncq(arg.value());
  346. }
  347. /*
  348. //
  349. // This doesn't actually work... rely on our own default version instead.
  350. //
  351. inline void eval_round(float128_backend& result, const float128_backend& arg)
  352. {
  353. if(isnanq(arg.value()) || isinf(arg.value()))
  354. {
  355. result = boost::math::policies::raise_rounding_error(
  356. "boost::multiprecision::trunc<%1%>(%1%)", 0,
  357. number<float128_backend, et_off>(arg),
  358. number<float128_backend, et_off>(arg),
  359. boost::math::policies::policy<>()).backend();
  360. return;
  361. }
  362. result.value() = roundq(arg.value());
  363. }
  364. */
  365. inline void eval_exp(float128_backend& result, const float128_backend& arg)
  366. {
  367. result.value() = expq(arg.value());
  368. }
  369. inline void eval_log(float128_backend& result, const float128_backend& arg)
  370. {
  371. result.value() = logq(arg.value());
  372. }
  373. inline void eval_log10(float128_backend& result, const float128_backend& arg)
  374. {
  375. result.value() = log10q(arg.value());
  376. }
  377. inline void eval_sin(float128_backend& result, const float128_backend& arg)
  378. {
  379. result.value() = sinq(arg.value());
  380. }
  381. inline void eval_cos(float128_backend& result, const float128_backend& arg)
  382. {
  383. result.value() = cosq(arg.value());
  384. }
  385. inline void eval_tan(float128_backend& result, const float128_backend& arg)
  386. {
  387. result.value() = tanq(arg.value());
  388. }
  389. inline void eval_asin(float128_backend& result, const float128_backend& arg)
  390. {
  391. result.value() = asinq(arg.value());
  392. }
  393. inline void eval_acos(float128_backend& result, const float128_backend& arg)
  394. {
  395. result.value() = acosq(arg.value());
  396. }
  397. inline void eval_atan(float128_backend& result, const float128_backend& arg)
  398. {
  399. result.value() = atanq(arg.value());
  400. }
  401. inline void eval_sinh(float128_backend& result, const float128_backend& arg)
  402. {
  403. result.value() = sinhq(arg.value());
  404. }
  405. inline void eval_cosh(float128_backend& result, const float128_backend& arg)
  406. {
  407. result.value() = coshq(arg.value());
  408. }
  409. inline void eval_tanh(float128_backend& result, const float128_backend& arg)
  410. {
  411. result.value() = tanhq(arg.value());
  412. }
  413. inline void eval_fmod(float128_backend& result, const float128_backend& a, const float128_backend& b)
  414. {
  415. result.value() = fmodq(a.value(), b.value());
  416. }
  417. inline void eval_pow(float128_backend& result, const float128_backend& a, const float128_backend& b)
  418. {
  419. result.value() = powq(a.value(), b.value());
  420. }
  421. inline void eval_atan2(float128_backend& result, const float128_backend& a, const float128_backend& b)
  422. {
  423. result.value() = atan2q(a.value(), b.value());
  424. }
  425. } // namespace backends
  426. }} // namespaces
  427. namespace boost{
  428. namespace archive{
  429. class binary_oarchive;
  430. class binary_iarchive;
  431. }
  432. namespace serialization{ namespace float128_detail{
  433. template <class Archive>
  434. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::false_&)
  435. {
  436. // saving
  437. // non-binary
  438. std::string s(val.str(0, std::ios_base::scientific));
  439. ar & s;
  440. }
  441. template <class Archive>
  442. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::false_&)
  443. {
  444. // loading
  445. // non-binary
  446. std::string s;
  447. ar & s;
  448. val = s.c_str();
  449. }
  450. template <class Archive>
  451. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::true_&)
  452. {
  453. // saving
  454. // binary
  455. ar.save_binary(&val, sizeof(val));
  456. }
  457. template <class Archive>
  458. void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::true_&)
  459. {
  460. // loading
  461. // binary
  462. ar.load_binary(&val, sizeof(val));
  463. }
  464. } // detail
  465. template <class Archive>
  466. void serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, unsigned int /*version*/)
  467. {
  468. typedef typename Archive::is_loading load_tag;
  469. typedef typename mpl::bool_<boost::is_same<Archive, boost::archive::binary_oarchive>::value || boost::is_same<Archive, boost::archive::binary_iarchive>::value> binary_tag;
  470. float128_detail::do_serialize(ar, val, load_tag(), binary_tag());
  471. }
  472. }}
  473. namespace std{
  474. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  475. class numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >
  476. {
  477. typedef boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> number_type;
  478. public:
  479. BOOST_STATIC_CONSTEXPR bool is_specialized = true;
  480. static number_type (min)() BOOST_NOEXCEPT { return 3.36210314311209350626267781732175260e-4932Q; }
  481. static number_type (max)() BOOST_NOEXCEPT { return 1.18973149535723176508575932662800702e4932Q; }
  482. static number_type lowest() BOOST_NOEXCEPT { return -(max)(); }
  483. BOOST_STATIC_CONSTEXPR int digits = 113;
  484. BOOST_STATIC_CONSTEXPR int digits10 = 34;
  485. BOOST_STATIC_CONSTEXPR int max_digits10 = 36;
  486. BOOST_STATIC_CONSTEXPR bool is_signed = true;
  487. BOOST_STATIC_CONSTEXPR bool is_integer = false;
  488. BOOST_STATIC_CONSTEXPR bool is_exact = false;
  489. BOOST_STATIC_CONSTEXPR int radix = 2;
  490. static number_type epsilon() { return 1.92592994438723585305597794258492732e-34Q; }
  491. static number_type round_error() { return 0.5; }
  492. BOOST_STATIC_CONSTEXPR int min_exponent = -16381;
  493. BOOST_STATIC_CONSTEXPR int min_exponent10 = min_exponent * 301L / 1000L;
  494. BOOST_STATIC_CONSTEXPR int max_exponent = 16384;
  495. BOOST_STATIC_CONSTEXPR int max_exponent10 = max_exponent * 301L / 1000L;
  496. BOOST_STATIC_CONSTEXPR bool has_infinity = true;
  497. BOOST_STATIC_CONSTEXPR bool has_quiet_NaN = true;
  498. BOOST_STATIC_CONSTEXPR bool has_signaling_NaN = false;
  499. BOOST_STATIC_CONSTEXPR float_denorm_style has_denorm = denorm_absent;
  500. BOOST_STATIC_CONSTEXPR bool has_denorm_loss = false;
  501. static number_type infinity() { return 1.0q / 0.0q; }
  502. static number_type quiet_NaN() { return number_type("nan"); }
  503. static number_type signaling_NaN() { return 0; }
  504. static number_type denorm_min() { return 0; }
  505. BOOST_STATIC_CONSTEXPR bool is_iec559 = true;
  506. BOOST_STATIC_CONSTEXPR bool is_bounded = false;
  507. BOOST_STATIC_CONSTEXPR bool is_modulo = false;
  508. BOOST_STATIC_CONSTEXPR bool traps = false;
  509. BOOST_STATIC_CONSTEXPR bool tinyness_before = false;
  510. BOOST_STATIC_CONSTEXPR float_round_style round_style = round_to_nearest;
  511. };
  512. } // namespace std
  513. #endif