Coverage Report

Created: 2025-08-26 07:00

/src/shaderc/third_party/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
28.1k
                        const spv_parsed_operand_t& operand) {
26
28.1k
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
28.1k
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
28.1k
      operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
29
28.1k
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
30
28.1k
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
31
0
    return;
32
28.1k
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
28.1k
  if (operand.num_words > 2) return;
35
36
28.1k
  const uint32_t word = inst.words[operand.offset];
37
28.1k
  if (operand.num_words == 1) {
38
27.8k
    switch (operand.number_kind) {
39
2.35k
      case SPV_NUMBER_SIGNED_INT:
40
2.35k
        *out << int32_t(word);
41
2.35k
        break;
42
22.8k
      case SPV_NUMBER_UNSIGNED_INT:
43
22.8k
        *out << word;
44
22.8k
        break;
45
2.71k
      case SPV_NUMBER_FLOATING:
46
2.71k
        switch (operand.fp_encoding) {
47
0
          case SPV_FP_ENCODING_IEEE754_BINARY16:
48
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
49
0
                uint16_t(word & 0xFFFF));
50
0
            break;
51
0
          case SPV_FP_ENCODING_IEEE754_BINARY32:
52
0
            *out << spvtools::utils::FloatProxy<float>(word);
53
0
            break;
54
0
          case SPV_FP_ENCODING_FLOAT8_E4M3:
55
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E4M3>(
56
0
                uint8_t(word & 0xFF));
57
0
            break;
58
0
          case SPV_FP_ENCODING_FLOAT8_E5M2:
59
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E5M2>(
60
0
                uint8_t(word & 0xFF));
61
0
            break;
62
0
          case SPV_FP_ENCODING_BFLOAT16:
63
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::BFloat16>(
64
0
                uint16_t(word & 0xFFFF));
65
0
            break;
66
2.71k
          case SPV_FP_ENCODING_UNKNOWN:
67
2.71k
            switch (operand.number_bit_width) {
68
38
              case 16:
69
38
                *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
70
38
                    uint16_t(word & 0xFFFF));
71
38
                break;
72
2.68k
              case 32:
73
2.68k
                *out << spvtools::utils::FloatProxy<float>(word);
74
2.68k
                break;
75
2.71k
            }
76
2.71k
          default:
77
2.71k
            break;
78
2.71k
        }
79
2.71k
        break;
80
2.71k
      default:
81
0
        break;
82
27.8k
    }
83
27.8k
  } else if (operand.num_words == 2) {
84
    // Multi-word numbers are presented with lower order words first.
85
258
    uint64_t bits =
86
258
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
87
258
    switch (operand.number_kind) {
88
57
      case SPV_NUMBER_SIGNED_INT:
89
57
        *out << int64_t(bits);
90
57
        break;
91
195
      case SPV_NUMBER_UNSIGNED_INT:
92
195
        *out << bits;
93
195
        break;
94
6
      case SPV_NUMBER_FLOATING:
95
        // Assume only 64-bit floats.
96
6
        *out << spvtools::utils::FloatProxy<double>(bits);
97
6
        break;
98
0
      default:
99
0
        break;
100
258
    }
101
258
  }
102
28.1k
}
103
}  // namespace spvtools