Coverage Report

Created: 2024-09-11 07:09

/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
31.5M
                        const spv_parsed_operand_t& operand) {
26
31.5M
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
31.5M
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
31.5M
      operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
29
31.5M
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
30
31.5M
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
31
0
    return;
32
31.5M
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
30.1M
  if (operand.num_words > 2) return;
35
36
30.0M
  const uint32_t word = inst.words[operand.offset];
37
30.0M
  if (operand.num_words == 1) {
38
30.0M
    switch (operand.number_kind) {
39
519k
      case SPV_NUMBER_SIGNED_INT:
40
519k
        *out << int32_t(word);
41
519k
        break;
42
28.9M
      case SPV_NUMBER_UNSIGNED_INT:
43
28.9M
        *out << word;
44
28.9M
        break;
45
586k
      case SPV_NUMBER_FLOATING:
46
586k
        if (operand.number_bit_width == 16) {
47
130k
          *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
48
130k
              uint16_t(word & 0xFFFF));
49
456k
        } else {
50
          // Assume 32-bit floats.
51
456k
          *out << spvtools::utils::FloatProxy<float>(word);
52
456k
        }
53
586k
        break;
54
0
      default:
55
0
        break;
56
30.0M
    }
57
30.0M
  } else if (operand.num_words == 2) {
58
    // Multi-word numbers are presented with lower order words first.
59
49.3k
    uint64_t bits =
60
49.3k
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
61
49.3k
    switch (operand.number_kind) {
62
14.5k
      case SPV_NUMBER_SIGNED_INT:
63
14.5k
        *out << int64_t(bits);
64
14.5k
        break;
65
14.5k
      case SPV_NUMBER_UNSIGNED_INT:
66
14.5k
        *out << bits;
67
14.5k
        break;
68
20.2k
      case SPV_NUMBER_FLOATING:
69
        // Assume only 64-bit floats.
70
20.2k
        *out << spvtools::utils::FloatProxy<double>(bits);
71
20.2k
        break;
72
0
      default:
73
0
        break;
74
49.3k
    }
75
49.3k
  }
76
30.0M
}
77
}  // namespace spvtools