safe_numerics.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <library dirname="safe_numerics" id="safe_numerics" last-revision="$Date"
  5. name="Safe Numerics">
  6. <title>Safe Numerics</title>
  7. <libraryinfo last-revision="January 29, 2015">
  8. <author>
  9. <firstname>Robert</firstname>
  10. <surname>Ramey</surname>
  11. </author>
  12. <copyright>
  13. <year>2012-2018</year>
  14. <holder>Robert Ramey</holder>
  15. </copyright>
  16. <legalnotice>
  17. <para><ulink url="http://www.boost.org/LICENSE_1_0.txt">Subject to Boost
  18. Software License</ulink></para>
  19. </legalnotice>
  20. <librarypurpose>Safe integer operations</librarypurpose>
  21. <librarycategory name="Numerics">Numerics</librarycategory>
  22. </libraryinfo>
  23. <xi:include href="safe_introduction.xml" xpointer="element(/1)"
  24. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  25. <xi:include href="tutorial.xml" xpointer="element(/1)"
  26. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  27. <xi:include href="eliminate_runtime_penalty.xml" xpointer="element(/1)"
  28. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  29. <section id="safe_numerics.case_studies">
  30. <title>Case Studies</title>
  31. <xi:include href="rational.xml" xpointer="element(/1)"
  32. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  33. <xi:include href="motor.xml" xpointer="element(/1)"
  34. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  35. </section>
  36. <xi:include href="notes.xml" xpointer="element(/1)"
  37. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  38. <section id="safe_numerics.concepts">
  39. <title>Type Requirements</title>
  40. <xi:include href="numeric_concept.xml" xpointer="element(/1)"
  41. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  42. <xi:include href="integer_concept.xml" xpointer="element(/1)"
  43. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  44. <xi:include href="safe_numeric_concept.xml" xpointer="element(/1)"
  45. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  46. <xi:include href="promotion_policy_concept.xml" xpointer="element(/1)"
  47. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  48. <xi:include href="exception_policy_concept.xml" xpointer="element(/1)"
  49. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  50. </section>
  51. <section id="safe_numerics.types">
  52. <title>Types</title>
  53. <xi:include href="safe.xml" xpointer="element(/1)"
  54. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  55. <xi:include href="safe_range.xml" xpointer="element(/1)"
  56. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  57. <xi:include href="safe_literal.xml" xpointer="element(/1)"
  58. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  59. <xi:include href="exception.xml" xpointer="element(/1)"
  60. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  61. <xi:include href="exception_policy.xml" xpointer="element(/1)"
  62. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  63. <section id="safe_numerics.promotion_policies">
  64. <title>Promotion Policies</title>
  65. <xi:include href="native.xml" xpointer="element(/1)"
  66. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  67. <xi:include href="automatic.xml" xpointer="element(/1)"
  68. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  69. <xi:include href="cpp.xml" xpointer="element(/1)"
  70. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  71. </section>
  72. </section>
  73. <section id="safe_numerics.exception_safety">
  74. <title>Exception Safety</title>
  75. <para>All operations in this library are exception safe and meet the
  76. strong guarantee.</para>
  77. </section>
  78. <section id="safe_numerics.library_implementation">
  79. <title>Library Implementation</title>
  80. <para>This library should compile and run correctly on any conforming
  81. C++14 compiler.</para>
  82. <para>The Safe Numerics library is implemented in terms of some more
  83. fundamental software components described here. It is not necessary to
  84. know about these components to use the library. This information has been
  85. included to help those who want to understand how the library works so
  86. they can extend it, correct bugs in it, or understand its limitations.
  87. These components are also interesting and likely useful in their own
  88. right. For all these reasons, they are documented here.</para>
  89. <para>In general terms, the library works in the following manner:</para>
  90. <para>At compile time:</para>
  91. <itemizedlist>
  92. <listitem>
  93. <para>The library defines "safe" versions of C++ primitive arithmetic
  94. types such as <code>int</code>, <code>unsigned int</code>, etc.</para>
  95. </listitem>
  96. <listitem>
  97. <para>Arithmetic operators are defined for these "safe" types. These
  98. operators are enhanced versions of the standard C/C++ implementations.
  99. These operators are declared and implemented in the files "<ulink
  100. url="../../include/boost/safe_numerics/safe_base.hpp">safe_base.hpp</ulink>"
  101. and "<ulink
  102. url="../../include/boost/safe_numerics/safe_base_operations.hpp">safe_base_operations.hpp</ulink>".</para>
  103. </listitem>
  104. <listitem>
  105. <para>For binary operators, verify that both operands have the same
  106. promotion and and exception handling policies. If they don't, invoke
  107. compilation error.</para>
  108. </listitem>
  109. <listitem>
  110. <para>Invoke the promotion policy to determine the result type R of
  111. the operation.</para>
  112. </listitem>
  113. <listitem>
  114. <para>For each operand of type T retrieve the range of values from
  115. <code>std::numeric_limits&lt;T&gt;::min()</code> and
  116. <code>std::numeric_limits&lt;T&gt;::max()</code>. A range is a pair of
  117. values representing a closed interval with a minimum and maximum
  118. value.</para>
  119. </listitem>
  120. <listitem>
  121. <para>These ranges are cast to equivalent values of the result type,
  122. R. It's possible that values cannot be cast to the result type so the
  123. result of the cast is returned as a variant type, <link
  124. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.
  125. <link
  126. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>
  127. may hold either a value of type R or a <link
  128. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link>
  129. value indicating why the cast could not be accomplished. Ranges are
  130. represented as a pair of values of the type <link
  131. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.</para>
  132. </listitem>
  133. <listitem>
  134. <para><link
  135. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>
  136. can be considered enhanced versions of the underlying type R.
  137. Operations which are legal on values of type R such as +, -, ... are
  138. also legal on values of <link
  139. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.
  140. The difference is that the latter can record operation failures and
  141. propagate such failures to subsequent operations.<link
  142. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>
  143. is implemented in the header file "<ulink
  144. url="../../include/boost/safe_numerics/checked_result.hpp">checked_result.hpp</ulink>".
  145. Operations on such types are implemented in "<ulink
  146. url="../../include/boost/safe_numerics/checked_result_operations.hpp">checked_result_operations.hpp</ulink>".</para>
  147. </listitem>
  148. <listitem>
  149. <para>Given the ranges of the operands, determine the range of the
  150. result of the operation using compile-time interval arithmetic. The
  151. <code>constexpr</code> facility of C++14 permits the range of the
  152. result to be calculated at compile time. Interval arithmetic is
  153. implemented in the header file "<ulink
  154. url="../../include/boost/safe_numerics/interval.hpp">interval.hpp</ulink>".
  155. The range of the result is also represented as a pair of values of the
  156. type <link
  157. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.</para>
  158. </listitem>
  159. <listitem>
  160. <para>Operations on primitives are implemented via free standing
  161. functions described as <link
  162. linkend="safe_numerics.checked_arithmetic">checked arithmetic</link>.
  163. These operations will return instances of <link
  164. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>
  165. .</para>
  166. </listitem>
  167. </itemizedlist>
  168. <para>At run time:</para>
  169. <itemizedlist>
  170. <listitem>
  171. <para>If the range of the result type includes only arithmetically
  172. valid values, the operation is guaranteed to produce an arithmetically
  173. correct result and no runtime checking is necessary. The operation
  174. invokes the original built-in C/C++ operation and returns the result
  175. value.</para>
  176. </listitem>
  177. <listitem>
  178. <para>Otherwise, operands are cast to the result type, R, according to
  179. the selected promotion policy. These "checked" cast operations return
  180. values of type <link
  181. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.</para>
  182. </listitem>
  183. <listitem>
  184. <para>If either of the casting operations fails, an exception is
  185. handled in accordance with the exception policy.</para>
  186. </listitem>
  187. <listitem>
  188. <para>Otherwise, the operation is performed using "<link
  189. linkend="safe_numerics.checked_arithmetic">checked arithmetic</link>".
  190. These free functions mirror the normal operators +, -, *, ... except
  191. that rather than returning values of type R, they return values of the
  192. type <link
  193. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.
  194. They are defined in files "<ulink
  195. url="../../include/boost/safe_numerics/checked_default.hpp">checked_default.hpp</ulink>",
  196. "<ulink
  197. url="../../include/boost/safe_numerics/checked_integer.hpp">checked_integer.hpp</ulink>"
  198. ,"<ulink
  199. url="../../include/boost/safe_numerics/checked_float.hpp">checked_float.hpp</ulink>".</para>
  200. </listitem>
  201. <listitem>
  202. <para>If the operation is not successful, the designated exception
  203. policy function is invoked.</para>
  204. </listitem>
  205. <listitem>
  206. <para>Otherwise, the result value is returned as a
  207. <code>safe&lt;R&gt;</code> type with the above calculated result
  208. range.</para>
  209. </listitem>
  210. </itemizedlist>
  211. <para>The following components realize the design described here.</para>
  212. <xi:include href="checked_result.xml" xpointer="element(/1)"
  213. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  214. <xi:include href="checked.xml" xpointer="element(/1)"
  215. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  216. <xi:include href="interval.xml" xpointer="element(/1)"
  217. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  218. <xi:include href="safe_compare.xml" xpointer="element(/1)"
  219. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  220. </section>
  221. <section id="safe_numerics.performance_tests">
  222. <title>Performance Tests</title>
  223. <para>Our goal is to create facilities which make it possible to write
  224. programs known to be correct. But we also want programmers to actually use
  225. the facilities we provide here. This won't happen if using these
  226. facilities impacts performance to a significant degree. Although we've
  227. taken precautions to avoid doing this, the only real way to know is to
  228. create and run some tests.</para>
  229. <para>So far we've only run one explicit performance test -
  230. <filename><ulink
  231. url="../../test/test_performance.cpp">test_performance.cpp</ulink></filename>.
  232. This runs a test from the Boost Multiprecision library to count prime
  233. numbers and uses on integer arithmetic. We've run the tests with
  234. <code>unsigned</code> integers and with <code>safe&lt;unsigned&gt;</code>
  235. on two different compilers.. No other change was made to the program. We
  236. list the results without further comment.</para>
  237. <screen>g++ (GCC) 6.2.0
  238. Testing type unsigned:
  239. time = 17.6215
  240. count = 1857858
  241. Testing type safe&lt;unsigned&gt;:
  242. time = 22.4226
  243. count = 1857858
  244. clang-802.0.41
  245. Testing type unsigned:
  246. time = 16.9174
  247. count = 1857858
  248. Testing type safe&lt;unsigned&gt;:
  249. time = 36.5166
  250. count = 1857858
  251. </screen>
  252. </section>
  253. <xi:include href="faq.xml" xpointer="element(/1)"
  254. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  255. <xi:include href="pending.xml" xpointer="element(/1)"
  256. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  257. <xi:include href="acknowledgements.xml" xpointer="element(/1)"
  258. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  259. <section id="safe_numerics.change_log">
  260. <title>Release Log</title>
  261. <para>This is the third version.</para>
  262. <para><revhistory>
  263. <revision>
  264. <revnumber>1.69</revnumber>
  265. <date>29 September 2018</date>
  266. <revdescription>
  267. <para>First Boost Release</para>
  268. </revdescription>
  269. </revision>
  270. <revision>
  271. <revnumber>1.70</revnumber>
  272. <date>9 March 2019</date>
  273. <revdescription>
  274. <para>Fixed Exception Policies for <code>trap</code> and
  275. <code>ignore</code>.</para>
  276. </revdescription>
  277. </revision>
  278. </revhistory></para>
  279. </section>
  280. <xi:include href="bibliography.xml" xpointer="element(/1)"
  281. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  282. </library>