1 // Copyright (c) 2016 Google Inc. 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 15 // This file contains utility functions for spv_parsed_operand_t. 16 17 #include "parsed_operand.h" 18 19 #include <cassert> 20 #include "util/hex_float.h" 21 22 namespace libspirv { 23 EmitNumericLiteral(std::ostream * out,const spv_parsed_instruction_t & inst,const spv_parsed_operand_t & operand)24void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst, 25 const spv_parsed_operand_t& operand) { 26 assert(operand.type == SPV_OPERAND_TYPE_LITERAL_INTEGER || 27 operand.type == SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER); 28 assert(1 <= operand.num_words); 29 assert(operand.num_words <= 2); 30 31 const uint32_t word = inst.words[operand.offset]; 32 if (operand.num_words == 1) { 33 switch (operand.number_kind) { 34 case SPV_NUMBER_SIGNED_INT: 35 *out << int32_t(word); 36 break; 37 case SPV_NUMBER_UNSIGNED_INT: 38 *out << word; 39 break; 40 case SPV_NUMBER_FLOATING: 41 if (operand.number_bit_width == 16) { 42 *out << spvutils::FloatProxy<spvutils::Float16>( 43 uint16_t(word & 0xFFFF)); 44 } else { 45 // Assume 32-bit floats. 46 *out << spvutils::FloatProxy<float>(word); 47 } 48 break; 49 default: 50 assert(false && "Unreachable"); 51 } 52 } else if (operand.num_words == 2) { 53 // Multi-word numbers are presented with lower order words first. 54 uint64_t bits = 55 uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32); 56 switch (operand.number_kind) { 57 case SPV_NUMBER_SIGNED_INT: 58 *out << int64_t(bits); 59 break; 60 case SPV_NUMBER_UNSIGNED_INT: 61 *out << bits; 62 break; 63 case SPV_NUMBER_FLOATING: 64 // Assume only 64-bit floats. 65 *out << spvutils::FloatProxy<double>(bits); 66 break; 67 default: 68 assert(false && "Unreachable"); 69 } 70 } else { 71 // TODO(dneto): Support more than 64-bits at a time. 72 assert(false && "Unhandled"); 73 } 74 } 75 } // namespace libspirv 76