promotion_policy_concept.xml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.promotion_policy">
  5. <title>PromotionPolicy&lt;PP&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>In C++, arithmetic operations result in types which may or may not
  10. be the same as the constituent types. A promotion policy determines the
  11. type of the result of an arithmetic operation. For example, in the
  12. following code<programlisting>int x;
  13. char y;
  14. auto z = x + y</programlisting>the type of <code>z</code> will be an
  15. <code>int</code>. This is a consequence for the standard rules for type
  16. promotion for C/C++ arithmetic. A key feature of library permits one to
  17. specify his own type promotion rules via a PromotionPolicy class.</para>
  18. </section>
  19. <section>
  20. <title>Notation</title>
  21. <informaltable>
  22. <tgroup cols="2" colsep="1" rowsep="1">
  23. <colspec align="left" colwidth="1*"/>
  24. <colspec align="left" colwidth="4*"/>
  25. <tbody>
  26. <row>
  27. <entry><code>PP</code></entry>
  28. <entry>A type that full fills the requirements of a
  29. PromotionPollicy</entry>
  30. </row>
  31. <row>
  32. <entry><code>T, U</code></entry>
  33. <entry>A type that is a model of the Numeric concept</entry>
  34. </row>
  35. <row>
  36. <entry><code>R</code></entry>
  37. <entry>An object of type modeling Numeric which can be used to
  38. construct a SafeNumeric type.</entry>
  39. </row>
  40. </tbody>
  41. </tgroup>
  42. </informaltable>
  43. </section>
  44. <section>
  45. <title>Valid Expressions</title>
  46. <para>Any operations which result in integers which cannot be represented
  47. as some Numeric type will throw an exception.These expressions return a
  48. type which can be used as the basis create a SafeNumeric type.</para>
  49. <para><informaltable>
  50. <tgroup cols="2">
  51. <colspec align="left"/>
  52. <colspec align="left"/>
  53. <thead>
  54. <row>
  55. <entry align="left">Expression</entry>
  56. <entry>Return Value</entry>
  57. </row>
  58. </thead>
  59. <tbody>
  60. <row>
  61. <entry><code>PP::addition_result&lt;T,
  62. U&gt;::type</code></entry>
  63. <entry>unspecified Numeric type</entry>
  64. </row>
  65. <row>
  66. <entry><code>PP::subtraction_result&lt;T,
  67. U&gt;::type</code></entry>
  68. <entry>unspecified Numeric type</entry>
  69. </row>
  70. <row>
  71. <entry><code>PP::multiplication_result&lt;T,
  72. U&gt;::type</code></entry>
  73. <entry>unspecified Numeric type</entry>
  74. </row>
  75. <row>
  76. <entry><code>PP::division_result&lt;T,
  77. U&gt;::type</code></entry>
  78. <entry>unspecified Numeric type</entry>
  79. </row>
  80. <row>
  81. <entry><code>PP::modulus_result&lt;T, U&gt;::type</code></entry>
  82. <entry>unspecified Numeric type</entry>
  83. </row>
  84. <row>
  85. <entry><code>PP::comparison_result&lt;T,
  86. U&gt;::type</code></entry>
  87. <entry>bool</entry>
  88. </row>
  89. <row>
  90. <entry><code>PP::left_shift_result&lt;T,
  91. U&gt;::type</code></entry>
  92. <entry>unspecified Numeric type</entry>
  93. </row>
  94. <row>
  95. <entry><code>PP::right_shift_result&lt;T,
  96. u&gt;::type</code></entry>
  97. <entry>unspecified Numeric type</entry>
  98. </row>
  99. <row>
  100. <entry><code>PP::bitwise_or_result&lt;T,
  101. U&gt;::type</code></entry>
  102. <entry>unspecified Numeric type</entry>
  103. </row>
  104. <row>
  105. <entry><code>PP::bitwise_and_result&lt;T,
  106. U&gt;::type</code></entry>
  107. <entry>unspecified Numeric type</entry>
  108. </row>
  109. <row>
  110. <entry><code>PP::bitwise_xor_result&lt;T,
  111. U&gt;::type</code></entry>
  112. <entry>unspecified Numeric type</entry>
  113. </row>
  114. </tbody>
  115. </tgroup>
  116. </informaltable></para>
  117. </section>
  118. <section>
  119. <title>Models</title>
  120. <para>The library contains a number of pre-made promotion policies:</para>
  121. <itemizedlist>
  122. <listitem id="safe_numerics.promotion_policy.models.native">
  123. <para><code>boost::numeric::native</code></para>
  124. <para>Use the normal C/C++ expression type promotion rules.
  125. <programlisting name="cpp">int x;
  126. char y;
  127. auto z = x + y; // could result in overflow
  128. safe&lt;int, native&gt; sx;
  129. auto sz = sx + y; // standard C++ code which detects errors</programlisting></para>
  130. <para>Type sz will be a <link
  131. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link> type
  132. based on <code>int</code>. If the result exceeds the maximum value
  133. that can be stored in an <code>int</code>, an error is
  134. detected.</para>
  135. <para>The <code>native</code> policy is documented in <link
  136. linkend="safe_numerics.promotion_policies.native">Promotion Policies -
  137. native</link>.</para>
  138. </listitem>
  139. <listitem id="safe_numerics.promotion_policy.models.automatic">
  140. <para><code>boost::numeric::automatic</code></para>
  141. <para>Use optimizing expression type promotion rules. These rules
  142. replace the normal C/C++ type promotion rules with other rules which
  143. are designed to result in more efficient computations. Expression
  144. types are promoted to the smallest type which can be guaranteed to
  145. hold the result without overflow. If there is no such type, the result
  146. will be checked for overflow. Consider the following
  147. example:<programlisting>int x;
  148. char y;
  149. auto z = x + y; // could result in overflow
  150. safe&lt;int, automatic&gt; sx;
  151. auto sz = sx + y;
  152. // sz is a safe type based on long
  153. // hence sz is guaranteed not to overflow.
  154. safe_unsigned_range&lt;1, 4&gt; a;
  155. safe_unsigned_range&lt;2, 4&gt; b;
  156. auto c = a + b; // c will be a safe type with a range [3,8] and cannot overflow
  157. </programlisting></para>
  158. <para>Type sz will be a <link
  159. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link> type
  160. which is guaranteed to hold he result of x + y. In this case that will
  161. be a long int (or perhaps a long long) depending upon the compiler and
  162. machine architecture. In this case, there will be no need for any
  163. special checking on the result and there can be no overflow.</para>
  164. <para>Type of c will be a signed character as that type can be
  165. guaranteed to hold the sum so no overflow checking is done.</para>
  166. <para>This policy is documented in <link
  167. linkend="safe_numerics.promotion_policies.automatic">Promotion
  168. Policies - automatic</link></para>
  169. </listitem>
  170. <listitem>
  171. <para>boost::numeric::cpp</para>
  172. <para>Use expression type promotion rules to emulate another
  173. processor. When this policy is used, C++ type for safe integers
  174. follows the rules that a compiler on the target processor would use.
  175. This permits one to test code destined for a one processor on the
  176. another one. One situation there this can be very, very useful is when
  177. testing code destined for a micro controller which doesn't have the
  178. logging, debugging, input/output facilities of a
  179. desktop.<programlisting>// specify a promotion policy to support proper emulation of
  180. // PIC 18f2520 types on the desktop
  181. using pic16_promotion = boost::numeric::cpp&lt;
  182. 8, // char 8 bits
  183. 16, // short 16 bits
  184. 16, // int 16 bits
  185. 16, // long 16 bits
  186. 32 // long long 32 bits
  187. &gt;;
  188. ...
  189. safe&lt;std::uint16_t, pic16_promotion&gt; x, y;
  190. ...
  191. x + y; // detect possible overflow on the pic.</programlisting></para>
  192. <para>For a complete example see <link
  193. linkend="safe_numerics.safety_critical_embedded_controller">Safety
  194. Critical Embedded Controller</link>.</para>
  195. </listitem>
  196. </itemizedlist>
  197. </section>
  198. <section>
  199. <title>Header</title>
  200. <para><ulink
  201. url="../../include/boost/safe_numerics/concept/promotion_policy.hpp"><code>#include
  202. &lt;boost/numeric/safe_numerics/concepts/promotion_policy.hpp&gt;
  203. </code></ulink></para>
  204. </section>
  205. </section>