2013-07-12 18:22:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkTFitsIn_DEFINED
|
|
|
|
#define SkTFitsIn_DEFINED
|
|
|
|
|
2016-03-19 22:06:56 +00:00
|
|
|
#include "../private/SkTLogic.h"
|
2013-07-12 18:22:49 +00:00
|
|
|
#include <limits>
|
2016-01-05 22:59:40 +00:00
|
|
|
#include <type_traits>
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/**
|
|
|
|
* In C++ an unsigned to signed cast where the source value cannot be represented in the destination
|
|
|
|
* type results in an implementation defined destination value. Unlike C, C++ does not allow a trap.
|
|
|
|
* This makes "(S)(D)s == s" a possibly useful test. However, there are two cases where this is
|
|
|
|
* incorrect:
|
|
|
|
*
|
|
|
|
* when testing if a value of a smaller signed type can be represented in a larger unsigned type
|
|
|
|
* (int8_t)(uint16_t)-1 == -1 => (int8_t)0xFFFF == -1 => [implementation defined] == -1
|
|
|
|
*
|
|
|
|
* when testing if a value of a larger unsigned type can be represented in a smaller signed type
|
|
|
|
* (uint16_t)(int8_t)0xFFFF == 0xFFFF => (uint16_t)-1 == 0xFFFF => 0xFFFF == 0xFFFF => true.
|
|
|
|
*
|
|
|
|
* Consider the cases:
|
|
|
|
* u = unsigned, s = signed, X = more digits, x = less digits
|
|
|
|
* ux -> uX: (ux)(uX)ux == ux, trivially true
|
|
|
|
* uX -> ux: (uX)(ux)uX == uX, both casts well defined, test works
|
|
|
|
* sx -> sX: (sx)(sX)sx == sx, trivially true
|
|
|
|
* sX -> sx: (sX)(sx)sX == sX, first cast implementation value, second cast defined, test works
|
|
|
|
* sx -> uX: (sx)(uX)sx == sx, this is bad, the second cast results in implementation defined value
|
|
|
|
* sX -> ux: (sX)(ux)sX == sX, the second cast is required to prevent promotion of rhs to unsigned
|
|
|
|
* ux -> sX: (ux)(sX)ux == ux, trivially true
|
|
|
|
* uX -> sx: (uX)(sx)uX == uX, this is bad,
|
|
|
|
* first cast results in implementation defined value,
|
|
|
|
* second cast is defined. However, this creates false positives
|
|
|
|
* uint16_t x = 0xFFFF
|
|
|
|
* (uint16_t)(int8_t)x == x
|
|
|
|
* => (uint16_t)-1 == x
|
|
|
|
* => 0xFFFF == x
|
|
|
|
* => true
|
|
|
|
*
|
|
|
|
* So for the eight cases three are trivially true, three more are valid casts, and two are special.
|
|
|
|
* The two 'full' checks which otherwise require two comparisons are valid cast checks.
|
|
|
|
* The two remaining checks uX -> sx [uX < max(sx)] and sx -> uX [sx > 0] can be done with one op.
|
|
|
|
*/
|
|
|
|
|
2013-07-12 18:22:49 +00:00
|
|
|
namespace sktfitsin {
|
|
|
|
namespace Private {
|
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
/** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */
|
|
|
|
template <bool a, bool b, typename Both, typename A, typename B, typename Neither>
|
|
|
|
struct SkTMux {
|
|
|
|
using type = skstd::conditional_t<a, skstd::conditional_t<b, Both, A>,
|
|
|
|
skstd::conditional_t<b, B, Neither>>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
/** SkTHasMoreDigits = (digits(A) >= digits(B)) ? true_type : false_type. */
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename A, typename B> struct SkTHasMoreDigits
|
2015-08-28 14:09:20 +00:00
|
|
|
: skstd::bool_constant<std::numeric_limits<A>::digits >= std::numeric_limits<B>::digits>
|
|
|
|
{ };
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** Returns true.
|
|
|
|
* Used when it is statically known that source values are in the range of the Destination.
|
2013-07-12 18:22:49 +00:00
|
|
|
*/
|
2017-08-24 16:06:38 +00:00
|
|
|
template <typename S> struct SkTInRange_True {
|
2017-10-17 21:40:56 +00:00
|
|
|
static constexpr bool fits(S) {
|
2017-08-24 16:06:38 +00:00
|
|
|
return true;
|
2013-07-12 18:22:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** Tests that (S)(D)s == s.
|
|
|
|
* This is not valid for uX -> sx and sx -> uX conversions.
|
2013-07-12 18:22:49 +00:00
|
|
|
*/
|
2017-08-24 16:06:38 +00:00
|
|
|
template <typename D, typename S> struct SkTInRange_Cast {
|
2017-10-17 21:40:56 +00:00
|
|
|
static constexpr bool fits(S s) {
|
2017-08-24 16:06:38 +00:00
|
|
|
using S_is_bigger = SkTHasMoreDigits<S, D>;
|
|
|
|
using D_is_bigger = SkTHasMoreDigits<D, S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
using S_is_signed = skstd::bool_constant<std::numeric_limits<S>::is_signed>;
|
|
|
|
using D_is_signed = skstd::bool_constant<std::numeric_limits<D>::is_signed>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
using precondition = skstd::bool_constant<
|
|
|
|
!((!S_is_signed::value && D_is_signed::value && S_is_bigger::value) ||
|
|
|
|
( S_is_signed::value && !D_is_signed::value && D_is_bigger::value) )>;
|
|
|
|
static_assert(precondition::value, "not valid for uX -> sx and sx -> uX conversions");
|
|
|
|
|
|
|
|
return static_cast<S>(static_cast<D>(s)) == s;
|
2013-07-12 18:22:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** Tests if the source value <= Max(D).
|
2013-07-12 18:22:49 +00:00
|
|
|
* Assumes that Max(S) >= Max(D).
|
|
|
|
*/
|
2017-08-24 16:06:38 +00:00
|
|
|
template <typename D, typename S> struct SkTInRange_LE_MaxD {
|
2017-10-17 21:40:56 +00:00
|
|
|
static constexpr bool fits(S s) {
|
2016-03-19 22:06:56 +00:00
|
|
|
using precondition = SkTHasMoreDigits<S, D>;
|
|
|
|
static_assert(precondition::value, "maxS < maxD");
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
return s <= static_cast<S>((std::numeric_limits<D>::max)());
|
2013-07-12 18:22:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** Tests if the source value >= 0. */
|
|
|
|
template <typename D, typename S> struct SkTInRange_GE_Zero {
|
2017-10-17 21:40:56 +00:00
|
|
|
static constexpr bool fits(S s) {
|
2017-08-24 16:06:38 +00:00
|
|
|
return static_cast<S>(0) <= s;
|
2013-07-12 18:22:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** SkTFitsIn_Unsigned2Unsiged::type is an SkTInRange with an fits(S s) method
|
2013-07-12 18:22:49 +00:00
|
|
|
* the implementation of which is tailored for the source and destination types.
|
|
|
|
* Assumes that S and D are unsigned integer types.
|
|
|
|
*/
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename D, typename S> struct SkTFitsIn_Unsigned2Unsiged {
|
2017-08-24 16:06:38 +00:00
|
|
|
using CastCheck = SkTInRange_Cast<D, S>;
|
|
|
|
using NoCheck = SkTInRange_True<S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
|
|
|
// If std::numeric_limits<D>::digits >= std::numeric_limits<S>::digits, nothing to check.
|
2016-03-19 22:06:56 +00:00
|
|
|
using sourceFitsInDesitination = SkTHasMoreDigits<D, S>;
|
2017-08-24 16:06:38 +00:00
|
|
|
using type = skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, CastCheck>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** SkTFitsIn_Signed2Signed::type is an SkTInRange with an fits(S s) method
|
2013-07-12 18:22:49 +00:00
|
|
|
* the implementation of which is tailored for the source and destination types.
|
|
|
|
* Assumes that S and D are signed integer types.
|
|
|
|
*/
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename D, typename S> struct SkTFitsIn_Signed2Signed {
|
2017-08-24 16:06:38 +00:00
|
|
|
using CastCheck = SkTInRange_Cast<D, S>;
|
|
|
|
using NoCheck = SkTInRange_True<S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
|
|
|
// If std::numeric_limits<D>::digits >= std::numeric_limits<S>::digits, nothing to check.
|
2016-03-19 22:06:56 +00:00
|
|
|
using sourceFitsInDesitination = SkTHasMoreDigits<D, S>;
|
2017-08-24 16:06:38 +00:00
|
|
|
using type = skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, CastCheck>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** SkTFitsIn_Signed2Unsigned::type is an SkTInRange with an fits(S s) method
|
2013-07-12 18:22:49 +00:00
|
|
|
* the implementation of which is tailored for the source and destination types.
|
|
|
|
* Assumes that S is a signed integer type and D is an unsigned integer type.
|
|
|
|
*/
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename D, typename S> struct SkTFitsIn_Signed2Unsigned {
|
2017-08-24 16:06:38 +00:00
|
|
|
using CastCheck = SkTInRange_Cast<D, S>;
|
|
|
|
using LowSideOnlyCheck = SkTInRange_GE_Zero<D, S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
|
|
|
// If std::numeric_limits<D>::max() >= std::numeric_limits<S>::max(),
|
|
|
|
// no need to check the high side. (Until C++11, assume more digits means greater max.)
|
2017-08-24 16:06:38 +00:00
|
|
|
// This also protects the precondition of SkTInRange_Cast.
|
2016-03-19 22:06:56 +00:00
|
|
|
using sourceCannotExceedDest = SkTHasMoreDigits<D, S>;
|
2017-08-24 16:06:38 +00:00
|
|
|
using type = skstd::conditional_t<sourceCannotExceedDest::value, LowSideOnlyCheck, CastCheck>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** SkTFitsIn_Unsigned2Signed::type is an SkTInRange with an fits(S s) method
|
2013-07-12 18:22:49 +00:00
|
|
|
* the implementation of which is tailored for the source and destination types.
|
|
|
|
* Assumes that S is an usigned integer type and D is a signed integer type.
|
|
|
|
*/
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename D, typename S> struct SkTFitsIn_Unsigned2Signed {
|
2017-08-24 16:06:38 +00:00
|
|
|
using HighSideCheck = SkTInRange_LE_MaxD<D, S>;
|
|
|
|
using NoCheck = SkTInRange_True<S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
|
|
|
// If std::numeric_limits<D>::max() >= std::numeric_limits<S>::max(), nothing to check.
|
|
|
|
// (Until C++11, assume more digits means greater max.)
|
2016-03-19 22:06:56 +00:00
|
|
|
using sourceCannotExceedDest = SkTHasMoreDigits<D, S>;
|
2017-08-24 16:06:38 +00:00
|
|
|
using type = skstd::conditional_t<sourceCannotExceedDest::value, NoCheck, HighSideCheck>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 16:06:38 +00:00
|
|
|
/** SkTFitsIn::type is an SkTInRange with an fits(S s) method
|
2013-07-12 18:22:49 +00:00
|
|
|
* the implementation of which is tailored for the source and destination types.
|
|
|
|
* Assumes that S and D are integer types.
|
|
|
|
*/
|
2016-03-19 22:06:56 +00:00
|
|
|
template <typename D, typename S> struct SkTFitsIn {
|
2013-07-12 18:22:49 +00:00
|
|
|
// One of the following will be the 'selector' type.
|
2016-03-19 22:06:56 +00:00
|
|
|
using S2S = SkTFitsIn_Signed2Signed<D, S>;
|
|
|
|
using S2U = SkTFitsIn_Signed2Unsigned<D, S>;
|
|
|
|
using U2S = SkTFitsIn_Unsigned2Signed<D, S>;
|
|
|
|
using U2U = SkTFitsIn_Unsigned2Unsiged<D, S>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2016-03-19 22:06:56 +00:00
|
|
|
using S_is_signed = skstd::bool_constant<std::numeric_limits<S>::is_signed>;
|
|
|
|
using D_is_signed = skstd::bool_constant<std::numeric_limits<D>::is_signed>;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2016-03-19 22:06:56 +00:00
|
|
|
using selector = typename SkTMux<S_is_signed::value, D_is_signed::value,
|
|
|
|
S2S, S2U, U2S, U2U>::type;
|
2017-08-24 16:06:38 +00:00
|
|
|
// This type is an SkTInRange.
|
2016-03-19 22:06:56 +00:00
|
|
|
using type = typename selector::type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, bool = std::is_enum<T>::value> struct underlying_type {
|
|
|
|
using type = skstd::underlying_type_t<T>;
|
|
|
|
};
|
|
|
|
template <typename T> struct underlying_type<T, false> {
|
|
|
|
using type = T;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Private
|
|
|
|
} // namespace sktfitsin
|
|
|
|
|
|
|
|
/** Returns true if the integer source value 's' will fit in the integer destination type 'D'. */
|
2017-10-17 21:40:56 +00:00
|
|
|
template <typename D, typename S> constexpr inline bool SkTFitsIn(S s) {
|
2016-03-19 22:06:56 +00:00
|
|
|
static_assert(std::is_integral<S>::value || std::is_enum<S>::value, "S must be integral.");
|
|
|
|
static_assert(std::is_integral<D>::value || std::is_enum<D>::value, "D must be integral.");
|
|
|
|
|
|
|
|
using RealS = typename sktfitsin::Private::underlying_type<S>::type;
|
|
|
|
using RealD = typename sktfitsin::Private::underlying_type<D>::type;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2018-04-17 15:16:32 +00:00
|
|
|
return sktfitsin::Private::SkTFitsIn<RealD, RealS>::type::fits(static_cast<RealS>(s));
|
2013-07-12 18:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|