mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-22 03:32:24 +00:00
improved TOC and chunking. This is complicated by the fact we that we desire different depths. put copies of boost logo in subdirectories
187 lines
12 KiB
HTML
187 lines
12 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>cpp<int C, int S, int I, int L, int LL></title>
|
|
<link rel="stylesheet" href="boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
|
|
<link rel="home" href="index.html" title="Safe Numerics">
|
|
<link rel="up" href="promotion_policies.html" title="Promotion Policies">
|
|
<link rel="prev" href="automatic_policy.html" title="automatic">
|
|
<link rel="next" href="exception_policies.html" title="Exception Policies">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
|
|
<td><h2>Safe Numerics</h2></td>
|
|
</tr></table>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="automatic_policy.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="promotion_policies.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="exception_policies.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="safe_numerics.cpp_policy"></a>cpp<int C, int S, int I, int L, int LL></h4></div></div></div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm291788402256"></a>Description</h5></div></div></div>
|
|
<p>This policy is used to promote safe types in arithmetic expressions
|
|
according the standard rules in the C++ standard. But rather than using
|
|
the native C++ standard types supported by the compiler, it uses types
|
|
whose length in number of bits is specified by the template
|
|
parameters.</p>
|
|
<p>This policy is useful for running test program which use C++
|
|
portable integer types but which are destined to run on an architecture
|
|
which is different than the one on which the test program is being built
|
|
and run. This can happen when developing code for embedded systems.
|
|
Algorithms developed or borrowed from one architecture but destined for
|
|
another can be tested on the desk top.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm291788400000"></a>Template Parameters</h5></div></div></div>
|
|
<div class="informaltable"><table class="table">
|
|
<colgroup>
|
|
<col align="left">
|
|
<col align="left">
|
|
<col align="left">
|
|
</colgroup>
|
|
<thead><tr>
|
|
<th align="left">Parameter</th>
|
|
<th align="left">Type</th>
|
|
<th align="left">Description</th>
|
|
</tr></thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">C</code></td>
|
|
<td align="left">int</td>
|
|
<td align="left">Number of bits in a char</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">S</code></td>
|
|
<td align="left">int</td>
|
|
<td align="left">Number of bits in a short</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">I</code></td>
|
|
<td align="left">int</td>
|
|
<td align="left">Number of bits in an integer</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">L</code></td>
|
|
<td align="left">int</td>
|
|
<td align="left">Number of bits in a long</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">LL</code></td>
|
|
<td align="left">int</td>
|
|
<td align="left">Number of bits in a long long</td>
|
|
</tr>
|
|
</tbody>
|
|
</table></div>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm291788383056"></a>Model of</h5></div></div></div>
|
|
<p><a class="link" href="promotion_policy.html" title="PromotionPolicy<PP>">PromotionPolicy</a></p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm291788381344"></a>Header</h5></div></div></div>
|
|
<p><code class="computeroutput"><a href="../../examples/example9.cpp" target="_top"><code class="computeroutput">#include
|
|
<boost/safe_numerics/cpp.hpp> </code></a></code></p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm291788378816"></a>Example of Use</h5></div></div></div>
|
|
<p>Consider the following problem. One is developing software which
|
|
uses a very small microprocessor and a very limited C compiler. The chip
|
|
is so small, you can't print anything from the code, log, debug or
|
|
anything else. One debugs this code by using the "burn" and "crash" method
|
|
- you burn the chip (download the code) run the code, observe the results,
|
|
make changes and try again. This is a crude method which is usually the
|
|
one used. But it can be quite time consuming.</p>
|
|
<p>Consider an alternative. Build and compile your code in testable
|
|
modules. For each module write a test which exercises all the code and
|
|
makes it work. Finally download your code into the chip and - voilà -
|
|
working product. This sounds great, but there's one problem. Our target
|
|
processor - in this case a PIC162550 from Microchip Technology is only an
|
|
8 bit CPU. The compiler we use defines INT as 8 bits. This (and a few
|
|
other problems), make our algorithm testing environment differ from our
|
|
target environment. We can address this by defining INT as a safe integer
|
|
with a range of 8 bits. By using a custom promotion policy, we can force
|
|
the evaluation of C++ expressions test environment to be the same as that
|
|
in the target environment. Also in our target environment, we can trap any
|
|
overflows or other errors. So we can write and test our code on our
|
|
desktop system and download the code to the target knowing that it just
|
|
has to work. This is a huge time saver and confidence builder. The
|
|
following code is taken from a real project which has used this
|
|
method.</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="string">"../include/safe_integer.hpp"</span>
|
|
<span class="preprocessor">#include</span> <span class="string">"../include/cpp.hpp"</span>
|
|
|
|
<span class="comment">//////////////////////////////////////////////////////////////</span>
|
|
<span class="comment">// Stepper Motor Control</span>
|
|
|
|
<span class="comment">// emululate evironment for pic162550</span>
|
|
<span class="comment">// data widths used by the CCS compiler for pic 16xxx series</span>
|
|
|
|
<span class="keyword">using</span> <span class="identifier">pic16_promotion</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">cpp</span><span class="special"><</span>
|
|
<span class="number">8</span><span class="special">,</span> <span class="comment">// char</span>
|
|
<span class="number">8</span><span class="special">,</span> <span class="comment">// short - not used by pic 16xxxx</span>
|
|
<span class="number">8</span><span class="special">,</span> <span class="comment">// int</span>
|
|
<span class="number">16</span><span class="special">,</span> <span class="comment">// long</span>
|
|
<span class="number">32</span> <span class="comment">// long long</span>
|
|
<span class="special">></span><span class="special">;</span>
|
|
|
|
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">></span> <span class="comment">// T is char, int, etc data type</span>
|
|
<span class="keyword">using</span> <span class="identifier">safe_t</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">safe</span><span class="special"><</span>
|
|
<span class="identifier">T</span><span class="special">,</span>
|
|
<span class="identifier">pic16_promotion</span><span class="special">,</span>
|
|
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">trap_exception</span> <span class="comment">// use for compiling and running tests</span>
|
|
<span class="special">></span><span class="special">;</span>
|
|
|
|
<span class="keyword">using</span> <span class="identifier">int8</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int8_t</span><span class="special">></span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">int16</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">></span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">int32</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int32_t</span><span class="special">></span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">uint8</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uint8_t</span><span class="special">></span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">uint16</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uint16_t</span><span class="special">></span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">uint32</span> <span class="special">=</span> <span class="identifier">safe_t</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span><span class="special">></span><span class="special">;</span>
|
|
|
|
<span class="comment">////////////////////////////////////////////////////////////////</span>
|
|
<span class="comment">// Mock defines, functions etc which are in he "real application</span>
|
|
|
|
<span class="special">...</span>
|
|
|
|
<span class="comment">// return value in steps</span>
|
|
<span class="comment">/*
|
|
Use the formula:
|
|
stopping dist = v **2 / a / 2
|
|
*/</span>
|
|
<span class="identifier">uint16</span> <span class="identifier">get_stopping_distance</span><span class="special">(</span><span class="identifier">LEMPARAMETER</span> <span class="identifier">velocity</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="identifier">int32</span> <span class="identifier">d</span><span class="special">;</span>
|
|
<span class="identifier">d</span> <span class="special">=</span> <span class="identifier">velocity</span><span class="special">;</span>
|
|
<span class="identifier">d</span> <span class="special">*=</span> <span class="identifier">velocity</span><span class="special">;</span>
|
|
<span class="identifier">d</span> <span class="special">/=</span> <span class="identifier">lem</span><span class="special">.</span><span class="identifier">acceleration</span><span class="special">;</span>
|
|
<span class="identifier">d</span> <span class="special">/=</span> <span class="number">2</span><span class="special">;</span>
|
|
<span class="keyword">return</span> <span class="identifier">d</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
|
|
<span class="special">...</span></pre>
|
|
<p>Note the usage of the compile time trap policy in order to detect at
|
|
compile time any possible error conditions. As I write this, this is still
|
|
being refined. Hopefully this will be available by the time you read
|
|
this.</p>
|
|
</div>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2012 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
|
|
Software License</a></p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="automatic_policy.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="promotion_policies.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="exception_policies.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|