/src/spirv-tools/source/val/validate_literals.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2017 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 | | // Validates literal numbers. |
16 | | |
17 | | #include <cassert> |
18 | | |
19 | | #include "source/val/instruction.h" |
20 | | #include "source/val/validate.h" |
21 | | #include "source/val/validation_state.h" |
22 | | |
23 | | namespace spvtools { |
24 | | namespace val { |
25 | | namespace { |
26 | | |
27 | | // Returns true if the operand holds a literal number |
28 | 15.2M | bool IsLiteralNumber(const spv_parsed_operand_t& operand) { |
29 | 15.2M | switch (operand.number_kind) { |
30 | 79.1k | case SPV_NUMBER_SIGNED_INT: |
31 | 1.02M | case SPV_NUMBER_UNSIGNED_INT: |
32 | 1.06M | case SPV_NUMBER_FLOATING: |
33 | 1.06M | return true; |
34 | 14.1M | default: |
35 | 14.1M | return false; |
36 | 15.2M | } |
37 | 15.2M | } |
38 | | |
39 | | // Verifies that the upper bits of the given upper |word| with given |
40 | | // lower |width| are zero- or sign-extended when |signed_int| is true |
41 | 105 | bool VerifyUpperBits(uint32_t word, uint32_t width, bool signed_int) { |
42 | 105 | assert(width < 32); |
43 | 105 | assert(0 < width); |
44 | 105 | const uint32_t upper_mask = 0xFFFFFFFFu << width; |
45 | 105 | const uint32_t upper_bits = word & upper_mask; |
46 | | |
47 | 105 | bool result = false; |
48 | 105 | if (signed_int) { |
49 | 69 | const uint32_t sign_bit = word & (1u << (width - 1)); |
50 | 69 | if (sign_bit) { |
51 | 17 | result = upper_bits == upper_mask; |
52 | 52 | } else { |
53 | 52 | result = upper_bits == 0; |
54 | 52 | } |
55 | 69 | } else { |
56 | 36 | result = upper_bits == 0; |
57 | 36 | } |
58 | 105 | return result; |
59 | 105 | } |
60 | | |
61 | | } // namespace |
62 | | |
63 | | // Validates that literal numbers are represented according to the spec |
64 | 10.1M | spv_result_t LiteralsPass(ValidationState_t& _, const Instruction* inst) { |
65 | | // For every operand that is a literal number |
66 | 25.4M | for (size_t i = 0; i < inst->operands().size(); i++) { |
67 | 15.2M | const spv_parsed_operand_t& operand = inst->operand(i); |
68 | 15.2M | if (!IsLiteralNumber(operand)) continue; |
69 | | |
70 | | // The upper bits are always in the last word (little-endian) |
71 | 1.06M | int last_index = operand.offset + operand.num_words - 1; |
72 | 1.06M | const uint32_t upper_word = inst->word(last_index); |
73 | | |
74 | | // TODO(jcaraban): is the |word size| defined in some header? |
75 | 1.06M | const uint32_t word_size = 32; |
76 | 1.06M | uint32_t bit_width = operand.number_bit_width; |
77 | | |
78 | | // Bit widths that are a multiple of the word size have no upper bits |
79 | 1.06M | const auto remaining_value_bits = bit_width % word_size; |
80 | 1.06M | if (remaining_value_bits == 0) continue; |
81 | | |
82 | 105 | const bool signedness = operand.number_kind == SPV_NUMBER_SIGNED_INT; |
83 | | |
84 | 105 | if (!VerifyUpperBits(upper_word, remaining_value_bits, signedness)) { |
85 | 55 | return _.diag(SPV_ERROR_INVALID_VALUE, inst) |
86 | 55 | << "The high-order bits of a literal number in instruction <id> " |
87 | 55 | << inst->id() << " must be 0 for a floating-point type, " |
88 | 55 | << "or 0 for an integer type with Signedness of 0, " |
89 | 55 | << "or sign extended when Signedness is 1"; |
90 | 55 | } |
91 | 105 | } |
92 | 10.1M | return SPV_SUCCESS; |
93 | 10.1M | } |
94 | | |
95 | | } // namespace val |
96 | | } // namespace spvtools |