SkTFitsIn.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef SkTFitsIn_DEFINED
  8. #define SkTFitsIn_DEFINED
  9. #include <limits>
  10. #include <type_traits>
  11. /**
  12. * In C++ an unsigned to signed cast where the source value cannot be represented in the destination
  13. * type results in an implementation defined destination value. Unlike C, C++ does not allow a trap.
  14. * This makes "(S)(D)s == s" a possibly useful test. However, there are two cases where this is
  15. * incorrect:
  16. *
  17. * when testing if a value of a smaller signed type can be represented in a larger unsigned type
  18. * (int8_t)(uint16_t)-1 == -1 => (int8_t)0xFFFF == -1 => [implementation defined] == -1
  19. *
  20. * when testing if a value of a larger unsigned type can be represented in a smaller signed type
  21. * (uint16_t)(int8_t)0xFFFF == 0xFFFF => (uint16_t)-1 == 0xFFFF => 0xFFFF == 0xFFFF => true.
  22. *
  23. * Consider the cases:
  24. * u = unsigned, less digits
  25. * U = unsigned, more digits
  26. * s = signed, less digits
  27. * S = signed, more digits
  28. * v is the value we're considering.
  29. *
  30. * u -> U: (u)(U)v == v, trivially true
  31. * U -> u: (U)(u)v == v, both casts well defined, test works
  32. * s -> S: (s)(S)v == v, trivially true
  33. * S -> s: (S)(s)v == v, first cast implementation value, second cast defined, test works
  34. * s -> U: (s)(U)v == v, *this is bad*, the second cast results in implementation defined value
  35. * S -> u: (S)(u)v == v, the second cast is required to prevent promotion of rhs to unsigned
  36. * u -> S: (u)(S)v == v, trivially true
  37. * U -> s: (U)(s)v == v, *this is bad*,
  38. * first cast results in implementation defined value,
  39. * second cast is defined. However, this creates false positives
  40. * uint16_t x = 0xFFFF
  41. * (uint16_t)(int8_t)x == x
  42. * => (uint16_t)-1 == x
  43. * => 0xFFFF == x
  44. * => true
  45. *
  46. * So for the eight cases three are trivially true, three more are valid casts, and two are special.
  47. * The two 'full' checks which otherwise require two comparisons are valid cast checks.
  48. * The two remaining checks s -> U [v >= 0] and U -> s [v <= max(s)] can be done with one op.
  49. */
  50. template <typename D, typename S>
  51. static constexpr inline
  52. typename std::enable_if<(std::is_integral<S>::value || std::is_enum<S>::value) &&
  53. (std::is_integral<D>::value || std::is_enum<D>::value), bool>::type
  54. /*bool*/ SkTFitsIn(S src) {
  55. // SkTFitsIn() is used in public headers, so needs to be written targeting at most C++11.
  56. return
  57. // E.g. (int8_t)(uint8_t) int8_t(-1) == -1, but the uint8_t == 255, not -1.
  58. (std::is_signed<S>::value && std::is_unsigned<D>::value && sizeof(S) <= sizeof(D)) ?
  59. (S)0 <= src :
  60. // E.g. (uint8_t)(int8_t) uint8_t(255) == 255, but the int8_t == -1.
  61. (std::is_signed<D>::value && std::is_unsigned<S>::value && sizeof(D) <= sizeof(S)) ?
  62. src <= (S)std::numeric_limits<D>::max() :
  63. // else
  64. (S)(D)src == src;
  65. }
  66. #endif