Coverage Report

Created: 2026-03-07 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/parsed_operand.cpp
Line
Count
Source
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
7.18k
                        const spv_parsed_operand_t& operand) {
26
7.18k
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
1.99k
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
1.99k
      operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
29
0
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
30
0
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
31
0
    return;
32
7.18k
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
7.18k
  if (operand.num_words > 2) return;
35
36
7.18k
  const uint32_t word = inst.words[operand.offset];
37
7.18k
  if (operand.num_words == 1) {
38
7.02k
    switch (operand.number_kind) {
39
628
      case SPV_NUMBER_SIGNED_INT:
40
628
        *out << int32_t(word);
41
628
        break;
42
5.86k
      case SPV_NUMBER_UNSIGNED_INT:
43
5.86k
        *out << word;
44
5.86k
        break;
45
528
      case SPV_NUMBER_FLOATING:
46
528
        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
528
          case SPV_FP_ENCODING_UNKNOWN:
67
528
            switch (operand.number_bit_width) {
68
36
              case 16:
69
36
                *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
70
36
                    uint16_t(word & 0xFFFF));
71
36
                break;
72
492
              case 32:
73
492
                *out << spvtools::utils::FloatProxy<float>(word);
74
492
                break;
75
528
            }
76
528
          default:
77
528
            break;
78
528
        }
79
528
        break;
80
528
      default:
81
0
        break;
82
7.02k
    }
83
7.02k
  } else if (operand.num_words == 2) {
84
    // Multi-word numbers are presented with lower order words first.
85
166
    uint64_t bits =
86
166
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
87
166
    switch (operand.number_kind) {
88
41
      case SPV_NUMBER_SIGNED_INT:
89
41
        *out << int64_t(bits);
90
41
        break;
91
121
      case SPV_NUMBER_UNSIGNED_INT:
92
121
        *out << bits;
93
121
        break;
94
4
      case SPV_NUMBER_FLOATING:
95
        // Assume only 64-bit floats.
96
4
        *out << spvtools::utils::FloatProxy<double>(bits);
97
4
        break;
98
0
      default:
99
0
        break;
100
166
    }
101
166
  }
102
7.18k
}
103
}  // namespace spvtools