validate_literals.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2017 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. // Validates literal numbers.
  15. #include "validate.h"
  16. #include <cassert>
  17. #include "diagnostic.h"
  18. #include "opcode.h"
  19. #include "val/instruction.h"
  20. #include "val/validation_state.h"
  21. namespace libspirv {
  22. namespace {
  23. // Returns true if the operand holds a literal number
  24. bool IsLiteralNumber(const spv_parsed_operand_t* operand) {
  25. switch (operand->number_kind) {
  26. case SPV_NUMBER_SIGNED_INT:
  27. case SPV_NUMBER_UNSIGNED_INT:
  28. case SPV_NUMBER_FLOATING:
  29. return true;
  30. default:
  31. return false;
  32. }
  33. }
  34. // Verifies that the upper bits of the given upper |word| with given
  35. // lower |width| are zero- or sign-extended when |signed_int| is true
  36. bool VerifyUpperBits(uint32_t word, uint32_t width, bool signed_int) {
  37. assert(width < 32);
  38. assert(0 < width);
  39. const uint32_t upper_mask = 0xFFFFFFFFu << width;
  40. const uint32_t upper_bits = word & upper_mask;
  41. bool result = false;
  42. if (signed_int) {
  43. const uint32_t sign_bit = word & (1u << (width - 1));
  44. if (sign_bit) {
  45. result = upper_bits == upper_mask;
  46. } else {
  47. result = upper_bits == 0;
  48. }
  49. } else {
  50. result = upper_bits == 0;
  51. }
  52. return result;
  53. }
  54. } // namespace
  55. // Validates that literal numbers are represented according to the spec
  56. spv_result_t LiteralsPass(ValidationState_t& _,
  57. const spv_parsed_instruction_t* inst) {
  58. // For every operand that is a literal number
  59. for (uint16_t i = 0; i < inst->num_operands; i++) {
  60. const spv_parsed_operand_t* operand = inst->operands + i;
  61. if (!IsLiteralNumber(operand)) continue;
  62. // The upper bits are always in the last word (little-endian)
  63. int last_index = operand->offset + operand->num_words - 1;
  64. const uint32_t upper_word = inst->words[last_index];
  65. // TODO(jcaraban): is the |word size| defined in some header?
  66. const uint32_t word_size = 32;
  67. uint32_t bit_width = operand->number_bit_width;
  68. // Bit widths that are a multiple of the word size have no upper bits
  69. const auto remaining_value_bits = bit_width % word_size;
  70. if (remaining_value_bits == 0) continue;
  71. const bool signedness = operand->number_kind == SPV_NUMBER_SIGNED_INT;
  72. if (!VerifyUpperBits(upper_word, remaining_value_bits, signedness)) {
  73. return _.diag(SPV_ERROR_INVALID_VALUE)
  74. << "The high-order bits of a literal number in instruction <id> "
  75. << inst->result_id << " must be 0 for a floating-point type, "
  76. << "or 0 for an integer type with Signedness of 0, "
  77. << "or sign extended when Signedness is 1";
  78. }
  79. }
  80. return SPV_SUCCESS;
  81. }
  82. } // namespace libspirv