Coverage Report

Created: 2026-09-03 06:33

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.07k
                        const spv_parsed_operand_t& operand) {
26
7.07k
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
1.97k
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
1.97k
      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.07k
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
7.07k
  if (operand.num_words > 2) return;
35
36
7.07k
  const uint32_t word = inst.words[operand.offset];
37
7.07k
  if (operand.num_words == 1) {
38
6.91k
    switch (operand.number_kind) {
39
617
      case SPV_NUMBER_SIGNED_INT:
40
617
        *out << int32_t(word);
41
617
        break;
42
5.78k
      case SPV_NUMBER_UNSIGNED_INT:
43
5.78k
        *out << word;
44
5.78k
        break;
45
514
      case SPV_NUMBER_FLOATING:
46
514
        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
514
          case SPV_FP_ENCODING_UNKNOWN:
87
514
            switch (operand.number_bit_width) {
88
36
              case 16:
89
36
                *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
90
36
                    uint16_t(word & 0xFFFF));
91
36
                break;
92
478
              case 32:
93
478
                *out << spvtools::utils::FloatProxy<float>(word);
94
478
                break;
95
514
            }
96
514
          default:
97
514
            break;
98
514
        }
99
514
        break;
100
514
      default:
101
0
        break;
102
6.91k
    }
103
6.91k
  } else if (operand.num_words == 2) {
104
    // Multi-word numbers are presented with lower order words first.
105
166
    uint64_t bits =
106
166
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
107
166
    switch (operand.number_kind) {
108
41
      case SPV_NUMBER_SIGNED_INT:
109
41
        *out << int64_t(bits);
110
41
        break;
111
121
      case SPV_NUMBER_UNSIGNED_INT:
112
121
        *out << bits;
113
121
        break;
114
4
      case SPV_NUMBER_FLOATING:
115
        // Assume only 64-bit floats.
116
4
        *out << spvtools::utils::FloatProxy<double>(bits);
117
4
        break;
118
0
      default:
119
0
        break;
120
166
    }
121
166
  }
122
7.07k
}
123
}  // namespace spvtools