Coverage Report

Created: 2026-12-01 07:22

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
27.8k
                        const spv_parsed_operand_t& operand) {
26
27.8k
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
7.31k
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
7.31k
      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
27.8k
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
27.8k
  if (operand.num_words > 2) return;
35
36
27.8k
  const uint32_t word = inst.words[operand.offset];
37
27.8k
  if (operand.num_words == 1) {
38
27.5k
    switch (operand.number_kind) {
39
2.45k
      case SPV_NUMBER_SIGNED_INT:
40
2.45k
        *out << int32_t(word);
41
2.45k
        break;
42
22.6k
      case SPV_NUMBER_UNSIGNED_INT:
43
22.6k
        *out << word;
44
22.6k
        break;
45
2.41k
      case SPV_NUMBER_FLOATING:
46
2.41k
        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_FLOAT4_E2M1:
55
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float4_E2M1>(
56
0
                uint8_t(word & 0xF));
57
0
            break;
58
0
          case SPV_FP_ENCODING_FLOAT6_E2M3:
59
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float6_E2M3>(
60
0
                uint8_t(word & 0x3F));
61
0
            break;
62
0
          case SPV_FP_ENCODING_FLOAT6_E3M2:
63
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float6_E3M2>(
64
0
                uint8_t(word & 0x3F));
65
0
            break;
66
0
          case SPV_FP_ENCODING_FLOAT8_E4M3:
67
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E4M3>(
68
0
                uint8_t(word & 0xFF));
69
0
            break;
70
0
          case SPV_FP_ENCODING_FLOAT8_E5M2:
71
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E5M2>(
72
0
                uint8_t(word & 0xFF));
73
0
            break;
74
0
          case SPV_FP_ENCODING_FLOAT8_UNSIGNED_E8M0:
75
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E8M0>(
76
0
                uint8_t(word & 0xFF));
77
0
            break;
78
0
          case SPV_FP_ENCODING_MXINT8:
79
0
            *out << spvtools::utils::HexFixedPoint<spvtools::utils::MXInt8>(
80
0
                uint8_t(word & 0xFF));
81
0
            break;
82
0
          case SPV_FP_ENCODING_BFLOAT16:
83
0
            *out << spvtools::utils::FloatProxy<spvtools::utils::BFloat16>(
84
0
                uint16_t(word & 0xFFFF));
85
0
            break;
86
2.41k
          case SPV_FP_ENCODING_UNKNOWN:
87
2.41k
            switch (operand.number_bit_width) {
88
54
              case 16:
89
54
                *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
90
54
                    uint16_t(word & 0xFFFF));
91
54
                break;
92
2.35k
              case 32:
93
2.35k
                *out << spvtools::utils::FloatProxy<float>(word);
94
2.35k
                break;
95
2.41k
            }
96
2.41k
          default:
97
2.41k
            break;
98
2.41k
        }
99
2.41k
        break;
100
2.41k
      default:
101
0
        break;
102
27.5k
    }
103
27.5k
  } else if (operand.num_words == 2) {
104
    // Multi-word numbers are presented with lower order words first.
105
312
    uint64_t bits =
106
312
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
107
312
    switch (operand.number_kind) {
108
109
      case SPV_NUMBER_SIGNED_INT:
109
109
        *out << int64_t(bits);
110
109
        break;
111
181
      case SPV_NUMBER_UNSIGNED_INT:
112
181
        *out << bits;
113
181
        break;
114
22
      case SPV_NUMBER_FLOATING:
115
        // Assume only 64-bit floats.
116
22
        *out << spvtools::utils::FloatProxy<double>(bits);
117
22
        break;
118
0
      default:
119
0
        break;
120
312
    }
121
312
  }
122
27.8k
}
123
}  // namespace spvtools