bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkTFitsIn_DEFINED |
| 9 | #define SkTFitsIn_DEFINED |
| 10 | |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 11 | #include <limits> |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 12 | #include <stdint.h> |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 13 | #include <type_traits> |
| 14 | |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 15 | /** |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 16 | * std::underlying_type is only defined for enums. For integral types, we just want the type. |
| 17 | */ |
| 18 | template <typename T, class Enable = void> |
| 19 | struct sk_strip_enum { |
| 20 | typedef T type; |
| 21 | }; |
| 22 | |
| 23 | template <typename T> |
| 24 | struct sk_strip_enum<T, typename std::enable_if<std::is_enum<T>::value>::type> { |
| 25 | typedef typename std::underlying_type<T>::type type; |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | /** |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 30 | * In C++ an unsigned to signed cast where the source value cannot be represented in the destination |
| 31 | * type results in an implementation defined destination value. Unlike C, C++ does not allow a trap. |
| 32 | * This makes "(S)(D)s == s" a possibly useful test. However, there are two cases where this is |
| 33 | * incorrect: |
| 34 | * |
| 35 | * when testing if a value of a smaller signed type can be represented in a larger unsigned type |
| 36 | * (int8_t)(uint16_t)-1 == -1 => (int8_t)0xFFFF == -1 => [implementation defined] == -1 |
| 37 | * |
| 38 | * when testing if a value of a larger unsigned type can be represented in a smaller signed type |
| 39 | * (uint16_t)(int8_t)0xFFFF == 0xFFFF => (uint16_t)-1 == 0xFFFF => 0xFFFF == 0xFFFF => true. |
| 40 | * |
| 41 | * Consider the cases: |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 42 | * u = unsigned, less digits |
| 43 | * U = unsigned, more digits |
| 44 | * s = signed, less digits |
| 45 | * S = signed, more digits |
| 46 | * v is the value we're considering. |
| 47 | * |
| 48 | * u -> U: (u)(U)v == v, trivially true |
| 49 | * U -> u: (U)(u)v == v, both casts well defined, test works |
| 50 | * s -> S: (s)(S)v == v, trivially true |
| 51 | * S -> s: (S)(s)v == v, first cast implementation value, second cast defined, test works |
| 52 | * s -> U: (s)(U)v == v, *this is bad*, the second cast results in implementation defined value |
| 53 | * S -> u: (S)(u)v == v, the second cast is required to prevent promotion of rhs to unsigned |
| 54 | * u -> S: (u)(S)v == v, trivially true |
| 55 | * U -> s: (U)(s)v == v, *this is bad*, |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 56 | * first cast results in implementation defined value, |
| 57 | * second cast is defined. However, this creates false positives |
| 58 | * uint16_t x = 0xFFFF |
| 59 | * (uint16_t)(int8_t)x == x |
| 60 | * => (uint16_t)-1 == x |
| 61 | * => 0xFFFF == x |
| 62 | * => true |
| 63 | * |
| 64 | * So for the eight cases three are trivially true, three more are valid casts, and two are special. |
| 65 | * The two 'full' checks which otherwise require two comparisons are valid cast checks. |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 66 | * The two remaining checks s -> U [v >= 0] and U -> s [v <= max(s)] can be done with one op. |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 67 | */ |
| 68 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 69 | template <typename D, typename S> |
Mike Klein | 4d46654 | 2018-06-13 11:41:38 -0400 | [diff] [blame] | 70 | static constexpr inline |
| 71 | typename std::enable_if<(std::is_integral<S>::value || std::is_enum<S>::value) && |
| 72 | (std::is_integral<D>::value || std::is_enum<D>::value), bool>::type |
| 73 | /*bool*/ SkTFitsIn(S src) { |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 74 | // SkTFitsIn() is used in public headers, so needs to be written targeting at most C++11. |
Mike Klein | 4d46654 | 2018-06-13 11:41:38 -0400 | [diff] [blame] | 75 | return |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 76 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 77 | // E.g. (int8_t)(uint8_t) int8_t(-1) == -1, but the uint8_t == 255, not -1. |
| 78 | (std::is_signed<S>::value && std::is_unsigned<D>::value && sizeof(S) <= sizeof(D)) ? |
| 79 | (S)0 <= src : |
Mike Klein | 2652223 | 2018-06-12 21:07:48 +0000 | [diff] [blame] | 80 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 81 | // E.g. (uint8_t)(int8_t) uint8_t(255) == 255, but the int8_t == -1. |
| 82 | (std::is_signed<D>::value && std::is_unsigned<S>::value && sizeof(D) <= sizeof(S)) ? |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 83 | src <= (S)std::numeric_limits<typename sk_strip_enum<D>::type>::max() : |
Mike Klein | 2652223 | 2018-06-12 21:07:48 +0000 | [diff] [blame] | 84 | |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 85 | #if !defined(SK_DEBUG) && !defined(__MSVC_RUNTIME_CHECKS ) |
| 86 | // Correct (simple) version. This trips up MSVC's /RTCc run-time checking. |
| 87 | (S)(D)src == src; |
| 88 | #else |
| 89 | // More complex version that's safe with /RTCc. Used in all debug builds, for coverage. |
| 90 | (std::is_signed<S>::value) ? |
| 91 | (intmax_t)src >= (intmax_t)std::numeric_limits<typename sk_strip_enum<D>::type>::min() && |
| 92 | (intmax_t)src <= (intmax_t)std::numeric_limits<typename sk_strip_enum<D>::type>::max() : |
| 93 | |
| 94 | // std::is_unsigned<S> ? |
| 95 | (uintmax_t)src <= (uintmax_t)std::numeric_limits<typename sk_strip_enum<D>::type>::max(); |
| 96 | #endif |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | #endif |