parse_number.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifndef SOURCE_UTIL_PARSE_NUMBER_H_
  15. #define SOURCE_UTIL_PARSE_NUMBER_H_
  16. #include <functional>
  17. #include <string>
  18. #include <tuple>
  19. #include "source/util/hex_float.h"
  20. #include "spirv-tools/libspirv.h"
  21. namespace spvtools {
  22. namespace utils {
  23. // A struct to hold the expected type information for the number in text to be
  24. // parsed.
  25. struct NumberType {
  26. uint32_t bitwidth;
  27. // SPV_NUMBER_NONE means the type is unknown and is invalid to be used with
  28. // ParseAndEncode{|Integer|Floating}Number().
  29. spv_number_kind_t kind;
  30. };
  31. // Returns true if the type is a scalar integer type.
  32. inline bool IsIntegral(const NumberType& type) {
  33. return type.kind == SPV_NUMBER_UNSIGNED_INT ||
  34. type.kind == SPV_NUMBER_SIGNED_INT;
  35. }
  36. // Returns true if the type is a scalar floating point type.
  37. inline bool IsFloating(const NumberType& type) {
  38. return type.kind == SPV_NUMBER_FLOATING;
  39. }
  40. // Returns true if the type is a signed value.
  41. inline bool IsSigned(const NumberType& type) {
  42. return type.kind == SPV_NUMBER_FLOATING || type.kind == SPV_NUMBER_SIGNED_INT;
  43. }
  44. // Returns true if the type is unknown.
  45. inline bool IsUnknown(const NumberType& type) {
  46. return type.kind == SPV_NUMBER_NONE;
  47. }
  48. // Returns the number of bits in the type. This is only valid for integer and
  49. // floating types.
  50. inline int AssumedBitWidth(const NumberType& type) {
  51. switch (type.kind) {
  52. case SPV_NUMBER_SIGNED_INT:
  53. case SPV_NUMBER_UNSIGNED_INT:
  54. case SPV_NUMBER_FLOATING:
  55. return type.bitwidth;
  56. default:
  57. break;
  58. }
  59. // We don't care about this case.
  60. return 0;
  61. }
  62. // A templated class with a static member function Clamp, where Clamp sets a
  63. // referenced value of type T to 0 if T is an unsigned integer type, and
  64. // returns true if it modified the referenced value.
  65. template <typename T, typename = void>
  66. class ClampToZeroIfUnsignedType {
  67. public:
  68. // The default specialization does not clamp the value.
  69. static bool Clamp(T*) { return false; }
  70. };
  71. // The specialization of ClampToZeroIfUnsignedType for unsigned integer types.
  72. template <typename T>
  73. class ClampToZeroIfUnsignedType<
  74. T, typename std::enable_if<std::is_unsigned<T>::value>::type> {
  75. public:
  76. static bool Clamp(T* value_pointer) {
  77. if (*value_pointer) {
  78. *value_pointer = 0;
  79. return true;
  80. }
  81. return false;
  82. }
  83. };
  84. // Returns true if the given value fits within the target scalar integral type.
  85. // The target type may have an unusual bit width. If the value was originally
  86. // specified as a hexadecimal number, then the overflow bits should be zero.
  87. // If it was hex and the target type is signed, then return the sign-extended
  88. // value through the updated_value_for_hex pointer argument. On failure,
  89. // returns false.
  90. template <typename T>
  91. bool CheckRangeAndIfHexThenSignExtend(T value, const NumberType& type,
  92. bool is_hex, T* updated_value_for_hex) {
  93. // The encoded result has three regions of bits that are of interest, from
  94. // least to most significant:
  95. // - magnitude bits, where the magnitude of the number would be stored if
  96. // we were using a signed-magnitude representation.
  97. // - an optional sign bit
  98. // - overflow bits, up to bit 63 of a 64-bit number
  99. // For example:
  100. // Type Overflow Sign Magnitude
  101. // --------------- -------- ---- ---------
  102. // unsigned 8 bit 8-63 n/a 0-7
  103. // signed 8 bit 8-63 7 0-6
  104. // unsigned 16 bit 16-63 n/a 0-15
  105. // signed 16 bit 16-63 15 0-14
  106. // We'll use masks to define the three regions.
  107. // At first we'll assume the number is unsigned.
  108. const uint32_t bit_width = AssumedBitWidth(type);
  109. uint64_t magnitude_mask =
  110. (bit_width == 64) ? -1 : ((uint64_t(1) << bit_width) - 1);
  111. uint64_t sign_mask = 0;
  112. uint64_t overflow_mask = ~magnitude_mask;
  113. if (value < 0 || IsSigned(type)) {
  114. // Accommodate the sign bit.
  115. magnitude_mask >>= 1;
  116. sign_mask = magnitude_mask + 1;
  117. }
  118. bool failed = false;
  119. if (value < 0) {
  120. // The top bits must all be 1 for a negative signed value.
  121. failed = ((value & overflow_mask) != overflow_mask) ||
  122. ((value & sign_mask) != sign_mask);
  123. } else {
  124. if (is_hex) {
  125. // Hex values are a bit special. They decode as unsigned values, but may
  126. // represent a negative number. In this case, the overflow bits should
  127. // be zero.
  128. failed = (value & overflow_mask) != 0;
  129. } else {
  130. const uint64_t value_as_u64 = static_cast<uint64_t>(value);
  131. // Check overflow in the ordinary case.
  132. failed = (value_as_u64 & magnitude_mask) != value_as_u64;
  133. }
  134. }
  135. if (failed) {
  136. return false;
  137. }
  138. // Sign extend hex the number.
  139. if (is_hex && (value & sign_mask))
  140. *updated_value_for_hex = (value | overflow_mask);
  141. return true;
  142. }
  143. // Parses a numeric value of a given type from the given text. The number
  144. // should take up the entire string, and should be within bounds for the target
  145. // type. On success, returns true and populates the object referenced by
  146. // value_pointer. On failure, returns false.
  147. template <typename T>
  148. bool ParseNumber(const char* text, T* value_pointer) {
  149. // C++11 doesn't define std::istringstream(int8_t&), so calling this method
  150. // with a single-byte type leads to implementation-defined behaviour.
  151. // Similarly for uint8_t.
  152. static_assert(sizeof(T) > 1,
  153. "Single-byte types are not supported in this parse method");
  154. if (!text) return false;
  155. std::istringstream text_stream(text);
  156. // Allow both decimal and hex input for integers.
  157. // It also allows octal input, but we don't care about that case.
  158. text_stream >> std::setbase(0);
  159. text_stream >> *value_pointer;
  160. // We should have read something.
  161. bool ok = (text[0] != 0) && !text_stream.bad();
  162. // It should have been all the text.
  163. ok = ok && text_stream.eof();
  164. // It should have been in range.
  165. ok = ok && !text_stream.fail();
  166. // Work around a bug in the GNU C++11 library. It will happily parse
  167. // "-1" for uint16_t as 65535.
  168. if (ok && text[0] == '-')
  169. ok = !ClampToZeroIfUnsignedType<T>::Clamp(value_pointer);
  170. return ok;
  171. }
  172. // Enum to indicate the parsing and encoding status.
  173. enum class EncodeNumberStatus {
  174. kSuccess = 0,
  175. // Unsupported bit width etc.
  176. kUnsupported,
  177. // Expected type (NumberType) is not a scalar int or float, or putting a
  178. // negative number in an unsigned literal.
  179. kInvalidUsage,
  180. // Number value does not fit the bit width of the expected type etc.
  181. kInvalidText,
  182. };
  183. // Parses an integer value of a given |type| from the given |text| and encodes
  184. // the number by the given |emit| function. On success, returns
  185. // EncodeNumberStatus::kSuccess and the parsed number will be consumed by the
  186. // given |emit| function word by word (least significant word first). On
  187. // failure, this function returns the error code of the encoding status and
  188. // |emit| function will not be called. If the string pointer |error_msg| is not
  189. // a nullptr, it will be overwritten with error messages in case of failure. In
  190. // case of success, |error_msg| will not be touched. Integers up to 64 bits are
  191. // supported.
  192. EncodeNumberStatus ParseAndEncodeIntegerNumber(
  193. const char* text, const NumberType& type,
  194. std::function<void(uint32_t)> emit, std::string* error_msg);
  195. // Parses a floating point value of a given |type| from the given |text| and
  196. // encodes the number by the given |emit| funciton. On success, returns
  197. // EncodeNumberStatus::kSuccess and the parsed number will be consumed by the
  198. // given |emit| function word by word (least significant word first). On
  199. // failure, this function returns the error code of the encoding status and
  200. // |emit| function will not be called. If the string pointer |error_msg| is not
  201. // a nullptr, it will be overwritten with error messages in case of failure. In
  202. // case of success, |error_msg| will not be touched. Only 16, 32 and 64 bit
  203. // floating point numbers are supported.
  204. EncodeNumberStatus ParseAndEncodeFloatingPointNumber(
  205. const char* text, const NumberType& type,
  206. std::function<void(uint32_t)> emit, std::string* error_msg);
  207. // Parses an integer or floating point number of a given |type| from the given
  208. // |text| and encodes the number by the given |emit| function. On success,
  209. // returns EncodeNumberStatus::kSuccess and the parsed number will be consumed
  210. // by the given |emit| function word by word (least significant word first). On
  211. // failure, this function returns the error code of the encoding status and
  212. // |emit| function will not be called. If the string pointer |error_msg| is not
  213. // a nullptr, it will be overwritten with error messages in case of failure. In
  214. // case of success, |error_msg| will not be touched. Integers up to 64 bits
  215. // and 16/32/64 bit floating point values are supported.
  216. EncodeNumberStatus ParseAndEncodeNumber(const char* text,
  217. const NumberType& type,
  218. std::function<void(uint32_t)> emit,
  219. std::string* error_msg);
  220. } // namespace utils
  221. } // namespace spvtools
  222. #endif // SOURCE_UTIL_PARSE_NUMBER_H_