xref: /aosp_15_r20/external/skia/include/private/base/SkTFitsIn.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
11 #include "include/private/base/SkDebug.h"
12 
13 #include <limits>
14 #include <type_traits>
15 
16 /**
17  * std::underlying_type is only defined for enums. For integral types, we just want the type.
18  */
19 template <typename T, class Enable = void>
20 struct sk_strip_enum {
21     typedef T type;
22 };
23 
24 template <typename T>
25 struct sk_strip_enum<T, typename std::enable_if<std::is_enum<T>::value>::type> {
26     typedef typename std::underlying_type<T>::type type;
27 };
28 
29 
30 /**
31  * In C++ an unsigned to signed cast where the source value cannot be represented in the destination
32  * type results in an implementation defined destination value. Unlike C, C++ does not allow a trap.
33  * This makes "(S)(D)s == s" a possibly useful test. However, there are two cases where this is
34  * incorrect:
35  *
36  * when testing if a value of a smaller signed type can be represented in a larger unsigned type
37  * (int8_t)(uint16_t)-1 == -1 => (int8_t)0xFFFF == -1 => [implementation defined] == -1
38  *
39  * when testing if a value of a larger unsigned type can be represented in a smaller signed type
40  * (uint16_t)(int8_t)0xFFFF == 0xFFFF => (uint16_t)-1 == 0xFFFF => 0xFFFF == 0xFFFF => true.
41  *
42  * Consider the cases:
43  *   u = unsigned, less digits
44  *   U = unsigned, more digits
45  *   s = signed, less digits
46  *   S = signed, more digits
47  *   v is the value we're considering.
48  *
49  * u -> U: (u)(U)v == v, trivially true
50  * U -> u: (U)(u)v == v, both casts well defined, test works
51  * s -> S: (s)(S)v == v, trivially true
52  * S -> s: (S)(s)v == v, first cast implementation value, second cast defined, test works
53  * s -> U: (s)(U)v == v, *this is bad*, the second cast results in implementation defined value
54  * S -> u: (S)(u)v == v, the second cast is required to prevent promotion of rhs to unsigned
55  * u -> S: (u)(S)v == v, trivially true
56  * U -> s: (U)(s)v == v, *this is bad*,
57  *                             first cast results in implementation defined value,
58  *                             second cast is defined. However, this creates false positives
59  *                             uint16_t x = 0xFFFF
60  *                                (uint16_t)(int8_t)x == x
61  *                             => (uint16_t)-1        == x
62  *                             => 0xFFFF              == x
63  *                             => true
64  *
65  * So for the eight cases three are trivially true, three more are valid casts, and two are special.
66  * The two 'full' checks which otherwise require two comparisons are valid cast checks.
67  * The two remaining checks s -> U [v >= 0] and U -> s [v <= max(s)] can be done with one op.
68  */
69 
70 template <typename D, typename S>
71 static constexpr inline
72 typename std::enable_if<(std::is_integral<S>::value || std::is_enum<S>::value) &&
73                         (std::is_integral<D>::value || std::is_enum<D>::value), bool>::type
74 /*bool*/ SkTFitsIn(S src) {
75     // Ensure that is_signed and is_unsigned are passed the arithmetic underlyng types of enums.
76     using Sa = typename sk_strip_enum<S>::type;
77     using Da = typename sk_strip_enum<D>::type;
78 
79     // SkTFitsIn() is used in public headers, so needs to be written targeting at most C++11.
80     return
81 
82     // E.g. (int8_t)(uint8_t) int8_t(-1) == -1, but the uint8_t == 255, not -1.
83     (std::is_signed<Sa>::value && std::is_unsigned<Da>::value && sizeof(Sa) <= sizeof(Da)) ?
84         (S)0 <= src :
85 
86     // E.g. (uint8_t)(int8_t) uint8_t(255) == 255, but the int8_t == -1.
87     (std::is_signed<Da>::value && std::is_unsigned<Sa>::value && sizeof(Da) <= sizeof(Sa)) ?
88         src <= (S)std::numeric_limits<Da>::max() :
89 
90     // This trips up MSVC's /RTCc run-time checking, which we don't support.
91     (S)(D)src == src;
92 }
93 
94 #endif
95