Coverage Report

Created: 2023-03-01 07:33

/src/spirv-tools/source/parsed_operand.cpp
Line
Count
Source (jump to first uncovered line)
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 "source/parsed_operand.h"
18
19
#include <cassert>
20
#include "source/util/hex_float.h"
21
22
namespace spvtools {
23
24
void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst,
25
9.84M
                        const spv_parsed_operand_t& operand) {
26
9.84M
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
9.84M
      operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
28
9.84M
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
29
9.84M
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
30
0
    return;
31
9.84M
  if (operand.num_words < 1) return;
32
  // TODO(dneto): Support more than 64-bits at a time.
33
7.66M
  if (operand.num_words > 2) return;
34
35
7.66M
  const uint32_t word = inst.words[operand.offset];
36
7.66M
  if (operand.num_words == 1) {
37
7.60M
    switch (operand.number_kind) {
38
771k
      case SPV_NUMBER_SIGNED_INT:
39
771k
        *out << int32_t(word);
40
771k
        break;
41
6.29M
      case SPV_NUMBER_UNSIGNED_INT:
42
6.29M
        *out << word;
43
6.29M
        break;
44
534k
      case SPV_NUMBER_FLOATING:
45
534k
        if (operand.number_bit_width == 16) {
46
159k
          *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
47
159k
              uint16_t(word & 0xFFFF));
48
375k
        } else {
49
          // Assume 32-bit floats.
50
375k
          *out << spvtools::utils::FloatProxy<float>(word);
51
375k
        }
52
534k
        break;
53
0
      default:
54
0
        break;
55
7.60M
    }
56
7.60M
  } else if (operand.num_words == 2) {
57
    // Multi-word numbers are presented with lower order words first.
58
63.5k
    uint64_t bits =
59
63.5k
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
60
63.5k
    switch (operand.number_kind) {
61
14.8k
      case SPV_NUMBER_SIGNED_INT:
62
14.8k
        *out << int64_t(bits);
63
14.8k
        break;
64
20.4k
      case SPV_NUMBER_UNSIGNED_INT:
65
20.4k
        *out << bits;
66
20.4k
        break;
67
28.2k
      case SPV_NUMBER_FLOATING:
68
        // Assume only 64-bit floats.
69
28.2k
        *out << spvtools::utils::FloatProxy<double>(bits);
70
28.2k
        break;
71
0
      default:
72
0
        break;
73
63.5k
    }
74
63.5k
  }
75
7.66M
}
76
}  // namespace spvtools