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> |
| 12 | #include <type_traits> |
| 13 | |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 14 | /** |
| 15 | * In C++ an unsigned to signed cast where the source value cannot be represented in the destination |
| 16 | * type results in an implementation defined destination value. Unlike C, C++ does not allow a trap. |
| 17 | * This makes "(S)(D)s == s" a possibly useful test. However, there are two cases where this is |
| 18 | * incorrect: |
| 19 | * |
| 20 | * when testing if a value of a smaller signed type can be represented in a larger unsigned type |
| 21 | * (int8_t)(uint16_t)-1 == -1 => (int8_t)0xFFFF == -1 => [implementation defined] == -1 |
| 22 | * |
| 23 | * when testing if a value of a larger unsigned type can be represented in a smaller signed type |
| 24 | * (uint16_t)(int8_t)0xFFFF == 0xFFFF => (uint16_t)-1 == 0xFFFF => 0xFFFF == 0xFFFF => true. |
| 25 | * |
| 26 | * Consider the cases: |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 27 | * u = unsigned, less digits |
| 28 | * U = unsigned, more digits |
| 29 | * s = signed, less digits |
| 30 | * S = signed, more digits |
| 31 | * v is the value we're considering. |
| 32 | * |
| 33 | * u -> U: (u)(U)v == v, trivially true |
| 34 | * U -> u: (U)(u)v == v, both casts well defined, test works |
| 35 | * s -> S: (s)(S)v == v, trivially true |
| 36 | * S -> s: (S)(s)v == v, first cast implementation value, second cast defined, test works |
| 37 | * s -> U: (s)(U)v == v, *this is bad*, the second cast results in implementation defined value |
| 38 | * S -> u: (S)(u)v == v, the second cast is required to prevent promotion of rhs to unsigned |
| 39 | * u -> S: (u)(S)v == v, trivially true |
| 40 | * U -> s: (U)(s)v == v, *this is bad*, |
Ben Wagner | 59c7a6e | 2017-08-24 12:06:38 -0400 | [diff] [blame] | 41 | * first cast results in implementation defined value, |
| 42 | * second cast is defined. However, this creates false positives |
| 43 | * uint16_t x = 0xFFFF |
| 44 | * (uint16_t)(int8_t)x == x |
| 45 | * => (uint16_t)-1 == x |
| 46 | * => 0xFFFF == x |
| 47 | * => true |
| 48 | * |
| 49 | * So for the eight cases three are trivially true, three more are valid casts, and two are special. |
| 50 | * 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] | 51 | * 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] | 52 | */ |
| 53 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 54 | template <typename D, typename S> |
Mike Klein | 4d46654 | 2018-06-13 11:41:38 -0400 | [diff] [blame] | 55 | static constexpr inline |
| 56 | typename std::enable_if<(std::is_integral<S>::value || std::is_enum<S>::value) && |
| 57 | (std::is_integral<D>::value || std::is_enum<D>::value), bool>::type |
| 58 | /*bool*/ SkTFitsIn(S src) { |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 59 | // 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] | 60 | return |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 61 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 62 | // E.g. (int8_t)(uint8_t) int8_t(-1) == -1, but the uint8_t == 255, not -1. |
| 63 | (std::is_signed<S>::value && std::is_unsigned<D>::value && sizeof(S) <= sizeof(D)) ? |
| 64 | (S)0 <= src : |
Mike Klein | 2652223 | 2018-06-12 21:07:48 +0000 | [diff] [blame] | 65 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 66 | // E.g. (uint8_t)(int8_t) uint8_t(255) == 255, but the int8_t == -1. |
| 67 | (std::is_signed<D>::value && std::is_unsigned<S>::value && sizeof(D) <= sizeof(S)) ? |
| 68 | src <= (S)std::numeric_limits<D>::max() : |
Mike Klein | 2652223 | 2018-06-12 21:07:48 +0000 | [diff] [blame] | 69 | |
Mike Klein | b6b5b7a | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 70 | // else |
| 71 | (S)(D)src == src; |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | #endif |