<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><p><strong>Document number</strong>: LEWG, EWG, SG14, SG6: P0037R1<br>
<strong>Date</strong>: 2016-02-11<br>
<strong>Project</strong>: Programming Language C++, Library Evolution WG, SG14<br>
<strong>Reply-to</strong>: John McFarlane, <a href="mailto:fixed-point@john.mcfarlane.name">fixed-point@john.mcfarlane.name</a></p>
<h1>Fixed-Point Real Numbers</h1>
<h2>I. Introduction</h2>
<p>This proposal introduces a system for performing binary fixed-point
arithmetic using integral types.</p>
<h2>II. Motivation</h2>
<p>Floating-point types are an exceedingly versatile and widely supported
method of expressing real numbers on modern architectures.</p>
<p>However, there are certain situations where fixed-point arithmetic is
preferable. Some systems lack native floating-point registers and must
emulate them in software. Many others are capable of performing some
or all operations more efficiently using integer arithmetic. Certain
applications can suffer from the variability in precision which comes
from a dynamic radix point <a href="http://www.pathengine.com/Contents/Overview/FundamentalConcepts/WhyIntegerCoordinates/page.php">[1]</a>.
In situations where a variable exponent is not desired, it takes
valuable space away from the significand and reduces precision.</p>
<p>Integer types provide the basis for an efficient
representation of binary fixed-point real numbers. However, laborious,
error-prone steps are required to normalize the results of certain
operations and to convert to and from fixed-point types.</p>
<p>A set of tools for defining and manipulating fixed-point types is
proposed. These tools are designed to make work easier for those who
traditionally use integers to perform low-level, high-performance
fixed-point computation.
They are composable such that a wide range of trade-offs between speed, accuracy and safety are supported.</p>
<h2>III. Impact On the Standard</h2>
<p>This proposal is a pure library extension. It does not require
changes to any standard classes or functions.
It adds two new class templates, <code>resize&lt;class, int&gt;</code> and <code>resize_t&lt;class, int&gt;</code>,
to existing header file, <code>&lt;type_traits&gt;</code>.
It adds several new class and function templates
to new header file, <code>&lt;fixed_point&gt;</code>.</p>
<h2>IV. Design Decisions</h2>
<p>The design is driven by the following aims in roughly descending
order:</p>
<ol>
<li>to automate the task of using integer types to perform low-level
binary fixed-point arithmetic;</li>
<li>to facilitate a style of code that is intuitive to anyone who is
comfortable with integer and floating-point arithmetic;</li>
<li>to avoid type promotion, implicit conversion or other behavior
that might lead to surprising results;</li>
<li>to preserve significant digits at the expense of insignificant digits,
i.e. to prefer underflow to overflow and</li>
<li>to avoid incurring expense for unused features - including compilation time.</li>
</ol>
<p>More generally, the aim of this proposal is to contain within a single API
all the tools necessary to perform binary fixed-point arithmetic.
The design facilitates a wide range of competing compile-time strategies for
avoiding overflow and precision loss, but implements only the simplest by default.
Similarly, orthogonal concerns such as run-time overflow detection and rounding modes
are deferred to the underlying integer types used as storage.</p>
<h3>Class Template</h3>
<p>Fixed-point numbers are specializations of</p>
<pre><code>template &lt;class ReprType, int Exponent&gt;
class fixed_point;
</code></pre>
<p>where the template parameters are described as follows.</p>
<h4><code>ReprType</code> Type Template Parameter</h4>
<p>This parameter identifies the capacity and signedness of the
underlying type used to represent the value. In other words, the size
of the resulting type will be <code>sizeof(ReprType)</code> and it will be
signed iff <code>is_signed&lt;ReprType&gt;::value</code> is true. The default is
<code>int</code>.</p>
<p><code>ReprType</code> may be a fundamental integral type or similar type with bit-shift and arithmetic operators.
The most suitable types are: <code>std::int8_t</code>, <code>std::uint8_t</code>,
<code>std::int16_t</code>, <code>std::uint16_t</code>, <code>std::int32_t</code> and <code>std::uint32_t</code>.
In limited situations, <code>std::int64_t</code> and <code>std::uint64_t</code> can be used.
The  reasons for these limitations relate to the difficulty in finding
a type that is suitable for performing lossless integer
multiplication.</p>
<h4><code>Exponent</code> Non-Type Template Parameter</h4>
<p>The exponent of a fixed-point type is the equivalent of the exponent
field in a floating-point type and shifts the stored value by the
requisite number of bits necessary to produce the desired range. The
default value of <code>Exponent</code> is zero, giving <code>fixed_point&lt;T&gt;</code> the same
range as <code>T</code>.</p>
<p>The resolution of a specialization of <code>fixed_point</code> is</p>
<pre><code>pow(2, Exponent)
</code></pre>
<p>and the minimum and maximum values are</p>
<pre><code>std::numeric_limits&lt;ReprType&gt;::min() * pow(2, Exponent)
</code></pre>
<p>and</p>
<pre><code>std::numeric_limits&lt;ReprType&gt;::max() * pow(2, Exponent)
</code></pre>
<p>respectively.</p>
<p>Any usage that results in values of <code>Exponent</code> which lie outside the
range, (<code>INT_MIN / 2</code>, <code>INT_MAX / 2</code>), may result in undefined
behavior and/or overflow or underflow. This range of exponent values
is far in excess of the largest built-in floting-point type and should
be adequate for all intents and purposes.</p>
<h3><code>make_fixed</code> and <code>make_ufixed</code> Helper Type</h3>
<p>The <code>Exponent</code> template parameter is versatile and concise. It is an
intuitive scale to use when considering the full range of positive and
negative exponents a fixed-point type might possess. It also
corresponds to the exponent field of built-in floating-point types.</p>
<p>However, most fixed-point formats can be described more intuitively by
the cardinal number of integer and/or fractional digits they contain.
Most users will prefer to distinguish fixed-point types using these
parameters.</p>
<p>For this reason, two aliases are defined in the style of
<code>make_signed</code>.</p>
<p>These aliases are declared as:</p>
<pre><code>template &lt;unsigned IntegerDigits, unsigned FractionalDigits = 0, class Archetype = signed&gt;
using make_fixed;
</code></pre>
<p>and</p>
<pre><code>template &lt;unsigned IntegerDigits, unsigned FractionalDigits = 0, class Archetype = unsigned&gt;
using make_ufixed;
</code></pre>
<p>They resolve to a <code>fixed_point</code> specialization with the given
signedness and number of integer and fractional digits. They may
contain additional integer and fractional digits.</p>
<p>For example, one could define and initialize an 8-bit, unsigned,
fixed-point variable with four integer digits and four fractional
digits:</p>
<pre><code>make_ufixed&lt;4, 4&gt; value { 15.9375 };
</code></pre>
<p>or a 32-bit, signed, fixed-point number with two integer digits and 29
fractional digits:</p>
<pre><code>make_fixed&lt;2, 29&gt; value { 3.141592653 };
</code></pre>
<p>Type parameter, <code>Archetype</code>, is provided in the case that a
<code>fixed_point</code> specialization is desired which has as the <code>ReprType</code>
parameter some type other than a built-in integral. The signedness of
<code>Archetype</code> corresponds to the signedness of the resultant
<code>fixed_point</code> specialization although the size does not.</p>
<h3>Conversion</h3>
<p>Fixed-point numbers can be explicitly converted to and from
arithmetic types.</p>
<p>While effort is made to ensure that significant digits are not lost
during conversion, no effort is made to avoid rounding errors.
Whatever would happen when converting to and from <code>ReprType</code> largely
applies to <code>fixed_point</code> objects also. For example:</p>
<pre><code>make_ufixed&lt;4, 4&gt;(.006) == make_ufixed&lt;4, 4&gt;(0)
</code></pre>
<p>...equates to <code>true</code> and is considered an acceptable rounding error.</p>
<h3>Operator Overloads</h3>
<p>Any operators that might be applied to integer types can also be
applied to fixed-point types. A guiding principle of operator
overloads is that they perform as little run-time computation as is
practically possible.</p>
<p>With the exception of shift and comparison operators, binary operators
can take any combination of:</p>
<ul>
<li>one or two fixed-point arguments and</li>
<li>zero or one arguments of any arithmetic type, i.e. a type for which
<code>is_arithmetic</code> is true.</li>
</ul>
<p>Where the inputs are not identical fixed-point types, a simple set of
promotion-like rules are applied to determine the return type:</p>
<ol>
<li>If both arguments are fixed-point, a type is chosen which is the
size of the larger type, is signed if either input is signed and
has the maximum integer bits of the two inputs, i.e. cannot lose
high-significance bits through conversion alone.</li>
<li>If one of the arguments is a floating-point type, then the type of
the result is the smallest floating-point type of equal or greater
size than the inputs.</li>
<li>If one of the arguments is an integral type, then the result is the
other, fixed-point type.</li>
</ol>
<p>Some examples:</p>
<pre><code>make_ufixed&lt;5, 3&gt;{8} + make_ufixed&lt;4, 4&gt;{3} == make_ufixed&lt;5, 3&gt;{11};
make_ufixed&lt;5, 3&gt;{8} + 3 == make_ufixed&lt;5, 3&gt;{11};
make_ufixed&lt;5, 3&gt;{8} + float{3} == float{11};
</code></pre>
<p>The reasoning behind this choice is a combination of predictability
and performance. It is explained for each rule as follows:</p>
<ol>
<li>ensures that the least computation is performed where fixed-point
types are used exclusively. Aside from multiplication and division
requiring shift operations, should require similar computational
costs to equivalent integer operations;</li>
<li>loosely follows the promotion rules for mixed-mode arithmetic,
ensures values with exponents far beyond the range of the
fixed-point type are catered for and avoids costly conversion from
floating-point to integer and</li>
<li>preserves the input fixed-point type whose range is far more likely
to be of deliberate importance to the operation.</li>
</ol>
<p>Shift operator overloads require an integer type as the right-hand
parameter and return a type which is adjusted to accommodate the new
value without risk of overflow or underflow.</p>
<p>Comparison operators convert the inputs to a common result type
following the rules above before performing a comparison and returning
<code>true</code> or <code>false</code>.</p>
<h4>Overflow</h4>
<p>Because arithmetic operators return a result of equal capacity to
their inputs, they carry a risk of overflow. For instance,</p>
<pre><code>make_fixed&lt;4, 3&gt;(15) + make_fixed&lt;4, 3&gt;(1)
</code></pre>
<p>causes overflow because because a type with 4 integer bits cannot
store a value of 16.</p>
<p>The result of overflow of any bits in a fixed-point value depends
entirely on how <code>ReprType</code> handles overflow. Thus, for built-in
signed types, the result is undefined and for built-in unsigned types,
the value wraps around.</p>
<h4>Underflow</h4>
<p>The other typical cause of lost bits is underflow where, for example,</p>
<pre><code>make_fixed&lt;7, 0&gt;(15) / make_fixed&lt;7, 0&gt;(2)
</code></pre>
<p>results in a value of 7. This results in loss of precision but is
generally considered acceptable.</p>
<p>However, when all bits are lost due to underflow, the value is said
to be flushed. As with overflow, the result of a flush is the same for
a fixed-point type as it is for its underlying <code>ReprType</code>. In the case
of built-in integral types, the value becomes zero.</p>
<h3>Dealing With Overflow and Flushes</h3>
<p>Errors resulting from overflow and flushes are two of the biggest
headaches related to fixed-point arithmetic. Integers suffer the same
kinds of errors but are somewhat easier to reason about as they lack
fractional digits. Floating-point numbers are largely shielded from
these errors by their variable exponent and implicit bit.</p>
<p>Four strategies for avoiding overflow in fixed-point types are
presented:</p>
<ol>
<li>simply leave it to the user to avoid overflow;</li>
<li>allow the user to provide a custom type for <code>ReprType</code>
which behaves differently from built-in integral types;</li>
<li>promote the result to a larger type to ensure sufficient capacity
or</li>
<li>adjust the exponent of the result upward to ensure that the top
limit of the type is sufficient to preserve the most significant
digits at the expense of the less significant digits.</li>
</ol>
<p>For arithmetic operators, choice 1) is taken because it most closely
follows the behavior of integer types. Thus it should cause the least
surprise to the fewest users. This makes it far easier to reason
about in code where functions are written with a particular type in
mind. It also requires the least computation in most cases.</p>
<p>Choice 2) is beyond the scope of this proposal
and is covered in more detail in the section, <strong>Alternative Types for <code>ReprType</code></strong>.</p>
<p>Choices 3) and 4) are reasonably robust to overflow events.
However, they represent different trade-offs and neither one is the best fit in all situations.
Notably, where any instance of <code>c = a + b</code> is replaced with <code>a += b</code>, results may change in surprising ways.
For these reasons, they are presented as named functions.</p>
<h4>Type Promotion</h4>
<p>Function template, <code>promote</code>, borrows a term from the language
feature which avoids integer overflow prior to certain operations. It
takes a <code>fixed_point</code> object and returns the same value represented
by a larger <code>fixed_point</code> specialization.</p>
<p>For example,</p>
<pre><code>promote(make_fixed&lt;5, 2&gt;(15.5))
</code></pre>
<p>is equivalent to</p>
<pre><code>make_fixed&lt;11, 4&gt;(15.5)
</code></pre>
<p>Complimentary function template, <code>demote</code>, reverses the process,
returning a value of a smaller type.</p>
<h4>Named Arithmetic Functions</h4>
<p>The following named function templates can be used as a hassle-free
alternative to arithmetic operators in situations where the aim is
to avoid overflow.</p>
<p>Unary functions:</p>
<pre><code>trunc_reciprocal, trunc_square, trunc_sqrt,
promote_reciprocal, promote_square
</code></pre>
<p>Binary functions:</p>
<pre><code>trunc_add, trunc_subtract, trunc_multiply, trunc_divide
trunc_shift_left, trunc_shift_right,
promote_add, promote_sub, promote_multiply, promote_divide
</code></pre>
<p>Some notes:</p>
<ol>
<li>The <code>trunc_</code> functions return the result as a type no larger than
the inputs and with an exponent adjusted to avoid overflow;</li>
<li>the <code>promote_</code> functions return the result as a type large enough
to avoid overflow and underflow;</li>
<li>the <code>_multiply</code> and <code>_square</code> functions are not guaranteed to be
available for 64-bit types;</li>
<li>the <code>_multiply</code> and <code>_square</code> functions produce undefined behavior
when all input parameters are the <em>most negative number</em>;</li>
<li>the <code>_square</code> functions return an unsigned type;</li>
<li>the <code>_add</code>, <code>_subtract</code>, <code>_multiply</code> and <code>_divide</code> functions take
heterogeneous <code>fixed_point</code> specializations;</li>
<li>the <code>_divide</code> and <code>_reciprocal</code> functions in no way guard against
divide-by-zero errors;</li>
<li>the <code>trunc_shift_</code> functions return results of the same type as
their first input parameter but with an adjusted <code>Exponent</code> value and</li>
<li>the list is by no means complete.</li>
</ol>
<h3>Alternative Types for <code>ReprType</code></h3>
<p>Using built-in integral types as the default underlying representation
minimizes certain costs:</p>
<ul>
<li>many fixed-point operations are as efficient as their integral equivalents;</li>
<li>compile-time complexity is kept relatively low and</li>
<li>the behavior of fixed-point types should cause few surprises.</li>
</ul>
<p>However, this choice also brings with it many of the deficiencies of built-in types.
For example:</p>
<ul>
<li>the typical rounding behavior is distinct for:
<ul>
<li>conversion from floating-point types;</li>
<li>right shift and</li>
<li>divide operations;</li>
</ul>
</li>
<li>all of these rounding behaviors promote drift and propagate error;</li>
<li>overflow, underflow and flush are handled silently with wrap-around or undefined behavior;</li>
<li>divide-by-zero similarly results in undefined behavior and</li>
<li>the range of values is limited by the largest type: <code>long long int</code>.</li>
</ul>
<p>The effort involved in addressing these deficiencies is non-trivial
and on-going (for example <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0105r0.html">[2]</a>).
As solutions are made available, it should become easier
to define custom integral types which address concerns surrounding robustness and correctness.
Such types deserve their place in the standard library.</p>
<h4>Example Custom Type, <code>integer</code></h4>
<p>A composable system of integer types that is suitable for use with <code>fixed_point</code> might take the following form:</p>
<pre><code>// size may be rounded up in some cases;
template &lt;int NumBytes, bool IsSigned = true&gt;
class sized_integer;

// may take built-in or sized_integer as Repr parameter
template &lt;class Repr = int, rounding Rounding = rounding::towards_odd&gt;
class rounding_integer;

// may take built-in, sized_integer or rounding_integer as Repr parameter
template &lt;class Repr = int, overflow Overflow = overflow::exception&gt;
class overflow_integer;

// a 'kitchen sink' custom integer type
template &lt;int NumBytes, bool IsSigned = true, rounding Rounding = rounding::towards_odd, overflow Overflow = overflow::exception&gt;
using integer =
  overflow_integer&lt;
    rounding_integer&lt;
      sized_integer&lt;NumBytes, IsSigned&gt;,
      Rounding&gt;,
    Overflow&gt;;
</code></pre>
<p>Any of these types might be used to compose <code>fixed_point</code> specializations
without paying (in compile-time complexity) for features that are not used.</p>
<p>While the issues related to integer types affect the fixed-point types they support,
they are not specific to fixed-point.
It would not only be premature - but inappropriate -
to attempt to address rounding and error handling at the level of a fixed-point type.</p>
<h4>Required Specializations</h4>
<p>For a type to be suitable as parameter, <code>ReprType</code>, of <code>fixed_point</code>,
it must meet the following requirements:</p>
<ul>
<li>it must have specialized the following existing standard library types:
<ul>
<li><code>is_signed</code> or <code>is_unsigned</code></li>
<li><code>make_signed</code> and <code>make_unsigned</code></li>
</ul>
</li>
<li>it must have specialized the following proposed standard library type:
<ul>
<li><code>resize</code></li>
</ul>
</li>
</ul>
<h4><code>resize</code> and <code>resize_t</code> Helper Types</h4>
<p>Any type used as <code>ReprType</code> (including built-in integers) must define a specialization of <code>resize</code>:</p>
<pre><code>template &lt;class Archetype, int NumBytes&gt;
struct resize;
</code></pre>
<p>An alias to <code>resize</code> is <code>resize_t</code> which can be defined as:</p>
<pre><code>template &lt;class Archetype, int NumBytes&gt;
using resize_t = typename resize&lt;Archetype, NumBytes&gt;::type;
</code></pre>
<p>The resultant type is of the same signedness as <code>Archetype</code>
and has a capacity of no less than <code>NumBytes</code> bytes
such that for any Integer, <code>T</code> and positive integer value, 'N':</p>
<pre><code>is_signed_v&lt;resize_t&lt;T, N&gt;&gt; == is_signed_v&lt;T&gt;
</code></pre>
<p>and</p>
<pre><code>sizeof(resize_t&lt;T, N&gt;) &gt;= N
</code></pre>
<p>are both true.</p>
<p>For example, the type of <code>resize_t&lt;unsigned, 1&gt;</code> is <code>uint8_t</code>
and the type of <code>resize_t&lt;int8_t, 8&gt;</code> is <code>int64_t</code>.</p>
<h3>Example</h3>
<p>The following example calculates the magnitude of a 3-dimensional vector.</p>
<pre><code>template &lt;class Fp&gt;
constexpr auto magnitude(const Fp &amp; x, const Fp &amp; y, const Fp &amp; z)
-&gt; decltype(trunc_sqrt(trunc_add(trunc_square(x), trunc_square(y), trunc_square(z))))
{
    return trunc_sqrt(trunc_add(trunc_square(x), trunc_square(y), trunc_square(z)));
}
</code></pre>
<p>Calling the above function as follows</p>
<pre><code>static_cast&lt;double&gt;(magnitude(
    make_ufixed&lt;4, 12&gt;(1),
    make_ufixed&lt;4, 12&gt;(4),
    make_ufixed&lt;4, 12&gt;(9)));
</code></pre>
<p>returns the value, 9.890625.</p>
<h2>V. Technical Specification</h2>
<h3>Header &lt;fixed_point&gt; Synopsis</h3>
<pre><code>namespace std {
  template &lt;class ReprType, int Exponent&gt; class fixed_point;

  template &lt;unsigned IntegerDigits, unsigned FractionalDigits = 0, class Archetype = signed&gt;
    using make_fixed;
  template &lt;unsigned IntegerDigits, unsigned FractionalDigits = 0, class Archetype = unsigned&gt;
    using make_ufixed;
  template &lt;class ReprType, int IntegerDigits&gt;
    using make_fixed_from_repr;

  template &lt;class ReprType, int Exponent, int NumBytes&gt;
    struct resize&lt;fixed_point&lt;ReprType, Exponent&gt;, NumBytes&gt;;

  template &lt;class FixedPoint&gt;
    using promote_result;
  template &lt;class FixedPoint&gt;
    promote_result&lt;FixedPoint&gt;
      constexpr promote(const FixedPoint &amp; from) noexcept;

  template &lt;class FixedPoint&gt;
    using demote_result;
  template &lt;class FixedPoint&gt;
    demote_result&lt;FixedPoint&gt;
      constexpr demote(const FixedPoint &amp; from) noexcept;

  template &lt;class ReprType, int Exponent&gt;
  	constexpr bool operator==(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
    constexpr bool operator!=(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
      const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr bool operator&lt;(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr bool operator&gt;(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr bool operator&gt;=(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr bool operator&lt;=(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;

  template &lt;class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent&gt; operator-(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent&gt; operator+(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent&gt; operator-(
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	fixed_point&lt;ReprType, Exponent&gt; &amp; operator+=(
  	  fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	fixed_point&lt;ReprType, Exponent&gt; &amp; operator-=(
  	  fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	fixed_point&lt;ReprType, Exponent&gt; &amp; operator*=(
  	  fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	fixed_point&lt;ReprType, Exponent&gt; &amp; operator/=(
  	  fixed_point&lt;ReprType, Exponent&gt; &amp; lhs,
  	  const fixed_point&lt;ReprType, Exponent&gt; &amp; rhs) noexcept;

  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator==(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator!=(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator&lt;(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator&gt;(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator&gt;=(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator&lt;=(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;

  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator+(
  	  const Lhs &amp; lhs,
  	  const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs&gt;
  	constexpr auto operator-(
  	  const Lhs &amp; lhs,
  	  const Rhs &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator*(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator/(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class Integer&gt;
  	constexpr auto operator*(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const Integer &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class Integer&gt;
  	constexpr auto operator/(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const Integer &amp; rhs) noexcept;
  template &lt;class Integer, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator*(
  	  const Integer &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class Integer, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator/(
  	  const Integer &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class Float&gt;
  	constexpr auto operator*(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const Float &amp; rhs) noexcept;
  template &lt;class LhsReprType, int LhsExponent, class Float&gt;
  	constexpr auto operator/(
  	  const fixed_point&lt;LhsReprType, LhsExponent&gt; &amp; lhs,
  	  const Float &amp; rhs) noexcept;
  template &lt;class Float, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator*(
  	  const Float &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class Float, class RhsReprType, int RhsExponent&gt;
  	constexpr auto operator/(
  	  const Float &amp; lhs,
  	  const fixed_point&lt;RhsReprType, RhsExponent&gt; &amp; rhs) noexcept;
  template &lt;class LhsReprType, int Exponent, class Rhs&gt;
  	fixed_point&lt;LhsReprType, Exponent&gt; &amp; operator+=(fixed_point&lt;LhsReprType, Exponent&gt; &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class LhsReprType, int Exponent, class Rhs&gt;
  	fixed_point&lt;LhsReprType, Exponent&gt; &amp; operator-=(fixed_point&lt;LhsReprType, Exponent&gt; &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class LhsReprType, int Exponent&gt;
  template &lt;class Rhs, typename std::enable_if&lt;std::is_arithmetic&lt;Rhs&gt;::value, int&gt;::type Dummy&gt;
  	fixed_point&lt;LhsReprType, Exponent&gt; &amp;
  	fixed_point&lt;LhsReprType, Exponent&gt;::operator*=(const Rhs &amp; rhs) noexcept;
  template &lt;class LhsReprType, int Exponent&gt;
  template &lt;class Rhs, typename std::enable_if&lt;std::is_arithmetic&lt;Rhs&gt;::value, int&gt;::type Dummy&gt;
  	fixed_point&lt;LhsReprType, Exponent&gt; &amp;
  	fixed_point&lt;LhsReprType, Exponent&gt;::operator/=(const Rhs &amp; rhs) noexcept;
  template &lt;class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent&gt;
  	  sqrt(const fixed_point&lt;ReprType, Exponent&gt; &amp; x) noexcept;
  template &lt;class FixedPoint, unsigned N = 2&gt;
  	using trunc_add_result;

  template &lt;class FixedPoint, class ... Tail&gt;
  	trunc_add_result&lt;FixedPoint, sizeof...(Tail) + 1&gt;
      constexpr trunc_add(const FixedPoint &amp; addend1, const Tail &amp; ... addend_tail);
  template &lt;class Lhs, class Rhs = Lhs&gt;
  	using trunc_subtract_result;
  template &lt;class Lhs, class Rhs&gt;
  	trunc_subtract_result&lt;Lhs, Rhs&gt;
  	  constexpr trunc_subtract(const Lhs &amp; minuend, const Rhs &amp; subtrahend);
  template &lt;class Lhs, class Rhs = Lhs&gt;
  	using trunc_multiply_result;

  template &lt;class Lhs, class Rhs&gt;
  	trunc_multiply_result&lt;Lhs, Rhs&gt;
  	  constexpr trunc_multiply(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class FixedPointDividend, class FixedPointDivisor = FixedPointDividend&gt;
  	using trunc_divide_result;
  template &lt;class FixedPointDividend, class FixedPointDivisor&gt;
  	trunc_divide_result&lt;FixedPointDividend, FixedPointDivisor&gt;
  	  constexpr trunc_divide(const FixedPointDividend &amp; lhs, const FixedPointDivisor &amp; rhs) noexcept;
  template &lt;class FixedPoint&gt;
  	using trunc_reciprocal_result;
  template &lt;class FixedPoint&gt;
  	trunc_reciprocal_result&lt;FixedPoint&gt;
  	  constexpr trunc_reciprocal(const FixedPoint &amp; fixed_point) noexcept;
  template &lt;class FixedPoint&gt;
  	using trunc_square_result;

  template &lt;class FixedPoint&gt;
  	trunc_square_result&lt;FixedPoint&gt;
  	  constexpr trunc_square(const FixedPoint &amp; root) noexcept;
  template &lt;class FixedPoint&gt;
  	using trunc_sqrt_result;
  template &lt;class FixedPoint&gt;
  	trunc_sqrt_result&lt;FixedPoint&gt;
  	  constexpr trunc_sqrt(const FixedPoint &amp; square) noexcept;
  template &lt;int Integer, class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent + Integer&gt;
  	  trunc_shift_left(const fixed_point&lt;ReprType, Exponent&gt; &amp; fp) noexcept;
  template &lt;int Integer, class ReprType, int Exponent&gt;
  	constexpr fixed_point&lt;ReprType, Exponent - Integer&gt;
  	  trunc_shift_right(const fixed_point&lt;ReprType, Exponent&gt; &amp; fp) noexcept;
  template &lt;class FixedPoint, unsigned N = 2&gt;
  	using promote_add_result;

  template &lt;class FixedPoint, class ... Tail&gt;
  	promote_add_result&lt;FixedPoint, sizeof...(Tail) + 1&gt;
  	  constexpr promote_add(const FixedPoint &amp; addend1, const Tail &amp; ... addend_tail);
  template &lt;class Lhs, class Rhs = Lhs&gt;
  	using promote_subtract_result
  template &lt;class Lhs, class Rhs&gt;
  	promote_subtract_result&lt;Lhs, Rhs&gt;
  	  constexpr promote_subtract(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs = Lhs&gt;
  	using promote_multiply_result;
  template &lt;class Lhs, class Rhs&gt;
  	promote_multiply_result&lt;Lhs, Rhs&gt;
  	  constexpr promote_multiply(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class Lhs, class Rhs = Lhs&gt;
  	using promote_divide_result;
  template &lt;class Lhs, class Rhs&gt;
  	promote_divide_result&lt;Lhs, Rhs&gt;
  	  constexpr promote_divide(const Lhs &amp; lhs, const Rhs &amp; rhs) noexcept;
  template &lt;class FixedPoint&gt;
  	using promote_square_result;
  template &lt;class FixedPoint&gt;
  	promote_square_result&lt;FixedPoint&gt;
  	  constexpr promote_square(const FixedPoint &amp; root) noexcept;

  template &lt;class Archetype, int NumBytes&gt;
    struct resize;
  template &lt;class Archetype, int NumBytes&gt;
    using resize_t;
}
</code></pre>
<h4><code>fixed_point&lt;&gt;</code> Class Template</h4>
<pre><code>template &lt;class ReprType = int, int Exponent = 0&gt;
class fixed_point
{
public:
  using repr_type = ReprType;

  constexpr static int exponent;
  constexpr static int digits;
  constexpr static int integer_digits;
  constexpr static int fractional_digits;

  fixed_point() noexcept;
  template &lt;class S, typename std::enable_if&lt;_impl::is_integral&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    explicit constexpr fixed_point(S s) noexcept;
  template &lt;class S, typename std::enable_if&lt;std::is_floating_point&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    explicit constexpr fixed_point(S s) noexcept;
  template &lt;class FromReprType, int FromExponent&gt;
    explicit constexpr fixed_point(const fixed_point&lt;FromReprType, FromExponent&gt; &amp; rhs) noexcept;
  template &lt;class S, typename std::enable_if&lt;_impl::is_integral&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    fixed_point &amp; operator=(S s) noexcept;
  template &lt;class S, typename std::enable_if&lt;std::is_floating_point&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    fixed_point &amp; operator=(S s) noexcept;
  template &lt;class FromReprType, int FromExponent&gt;
    fixed_point &amp; operator=(const fixed_point&lt;FromReprType, FromExponent&gt; &amp; rhs) noexcept;

  template &lt;class S, typename std::enable_if&lt;_impl::is_integral&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    explicit constexpr operator S() const noexcept;
  template &lt;class S, typename std::enable_if&lt;std::is_floating_point&lt;S&gt;::value, int&gt;::type Dummy = 0&gt;
    explicit constexpr operator S() const noexcept;
  explicit constexpr operator bool() const noexcept;

  template &lt;class Rhs, typename std::enable_if&lt;std::is_arithmetic&lt;Rhs&gt;::value, int&gt;::type Dummy = 0&gt;
    fixed_point &amp;operator*=(const Rhs &amp; rhs) noexcept;
  template &lt;class Rhs, typename std::enable_if&lt;std::is_arithmetic&lt;Rhs&gt;::value, int&gt;::type Dummy = 0&gt;
    fixed_point &amp; operator/=(const Rhs &amp; rhs) noexcept;

  constexpr repr_type data() const noexcept;
  static constexpr fixed_point from_data(repr_type repr) noexcept;
};
</code></pre>
<h3>Header &lt;type_traits&gt; Synopsis</h3>
<p>The following definitions are added to <code>&lt;type_traits&gt;</code>:</p>
<pre><code>template &lt;class Archetype, int NumBytes&gt;
  struct resize;
template &lt;class Archetype, int NumBytes&gt;
  using resize_t = typename resize&lt;Archetype, NumBytes&gt;::type;
</code></pre>
<h2>VI. Future Issues</h2>
<h3>Library Support</h3>
<p>Because the aim is to provide an alternative to existing arithmetic
types which are supported by the standard library, it is conceivable
that a future proposal might specialize existing class templates and
overload existing functions.</p>
<p>Possible candidates for overloading include the functions defined in
&lt;cmath&gt; and a templated specialization of <code>numeric_limits</code>. A new type
trait, <code>is_fixed_point</code>, would also be useful.</p>
<p>While <code>fixed_point</code> is intended to provide drop-in replacements to
existing built-ins, it may be preferable to deviate slightly from the
behavior of certain standard functions. For example, overloads of
functions from &lt;cmath&gt; will be considerably less concise, efficient
and versatile if they obey rules surrounding error cases. In
particular, the guarantee of setting <code>errno</code> in the case of an error
prevents a function from being defined as pure. This highlights a
wider issue surrounding the adoption of the functional approach and
compile-time computation that is beyond the scope of this document.</p>
<h3>Bounded Integers</h3>
<p>The bounded::integer library <a href="http://doublewise.net/c++/bounded/">[3]</a>
exemplifies the benefits of keeping track of ranges of values in
arithmetic types at compile time.</p>
<p>To a limited extent, the <code>trunc_</code> functions defined here also keep
track of - and modify - the limits of values. However, a combination
of techniques is capable of producing superior results.</p>
<p>For instance, consider the following expression:</p>
<pre><code>make_ufixed&lt;2, 6&gt; three(3);
auto n = trunc_square(trunc_square(three));
</code></pre>
<p>The type of <code>n</code> is <code>make_ufixed&lt;8, 0&gt;</code> but its value does not
exceed 81. Hence, an integer bit has been wasted. It may be possible
to track more accurate limits in the same manner as the
bounded::integer library in order to improve the precision of types
returned by <code>trunc_</code> functions. For this reason, the exact value of
the exponents of these return types is not given.</p>
<p>Notes:</p>
<ul>
<li>Bounded::integer is already supported by fixed-point library,
<em>fp</em> <a href="https://github.com/mizvekov/fp">[4]</a>.</li>
<li>A similar library is the boost <em>constrained_value</em> library
<a href="http://rk.hekko.pl/constrained_value/">[5]</a>.</li>
</ul>
<h3>Compile-Time Bit-Shift Operations</h3>
<p>A notable feature of the <em>fp</em> library <a href="https://github.com/mizvekov/fp">[4]</a>
is the creation of an alias for <code>integral_constant</code> which can be applied to the right-hand side of bit-shift operations.
The type returned from this operation has identical bit-wise value to the left-hand input
but with <code>Exponent</code> value adjusted by the amount of the right-hand side.
It is essentially the same as the <code>trunc_shift_</code> functions
and means that when shifting by literal values what looks looks like run-time operation
is a compile-time calculation which guarantees no overflow or underflow.</p>
<h3>Alternative Return Type Policies</h3>
<p>When devising a strategy for mitigating the risk of overflow during arithmetic operations,
the number of integer and fractional bits stored in the result is an important choice.
The <code>fixed_point</code> type picks one of the simpler options by default,
but it is by no means the only viable one.</p>
<p>The <em>fp</em> library <a href="https://github.com/mizvekov/fp">[4]</a> returns a type
whose size matches the inputs but whose exponent is shifted to preserve high bits.
The arithmetic types proposed in P0106
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html">[9]</a>
increase capacity to ensure that precision is preserved.
(These two approaches share common aims with
the <code>trunc_</code> and <code>promote_</code> named arithmetic functions proposed earlier.)
Even greater control of the required capacity of a fixed-point type can be afforded by
systems such as the <em>bounded::integer</em> library <a href="http://doublewise.net/c++/bounded/">[3]</a>.</p>
<p>A common requirement among these approaches is the ability
to specify the return type of arithmetic operations.
For this reason, named-function arithmetic operators
which are more expressive than those proposed so far may be necessary.</p>
<p>These functions would specify a return type as a template parameter. For example:</p>
<pre><code>template &lt;class Result, class Lhs, class Rhs&gt;
constexpr Result fixed_point_multiply(const Lhs &amp; lhs, const Rhs &amp; rhs);
</code></pre>
<h2>VII. Prior Art</h2>
<p>Many examples of fixed-point support in C and C++ exist. While almost
all of them aim for low run-time cost and expressive alternatives to
raw integer manipulation, they vary greatly in detail and in terms of
their interface.</p>
<p>One especially interesting dichotomy is between solutions which offer
a discrete selection of fixed-point types and libraries which contain
a continuous range of exponents through type parameterization.</p>
<h3>N1169</h3>
<p>One example of the former is found in proposal N1169
<a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1169.pdf">[6]</a>,
the intent of which is to expose features found in certain embedded
hardware. It introduces a succinct set of language-level fixed-point
types and impose constraints on the number of integer or fractional
digits each can possess.</p>
<p>As with all examples of discrete-type fixed-point support, the limited
choice of exponents is a considerable restriction on the versatility
and expressiveness of the API.</p>
<p>Nevertheless, it may be possible to harness performance gains provided
by N1169 fixed-point types through explicit template specialization.
This is likely to be a valuable proposition to potential users of the
library who find themselves targeting platforms which support
fixed-point arithmetic at the hardware level.</p>
<h3>P0106</h3>
<p>There are many other C++ libraries available which fall into the
latter category of continuous-range fixed-point arithmetic
<a href="https://github.com/mizvekov/fp">[4]</a>
<a href="http://www.codeproject.com/Articles/37636/Fixed-Point-Class">[7]</a>
<a href="https://github.com/viboes/fixed_point">[8]</a>. In particular, an
existing library proposal, P0106 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html">[9]</a>,
aims to achieve very similar goals through similar means and warrants
closer comparison than N1169.</p>
<p>P0106 introduces four class templates covering the quadrant of signed
versus unsigned and fractional versus integer numeric types. It is
intended to replace built-in types in a wide variety of situations and
accordingly, is highly compile-time configurable in terms of how
rounding and overflow are handled. Parameters to these four class
templates include the storage in bits and - for fractional types - the
resolution.</p>
<p>The <code>fixed_point</code> class template could probably - with a few caveats -
be generated using the two fractional types, <code>nonnegative</code> and
<code>negatable</code>, replacing the <code>ReprType</code> parameter with the integer bit
count of <code>ReprType</code>, specifying <code>fastest</code> for the rounding mode and
specifying <code>undefined</code> as the overflow mode.</p>
<p>However, <code>fixed_point</code> more closely and concisely caters to the needs of
users who already use integer types and simply desire a more concise,
less error-prone form. It more closely follows the four design aims of
the library and - it can be argued - more closely follows the spirit
of the standard in its pursuit of zero-cost abstraction.</p>
<p>Some aspects of the design of the P0106 API which back up these
conclusion are that:</p>
<ul>
<li>the result of arithmetic operations closely resemble the <code>trunc_</code>
function templates and are potentially more costly at run-time;</li>
<li>the nature of the range-specifying template parameters - through
careful framing in mathematical terms - abstracts away valuable
information regarding machine-critical type size information;</li>
<li>the breaking up of duties amongst four separate class templates
introduces four new concepts and incurs additional mental load for
relatively little gain while further detaching the interface from
vital machine-level details and</li>
<li>the absence of the most negative number from signed types reduces
the capacity of all types by one.</li>
</ul>
<p>The added versatility that the P0106 API provides regarding rounding
and overflow handling are of relatively low priority to users who
already bear the scars of battles with raw integer types.
Nevertheless, providing them as options to be turned on or off at
compile time is an ideal way to leave the choice in the hands of the
user.</p>
<p>Many high-performance applications - in which fixed-point is of
potential value - favor run-time checks during development which are
subsequently deactivated in production builds.
The P0106 interface is highly conducive to this style of development.
The design proposed in this paper aims to achieve similar results
by composing fixed-point types from custom integral types.</p>
<h2>VIII. Acknowledgements</h2>
<p>Subgroup: Guy Davidson, Michael Wong<br>
Contributors: Ed Ainsley, Billy Baker, Lance Dyson, Marco Foco,
Clément Grégoire, Nicolas Guillemot, Matt Kinzelman, Joël Lamotte,
Sean Middleditch, Patrice Roy, Peter Schregle, Ryhor Spivak</p>
<h2>IX. Revisions</h2>
<p>This paper revises <a href="http://johnmcfarlane.github.io/fixed_point/papers/p0037r0.html">P0037R0</a>:</p>
<ul>
<li>allows non-built-in types as template parameter to <code>fixed_point</code> to address issues such as rounding and overflow with;</li>
<li>adds to the list of design considerations;</li>
<li>adds new types, <code>resize</code> and <code>resize_t</code> to header, <code>type_traits</code>;</li>
<li>changes parameters of <code>make_fixed</code> and <code>make_ufixed</code>;</li>
<li>specifies behavior in case of overflow;</li>
<li>adds extra reason for choice of arithmetic operator return type;</li>
<li>corrects factual error in description of <code>trunc_shift_</code> functions;</li>
<li>adds sub-section, <strong>Alternative Types for <code>ReprType</code></strong>;</li>
<li>removes sub-section, <strong>Alternatives to Built-in Integer Types</strong>;</li>
<li>revises sub-section, <strong>Alternative Policies</strong> and renames <strong>Alternative Return Type Policies</strong>;</li>
<li>adds section, <strong>Revisions</strong>.</li>
</ul>
<h2>X. References</h2>
<ol>
<li>Why Integer Coordinates?, <a href="http://www.pathengine.com/Contents/Overview/FundamentalConcepts/WhyIntegerCoordinates/page.php">http://www.pathengine.com/Contents/Overview/FundamentalConcepts/WhyIntegerCoordinates/page.php</a></li>
<li>Rounding and Overflow in C++, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0105r0.html</li>
<li>C++ bounded::integer library, <a href="http://doublewise.net/c++/bounded/">http://doublewise.net/c++/bounded/</a></li>
<li>fp, C++14 Fixed Point Library, <a href="https://github.com/mizvekov/fp">https://github.com/mizvekov/fp</a></li>
<li>Boost Constrained Value Libarary, <a href="http://rk.hekko.pl/constrained_value/">http://rk.hekko.pl/constrained_value/</a></li>
<li>N1169, Extensions to support embedded processors, <a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1169.pdf">http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1169.pdf</a></li>
<li>fpmath, Fixed Point Math Library, <a href="http://www.codeproject.com/Articles/37636/Fixed-Point-Class">http://www.codeproject.com/Articles/37636/Fixed-Point-Class</a></li>
<li>Boost fixed_point (proposed), Fixed point integral and fractional types, <a href="https://github.com/viboes/fixed_point">https://github.com/viboes/fixed_point</a></li>
<li>P0106, C++ Binary Fixed-Point Arithmetic, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html</a></li>
<li>fixed_point, Reference Implementation of P0037, <a href="https://github.com/johnmcfarlane/fixed_point">https://github.com/johnmcfarlane/fixed_point</a></li>
</ol>
<h2>XI. Appendix 1: Reference Implementation</h2>
<p>An in-development implementation of the fixed_point class template and
its essential supporting functions and types is available
<a href="https://github.com/johnmcfarlane/fixed_point">[10]</a>.</p>
<p>Items include:</p>
<ul>
<li>utility header containing definitions for:
<ul>
<li>math and trigonometric functions and</li>
<li>a partial <code>numeric_limits</code> specialization;</li>
</ul>
</li>
<li>a type, <code>sg14::integer</code>, intended to explore and illustrate the potential of custom <code>ReprType</code>;</li>
<li>compile-time tests of <code>constexpr</code> operations;</li>
<li>run-time tests of assignment and exception-throwing behavior and</li>
<li>benchmarking support (used to generate results in this paper).</li>
</ul>
<h2>XII. Appendix 2: Performance</h2>
<p>Despite a focus on usable interface and direct translation from
integer-based fixed-point operations, there is an overwhelming
expectation that the source code result in minimal instructions and
clock cycles. A few preliminary numbers are presented to give a very
early idea of how the API might perform.</p>
<p>Some notes:</p>
<ul>
<li>
<p>A few test functions were run, ranging from single arithmetic
operations to basic geometric functions, performed against integer,
floating-point and fixed-point types for comparison.</p>
</li>
<li>
<p>Figures were taken from a single CPU, OS and compiler, namely:</p>
<pre><code>Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
</code></pre>
</li>
<li>
<p>Fixed inputs were provided to each function, meaning that branch
prediction rarely fails. Results may also not represent the full
range of inputs.</p>
</li>
<li>
<p>Details of the test harness used can be found in the source
project mentioned in Appendix 1;</p>
</li>
<li>
<p>Times are in nanoseconds;</p>
</li>
<li>
<p>Code has not yet been optimized for performance.</p>
</li>
</ul>
<h3>Types</h3>
<p>Where applicable various combinations of integer, floating-point and
fixed-point types were tested with the following identifiers:</p>
<ul>
<li><code>uint8_t</code>, <code>int8_t</code>, <code>uint16_t</code>, <code>int16_t</code>, <code>uint32_t</code>, <code>int32_t</code>,
<code>uint64_t</code> and <code>int64_t</code> built-in integer types;</li>
<li><code>float</code>, <code>double</code> and <code>long double</code> built-in floating-point types;</li>
<li>s3:4, u4:4, s7:8, u8:8, s15:16, u16:16, s31:32 and u32:32 format
fixed-point types.</li>
</ul>
<h3>Basic Arithmetic</h3>
<p>Plus, minus, multiplication and division were tested in isolation
using a number of different numeric types with the following results:</p>
<p>name	cpu_time<br>
add(float)	1.78011<br>
add(double)	1.73966<br>
add(long double)	3.46011<br>
add(u4_4)	1.87726<br>
add(s3_4)	1.85051<br>
add(u8_8)	1.85417<br>
add(s7_8)	1.82057<br>
add(u16_16)	1.94194<br>
add(s15_16)	1.93463<br>
add(u32_32)	1.94674<br>
add(s31_32)	1.94446<br>
add(int8_t)	2.14857<br>
add(uint8_t)	2.12571<br>
add(int16_t)	1.9936<br>
add(uint16_t)	1.88229<br>
add(int32_t)	1.82126<br>
add(uint32_t)	1.76<br>
add(int64_t)	1.76<br>
add(uint64_t)	1.83223<br>
sub(float)	1.96617<br>
sub(double)	1.98491<br>
sub(long double)	3.55474<br>
sub(u4_4)	1.77006<br>
sub(s3_4)	1.72983<br>
sub(u8_8)	1.72983<br>
sub(s7_8)	1.72983<br>
sub(u16_16)	1.73966<br>
sub(s15_16)	1.85051<br>
sub(u32_32)	1.88229<br>
sub(s31_32)	1.87063<br>
sub(int8_t)	1.76<br>
sub(uint8_t)	1.74994<br>
sub(int16_t)	1.82126<br>
sub(uint16_t)	1.83794<br>
sub(int32_t)	1.89074<br>
sub(uint32_t)	1.85417<br>
sub(int64_t)	1.83703<br>
sub(uint64_t)	2.04914<br>
mul(float)	1.9376<br>
mul(double)	1.93097<br>
mul(long double)	102.446<br>
mul(u4_4)	2.46583<br>
mul(s3_4)	2.09189<br>
mul(u8_8)	2.08<br>
mul(s7_8)	2.18697<br>
mul(u16_16)	2.12571<br>
mul(s15_16)	2.10789<br>
mul(u32_32)	2.10789<br>
mul(s31_32)	2.10789<br>
mul(int8_t)	1.76<br>
mul(uint8_t)	1.78011<br>
mul(int16_t)	1.8432<br>
mul(uint16_t)	1.76914<br>
mul(int32_t)	1.78011<br>
mul(uint32_t)	2.19086<br>
mul(int64_t)	1.7696<br>
mul(uint64_t)	1.79017<br>
div(float)	5.12<br>
div(double)	7.64343<br>
div(long double)	8.304<br>
div(u4_4)	3.82171<br>
div(s3_4)	3.82171<br>
div(u8_8)	3.84<br>
div(s7_8)	3.8<br>
div(u16_16)	9.152<br>
div(s15_16)	11.232<br>
div(u32_32)	30.8434<br>
div(s31_32)	34<br>
div(int8_t)	3.82171<br>
div(uint8_t)	3.82171<br>
div(int16_t)	3.8<br>
div(uint16_t)	3.82171<br>
div(int32_t)	3.82171<br>
div(uint32_t)	3.81806<br>
div(int64_t)	10.2286<br>
div(uint64_t)	8.304</p>
<p>Among the slowest types are <code>long double</code>. It is likely that they are
emulated in software. The next slowest operations are fixed-point
multiply and divide operations - especially with 64-bit types. This is
because values need to be promoted temporarily to double-width types.
This is a known fixed-point technique which inevitably experiences
slowdown where a 128-bit type is required on a 64-bit system.</p>
<p>Here is a section of the disassembly of the s15:16 multiply call:</p>
<pre><code>30:   mov    %r14,%rax
      mov    %r15,%rax
      movslq -0x28(%rbp),%rax
      movslq -0x30(%rbp),%rcx
      imul   %rax,%rcx
      shr    $0x10,%rcx
      mov    %ecx,-0x38(%rbp)
      mov    %r12,%rax
4c:   movzbl (%rbx),%eax
      cmp    $0x1,%eax
    ↓ jne    68
54:   mov    0x8(%rbx),%rax
      lea    0x1(%rax),%rcx
      mov    %rcx,0x8(%rbx)
      cmp    0x38(%rbx),%rax
    ↑ jb     30
</code></pre>
<p>The two 32-bit numbers are multiplied together and the result shifted
down - much as it would if raw <code>int</code> values were used. The efficiency
of this operation varies with the exponent. An exponent of zero should
mean no shift at all.</p>
<h3>3-Dimensional Magnitude Squared</h3>
<p>A fast <code>sqrt</code> implementation has not yet been tested with
<code>fixed_point</code>. (The naive implementation takes over 300ns.) For this
reason, a magnitude-squared function is measured, combining multiply
and add operations:</p>
<pre><code>template &lt;class FP&gt;
constexpr FP magnitude_squared(const FP &amp; x, const FP &amp; y, const FP &amp; z)
{
    return x * x + y * y + z * z;
}
</code></pre>
<p>Only real number formats are tested:</p>
<p>float  2.42606<br>
double  2.08<br>
long double  4.5056<br>
s3_4  2.768<br>
s7_8  2.77577<br>
s15_16  2.752<br>
s31_32  4.10331</p>
<p>Again, the size of the type seems to have the largest impact.</p>
<h3>Circle Intersection</h3>
<p>A similar operation includes a comparison and branch:</p>
<pre><code>template &lt;class Real&gt;
bool circle_intersect_generic(Real x1, Real y1, Real r1, Real x2, Real y2, Real r2)
{
    auto x_diff = x2 - x1;
	auto y_diff = y2 - y1;
    auto distance_squared = x_diff * x_diff + y_diff * y_diff;

    auto touch_distance = r1 + r2;
	auto touch_distance_squared = touch_distance * touch_distance;

    return distance_squared &lt;= touch_distance_squared;
}
</code></pre>
<p>float	3.46011<br>
double	3.48<br>
long double	6.4<br>
s3_4	3.88<br>
s7_8	4.5312<br>
s15_16	3.82171<br>
s31_32	5.92</p>
<p>Again, fixed-point and native performance are comparable.</p>
</body></html>
