Coverage Report

Created: 2026-09-28 07:06

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