133 lines
6.7 KiB
HTML
133 lines
6.7 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.79.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.html" title="automatic">
|
|
<link rel="next" href="../exception_safety.html" title="Exception Safety">
|
|
</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.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_safety.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.promotion_policies.cpp"></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="idm130202658416"></a>Description</h5></div></div></div>
|
|
<p>This policy is used to promote safe types in arithmetic expressions
|
|
according to the 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 programs 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 desktop.</p>
|
|
<p>Note that this policy is only applicable to safe types whose base
|
|
type is a type fulfilling the type requirements of <a class="link" href="integer.html" title="Integer<T>">Integer</a>.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm130202654880"></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="idm130202637904"></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="idm130202636192"></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 in the 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. For an
|
|
extended example on how this is done, look at <a class="link" href="safety_critical_embedded_controller.html" title="Safety Critical Embedded Controller">Safety
|
|
Critical Embedded Controller</a> .</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm130202633184"></a>Header</h5></div></div></div>
|
|
<p><code class="computeroutput"><a href="../../include/cpp.hpp" target="_top"><code class="computeroutput">#include
|
|
<boost/numeric/safe_numerics/cpp.hpp> </code></a></code></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-2018 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.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_safety.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|