Commit 66e55526 authored by Albert J. Wong's avatar Albert J. Wong Committed by Commit Bot

Small wording clarifications to safe_numerics.h readme.md.

Bug: none.
Change-Id: I28b7355d1a7d5cde785fbab40250ae2040c2ff65
Reviewed-on: https://chromium-review.googlesource.com/1212144Reviewed-by: default avatarJustin Schuh <jschuh@chromium.org>
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590351}
parent ce4648fe
......@@ -11,8 +11,8 @@ The public API is broken out into the following header files:
functions for performing arithmetic and conversion operations that detect
errors and boundary conditions (e.g. overflow, truncation, etc.).
* `clamped_math.h` contains the `ClampedNumeric` template class and
helper functions for performing fast, clamped (i.e. non-sticky saturating)
arithmetic operations and conversions.
helper functions for performing fast, clamped (i.e. [non-sticky](#notsticky)
saturating) arithmetic operations and conversions.
* `safe_conversions.h` contains the `StrictNumeric` template class and
a collection of custom casting templates and helper functions for safely
converting between a range of numeric types.
......@@ -34,7 +34,7 @@ The conversion priority for `Numeric` type coercions is:
The following covers the preferred style for the most common uses of this
library. Please don't cargo-cult from anywhere else. 😉
### Performing checked arithmetic conversions
### Performing checked arithmetic type conversions
The `checked_cast` template converts between arbitrary arithmetic types, and is
used for cases where a conversion failure should result in program termination:
......@@ -44,7 +44,7 @@ used for cases where a conversion failure should result in program termination:
size_t buff_size = checked_cast<size_t>(signed_value);
```
### Performing saturated (clamped) arithmetic conversions
### Performing saturated (clamped) arithmetic type conversions
The `saturated_cast` template converts between arbitrary arithmetic types, and
is used in cases where an out-of-bounds source value should be saturated to the
......@@ -55,12 +55,11 @@ corresponding maximum or minimum of the destination type:
int int_value = saturated_cast<int>(floating_point_value);
```
### Enforcing arithmetic conversions at compile-time
### Enforcing arithmetic type conversions at compile-time
The `strict_cast` enforces type restrictions at compile-time and results in
emitted code that is identical to a normal `static_cast`. However, a
`strict_cast` assignment will fail to compile if the destination type cannot
represent the full range of the source type:
The `strict_cast` emits code that is identical to `static_cast`. However,
provides static checks that will cause a compilation failure if the
destination type cannot represent the full range of the source type:
```cpp
// Throw a compiler error if byte_value is changed to an out-of-range-type.
......@@ -139,6 +138,7 @@ void Rect::Inset(int left, int top, int right, int bottom) {
```
*** note
<a name="notsticky"></a>
The `ClampedNumeric` type is not "sticky", which means the saturation is not
retained across individual operations. As such, one arithmetic operation may
result in a saturated value, while the next operation may then "desaturate"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment