constants_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/constants.h"
  15. #include <gtest/gtest-param-test.h>
  16. #include "gtest/gtest.h"
  17. #include "source/opt/types.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace analysis {
  21. namespace {
  22. using ConstantTest = ::testing::Test;
  23. using ::testing::ValuesIn;
  24. template <typename T>
  25. struct GetExtendedValueCase {
  26. bool is_signed;
  27. int width;
  28. std::vector<uint32_t> words;
  29. T expected_value;
  30. };
  31. using GetSignExtendedValueCase = GetExtendedValueCase<int64_t>;
  32. using GetZeroExtendedValueCase = GetExtendedValueCase<uint64_t>;
  33. using GetSignExtendedValueTest =
  34. ::testing::TestWithParam<GetSignExtendedValueCase>;
  35. using GetZeroExtendedValueTest =
  36. ::testing::TestWithParam<GetZeroExtendedValueCase>;
  37. TEST_P(GetSignExtendedValueTest, Case) {
  38. Integer type(GetParam().width, GetParam().is_signed);
  39. IntConstant value(&type, GetParam().words);
  40. EXPECT_EQ(GetParam().expected_value, value.GetSignExtendedValue());
  41. }
  42. TEST_P(GetZeroExtendedValueTest, Case) {
  43. Integer type(GetParam().width, GetParam().is_signed);
  44. IntConstant value(&type, GetParam().words);
  45. EXPECT_EQ(GetParam().expected_value, value.GetZeroExtendedValue());
  46. }
  47. const uint32_t k32ones = ~uint32_t(0);
  48. const uint64_t k64ones = ~uint64_t(0);
  49. const int64_t kSBillion = 1000 * 1000 * 1000;
  50. const uint64_t kUBillion = 1000 * 1000 * 1000;
  51. INSTANTIATE_TEST_SUITE_P(AtMost32Bits, GetSignExtendedValueTest,
  52. ValuesIn(std::vector<GetSignExtendedValueCase>{
  53. // 4 bits
  54. {false, 4, {0}, 0},
  55. {false, 4, {7}, 7},
  56. {false, 4, {15}, 15},
  57. {true, 4, {0}, 0},
  58. {true, 4, {7}, 7},
  59. {true, 4, {0xfffffff8}, -8},
  60. {true, 4, {k32ones}, -1},
  61. // 16 bits
  62. {false, 16, {0}, 0},
  63. {false, 16, {32767}, 32767},
  64. {false, 16, {32768}, 32768},
  65. {false, 16, {65000}, 65000},
  66. {true, 16, {0}, 0},
  67. {true, 16, {32767}, 32767},
  68. {true, 16, {0xfffffff8}, -8},
  69. {true, 16, {k32ones}, -1},
  70. // 32 bits
  71. {false, 32, {0}, 0},
  72. {false, 32, {1000000}, 1000000},
  73. {true, 32, {0xfffffff8}, -8},
  74. {true, 32, {k32ones}, -1},
  75. }));
  76. INSTANTIATE_TEST_SUITE_P(AtMost64Bits, GetSignExtendedValueTest,
  77. ValuesIn(std::vector<GetSignExtendedValueCase>{
  78. // 48 bits
  79. {false, 48, {0, 0}, 0},
  80. {false, 48, {5, 0}, 5},
  81. {false, 48, {0xfffffff8, k32ones}, -8},
  82. {false, 48, {k32ones, k32ones}, -1},
  83. {false, 48, {0xdcd65000, 1}, 8 * kSBillion},
  84. {true, 48, {0xfffffff8, k32ones}, -8},
  85. {true, 48, {k32ones, k32ones}, -1},
  86. {true, 48, {0xdcd65000, 1}, 8 * kSBillion},
  87. // 64 bits
  88. {false, 64, {12, 0}, 12},
  89. {false, 64, {0xdcd65000, 1}, 8 * kSBillion},
  90. {false, 48, {0xfffffff8, k32ones}, -8},
  91. {false, 64, {k32ones, k32ones}, -1},
  92. {true, 64, {12, 0}, 12},
  93. {true, 64, {0xdcd65000, 1}, 8 * kSBillion},
  94. {true, 48, {0xfffffff8, k32ones}, -8},
  95. {true, 64, {k32ones, k32ones}, -1},
  96. }));
  97. INSTANTIATE_TEST_SUITE_P(AtMost32Bits, GetZeroExtendedValueTest,
  98. ValuesIn(std::vector<GetZeroExtendedValueCase>{
  99. // 4 bits
  100. {false, 4, {0}, 0},
  101. {false, 4, {7}, 7},
  102. {false, 4, {15}, 15},
  103. {true, 4, {0}, 0},
  104. {true, 4, {7}, 7},
  105. {true, 4, {0xfffffff8}, 0xfffffff8},
  106. {true, 4, {k32ones}, k32ones},
  107. // 16 bits
  108. {false, 16, {0}, 0},
  109. {false, 16, {32767}, 32767},
  110. {false, 16, {32768}, 32768},
  111. {false, 16, {65000}, 65000},
  112. {true, 16, {0}, 0},
  113. {true, 16, {32767}, 32767},
  114. {true, 16, {0xfffffff8}, 0xfffffff8},
  115. {true, 16, {k32ones}, k32ones},
  116. // 32 bits
  117. {false, 32, {0}, 0},
  118. {false, 32, {1000000}, 1000000},
  119. {true, 32, {0xfffffff8}, 0xfffffff8},
  120. {true, 32, {k32ones}, k32ones},
  121. }));
  122. INSTANTIATE_TEST_SUITE_P(AtMost64Bits, GetZeroExtendedValueTest,
  123. ValuesIn(std::vector<GetZeroExtendedValueCase>{
  124. // 48 bits
  125. {false, 48, {0, 0}, 0},
  126. {false, 48, {5, 0}, 5},
  127. {false, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
  128. {false, 48, {k32ones, k32ones}, uint64_t(-1)},
  129. {false, 48, {0xdcd65000, 1}, 8 * kUBillion},
  130. {true, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
  131. {true, 48, {k32ones, k32ones}, uint64_t(-1)},
  132. {true, 48, {0xdcd65000, 1}, 8 * kUBillion},
  133. // 64 bits
  134. {false, 64, {12, 0}, 12},
  135. {false, 64, {0xdcd65000, 1}, 8 * kUBillion},
  136. {false, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
  137. {false, 64, {k32ones, k32ones}, k64ones},
  138. {true, 64, {12, 0}, 12},
  139. {true, 64, {0xdcd65000, 1}, 8 * kUBillion},
  140. {true, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
  141. {true, 64, {k32ones, k32ones}, k64ones},
  142. }));
  143. } // namespace
  144. } // namespace analysis
  145. } // namespace opt
  146. } // namespace spvtools