2
0

parsed_operand.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // This file contains utility functions for spv_parsed_operand_t.
  15. #include "source/parsed_operand.h"
  16. #include <cassert>
  17. #include "source/util/hex_float.h"
  18. namespace spvtools {
  19. void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst,
  20. const spv_parsed_operand_t& operand) {
  21. if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
  22. operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
  23. operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
  24. operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
  25. operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
  26. return;
  27. if (operand.num_words < 1) return;
  28. // TODO(dneto): Support more than 64-bits at a time.
  29. if (operand.num_words > 2) return;
  30. const uint32_t word = inst.words[operand.offset];
  31. if (operand.num_words == 1) {
  32. switch (operand.number_kind) {
  33. case SPV_NUMBER_SIGNED_INT:
  34. *out << int32_t(word);
  35. break;
  36. case SPV_NUMBER_UNSIGNED_INT:
  37. *out << word;
  38. break;
  39. case SPV_NUMBER_FLOATING:
  40. switch (operand.fp_encoding) {
  41. case SPV_FP_ENCODING_IEEE754_BINARY16:
  42. *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
  43. uint16_t(word & 0xFFFF));
  44. break;
  45. case SPV_FP_ENCODING_IEEE754_BINARY32:
  46. *out << spvtools::utils::FloatProxy<float>(word);
  47. break;
  48. case SPV_FP_ENCODING_FLOAT8_E4M3:
  49. *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E4M3>(
  50. uint8_t(word & 0xFF));
  51. break;
  52. case SPV_FP_ENCODING_FLOAT8_E5M2:
  53. *out << spvtools::utils::FloatProxy<spvtools::utils::Float8_E5M2>(
  54. uint8_t(word & 0xFF));
  55. break;
  56. case SPV_FP_ENCODING_BFLOAT16:
  57. *out << spvtools::utils::FloatProxy<spvtools::utils::BFloat16>(
  58. uint16_t(word & 0xFFFF));
  59. break;
  60. case SPV_FP_ENCODING_UNKNOWN:
  61. switch (operand.number_bit_width) {
  62. case 16:
  63. *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
  64. uint16_t(word & 0xFFFF));
  65. break;
  66. case 32:
  67. *out << spvtools::utils::FloatProxy<float>(word);
  68. break;
  69. }
  70. default:
  71. break;
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. } else if (operand.num_words == 2) {
  78. // Multi-word numbers are presented with lower order words first.
  79. uint64_t bits =
  80. uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
  81. switch (operand.number_kind) {
  82. case SPV_NUMBER_SIGNED_INT:
  83. *out << int64_t(bits);
  84. break;
  85. case SPV_NUMBER_UNSIGNED_INT:
  86. *out << bits;
  87. break;
  88. case SPV_NUMBER_FLOATING:
  89. // Assume only 64-bit floats.
  90. *out << spvtools::utils::FloatProxy<double>(bits);
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. }
  97. } // namespace spvtools