C++ Safe Numerics (PROPOSAL)
C++ Safe Numerics (PROPOSAL)
Contact: [email protected]
Date: 2016-02-16
1 Motivation
Arithmetic operations in C++ are NOT guaranteed to yield a correct mathematical
result. This feature is inherited from the early days of C. The behavior of int,
unsigned int 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 example illustrates this problem.
3 Design Decisions
The template class is designed to function as closely as possible as a drop-in
replacement for corresponding built-in integer types.
/Users/robertramey/…/doc/boostbook/proposal.xml page 1 of 13
one should be able to just substitute safe<T> for all instances of T in
any program and expect it compile and execute as before with no other
changes.
Since C++ permits freely mixing signed and unsigned integer types in
expressions, safe versions of these types can also be. This complicates
the implementation of the library to significant degree.
5. "Performance"
#include <cstdint>
/Users/robertramey/…/doc/boostbook/proposal.xml page 2 of 13
#include <safe>
Some processors have the ability to detect erroneous results but the C++
language doesn't include the ability to exploit these features.
Implementor's of this library will have the option to exploit these
features to diminish or eliminate runtime costs.
If all else fails and the runtime cost is deemed too large for the program
to bear, users will have the option of creating their own aliases for the
types the program uses and assign them according to the whether they
are building a "Debug" or "Release" version. This is not ideal, but would
still be preferable to the current approach which generally consists of
ignoring the possibility that C++ numeric operations may produce
arithmetically incorrect results.
/Users/robertramey/…/doc/boostbook/proposal.xml page 3 of 13
place for these ideas, (see below), but I don't think the standard library is
that place.
4 Existing Implementations
This proposal is a simpler version / subset of the Safe Numerics library in
development by Robert Ramey on the Boost Library Incubator. It is compatible
with this proposal but it also includes:
Policy classes for type promotion. These permit substitution of C++ standard
type promotion rules with other ones which can reduce or eliminate the need
for runtime error checking code.
Without comment, here are implementations of libraries which are in some way
similar to this proposal
5 Technical Specifications
5.1 Type Requirements
5.1.1 Numeric<T>
5.1.1.1 Description
5.1.1.2 Notation
/Users/robertramey/…/doc/boostbook/proposal.xml page 4 of 13
T, U, V A type that is a model of the Numeric
General
std::numeric_limits<T>.is_bounded true
std::numeric_limits<T>.is_specialized true
os << T os &i
is >> T is &
Unary Operators
-t T Invert sign
+t T unary plus - a no op
~ T complement
/Users/robertramey/…/doc/boostbook/proposal.xml page 5 of 13
Binary Operators
t - u V subtract u from t
t + u V add u to t
t * u V multiply t by u
t / u T divide t by u
t % u T t modulus u
t = u T assign value of u to t
/Users/robertramey/…/doc/boostbook/proposal.xml page 6 of 13
t |= u T or the value of t with u and assign to t
5.1.1.5 Header
#include <safe_numerics/include/concepts/numeric.hpp>
5.1.1.6 Models
int, safe_signed_integer<int>, safe_signed_range<int>, etc.
5.1.2 Integer<T>
5.1.2.1 Description
5.1.2.2 Refinement of
Numeric
5.1.2.4 Header
#include <safe_numerics/include/concepts/numeric.hpp>
5.1.2.5 Models
int, safe<int>, safe_unsigned_range<0, 11>, etc.
5.1.3 SafeNumeric<T>
/Users/robertramey/…/doc/boostbook/proposal.xml page 7 of 13
5.1.3.1 Description
This holds an arithmetic value which can be used as a replacement for built-in
C++ arithmetic values. These types differ from their built-in counter parts in that
the are guaranteed not to produce invalid arithmetic results.
5.1.3.2 Refinement of
Numeric
5.1.3.3 Notation
Symbol Description
t, u objects of types T, U
/Users/robertramey/…/doc/boostbook/proposal.xml page 8 of 13
s S1 convert t to type S1 and assign it to s1. If the value t
assign_op cannot be represented as an instance of type S1, it is
t
an error.
is_safe<S> std::true_type type trait to query whether any type T fulfills the
or requirements for a SafeNumeric type.
std::false_type
Binary expressions which are not assignments require that promotion and
exception policies be identical.
5.1.3.6 Invariants
5.1.3.7 Header
#include <safe_numerics/include/concepts/safe_numeric.hpp>
5.1.3.8 Models
safe<T>
safe_signed_range<-11, 11>
/Users/robertramey/…/doc/boostbook/proposal.xml page 9 of 13
safe_unsigned_range<0, 11>
safe_literal<4>
6 Types
6.1 safe<T>
6.1.1 Description
A safe<T> can be used anywhere a type T can be used. Any expression which
uses this type is guaranteed to return an arithmetically correct value or trap in
some way.
6.1.2 Notation
Symbol Description
6.1.4 Model of
Integer
SafeNumeric
6.1.6 Header
#include <safe>
/Users/robertramey/…/doc/boostbook/proposal.xml page 10 of 13
6.1.7 Example of use
#include <exception>
#include <iostream>
#include <safe>
void f(){
using namespace std;
safe<int> j;
try {
safe<int> i;
cin >> i; // could throw overflow !
j = i * i; // could throw overflow
}
catch(std::exception & e){
std::cout << e.what() << endl;
}
std::cout << j;
}
7 Acknowledgements
This proposal is a simplified version of Safe Numeics library proposed for Boost.
This effort was inspired by David LeBlanc's SafeInt Library .
8 References
Author Omer Katz
Abbrev Katz
/Users/robertramey/…/doc/boostbook/proposal.xml page 11 of 13
Title Integer Handling with the C++ SafeInt Class
Title SafeInt
Publishername CodePlex
Publishername Wikisource
/Users/robertramey/…/doc/boostbook/proposal.xml page 12 of 13
Title INT30-C. Ensure that operations on unsigned integers do
not wrap
/Users/robertramey/…/doc/boostbook/proposal.xml page 13 of 13