saturating.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // saturating.cpp ----------------------------------------------------------//
  2. // Copyright 2008 Howard Hinnant
  3. // Copyright 2008 Beman Dawes
  4. // Copyright 2009 Vicente J. Botet Escriba
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. /*
  8. This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
  9. was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
  10. Many thanks to Howard for making his code available under the Boost license.
  11. The original code was modified to conform to Boost conventions and to section
  12. 20.9 Time utilities [time] of the C++ committee's working paper N2798.
  13. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
  14. time2_demo contained this comment:
  15. Much thanks to Andrei Alexandrescu,
  16. Walter Brown,
  17. Peter Dimov,
  18. Jeff Garland,
  19. Terry Golubiewski,
  20. Daniel Krugler,
  21. Anthony Williams.
  22. */
  23. #define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
  24. #include <boost/chrono/chrono.hpp>
  25. #include <boost/type_traits.hpp>
  26. #include <iostream>
  27. //////////////////////////////////////////////////////////
  28. //////////////////// User2 Example ///////////////////////
  29. //////////////////////////////////////////////////////////
  30. // Demonstrate User2:
  31. // A "saturating" signed integral type is developed. This type has +/- infinity and a nan
  32. // (like IEEE floating point) but otherwise obeys signed integral arithmetic.
  33. // This class is subsequently used as the rep in boost::chrono::duration to demonstrate a
  34. // duration class that does not silently ignore overflow.
  35. #include <ostream>
  36. #include <stdexcept>
  37. #include <climits>
  38. namespace User2
  39. {
  40. template <class I>
  41. class saturate
  42. {
  43. public:
  44. typedef I int_type;
  45. static const int_type nan = int_type(int_type(1) << (sizeof(int_type) * CHAR_BIT - 1));
  46. static const int_type neg_inf = nan + 1;
  47. static const int_type pos_inf = -neg_inf;
  48. private:
  49. int_type i_;
  50. // static_assert(std::is_integral<int_type>::value && std::is_signed<int_type>::value,
  51. // "saturate only accepts signed integral types");
  52. // static_assert(nan == -nan && neg_inf < pos_inf,
  53. // "saturate assumes two's complement hardware for signed integrals");
  54. public:
  55. saturate() : i_(nan) {}
  56. explicit saturate(int_type i) : i_(i) {}
  57. // explicit
  58. operator int_type() const;
  59. saturate& operator+=(saturate x);
  60. saturate& operator-=(saturate x) {return *this += -x;}
  61. saturate& operator*=(saturate x);
  62. saturate& operator/=(saturate x);
  63. saturate& operator%=(saturate x);
  64. saturate operator- () const {return saturate(-i_);}
  65. saturate& operator++() {*this += saturate(int_type(1)); return *this;}
  66. saturate operator++(int) {saturate tmp(*this); ++(*this); return tmp;}
  67. saturate& operator--() {*this -= saturate(int_type(1)); return *this;}
  68. saturate operator--(int) {saturate tmp(*this); --(*this); return tmp;}
  69. friend saturate operator+(saturate x, saturate y) {return x += y;}
  70. friend saturate operator-(saturate x, saturate y) {return x -= y;}
  71. friend saturate operator*(saturate x, saturate y) {return x *= y;}
  72. friend saturate operator/(saturate x, saturate y) {return x /= y;}
  73. friend saturate operator%(saturate x, saturate y) {return x %= y;}
  74. friend bool operator==(saturate x, saturate y)
  75. {
  76. if (x.i_ == nan || y.i_ == nan)
  77. return false;
  78. return x.i_ == y.i_;
  79. }
  80. friend bool operator!=(saturate x, saturate y) {return !(x == y);}
  81. friend bool operator<(saturate x, saturate y)
  82. {
  83. if (x.i_ == nan || y.i_ == nan)
  84. return false;
  85. return x.i_ < y.i_;
  86. }
  87. friend bool operator<=(saturate x, saturate y)
  88. {
  89. if (x.i_ == nan || y.i_ == nan)
  90. return false;
  91. return x.i_ <= y.i_;
  92. }
  93. friend bool operator>(saturate x, saturate y)
  94. {
  95. if (x.i_ == nan || y.i_ == nan)
  96. return false;
  97. return x.i_ > y.i_;
  98. }
  99. friend bool operator>=(saturate x, saturate y)
  100. {
  101. if (x.i_ == nan || y.i_ == nan)
  102. return false;
  103. return x.i_ >= y.i_;
  104. }
  105. friend std::ostream& operator<<(std::ostream& os, saturate s)
  106. {
  107. switch (s.i_)
  108. {
  109. case pos_inf:
  110. return os << "inf";
  111. case nan:
  112. return os << "nan";
  113. case neg_inf:
  114. return os << "-inf";
  115. };
  116. return os << s.i_;
  117. }
  118. };
  119. template <class I>
  120. saturate<I>::operator I() const
  121. {
  122. switch (i_)
  123. {
  124. case nan:
  125. case neg_inf:
  126. case pos_inf:
  127. throw std::out_of_range("saturate special value can not convert to int_type");
  128. }
  129. return i_;
  130. }
  131. template <class I>
  132. saturate<I>&
  133. saturate<I>::operator+=(saturate x)
  134. {
  135. switch (i_)
  136. {
  137. case pos_inf:
  138. switch (x.i_)
  139. {
  140. case neg_inf:
  141. case nan:
  142. i_ = nan;
  143. }
  144. return *this;
  145. case nan:
  146. return *this;
  147. case neg_inf:
  148. switch (x.i_)
  149. {
  150. case pos_inf:
  151. case nan:
  152. i_ = nan;
  153. }
  154. return *this;
  155. }
  156. switch (x.i_)
  157. {
  158. case pos_inf:
  159. case neg_inf:
  160. case nan:
  161. i_ = x.i_;
  162. return *this;
  163. }
  164. if (x.i_ >= 0)
  165. {
  166. if (i_ < pos_inf - x.i_)
  167. i_ += x.i_;
  168. else
  169. i_ = pos_inf;
  170. return *this;
  171. }
  172. if (i_ > neg_inf - x.i_)
  173. i_ += x.i_;
  174. else
  175. i_ = neg_inf;
  176. return *this;
  177. }
  178. template <class I>
  179. saturate<I>&
  180. saturate<I>::operator*=(saturate x)
  181. {
  182. switch (i_)
  183. {
  184. case 0:
  185. switch (x.i_)
  186. {
  187. case pos_inf:
  188. case neg_inf:
  189. case nan:
  190. i_ = nan;
  191. }
  192. return *this;
  193. case pos_inf:
  194. switch (x.i_)
  195. {
  196. case nan:
  197. case 0:
  198. i_ = nan;
  199. return *this;
  200. }
  201. if (x.i_ < 0)
  202. i_ = neg_inf;
  203. return *this;
  204. case nan:
  205. return *this;
  206. case neg_inf:
  207. switch (x.i_)
  208. {
  209. case nan:
  210. case 0:
  211. i_ = nan;
  212. return *this;
  213. }
  214. if (x.i_ < 0)
  215. i_ = pos_inf;
  216. return *this;
  217. }
  218. switch (x.i_)
  219. {
  220. case 0:
  221. i_ = 0;
  222. return *this;
  223. case nan:
  224. i_ = nan;
  225. return *this;
  226. case pos_inf:
  227. if (i_ < 0)
  228. i_ = neg_inf;
  229. else
  230. i_ = pos_inf;
  231. return *this;
  232. case neg_inf:
  233. if (i_ < 0)
  234. i_ = pos_inf;
  235. else
  236. i_ = neg_inf;
  237. return *this;
  238. }
  239. int s = (i_ < 0 ? -1 : 1) * (x.i_ < 0 ? -1 : 1);
  240. i_ = i_ < 0 ? -i_ : i_;
  241. int_type x_i_ = x.i_ < 0 ? -x.i_ : x.i_;
  242. if (i_ <= pos_inf / x_i_)
  243. i_ *= x_i_;
  244. else
  245. i_ = pos_inf;
  246. i_ *= s;
  247. return *this;
  248. }
  249. template <class I>
  250. saturate<I>&
  251. saturate<I>::operator/=(saturate x)
  252. {
  253. switch (x.i_)
  254. {
  255. case pos_inf:
  256. case neg_inf:
  257. switch (i_)
  258. {
  259. case pos_inf:
  260. case neg_inf:
  261. case nan:
  262. i_ = nan;
  263. break;
  264. default:
  265. i_ = 0;
  266. break;
  267. }
  268. return *this;
  269. case nan:
  270. i_ = nan;
  271. return *this;
  272. case 0:
  273. switch (i_)
  274. {
  275. case pos_inf:
  276. case neg_inf:
  277. case nan:
  278. return *this;
  279. case 0:
  280. i_ = nan;
  281. return *this;
  282. }
  283. if (i_ > 0)
  284. i_ = pos_inf;
  285. else
  286. i_ = neg_inf;
  287. return *this;
  288. }
  289. switch (i_)
  290. {
  291. case 0:
  292. case nan:
  293. return *this;
  294. case pos_inf:
  295. case neg_inf:
  296. if (x.i_ < 0)
  297. i_ = -i_;
  298. return *this;
  299. }
  300. i_ /= x.i_;
  301. return *this;
  302. }
  303. template <class I>
  304. saturate<I>&
  305. saturate<I>::operator%=(saturate x)
  306. {
  307. // *this -= *this / x * x; // definition
  308. switch (x.i_)
  309. {
  310. case nan:
  311. case neg_inf:
  312. case 0:
  313. case pos_inf:
  314. i_ = nan;
  315. return *this;
  316. }
  317. switch (i_)
  318. {
  319. case neg_inf:
  320. case pos_inf:
  321. i_ = nan;
  322. case nan:
  323. return *this;
  324. }
  325. i_ %= x.i_;
  326. return *this;
  327. }
  328. // Demo overflow-safe integral durations ranging from picoseconds resolution to millennium resolution
  329. typedef boost::chrono::duration<saturate<long long>, boost::pico > picoseconds;
  330. typedef boost::chrono::duration<saturate<long long>, boost::nano > nanoseconds;
  331. typedef boost::chrono::duration<saturate<long long>, boost::micro > microseconds;
  332. typedef boost::chrono::duration<saturate<long long>, boost::milli > milliseconds;
  333. typedef boost::chrono::duration<saturate<long long> > seconds;
  334. typedef boost::chrono::duration<saturate<long long>, boost::ratio< 60LL> > minutes;
  335. typedef boost::chrono::duration<saturate<long long>, boost::ratio< 3600LL> > hours;
  336. typedef boost::chrono::duration<saturate<long long>, boost::ratio< 86400LL> > days;
  337. typedef boost::chrono::duration<saturate<long long>, boost::ratio< 31556952LL> > years;
  338. typedef boost::chrono::duration<saturate<long long>, boost::ratio<31556952000LL> > millennium;
  339. } // User2
  340. // Demonstrate custom promotion rules (needed only if there are no implicit conversions)
  341. namespace User2 { namespace detail {
  342. template <class T1, class T2, bool = boost::is_integral<T1>::value>
  343. struct promote_helper;
  344. template <class T1, class T2>
  345. struct promote_helper<T1, saturate<T2>, true> // integral
  346. {
  347. typedef typename boost::common_type<T1, T2>::type rep;
  348. typedef User2::saturate<rep> type;
  349. };
  350. template <class T1, class T2>
  351. struct promote_helper<T1, saturate<T2>, false> // floating
  352. {
  353. typedef T1 type;
  354. };
  355. } }
  356. namespace boost
  357. {
  358. template <class T1, class T2>
  359. struct common_type<User2::saturate<T1>, User2::saturate<T2> >
  360. {
  361. typedef typename common_type<T1, T2>::type rep;
  362. typedef User2::saturate<rep> type;
  363. };
  364. template <class T1, class T2>
  365. struct common_type<T1, User2::saturate<T2> >
  366. : User2::detail::promote_helper<T1, User2::saturate<T2> > {};
  367. template <class T1, class T2>
  368. struct common_type<User2::saturate<T1>, T2>
  369. : User2::detail::promote_helper<T2, User2::saturate<T1> > {};
  370. // Demonstrate specialization of duration_values:
  371. namespace chrono {
  372. template <class I>
  373. struct duration_values<User2::saturate<I> >
  374. {
  375. typedef User2::saturate<I> Rep;
  376. public:
  377. static Rep zero() {return Rep(0);}
  378. static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return Rep(Rep::pos_inf-1);}
  379. static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return -(max)();}
  380. };
  381. } // namespace chrono
  382. } // namespace boost
  383. #include <iostream>
  384. void testUser2()
  385. {
  386. std::cout << "*************\n";
  387. std::cout << "* testUser2 *\n";
  388. std::cout << "*************\n";
  389. using namespace User2;
  390. typedef seconds::rep sat;
  391. years yr(sat(100));
  392. std::cout << "100 years expressed as years = " << yr.count() << '\n';
  393. nanoseconds ns = yr;
  394. std::cout << "100 years expressed as nanoseconds = " << ns.count() << '\n';
  395. ns += yr;
  396. std::cout << "200 years expressed as nanoseconds = " << ns.count() << '\n';
  397. ns += yr;
  398. std::cout << "300 years expressed as nanoseconds = " << ns.count() << '\n';
  399. // yr = ns; // does not compile
  400. std::cout << "yr = ns; // does not compile\n";
  401. // picoseconds ps1 = yr; // does not compile, compile-time overflow in ratio arithmetic
  402. std::cout << "ps = yr; // does not compile\n";
  403. ns = yr;
  404. picoseconds ps = ns;
  405. std::cout << "100 years expressed as picoseconds = " << ps.count() << '\n';
  406. ps = ns / sat(1000);
  407. std::cout << "0.1 years expressed as picoseconds = " << ps.count() << '\n';
  408. yr = years(sat(-200000000));
  409. std::cout << "200 million years ago encoded in years: " << yr.count() << '\n';
  410. days d = boost::chrono::duration_cast<days>(yr);
  411. std::cout << "200 million years ago encoded in days: " << d.count() << '\n';
  412. millennium c = boost::chrono::duration_cast<millennium>(yr);
  413. std::cout << "200 million years ago encoded in millennium: " << c.count() << '\n';
  414. std::cout << "Demonstrate \"uninitialized protection\" behavior:\n";
  415. seconds sec;
  416. for (++sec; sec < seconds(sat(10)); ++sec)
  417. ;
  418. std::cout << sec.count() << '\n';
  419. std::cout << "\n";
  420. }
  421. void testStdUser()
  422. {
  423. std::cout << "***************\n";
  424. std::cout << "* testStdUser *\n";
  425. std::cout << "***************\n";
  426. using namespace boost::chrono;
  427. hours hr = hours(100);
  428. std::cout << "100 hours expressed as hours = " << hr.count() << '\n';
  429. nanoseconds ns = hr;
  430. std::cout << "100 hours expressed as nanoseconds = " << ns.count() << '\n';
  431. ns += hr;
  432. std::cout << "200 hours expressed as nanoseconds = " << ns.count() << '\n';
  433. ns += hr;
  434. std::cout << "300 hours expressed as nanoseconds = " << ns.count() << '\n';
  435. // hr = ns; // does not compile
  436. std::cout << "hr = ns; // does not compile\n";
  437. // hr * ns; // does not compile
  438. std::cout << "hr * ns; // does not compile\n";
  439. duration<double> fs(2.5);
  440. std::cout << "duration<double> has count() = " << fs.count() << '\n';
  441. // seconds sec = fs; // does not compile
  442. std::cout << "seconds sec = duration<double> won't compile\n";
  443. seconds sec = duration_cast<seconds>(fs);
  444. std::cout << "seconds has count() = " << sec.count() << '\n';
  445. std::cout << "\n";
  446. }
  447. int main()
  448. {
  449. testStdUser();
  450. testUser2();
  451. return 0;
  452. }