Coverage Report

Created: 2025-06-13 06:37

/src/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
143k
                        const spv_parsed_operand_t& operand) {
26
143k
  if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
27
143k
      operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
28
143k
      operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
29
143k
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
30
143k
      operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
31
0
    return;
32
143k
  if (operand.num_words < 1) return;
33
  // TODO(dneto): Support more than 64-bits at a time.
34
142k
  if (operand.num_words > 2) return;
35
36
142k
  const uint32_t word = inst.words[operand.offset];
37
142k
  if (operand.num_words == 1) {
38
141k
    switch (operand.number_kind) {
39
32.0k
      case SPV_NUMBER_SIGNED_INT:
40
32.0k
        *out << int32_t(word);
41
32.0k
        break;
42
93.4k
      case SPV_NUMBER_UNSIGNED_INT:
43
93.4k
        *out << word;
44
93.4k
        break;
45
16.3k
      case SPV_NUMBER_FLOATING:
46
16.3k
        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
          // TODO Bfloat16
63
16.2k
          case SPV_FP_ENCODING_UNKNOWN:
64
16.2k
            switch (operand.number_bit_width) {
65
933
              case 16:
66
933
                *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
67
933
                    uint16_t(word & 0xFFFF));
68
933
                break;
69
14.5k
              case 32:
70
14.5k
                *out << spvtools::utils::FloatProxy<float>(word);
71
14.5k
                break;
72
16.2k
            }
73
16.3k
          default:
74
16.3k
            break;
75
16.3k
        }
76
16.3k
        break;
77
16.3k
      default:
78
0
        break;
79
141k
    }
80
141k
  } else if (operand.num_words == 2) {
81
    // Multi-word numbers are presented with lower order words first.
82
798
    uint64_t bits =
83
798
        uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
84
798
    switch (operand.number_kind) {
85
239
      case SPV_NUMBER_SIGNED_INT:
86
239
        *out << int64_t(bits);
87
239
        break;
88
240
      case SPV_NUMBER_UNSIGNED_INT:
89
240
        *out << bits;
90
240
        break;
91
319
      case SPV_NUMBER_FLOATING:
92
        // Assume only 64-bit floats.
93
319
        *out << spvtools::utils::FloatProxy<double>(bits);
94
319
        break;
95
0
      default:
96
0
        break;
97
798
    }
98
798
  }
99
142k
}
100
}  // namespace spvtools