IntConstantBounds.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class IntConstantBounds
  7. {
  8. private:
  9. int32 lowerBound;
  10. int32 upperBound;
  11. public:
  12. IntConstantBounds() : lowerBound(0), upperBound(0)
  13. {
  14. }
  15. IntConstantBounds(const int32 lowerBound, const int32 upperBound)
  16. : lowerBound(lowerBound), upperBound(upperBound)
  17. {
  18. Assert(lowerBound <= upperBound);
  19. }
  20. public:
  21. int32 LowerBound() const
  22. {
  23. return lowerBound;
  24. }
  25. int32 UpperBound() const
  26. {
  27. return upperBound;
  28. }
  29. bool IsConstant() const
  30. {
  31. return lowerBound == upperBound;
  32. }
  33. bool IsTaggable() const
  34. {
  35. #if INT32VAR
  36. // All 32-bit ints are taggable on 64-bit architectures
  37. return true;
  38. #else
  39. return lowerBound >= Js::Constants::Int31MinValue && upperBound <= Js::Constants::Int31MaxValue;
  40. #endif
  41. }
  42. bool IsLikelyTaggable() const
  43. {
  44. #if INT32VAR
  45. // All 32-bit ints are taggable on 64-bit architectures
  46. return true;
  47. #else
  48. return lowerBound <= Js::Constants::Int31MaxValue && upperBound >= Js::Constants::Int31MinValue;
  49. #endif
  50. }
  51. ValueType GetValueType() const
  52. {
  53. return ValueType::GetInt(IsLikelyTaggable());
  54. }
  55. IntConstantBounds And_0x1f() const
  56. {
  57. const int32 mask = 0x1f;
  58. if(static_cast<UIntConstType>(upperBound - lowerBound) >= static_cast<UIntConstType>(mask) ||
  59. (lowerBound & mask) > (upperBound & mask))
  60. {
  61. // The range contains all items in the set {0-mask}, or the range crosses a boundary of {0-mask}. Since we cannot
  62. // represent ranges like {0-5,8-mask}, just use {0-mask}.
  63. return IntConstantBounds(0, mask);
  64. }
  65. return IntConstantBounds(lowerBound & mask, upperBound & mask);
  66. }
  67. bool Contains(const int32 value) const
  68. {
  69. return lowerBound <= value && value <= upperBound;
  70. }
  71. bool operator ==(const IntConstantBounds &other) const
  72. {
  73. return lowerBound == other.lowerBound && upperBound == other.upperBound;
  74. }
  75. };