template <typename Dst, typename Src, template <typename> class Bounds>

struct NarrowingRange

Defined at line 241 of file ../../third_party/mini_chromium/src/base/numerics/safe_conversions_impl.h

The following helper template addresses a corner case in range checks for

conversion from a floating-point type to an integral type of smaller range

but larger precision (e.g. float -> unsigned). The problem is as follows:

1. Integral maximum is always one less than a power of two, so it must be

truncated to fit the mantissa of the floating point. The direction of

rounding is implementation defined, but by default it's always IEEE

floats, which round to nearest and thus result in a value of larger

magnitude than the integral value.

Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX

// is 4294967295u.

2. If the floating point value is equal to the promoted integral maximum

value, a range check will erroneously pass.

Example: (4294967296f

<

= 4294967295u) // This is true due to a precision

// loss in rounding up to float.

3. When the floating point value is then converted to an integral, the

resulting value is out of range for the target integral type and

thus is implementation defined.

Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0.

To fix this bug we manually truncate the maximum value when the destination

type is an integral of larger precision than the source floating-point type,

such that the resulting maximum is represented exactly as a floating point.

Public Members

static const int kShift

Public Methods

template <typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
T Adjust (T value)

Masks out the integer bits that are beyond the precision of the

intermediate type used for comparison.

Defined at line 257 of file ../../third_party/mini_chromium/src/base/numerics/safe_conversions_impl.h

template <typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* =
                                    nullptr>
T Adjust (T value)

Defined at line 268 of file ../../third_party/mini_chromium/src/base/numerics/safe_conversions_impl.h

Dst max ()

Defined at line 274 of file ../../third_party/mini_chromium/src/base/numerics/safe_conversions_impl.h

Dst lowest ()

Defined at line 275 of file ../../third_party/mini_chromium/src/base/numerics/safe_conversions_impl.h