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