Add spvtools::opt::Operand::AsLiteralUint64 (#3320)

This commit is contained in:
David Neto 2020-04-27 06:38:06 -07:00 committed by GitHub
parent 94d6002dc5
commit eed48ae479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -92,6 +92,19 @@ struct Operand {
// Returns a string operand as a std::string.
std::string AsString() const { return AsCString(); }
// Returns a literal integer operand as a uint64_t
uint64_t AsLiteralUint64() const {
assert(type == SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER);
assert(1 <= words.size());
assert(words.size() <= 2);
// Load the low word.
uint64_t result = uint64_t(words[0]);
if (words.size() > 1) {
result = result | (uint64_t(words[1]) << 32);
}
return result;
}
friend bool operator==(const Operand& o1, const Operand& o2) {
return o1.type == o2.type && o1.words == o2.words;
}

View File

@ -74,6 +74,18 @@ TEST(InstructionTest, OperandAsString) {
EXPECT_EQ("abcde", operand.AsString());
}
TEST(InstructionTest, OperandAsLiteralUint64_32bits) {
Operand::OperandData words{0x1234};
Operand operand(SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, std::move(words));
EXPECT_EQ(uint64_t(0x1234), operand.AsLiteralUint64());
}
TEST(InstructionTest, OperandAsLiteralUint64_64bits) {
Operand::OperandData words{0x1234, 0x89ab};
Operand operand(SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, std::move(words));
EXPECT_EQ((uint64_t(0x89ab) << 32 | 0x1234), operand.AsLiteralUint64());
}
// The words for an OpTypeInt for 32-bit signed integer resulting in Id 44.
uint32_t kSampleInstructionWords[] = {(4 << 16) | uint32_t(SpvOpTypeInt), 44,
32, 1};