    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Correct Integer Operations with Minimal Runtime Penalties</title>
        <link>https://members.accu.org/index.php/articles/2344</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #137 - February 2017</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c370/">o137</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-370/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+370/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Correct Integer Operations with Minimal Runtime Penalties</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 02 February 2017 15:58:44 +00:00 or Thu, 02 February 2017 15:58:44 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Results of C++ integer operations are not guaranteed to be arithmetically correct. Robert Ramey introduces a library to enforce correct behaviour.</p>
<p><strong>Body:</strong>&nbsp;<p>This library is intended as a drop-in replacement for all built-in integer types in any program which must:</p>

<ul>
	<li>be demonstrably and verifiably correct.</li>
	
	<li>detect every user error such as input, assignment, etc.</li>
	
	<li>be efficient as possible subject to the constraints above.</li>
</ul>

<h2>Problem</h2>

<p>Arithmetic operations in C/C++ are NOT guaranteed to yield a correct mathematical result. This feature is inherited from the early days of C. The behavior of <code>int</code>, <code>unsigned int</code> and others were designed to map closely to the underlying hardware. Computer hardware implements these types as a fixed number of bits. When the result of arithmetic operations exceeds this number of bits, the result will not be arithmetically correct. The following is just one example of where this causes problems:</p>

<pre class="programlisting">
  int f(int x, int y){
    // this returns an invalid result for some
    // legal values of x and y !
    return x + y;
  }</pre>
  
<p>It is incumbent on the C/C++ programmer to guarantee that this behavior does not result in incorrect or unexpected operation of the program. There are no language facilities which implement such a guarantee. A programmer needs to examine each expression individually to know that his program will not return an invalid result.There are a number of ways to do this. In the above instance, INT32-C seems to recommend the following approach:</p>

<pre class="programlisting">
  int f(int x, int y){
    if (((y &gt; 0) &amp;&amp; (x &gt; (INT_MAX - y)))
    || ((y &lt; 0) &amp;&amp; (x &lt; (INT_MIN - y)))) {
      /* Handle error */
    }
    return x + y;
  }</pre>
  
<p>This will indeed trap the error. However, it would be tedious and laborious for a programmer to alter his code to do so. Altering code in this way for all arithmetic operations would likely render the code unreadable and add another source of potential programming errors. This approach is clearly not functional when the expression is even a little more complex as is shown in the following example.</p>

<pre class="programlisting">
  int f(int x, int y, int z){
    // this returns an invalid result for some 
    // legal values of x and y !
    return x + y * z;
  }</pre>
  
<p>This example addresses only the problem of undefined/erroneous behavior related to overflow of the addition operation as applied to the type <code>int</code>. Similar problems occur with other built-in integer types such as <code>unsigned</code>, <code>long</code>, etc. And it also applies to other operations such as subtraction, multiplication etc. C/C++ often automatically and silently converts some integer types to others in the course of implementing binary operations and similar problems occur in this case as well. Since the problems and their solution are similar, weâ€™ll confine the current discussion to just this example.</p>

<h2>Solution</h2>

<p>This library implements special versions of <code>int</code>, <code>unsigned</code>, etc. which behave exactly like the original ones <strong>except</strong> that the results of these operations are guaranteed to be either arithmetically correct or invoke an error. Using this library, the above example would be rendered as:</p>

<pre class="programlisting">
  #include &lt;boost/safe_numeric/safe_integer.hpp&gt;
  using namespace boost::numeric;
  safe&lt;int&gt; f(safe&lt;int&gt; x, safe&lt;int&gt; y){
    return x + y; // throw exception if correct
                  // result cannot be returned
  }</pre>
  
<p class="lesson">Library code in this document resides in the name space <code>boost::numeric</code>. This name space has generally been eliminated from text, code and examples in order to improve readability of the text.</p>

<p>The addition expression is checked at runtime or (if possible) at compile time to trap any possible errors resulting in incorrect arithmetic behavior. This will permit one to write arithmetic expressions that cannot produce an erroneous result. Instead, one and only one of the following is guaranteed to occur.</p>

<ul>
	<li>the expression will yield the correct mathematical result</li>
	
	<li>the expression will emit a compilation error.</li>
	
	<li>the expression will invoke a runtime exception.</li>
</ul>

<p>In other words, the <strong>library absolutely guarantees that no arithmetic expression will yield incorrect results</strong>.</p>

<h2>How it works</h2>

<p>The library implements special versions of <code>int</code>, <code>unsigned</code>, etc. named <code>safe&lt;int&gt;</code>, <code>safe&lt;unsigned int&gt;</code>, etc. These behave exactly like the underlying types <strong>except</strong> that expressions using these types fulfill the above guarantee. These types are meant to be â€˜drop-inâ€™ replacements for the built-in types they are meant to replace. So things which are legal â€“ such as assignment of a <code>signed</code> to an <code>unsigned</code> value â€“ are not trapped at compile time, as they are legal C/C++ code. Instead, they are checked at runtime to trap the case where this (legal) operation would lead to an arithmetically incorrect result.</p>

<p>Note that the library addresses arithmetical errors generated by straightforward C/C++ expressions. Some of these arithmetic errors are defined as conforming to the C/C++ standards while others are not. So characterizing this library as addressing undefined behavior of C/C++ numeric expressions would be misleading.</p>

<p>Facilities particular to C++14 are employed to minimize any runtime overhead. In many cases there is no runtime overhead at all. In other cases, a program using the library can be slightly altered to achieve the above guarantee without any runtime overhead.</p>

<h2>Additional features</h2>

<p>Operation of safe types is determined by template parameters which specify a pair of policy classes which specify the behavior for type promotion and error handling. In addition to the usage serving as a drop-in replacement for standard integer types, users of the library can:</p>

<ul>
	<li>Select or define an exception policy class to specify handling of exceptions.
		<ul>
			<li>throw exception on runtime, trap at compile time.</li>
			<li>trap at compiler time all operations which might fail at runtime.</li>
			<li>specify custom functions which should be called at runtime</li>
		</ul>
	</li>
	<li>Select or define a promotion policy class to alter the C/C++ type promotion rules. This can be used to
		<ul>
			<li>use C/C++ native type promotion rules so that, except for throwing/trapping of exceptions on operations resulting in incorrect arithmetic behavior, programs will operate identically when using/not using safe types.</li>
			
			<li>replace C/C++ native promotion rules with ones which are arithmetically equivalent but minimize the need for runtime checking of arithmetic results.</li>
			
			<li>replace C/C++ native promotion rules with ones which emulate other machine architectures. This is designed to permit the testing of C/C++ code destined to be run on another machine on oneâ€™s development platform. Such a situation often occurs while developing code for embedded systems.</li>
		</ul>
	</li>
	<li>Enforce of other program requirements using ranged integer types. The library includes the types <code>safe_range&lt;Min, Max&gt;</code> and <code>safe_literal&lt;N&gt;</code>. These types can be used to improve program correctness and performance.</li>
</ul>


<h2>Requirements</h2>
<p>This library is composed entirely of C++ Headers. It requires a compiler compatible with the C++14 standard.</p>

<p>The following Boost Libraries must be installed in order to use this library</p>

<ul>
	<li>MPL</li>
	
	<li>Integer</li>
	
	<li>Config</li>
	
	<li>Concept Checking</li>
	
	<li>Tribool</li>
	
	<li>Enable_if</li>
</ul>

<p>The Safe Numerics library is delivered with an exhaustive suite of test programs. Users who choose to run this test suite will also need to install the Boost.Preprocessor library.</p>

<h2>Scope</h2>

<p>This library currently applies only to built-in integer types. Analogous issues arise for floating point types but they are not currently addressed by this version of the library. User or library defined types such as arbitrary precision integers can also have this problem. Extension of this library to these other types is not currently under development but may be addressed in the future. This is one reason why the library name is â€˜safe numericâ€™ rather than â€˜safe integerâ€™ library.</p>

<h2>Eliminating runtime penalty</h2>

<p>Up until now, weâ€™ve focused on detecting when incorrect results are produced and handling these occurrences either by throwing an exception or invoking some designated function. Weâ€™ve achieved our goal of detecting and handling arithmetically incorrect behavior â€“ but at what cost. It is a fact that many C++ programmers will find this trade-off unacceptable. So the question arises as to how we might minimize or eliminate this runtime penalty.</p>

<p>The first step is to determine what parts of a program might invoke exceptions. Listing 1 is similar to previous examples but uses a special exception policy: <code>trap_exception</code>.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;
#include &quot;../include/safe_integer.hpp&quot;
#include &quot;../include/exception.hpp&quot; 
  // include exception policies

using safe_t = boost::numeric::safe&lt;
int,
boost::numeric::native,
boost::numeric::trap_exception 
  // note use of &quot;trap_exception&quot; policy!
&gt;;

int main(int argc, const char * argv[]){
std::cout &lt;&lt; &quot;example 81:\n&quot;;
safe_t x(INT_MAX);
safe_t y(2);
safe_t z = x + y; // will fail to compile !
return 0;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>Now, any expression which <strong>might</strong> fail at runtime is flagged with a compile time error. There is no longer any need for <code>try</code>/<code>catch</code> blocks. Since this program does not compile, the <strong>library absolutely guarantees that no arithmetic expression will yield incorrect results</strong>. This is our original goal. Now all we need to do is make the program work. There are a couple of ways to do this.</p>

<h3>Using automatic type promotion</h3>

<p>The C++ standard describes how binary operations on different integer types are handled. Here is a simplified version of the rules:</p>

<ul>
	<li>promote any operand smaller than <code>int</code> to an <code>int</code> or <code>unsigned int</code>.</li>
	
	<li>if the signed operand is larger than the signed one, the result will be signed, otherwise the result will be unsigned.</li>
	
	<li>expand the smaller operand to the size of the larger one</li>
</ul>

<p>So the result of the sum of two integer types may result in another integer type. If the values are large, the result can exceed the size that the resulting integer type can hold. This is what we call â€˜overflowâ€™. The C/C++ standard characterises this as undefined behavior and leaves to compiler implementors the decision as to how such a situation will be handled. Usually, this means just truncating the result to fit into the result type â€“ which sometimes will make the result arithmetically incorrect. However, depending on the compiler, compile time switch settings, such a case may result in some sort of run time exception.</p>

<p>The complete signature for a safe integer type is:</p>

<pre class="programlisting">
  template &lt;
    class T,          // underlying integer type
    class P = native, // type promotion policy class
    class E = throw_exception 
                      // error handling policy class
  &gt;
  safe;</pre>
  
<p>The promotion rules implemented in the default <code>native</code> type promotion policy are consistent with those of standard C++. Up until now, weâ€™ve focused on detecting when this happens and invoking an interrupt or other kind of error handler.</p>

<p>But now we look at another option. Using the <code>automatic</code> type promotion policy, we can change the rules of C++ arithmetic for safe types to something like the following:</p>

<ul>
	<li>For any C++ numeric type, we know from <code>std::numeric_limits</code> what the maximum and minimum values that a variable can be â€“ this defines a closed interval.</li>
	
	<li>For any binary operation on these types, we can calculate the interval of the result at compile time.</li>
	
	<li>From this interval we can select a new type which can be guaranteed to hold the result and use this for the calculation. This is more or less equivalent to the following code:

	<pre class="programlisting">
      int x, y;
      int z = x + y // which could overflow
      int x, y;
      long z = (long)x + (long)y;
                   // which can never overflow</pre>
	
	<p>One could do this by editing this code manually, but such a task would be tedious, error prone, and leave the resulting code hard to read and verify. Using the <code>automatic</code> type promotion policy will achieve the equivalent result without these problems.</p></li>
	
	<li>Since the result type is guaranteed to hold the result, there is no need to check for errors â€“ they canâ€™t happen!!! The usage of the <code>trap_exception</code> exception policy enforces this guarantee.</li>
	
	<li>Since there can be no errors, there is no need for <code>try</code>/<code>catch</code> blocks.</li>
	
	<li>The only runtime error checking we need to do is when safe values are initialized or assigned from values which are â€˜too largeâ€™. These are infrequent occurrences which generally have little or no impact on program running time. And many times, one can make small adjustments in selecting the types in order to eliminate all runtime penalties.</li>
</ul>

<p>In short, given a binary operation, we silently promote the types of the operands to a wider result type so the result cannot overflow. This is a fundamental departure from the C++ Standard behavior.</p>

<p>If the interval of the result cannot be guaranteed to fit in the largest type that the machine can handle (usually 64 bits these days), the largest available integer type with the correct result sign is used. So even with our â€˜automaticâ€™ type promotion scheme, itâ€™s still possible to overflow. In this case, and only this case, is runtime error checking code generated. Depending on the application, it should be rare to generate error checking code, and even more rare to actually invoke it. Any such instances are detected at compile time by the <code>trap_exception</code> exception policy.</p>

<p>Listing 2 illustrates how to use automatic type promotion to eliminate all runtime penalty. It produces the following output:</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;
#include &quot;../include/safe_integer.hpp&quot;
#include &quot;../include/exception.hpp&quot;
#include &quot;../include/automatic.hpp&quot;
#include &quot;safe_format.hpp&quot; 
  // prints out range and value of any type
using safe_t = boost::numeric::safe&lt;
  int,
  boost::numeric::automatic, 
    // note use of &quot;automatic&quot; policy!!!
  boost::numeric::trap_exception
&gt;;

int main(int argc, const char * argv[]){
  std::cout &lt;&lt; &quot;example 82:\n&quot;;
  safe_t x(INT_MAX);
  safe_t y = 2;
  std::cout &lt;&lt; &quot;x = &quot; &lt;&lt; safe_format(x) 
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;y = &quot; &lt;&lt; safe_format(y) 
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;z = &quot; &lt;&lt; safe_format(x + y) 
    &lt;&lt; std::endl;
  return 0;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<pre class="programlisting">
  example 82:
  x = &lt;int&gt;[-2147483648,2147483647] = 2147483647
  y = &lt;int&gt;[-2147483648,2147483647] = 2
  z = &lt;long&gt;[-4294967296,4294967294] = 2147483649</pre>
  
<p>The output uses a custom output manipulator for safe types to display the underlying type and its range as well as current value. Note that:</p>

<ul>
	<li>the <code>automatic</code> type promotion policy has rendered the result of the some of two <code>integers</code> as a <code>long</code> type.</li>
	
	<li>our program compiles without error â€“ even when using the <code>trap_exception</code> exception policy</li>
	
	<li>We do not need to use <code>try</code>/<code>catch</code> idiom to handle arithmetic errors â€“ we will have none.</li>
	
	<li>We only needed to change two lines of code to achieve our goal.</li>
</ul>

<h3>Using safe_range</h3>

<p>Instead of relying on automatic type promotion, we can just create our own types in such a way that we know they wonâ€™t overflow. In Listing 3, we presume we know that the values we want to work with fall in the range [-24,82]. So we â€˜knowâ€™ the program will always result in a correct result. But since we trust no one, and since the program could change and the expressions be replaced with other ones, weâ€™ll still use the <code>trap_exception</code> exception policy to verify at compile time that what we â€˜knowâ€™ to be true is in fact true:</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;
#include &quot;../include/safe_range.hpp&quot;
#include &quot;../include/safe_literal.hpp&quot;
#include &quot;../include/exception.hpp&quot;
#include &quot;../include/native.hpp&quot;
#include &quot;safe_format.hpp&quot;
  // prints out range and value of any type
using namespace boost::numeric;
  // for safe_literal

// create a type for holding small integers. We
// &quot;know&quot; that C++ type promotion rules will work
// such that addition will never overflow. If we
// change the program to break this, the usage
// of the trap_exception promotion policy will
// prevent compilation.
using safe_t = safe_signed_range&lt;
  -24,
  82,
  native, // C++ type promotion rules work 
          // OK for this example
  trap_exception // catch problems at compile time
&gt;;

int main(int argc, const char * argv[]){
  std::cout &lt;&lt; &quot;example 83:\n&quot;;
  // the following would result in a compile time
  // error since the sum of x and y wouldn't be in
  // the legal range for z.
  // const safe_signed_literal&lt;20&gt; x;
  const safe_signed_literal&lt;10&gt; x; // no problem
  const safe_signed_literal&lt;67&gt; y;

  const safe_t z = x + y;
  std::cout &lt;&lt; &quot;x = &quot; &lt;&lt; safe_format(x) 
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;y = &quot; &lt;&lt; safe_format(y) 
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;z = &quot; &lt;&lt; safe_format(z) 
    &lt;&lt; std::endl;
  return 0;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<ul>
	<li><code>safe_signed_range</code> defines a type which is limited to the indicated range. Out of range assignments will be detected at compile time if possible (as in this case) or at run time if necessary.</li>
	
	<li><code>safe_signed_literal</code> defines a constant with a specific value. Defining constants in this way enables the library to correctly anticipate the range of the results of arithmetic expressions.</li>
	
	<li>The usage of <code>trap_exception</code> will mean that any assignment to z which could be outside the legal range will result in a compile time error.</li>
	
	<li>So if this program compiles, itâ€™s guaranteed to return a valid result.</li>
</ul>

<p>This program produces the following run time output.</p>

<pre class="programlisting">
  example 83:
  x = &lt;signed char&gt;[10,10] = 10
  y = &lt;signed char&gt;[67,67] = 67
  z = &lt;signed char&gt;[-24,82] = 77</pre>

<h3>Mixing approaches</h3>

<p>For purposes of exposition, weâ€™ve divided the discussion of how to eliminate runtime penalties by the different approaches available. A realistic program would likely include all techniques mentioned above. Consider Listing 4:</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;stdexcept&gt;
#include &lt;iostream&gt;
#include &quot;../include/safe_range.hpp&quot;
#include &quot;../include/automatic.hpp&quot;
#include &quot;../include/exception.hpp&quot;

#include &quot;safe_format.hpp&quot;
  // prints out range and value of any type

using namespace boost::numeric;
using safe_t = safe_signed_range&lt;
  -24,
  82,
  automatic,
  trap_exception
&gt;;
// define variables use for input
using input_safe_t = safe_signed_range&lt;
  -24,
  82,
  automatic,
          // we don't need automatic in this case
  throw_exception // these variables need to
&gt;;

// function arguments can never be outside of
// limits
auto f(const safe_t &amp; x, const safe_t &amp; y){
  auto z = x + y; // we know that this cannot fail
  std::cout &lt;&lt; &quot;z = &quot; &lt;&lt; safe_format(z) 
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;(x + y) = &quot; &lt;&lt; safe_format(x + y)
    &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;(x - y) = &quot; &lt;&lt; safe_format(x - y)
    &lt;&lt; std::endl;
  return z;
}

int main(int argc, const char * argv[]){
  std::cout &lt;&lt; &quot;example 84:\n&quot;;
  input_safe_t x, y;
  try{
    std::cin &gt;&gt; x &gt;&gt; y; // read varibles,
                        // maybe throw exception
  }
  catch(const std::exception &amp; e){
    // none of the above should trap.
    // Mark failure if they do
    std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
    return 1;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<ul>
	<li>As before, we define a <code>safe_t</code> to reflect our view of legal values for this program. This uses <code>automatic</code> type promotion policy as well as <code>trap_exception</code> exception policy to enforce elimination of runtime penalties.</li>
	
	<li>The function <code>f</code> accepts only arguments of type <code>safe_t</code> so there is no need to check the input values. This performs the functionality of <strong>programming by contract</strong> with no runtime cost.</li>
	
	<li>In addition, we define <code>input_safe_t</code> to be used when reading variables from the program console. Clearly, these can only be checked at runtime so they use the <code>throw_exception</code> policy. When variables are read from the console they are checked for legal values. We need no ad hoc code to do this, as these types are guaranteed to contain legal values and will throw an exception when this guarantee is violated. In other words, we automatically get checking of input variables with no additional programming.</li>
	
	<li>On calling of the function <code>f</code>, arguments of type <code>input_safe_t</code> are converted to values of type <code>safe_t</code>.

		<p>In this particular example, it can be determined at compile time that construction of an instance of a <code>safe_t</code> from an <code>input_safe_t</code> can never fail. Hence, no <code>try</code>/<code>catch</code> block is necessary. The usage of the <code>trap_exception</code> policy for <code>safe_t</code> types guarantees this to be true at compile time.</p>
	</li>
</ul>

<p>Here is the output from the program when values 12 and 32 are input from the console:</p>

<pre class="programlisting">
  example 84:
  12 32
  x&lt;signed char&gt;[-24,82] = 12
  y&lt;signed char&gt;[-24,82] = 32
  z = &lt;short&gt;[-48,164] = 44
  (x + y) = &lt;short&gt;[-48,164] = 44
  (x - y) = &lt;short&gt;[-106,106] = -20
&lt;short&gt;[-48,164] = 44</pre>

<h2>Background</h2>

<p>This library started out as a re-implementation of the facilities provided by David LeBlancâ€™s SafeInt Library [<a href="http://safeint.codeplex.com">http://safeint.codeplex.com</a>]. I found this library very well done in every way. My main usage was to run unit tests for my embedded systems projects on my PC. Still, I had a few issues.</p>

<ul>
	<li>It was a lot of code in one header â€“ 6400 lines. Very unwieldy to understand, modify and maintain.</li>
	
	<li>I couldnâ€™t find separate documentation other than that in the header file.</li>
	
	<li>It didnâ€™t use Boost conventions for naming.</li>
	
	<li>It required porting to different compilers.</li>
	
	<li>It had a very long license associated with it.</li>
	
	<li>I could find no test suite for the library.</li>
</ul>

<p>This version addresses these issues. It exploits many facilities of C++14 and the Boost libraries to reduce the number of lines of source code to approximately 4700.</p>

<h2>Library internals</h2>

<p>This library should compile and run correctly on any conforming C++14 compiler.</p>

<p>The Safe Numerics library is implemented in terms of some more fundamental software components described here. It is not necessary to know about these components to use the library. This information has been included to help those who want to understand how the library works so they can extend it, correct bugs in it, or understand its limitations. These components are also interesting in their own right. For all these reasons, they are described here. In general terms, the library works in the following manner:</p>

<ul>
	<li>All unary/binary expressions where one of the operands is a â€˜safeâ€™ type are overloaded. These overloads are declared and defined in the header file <span class="filename">safe_integer.hpp</span>. SFINAE â€“ â€˜Substitution Failure Is Not An Errorâ€™ â€“ and <code>std::enable_if</code> are key features of C++ used to define these overloads in a correct manner.</li>
	
	<li>Each overloaded operation implements the following procedure at compile time:
		<ul>
			<li>Retrieve range of values for each operand of type T from both:<code>std::numeric_limits&lt;T&gt;::min()std::numeric_limits&lt;T&gt;::max()</code>.</li>
			
			<li>Given the ranges of the operands, determine the range of the result of the operation using interval arithmetic. This is implemented in the <span class="filename">interval.hpp</span> header file using <code>constexpr</code> facility of C++14.</li>
			
			<li>If the range of the result type includes the range of the result of the operation, no run time checking of the result is necessary, so the operation reduces to the original built-in C/C++ operation.</li>
			
			<li>Otherwise, the operation is implemented as a â€˜checked integer operationâ€™ at run time. This operation returns a variant which will contain either a correct result or an <code>enum</code> indicating why a correct result could not be obtained. The variant object is implemented in the header file <span class="filename">checked_result.hpp</span> and the checked operations are implemented in <span class="filename">checked.hpp</span>.</li>
			
			<li>If a valid result has been obtained, it is passed to the caller.</li>
			
			<li>Otherwise, an exception is invoked.</li>
		</ul>
	</li>
</ul>

<h2>Rationale and FAQ</h2>

<ol>
	<li>Is this really necessary? If Iâ€™m writing the program with the requisite care and competence, problems noted in the introduction will never arise. Should they arise, they should be fixed â€˜at the sourceâ€™ and not with a â€˜band aidâ€™ to cover up bad practice.
		<p class="blockquote">This surprised me when it was first raised. But some of the feedback Iâ€™ve received makes me thing that itâ€™s a widely held view. The best answer is to consider the cases in the Tutorials and Motivating Examples section of the library documentation.</p>
	</li>
	
	<li>Can safe types be used as drop-in replacement for built-in types?
		<p class="blockquote">Almost. Replacing all built-in types with their safe counterparts should result in a program that will compile and run as expected. In some cases compile time errors will occur and adjustments to the source code will be required. Typically these will result in code which is more correct.</p>
	</li>
	
	<li>Why is Boost.Convert not used?
		<p class="blockquote">I couldnâ€™t figure out how to use it from the documentation.</p>
	</li>
	
	<li>Why is the library named â€˜safe ...â€™ rather than something like â€˜checked ...â€™?
		<p class="blockquote">I used â€˜safeâ€™ in large part as this is what has been used by other similar libraries. Maybe a better word might have been â€˜correctâ€™ but that would raise similar concerns. Iâ€™m not inclined to change this. Iâ€™ve tried to make it clear in the documentation what the problem that the library addressed is.</p>
	</li>
	
	<li>Given that the library is called â€˜numericsâ€™, why is floating point arithmetic not addressed?
		<p class="blockquote">Actually, I believe that this can/should be applied to any type <code>T</code> which satisfies the type requirement â€˜Numericâ€™ type as defined in the documentation. So there should be specializations <code>safe&lt;float&gt;</code> and related types as well as new types like  <code>safe&lt;fixed_decimal&gt;</code> etc. But the current version of the library only addresses integer types. Hopefully the library will evolve to match the promise implied by its name.</p>
	</li>
	
	<li>Isnâ€™t putting a defensive check just before any potential undefined behavior often considered a bad practice?
		<p class="blockquote">By whom? Is leaving code which can produce incorrect results better? Note that the documentation contains references to various sources which recommend exactly this approach to mitigate the problems created by this C/C++ behavior. See [Seacord].</p>
	</li>
	
	<li>It looks like the implementation presumes twoâ€™s complement arithmetic at the hardware level. So this library is not portable, correct? What about other hardware architectures?
		<p class="blockquote">As far as is known as of this writing, the library does not presume that the underlying hardware is twoâ€™s complement. However, this has yet to be verified in a rigorous way.</p>
	</li>
	
	<li>Why do you specialize <code>numeric_limits</code> for â€˜safeâ€™ types? Do you need it?
		<p class="blockquote"><code>safe&lt;T&gt;</code> behaves like a â€˜numberâ€™ just as <code>int</code> does. It has <code>max</code>, <code>min</code>, etc Any code which uses numeric limits to test a type <code>T</code> should work with <code>safe&lt;T&gt;</code>. <code>safe&lt;T&gt;</code> is a drop-in replacement for <code>T</code> so it has to implement all the operations.</p>
	</li>
	
	<li>According to C/C++ standards, unsigned integers cannot overflow â€“they are modular integers which â€˜wrap aroundâ€™. Yet the Safe Numerics library detects and traps this behavior as errors. Why is that?
		<p class="blockquote">The guiding purpose of the library is to trap incorrect arithmetic behavior â€“ not just undefined behavior. Although a savvy user may understand and keep present in his mind that an unsigned integer is really a modular type, the plain reading of an arithmetic expression conveys the idea that all operands are integers. Also in many cases, unsigned integers are used in cases where modular arithmetic is not intended, such as array indexes. Finally, the modulus for such an integer would vary depending upon the machine architecture. For these reasons, in the context of this library, an unsigned integer is considered to a representation of a subset of integers. Note that this decision is consistent with INT30-C, â€œEnsure that unsigned integer operations do not wrapâ€ in the CERT C Secure Coding Standard [Seacord].</p>
	</li>
	
	<li>Why does the library require C++14?
		<p class="blockquote">The original version of the library used C++11. Feedback from CPPCon, Boost Library Incubator [<a href="www.blincubator.com">www.blincubator.com</a>] and Boost developerâ€™s mailing list convinced me that I had to address the issue of run-time penalty much more seriously. I resolved to eliminate or minimize it. This led to more elaborate meta-programming. But this wasnâ€™t enough. It became apparent that the only way to really minimize run-time penalty was to implement compile-time integer range arithmetic â€“ a pretty elaborate sub library. By doing range arithmetic at compiler-time, I could skip runtime checking on many/most integer operations. C++11 constexpr wasnâ€™t quite enough to do the job. C++14 constexpr can do the job. The library currently relies very heavily on C++14 constexpr. I think that those who delve into the library will be very surprised at the extent that minor changes in user code can produce guaranteed correct integer code with zero run-time penalty.</p>
	</li>
	
	<li>This is a C++ library, yet you refer to C/C++. Which is it?
		<p class="blockquote">C++ has evolved way beyond the original C language. But C++ is still (mostly) compatible with C. So most C programs can be compiled with a C++ compiler. The problems of incorrect arithmetic afflict both C and C++. Suppose we have a legacy C program designed for some embedded system.</p>
		<ul class="blockquote">
			<li>Replace all <code>int</code> declarations with <code>int16_t</code> and all <code>long</code> declarations with <code>int32_t</code>.</li>
			
			<li>Create a file containing something like Listing 5 and include it at the beginning of every source file.</li>
			
			<li>Compile tests on the desktop with a C++14 compiler and with the macro <code>TEST</code> defined.</li>
			
			<li>Run the tests and change code to address any thrown exceptions.</li>
		</ul>
		<p class="blockquote">This example illustrates how this library, implemented with C++14 can be
		useful in the development of correct code for programs written in C.</p>
	</li>
</ol>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#ifdef TEST
// using C++ on test platform
#include &lt;cstdint&gt;
#include &lt;safe_integer.hpp&gt;
#include &lt;cpp.hpp&gt;
using pic16_promotion = boost::numeric::cpp&lt;
  8,  // char
  8,  // short
  8,  // int
  16, // long
  32  // long long
&gt;;
// define safe types used desktop version of 
// the program.
template &lt;typename T&gt; 
  // T is char, int, etc data type
using safe_t = boost::numeric::safe&lt;
  T,
  pic16_promotion,
  boost::numeric::throw_exception 
    // use for compiling and running tests
&gt;;
typedef safe_t&lt;std::int16_t&gt; int16_t;
typedef safe_t&lt;std::int32_t&gt; int32_t;
#else
/* using C on embedded platform */
typedef int int16_t;
typedef long int32_t;
#endif
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 5</td>
	</tr>
</table>

<h2>Current status</h2>

<p>The library is currently in the Boost Review Queue [<a href="http://www.boost.org/community/review_schedule.html">http://www.boost.org/community/review_schedule.html</a>]. The proposal submission can be found in the Boost Library Incubator [<a href="http://blincubator.com/bi_library/safe-numerics/?gform_post_id=426">http://blincubator.com/bi_library/safe-numerics/?gform_post_id=426</a>]</p>

<ul>
	<li>The library is currently limited to integers.</li>
	<li>Although care has been taken to make the library portable, itâ€™s likely that at least some parts of the implementation â€“ particularly <code>checked</code> arithmetic â€“ depend upon twoâ€™s complement representation of integers. Hence the library is probably not currently portable to other architectures.</li>
	<li>Currently the library permits a <code>safe&lt;int&gt;</code> value to be uninitialized. This supports the goal of â€˜drop-inâ€™ replacement of C++/C built-in types with safe counter parts. On the other hand, this breaks the â€˜always validâ€™ guarantee.</li>
	<li>The library is not quite a â€˜drop-inâ€™ replacement for all built-in integer types. In particular, C/C++ implements implicit conversions and promotions between certain integer types which are not captured by the operation overloads used to implement the library. In practice these case are few and can be addressed with minor changes to the user program to avoid these silent implicit conversions.</li>
</ul>

<h2>Acknowledgements</h2>

<p>This library would never have been created without inspiration, collaboration and constructive criticism from multiple sources.</p>

<p><strong>David LeBlanc</strong></p>
<p style="margin-left:1em">This library is inspired by David LeBlancâ€™s SafeInt Library [<a href="http://safeint.codeplex.com">http://safeint.codeplex.com</a>]. I found this library very well done in every way and useful in my embedded systems work. This motivated me to take to the â€˜next levelâ€™.</p>

<p><strong>Andrzej KrzemieÅ„ski </strong>[<a href="https://akrzemi1.wordpress.com">https://akrzemi1.wordpress.com</a>]</p>

<p style="margin-left:1em">Andrzej Commented and reviewed the library as it was originally posted on the Boost Library Incubator [<a href="www.blincubator.com">www.blincubator.com</a>]. The the consequent back and forth motivated me to invest more effort in developing documentation and examples to justify the utility, indeed the necessity, for this library. He also noted many errors in code, documentation, and tests. Without his interest and effort, I do not believe the library would have progressed beyond its initial stages.</p>

<p><strong>Boost</strong> [<a href="www.boost.org">www.boost.org</a>]</p>

<p style="margin-left:1em">As always, the Boost Developerâ€™s mailing list has been the source of many useful observations from potential users and constructive criticism from very knowledgeable developers.</p>

<h2>Bibliography</h2>

<p class="bibliomixed">[coker] Zack Coker. Samir Hasan. Jeffrey Overbey. Munawar Hafiz. Christian KÃ¤stner. <em>Integers In C: An Open Invitation To Security Attacks?</em> [<a href="https://www.cs.cmu.edu/~ckaestne/pdf/csse14-01.pdf">https://www.cs.cmu.edu/~ckaestne/pdf/csse14-01.pdf</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?</a>]. JTC1/SC22/WG21 â€“ The C++ Standards Committee â€“ ISOCPP [<a href="http://www.open-std.org/jtc1/sc22/wg21/">http://www.open-std.org/jtc1/sc22/wg21/</a>]. January 15, 2012. Coker</p>

<p class="bibliomixed">[crowl1] Lawrence Crowl. <em>C++ Binary Fixed-Point Arithmetic</em> [<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?</a>]. JTC1/SC22/WG21 â€“ The C++ Standards Committee â€“ ISOCPP [<a href="http://www.open-std.org/jtc1/sc22/wg21/">http://www.open-std.org/jtc1/sc22/wg21/</a>]. January15, 2012. Crowl</p>

<p class="bibliomixed">[crowl2] Lawrence Crowl. Thorsten Ottosen. <em>Proposal to add Contract Programming to C++</em> [<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1962.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1962.html</a>] [<a href="http://www.cert.org/secure-coding/publications/books/securecoding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/securecoding-c-c-second-edition.cfm?</a>]. WG21/N1962 and J16/06-0032 â€“ The C++ Standards Committee â€“ ISOCPP [<a href="http://www.open-std.org/jtc1/sc22/wg21/">http://www.open-std.org/jtc1/sc22/wg21/</a>]. February 25, 2006. Crowl &amp; Ottosen</p>

<p class="bibliomixed">[dietz] Will Dietz. Peng Li. John Regehr. Vikram Adve. <em>Understanding Integer Overflow in C/C++</em> [<a href="http://www.cs.utah.edu/~regehr/papers/overflow12.pdf">http://www.cs.utah.edu/~regehr/papers/overflow12.pdf</a>]. <em>Proceedings of the 34th International Conference on Software Engineering (ICSE), Zurich, Switzerland</em> [<a href="http://dl.acm.org/citation.cfm?id=2337223&amp;picked=prox">http://dl.acm.org/citation.cfm?id=2337223&amp;picked=prox</a>]. June 2012. Dietz</p>

<p class="bibliomixed">[garcia] J. Daniel Garcia. <em>C++ language support for contract programming</em> [<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4293.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4293.pdf</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-secondedition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-secondedition.cfm?</a>]. WG21/N4293 â€“ The C++ Standards Committee â€“ ISOCPP [<a href="http://www.open-std.org/jtc1/sc22/wg21/">http://www.open-std.org/jtc1/sc22/wg21/</a>]. December 23, 2014. Garcia</p>

<p class="bibliomixed">[katz] Omer Katz. <em>SafeInt code proposal</em> [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-secondedition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-secondedition.cfm?</a>]. <em>Boost Developerâ€™s List</em> [<a href="https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/stdproposals">https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/stdproposals</a>]. Katz</p>

<p class="bibliomixed">[keaton] David Keaton, Thomas Plum, Robert C. Seacord, David Svoboda, Alex Volkovitsky, and Timothy Wilson. <em>As-if Infinitely Ranged Integer Model</em> [<a href="http://resources.sei.cmu.edu/asset_files/TechnicalNote/2009_004_001_15074.pdf">http://resources.sei.cmu.edu/asset_files/TechnicalNote/2009_004_001_15074.pdf</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?</a>] Software Engineering Institute [<a href="http://www.sei.cmu.edu">http://www.sei.cmu.edu</a>]. CMU/SEI-2009-TN-023.</p>

<p class="bibliomixed">[leblanc1] David LeBlanc. <em>Integer Handling with the C++ SafeInt Class</em> [<a href="https://msdn.microsoft.com/en-us/library/ms972705.aspx">https://msdn.microsoft.com/en-us/library/ms972705.aspx</a>]. Microsoft Developer Network [<a href="https://www.cert.org">https://www.cert.org</a>]. January 7, 2004. LeBlanc</p>

<p class="bibliomixed">[leblanc2] David LeBlanc. <em>SafeInt</em> [<a href="https://safeint.codeplex.com">https://safeint.codeplex.com</a>]. CodePlex [<a href="https://www.cert.org">https://www.cert.org</a>]. Dec 3, 2014. LeBlanc</p>

<p class="bibliomixed">[lions] Jacques-Louis Lions. <em>Ariane 501 Inquiry Board report</em> [<a href="https://en.wikisource.org/wiki/Ariane_501_Inquiry_Board_report">https://en.wikisource.org/wiki/Ariane_501_Inquiry_Board_report</a>]. Wikisource [<a href="https://en.wikisource.org/wiki/Main_Page">https://en.wikisource.org/wiki/Main_Page</a>]. July 19, 1996. Lions</p>

<p class="bibliomixed">[matthews] Hubert Matthews. <em>CheckedInt: A Policy-Based Range-Checked Integer</em> [<a href="https://accu.org/index.php/journals/324">https://accu.org/index.php/journals/324</a>] . <em>Overload</em> Journal #58 [<a href="https://accu.org/index.php">https://accu.org/index.php</a>]. December 2003. Matthews</p>

<p class="bibliomixed">[mouawad] Jad Mouawad. <em>F.A.A Orders Fix for Possible Power Loss in Boeing 787</em> [<a href="http://www.nytimes.com/2015/05/01/business/faa-orders-fix-for-possible-power-loss-in-boeing-787.html?_r=0">http://www.nytimes.com/2015/05/01/business/faa-orders-fix-for-possible-power-loss-in-boeing-787.html?_r=0</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?</a>]. New York Times. April 30, 2015.</p>

<p class="bibliomixed">[plakosh] Daniel Plakosh. <em>Safe Integer Operations</em> [<a href="https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/312-BSI.html">https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/312-BSI.html</a>]. U.S. Department of Homeland Security [<a href="https://buildsecurityin.us-cert.gov">https://buildsecurityin.us-cert.gov</a>]. May 10, 2013. Plakosh</p>

<p class="bibliomixed">[seacord1] Robert C. Seacord. <em>Secure Coding in C and C++</em> [http://www.cert.org/secure-coding/publications/books/securecoding-c-c-second-edition.cfm?]. 2nd Edition. Addison-Wesley Professional. April 12, 2013. 978-0321822130. Seacord</p>

<p class="bibliomixed">[seacord2] Robert C. Seacord. <em>INT30-C. Ensure that operations on unsigned integers do not wrap</em> [<a href="https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false">https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false</a>]. Software Engineering Institute, Carnegie Mellon University [<a href="https://www.cert.org">https://www.cert.org</a>]. August 17, 2014. INT30-C</p>

<p class="bibliomixed"><a id="[seacord3]"></a>[seacord3] Robert C. Seacord. <em>INT32-C. Ensure that operations on signed integers do not result in overflow</em> [<a href="https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap">https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap</a>]. Software Engineering Institute, Carnegie Mellon University [<a href="https://www.cert.org">https://www.cert.org</a>]. August 17, 2014. INT32-C</p>

<p class="bibliomixed">[stroustrup] Bjarn Stroustrup. <em>The C++ Programming Language Fourth Edition</em>. Addison-Wesley [<a href="http://www.open-std.org/jtc1/sc22/wg21/">http://www.open-std.org/jtc1/sc22/wg21/</a>] . Copyright Â© 2014 by Pearson Education, Inc. January 15, 2012. Stroustrup</p>

<p class="bibliomixed">[forum] Forum Posts. <em>C++ Binary Fixed-Point Arithmetic</em> [<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html</a>] [<a href="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?</a>]. ISO C++ Standard Future Proposals [<a href="https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/std-proposals">https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/std-proposals</a>]. Forum</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
