accu.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <article>
  5. <articleinfo>
  6. <title>Correct Integer Operations With Minimal Runtime Penalties</title>
  7. <author>
  8. <firstname>Robert</firstname>
  9. <surname>Ramey</surname>
  10. <affiliation>
  11. <orgname>Robert Ramey Software Development</orgname>
  12. </affiliation>
  13. </author>
  14. <pubdate>22 December 2016</pubdate>
  15. </articleinfo>
  16. <xi:include href="../safe_introduction.xml" xpointer="element(/1)"
  17. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  18. <xi:include href="../eliminate_runtime_penalty.xml" xpointer="element(/1)"
  19. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  20. <xi:include href="../notes.xml" xpointer="element(/1)"
  21. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  22. <section id="safe_numerics.library_implementation">
  23. <title>Library Internals</title>
  24. <para>This library should compile and run correctly on any conforming
  25. C++14 compiler.</para>
  26. <para>The Safe Numerics library is implemented in terms of some more
  27. fundamental software components described here. It is not necessary to
  28. know about these components to use the library. This information has been
  29. included to help those who want to understand how the library works so
  30. they can extend it, correct bugs in it, or understand it's limitations.
  31. These components are also interesting in their own right. For all these
  32. reasons, they are described here. In general terms, the library works in
  33. the following manner:</para>
  34. <itemizedlist>
  35. <listitem>
  36. <para>All unary/binary expressions where one of the operands is a
  37. "safe" type are Overloaded. These overloads are declared and defined
  38. in the header file "safe_integer.hpp". SFINAE - "Substitution Failure
  39. Is Not An Error and <code>std::enable_if</code> are key features of
  40. C++ used to define these overloads in a correct manner.</para>
  41. </listitem>
  42. <listitem>
  43. <para>Each overloaded operation implements the following at compile
  44. time:<itemizedlist>
  45. <listitem>
  46. <para>Retrieve range of values for each operand of type T from
  47. <code>std::numeric_limits&lt;T&gt;::min()</code> and
  48. <code>std::numeric_limits&lt;T&gt;::max()</code>.</para>
  49. </listitem>
  50. <listitem>
  51. <para>Given the ranges of the operands, determine the range of
  52. the result of the operation using interval arithmetic. This is
  53. implemented in the "interval.hpp" header file using constexpr
  54. facility of C++14.</para>
  55. </listitem>
  56. <listitem>
  57. <para>if the range of the result type includes the range of the
  58. result of the operation, no run time checking of the result is
  59. necessary. So the operation reduces to the original built-in
  60. C/C++ operation.</para>
  61. </listitem>
  62. <listitem>
  63. <para>Otherwise, the operation is implemented as a "checked
  64. integer operation" at run time. This operation returns a variant
  65. which will contain either a correct result or an exception enum
  66. indicating why a correct result could not be obtained. The
  67. variant object is implemented in the header file
  68. "checked_result.hpp" and the checked operations are implemented
  69. in "checked.hpp".</para>
  70. </listitem>
  71. <listitem>
  72. <para>if a valid result has been obtained, it is passed to the
  73. caller.</para>
  74. </listitem>
  75. <listitem>
  76. <para>Otherwise, an exception is invoked.</para>
  77. </listitem>
  78. </itemizedlist></para>
  79. </listitem>
  80. </itemizedlist>
  81. </section>
  82. <xi:include href="../faq.xml" xpointer="element(/1)"
  83. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  84. <section id="safe_numerics.pending_issues">
  85. <title>Current Status</title>
  86. <para>The library is currently in the <ulink
  87. url="http://www.boost.org/community/review_schedule.html">Boost Review
  88. Queue</ulink>. The proposal submission can be found in the <ulink
  89. url="http://blincubator.com/bi_library/safe-numerics/?gform_post_id=426">Boost
  90. Library Incubator</ulink></para>
  91. <itemizedlist>
  92. <listitem>
  93. <para>The library is currently limited to integers.</para>
  94. </listitem>
  95. <listitem>
  96. <para>Although care was taking to make the library portable, it's
  97. likely that at least some parts of the implementation - particularly
  98. <code>checked</code> arithmetic - depend upon two's complement
  99. representation of integers. Hence the library is probably not
  100. currently portable to other architectures.</para>
  101. </listitem>
  102. <listitem>
  103. <para>Currently the library permits a <code>safe&lt;int&gt;</code>
  104. value to be uninitialized. This supports the goal of "drop-in"
  105. replacement of C++/C built-in types with safe counter parts. On the
  106. other hand, this breaks the "always valid" guarantee.</para>
  107. </listitem>
  108. <listitem>
  109. <para>The library is not quite a "drop-in" replacement for all
  110. built-in integer types. In particular, C/C++ implements implicit
  111. conversions and promotions between certain integer types which are not
  112. captured by the operation overloads used to implement the library. In
  113. practice these case are few and can be addressed with minor changes to
  114. the user program to avoid these silent implicit conversions.</para>
  115. </listitem>
  116. </itemizedlist>
  117. </section>
  118. <xi:include href="../acknowledgements.xml" xpointer="element(/1)"
  119. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  120. <xi:include href="../bibliography.xml" xpointer="element(/1)"
  121. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  122. </article>