File: | tools/lldb/source/Utility/Scalar.cpp |
Warning: | line 172, column 5 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- Scalar.cpp ----------------------------------------------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | ||||
10 | #include "lldb/Utility/Scalar.h" | |||
11 | ||||
12 | #include "lldb/Utility/DataExtractor.h" | |||
13 | #include "lldb/Utility/Endian.h" | |||
14 | #include "lldb/Utility/Status.h" | |||
15 | #include "lldb/Utility/Stream.h" | |||
16 | #include "lldb/lldb-types.h" // for offset_t | |||
17 | ||||
18 | #include "llvm/ADT/SmallString.h" | |||
19 | ||||
20 | #include <cinttypes> | |||
21 | #include <cstdio> | |||
22 | ||||
23 | using namespace lldb; | |||
24 | using namespace lldb_private; | |||
25 | ||||
26 | //---------------------------------------------------------------------- | |||
27 | // Promote to max type currently follows the ANSI C rule for type promotion in | |||
28 | // expressions. | |||
29 | //---------------------------------------------------------------------- | |||
30 | static Scalar::Type PromoteToMaxType( | |||
31 | const Scalar &lhs, // The const left hand side object | |||
32 | const Scalar &rhs, // The const right hand side object | |||
33 | Scalar &temp_value, // A modifiable temp value than can be used to hold | |||
34 | // either the promoted lhs or rhs object | |||
35 | const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly | |||
36 | // promoted value of lhs (at most one of | |||
37 | // lhs/rhs will get promoted) | |||
38 | const Scalar *&promoted_rhs_ptr // Pointer to the resulting possibly | |||
39 | // promoted value of rhs (at most one of | |||
40 | // lhs/rhs will get promoted) | |||
41 | ) { | |||
42 | Scalar result; | |||
43 | // Initialize the promoted values for both the right and left hand side | |||
44 | // values to be the objects themselves. If no promotion is needed (both right | |||
45 | // and left have the same type), then the temp_value will not get used. | |||
46 | promoted_lhs_ptr = &lhs; | |||
47 | promoted_rhs_ptr = &rhs; | |||
48 | // Extract the types of both the right and left hand side values | |||
49 | Scalar::Type lhs_type = lhs.GetType(); | |||
50 | Scalar::Type rhs_type = rhs.GetType(); | |||
51 | ||||
52 | if (lhs_type > rhs_type) { | |||
53 | // Right hand side need to be promoted | |||
54 | temp_value = rhs; // Copy right hand side into the temp value | |||
55 | if (temp_value.Promote(lhs_type)) // Promote it | |||
56 | promoted_rhs_ptr = | |||
57 | &temp_value; // Update the pointer for the promoted right hand side | |||
58 | } else if (lhs_type < rhs_type) { | |||
59 | // Left hand side need to be promoted | |||
60 | temp_value = lhs; // Copy left hand side value into the temp value | |||
61 | if (temp_value.Promote(rhs_type)) // Promote it | |||
62 | promoted_lhs_ptr = | |||
63 | &temp_value; // Update the pointer for the promoted left hand side | |||
64 | } | |||
65 | ||||
66 | // Make sure our type promotion worked as expected | |||
67 | if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType()) | |||
68 | return promoted_lhs_ptr->GetType(); // Return the resulting max type | |||
69 | ||||
70 | // Return the void type (zero) if we fail to promote either of the values. | |||
71 | return Scalar::e_void; | |||
72 | } | |||
73 | ||||
74 | Scalar::Scalar() : m_type(e_void), m_float((float)0) {} | |||
75 | ||||
76 | Scalar::Scalar(const Scalar &rhs) | |||
77 | : m_type(rhs.m_type), m_integer(rhs.m_integer), m_float(rhs.m_float) {} | |||
78 | ||||
79 | bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const { | |||
80 | size_t byte_size = GetByteSize(); | |||
81 | if (byte_size > 0) { | |||
82 | const uint8_t *bytes = reinterpret_cast<const uint8_t *>(GetBytes()); | |||
83 | ||||
84 | if (limit_byte_size < byte_size) { | |||
85 | if (endian::InlHostByteOrder() == eByteOrderLittle) { | |||
86 | // On little endian systems if we want fewer bytes from the current | |||
87 | // type we just specify fewer bytes since the LSByte is first... | |||
88 | byte_size = limit_byte_size; | |||
89 | } else if (endian::InlHostByteOrder() == eByteOrderBig) { | |||
90 | // On big endian systems if we want fewer bytes from the current type | |||
91 | // have to advance our initial byte pointer and trim down the number of | |||
92 | // bytes since the MSByte is first | |||
93 | bytes += byte_size - limit_byte_size; | |||
94 | byte_size = limit_byte_size; | |||
95 | } | |||
96 | } | |||
97 | ||||
98 | data.SetData(bytes, byte_size, endian::InlHostByteOrder()); | |||
99 | return true; | |||
100 | } | |||
101 | data.Clear(); | |||
102 | return false; | |||
103 | } | |||
104 | ||||
105 | const void *Scalar::GetBytes() const { | |||
106 | const uint64_t *apint_words; | |||
107 | const uint8_t *bytes; | |||
108 | static float_t flt_val; | |||
109 | static double_t dbl_val; | |||
110 | static uint64_t swapped_words[4]; | |||
111 | switch (m_type) { | |||
112 | case e_void: | |||
113 | break; | |||
114 | case e_sint: | |||
115 | case e_uint: | |||
116 | case e_slong: | |||
117 | case e_ulong: | |||
118 | case e_slonglong: | |||
119 | case e_ulonglong: | |||
120 | bytes = reinterpret_cast<const uint8_t *>(m_integer.getRawData()); | |||
121 | // getRawData always returns a pointer to an uint64_t. If we have a | |||
122 | // smaller type, we need to update the pointer on big-endian systems. | |||
123 | if (endian::InlHostByteOrder() == eByteOrderBig) { | |||
124 | size_t byte_size = m_integer.getBitWidth() / 8; | |||
125 | if (byte_size < 8) | |||
126 | bytes += 8 - byte_size; | |||
127 | } | |||
128 | return bytes; | |||
129 | case e_sint128: | |||
130 | case e_uint128: | |||
131 | apint_words = m_integer.getRawData(); | |||
132 | // getRawData always returns a pointer to an array of two uint64_t values, | |||
133 | // where the least-significant word always comes first. On big-endian | |||
134 | // systems we need to swap the two words. | |||
135 | if (endian::InlHostByteOrder() == eByteOrderBig) { | |||
136 | swapped_words[0] = apint_words[1]; | |||
137 | swapped_words[1] = apint_words[0]; | |||
138 | apint_words = swapped_words; | |||
139 | } | |||
140 | return reinterpret_cast<const void *>(apint_words); | |||
141 | case e_sint256: | |||
142 | case e_uint256: | |||
143 | apint_words = m_integer.getRawData(); | |||
144 | // getRawData always returns a pointer to an array of four uint64_t values, | |||
145 | // where the least-significant word always comes first. On big-endian | |||
146 | // systems we need to swap the four words. | |||
147 | if (endian::InlHostByteOrder() == eByteOrderBig) { | |||
148 | swapped_words[0] = apint_words[3]; | |||
149 | swapped_words[1] = apint_words[2]; | |||
150 | swapped_words[2] = apint_words[1]; | |||
151 | swapped_words[3] = apint_words[0]; | |||
152 | apint_words = swapped_words; | |||
153 | } | |||
154 | return reinterpret_cast<const void *>(apint_words); | |||
155 | case e_float: | |||
156 | flt_val = m_float.convertToFloat(); | |||
157 | return reinterpret_cast<const void *>(&flt_val); | |||
158 | case e_double: | |||
159 | dbl_val = m_float.convertToDouble(); | |||
160 | return reinterpret_cast<const void *>(&dbl_val); | |||
161 | case e_long_double: | |||
162 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
163 | apint_words = ldbl_val.getRawData(); | |||
164 | // getRawData always returns a pointer to an array of two uint64_t values, | |||
165 | // where the least-significant word always comes first. On big-endian | |||
166 | // systems we need to swap the two words. | |||
167 | if (endian::InlHostByteOrder() == eByteOrderBig) { | |||
168 | swapped_words[0] = apint_words[1]; | |||
169 | swapped_words[1] = apint_words[0]; | |||
170 | apint_words = swapped_words; | |||
171 | } | |||
172 | return reinterpret_cast<const void *>(apint_words); | |||
| ||||
173 | } | |||
174 | return nullptr; | |||
175 | } | |||
176 | ||||
177 | size_t Scalar::GetByteSize() const { | |||
178 | switch (m_type) { | |||
179 | case e_void: | |||
180 | break; | |||
181 | case e_sint: | |||
182 | case e_uint: | |||
183 | case e_slong: | |||
184 | case e_ulong: | |||
185 | case e_slonglong: | |||
186 | case e_ulonglong: | |||
187 | case e_sint128: | |||
188 | case e_uint128: | |||
189 | case e_sint256: | |||
190 | case e_uint256: | |||
191 | return (m_integer.getBitWidth() / 8); | |||
192 | case e_float: | |||
193 | return sizeof(float_t); | |||
194 | case e_double: | |||
195 | return sizeof(double_t); | |||
196 | case e_long_double: | |||
197 | return sizeof(long_double_t); | |||
198 | } | |||
199 | return 0; | |||
200 | } | |||
201 | ||||
202 | bool Scalar::IsZero() const { | |||
203 | llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8); | |||
204 | switch (m_type) { | |||
205 | case e_void: | |||
206 | break; | |||
207 | case e_sint: | |||
208 | case e_uint: | |||
209 | case e_slong: | |||
210 | case e_ulong: | |||
211 | case e_slonglong: | |||
212 | case e_ulonglong: | |||
213 | case e_sint128: | |||
214 | case e_uint128: | |||
215 | case e_sint256: | |||
216 | case e_uint256: | |||
217 | return llvm::APInt::isSameValue(zero_int, m_integer); | |||
218 | case e_float: | |||
219 | case e_double: | |||
220 | case e_long_double: | |||
221 | return m_float.isZero(); | |||
222 | } | |||
223 | return false; | |||
224 | } | |||
225 | ||||
226 | void Scalar::GetValue(Stream *s, bool show_type) const { | |||
227 | if (show_type) | |||
228 | s->Printf("(%s) ", GetTypeAsCString()); | |||
229 | ||||
230 | switch (m_type) { | |||
231 | case e_void: | |||
232 | break; | |||
233 | case e_sint: | |||
234 | case e_slong: | |||
235 | case e_slonglong: | |||
236 | case e_sint128: | |||
237 | case e_sint256: | |||
238 | s->PutCString(m_integer.toString(10, true)); | |||
239 | break; | |||
240 | case e_uint: | |||
241 | case e_ulong: | |||
242 | case e_ulonglong: | |||
243 | case e_uint128: | |||
244 | case e_uint256: | |||
245 | s->PutCString(m_integer.toString(10, false)); | |||
246 | break; | |||
247 | case e_float: | |||
248 | case e_double: | |||
249 | case e_long_double: | |||
250 | llvm::SmallString<24> string; | |||
251 | m_float.toString(string); | |||
252 | s->Printf("%s", string.c_str()); | |||
253 | break; | |||
254 | } | |||
255 | } | |||
256 | ||||
257 | const char *Scalar::GetTypeAsCString() const { | |||
258 | switch (m_type) { | |||
259 | case e_void: | |||
260 | return "void"; | |||
261 | case e_sint: | |||
262 | return "int"; | |||
263 | case e_uint: | |||
264 | return "unsigned int"; | |||
265 | case e_slong: | |||
266 | return "long"; | |||
267 | case e_ulong: | |||
268 | return "unsigned long"; | |||
269 | case e_slonglong: | |||
270 | return "long long"; | |||
271 | case e_ulonglong: | |||
272 | return "unsigned long long"; | |||
273 | case e_sint128: | |||
274 | return "int128_t"; | |||
275 | case e_uint128: | |||
276 | return "unsigned int128_t"; | |||
277 | case e_sint256: | |||
278 | return "int256_t"; | |||
279 | case e_uint256: | |||
280 | return "unsigned int256_t"; | |||
281 | case e_float: | |||
282 | return "float"; | |||
283 | case e_double: | |||
284 | return "double"; | |||
285 | case e_long_double: | |||
286 | return "long double"; | |||
287 | } | |||
288 | return "<invalid Scalar type>"; | |||
289 | } | |||
290 | ||||
291 | Scalar &Scalar::operator=(const Scalar &rhs) { | |||
292 | if (this != &rhs) { | |||
293 | m_type = rhs.m_type; | |||
294 | m_integer = llvm::APInt(rhs.m_integer); | |||
295 | m_float = rhs.m_float; | |||
296 | } | |||
297 | return *this; | |||
298 | } | |||
299 | ||||
300 | Scalar &Scalar::operator=(const int v) { | |||
301 | m_type = e_sint; | |||
302 | m_integer = llvm::APInt(sizeof(int) * 8, v, true); | |||
303 | return *this; | |||
304 | } | |||
305 | ||||
306 | Scalar &Scalar::operator=(unsigned int v) { | |||
307 | m_type = e_uint; | |||
308 | m_integer = llvm::APInt(sizeof(int) * 8, v); | |||
309 | return *this; | |||
310 | } | |||
311 | ||||
312 | Scalar &Scalar::operator=(long v) { | |||
313 | m_type = e_slong; | |||
314 | m_integer = llvm::APInt(sizeof(long) * 8, v, true); | |||
315 | return *this; | |||
316 | } | |||
317 | ||||
318 | Scalar &Scalar::operator=(unsigned long v) { | |||
319 | m_type = e_ulong; | |||
320 | m_integer = llvm::APInt(sizeof(long) * 8, v); | |||
321 | return *this; | |||
322 | } | |||
323 | ||||
324 | Scalar &Scalar::operator=(long long v) { | |||
325 | m_type = e_slonglong; | |||
326 | m_integer = llvm::APInt(sizeof(long) * 8, v, true); | |||
327 | return *this; | |||
328 | } | |||
329 | ||||
330 | Scalar &Scalar::operator=(unsigned long long v) { | |||
331 | m_type = e_ulonglong; | |||
332 | m_integer = llvm::APInt(sizeof(long long) * 8, v); | |||
333 | return *this; | |||
334 | } | |||
335 | ||||
336 | Scalar &Scalar::operator=(float v) { | |||
337 | m_type = e_float; | |||
338 | m_float = llvm::APFloat(v); | |||
339 | return *this; | |||
340 | } | |||
341 | ||||
342 | Scalar &Scalar::operator=(double v) { | |||
343 | m_type = e_double; | |||
344 | m_float = llvm::APFloat(v); | |||
345 | return *this; | |||
346 | } | |||
347 | ||||
348 | Scalar &Scalar::operator=(long double v) { | |||
349 | m_type = e_long_double; | |||
350 | if (m_ieee_quad) | |||
351 | m_float = llvm::APFloat( | |||
352 | llvm::APFloat::IEEEquad(), | |||
353 | llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, ((type128 *)&v)->x)); | |||
354 | else | |||
355 | m_float = llvm::APFloat( | |||
356 | llvm::APFloat::x87DoubleExtended(), | |||
357 | llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, ((type128 *)&v)->x)); | |||
358 | return *this; | |||
359 | } | |||
360 | ||||
361 | Scalar &Scalar::operator=(llvm::APInt rhs) { | |||
362 | m_integer = llvm::APInt(rhs); | |||
363 | switch (m_integer.getBitWidth()) { | |||
364 | case 8: | |||
365 | case 16: | |||
366 | case 32: | |||
367 | if (m_integer.isSignedIntN(sizeof(sint_t) * 8)) | |||
368 | m_type = e_sint; | |||
369 | else | |||
370 | m_type = e_uint; | |||
371 | break; | |||
372 | case 64: | |||
373 | if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8)) | |||
374 | m_type = e_slonglong; | |||
375 | else | |||
376 | m_type = e_ulonglong; | |||
377 | break; | |||
378 | case 128: | |||
379 | if (m_integer.isSignedIntN(BITWIDTH_INT128128)) | |||
380 | m_type = e_sint128; | |||
381 | else | |||
382 | m_type = e_uint128; | |||
383 | break; | |||
384 | case 256: | |||
385 | if (m_integer.isSignedIntN(BITWIDTH_INT256256)) | |||
386 | m_type = e_sint256; | |||
387 | else | |||
388 | m_type = e_uint256; | |||
389 | break; | |||
390 | } | |||
391 | return *this; | |||
392 | } | |||
393 | ||||
394 | Scalar::~Scalar() = default; | |||
395 | ||||
396 | bool Scalar::Promote(Scalar::Type type) { | |||
397 | bool success = false; | |||
398 | switch (m_type) { | |||
399 | case e_void: | |||
400 | break; | |||
401 | ||||
402 | case e_sint: | |||
403 | switch (type) { | |||
404 | case e_void: | |||
405 | break; | |||
406 | case e_sint: | |||
407 | success = true; | |||
408 | break; | |||
409 | case e_uint: | |||
410 | m_integer = m_integer.sextOrTrunc(sizeof(uint_t) * 8); | |||
411 | success = true; | |||
412 | break; | |||
413 | ||||
414 | case e_slong: | |||
415 | m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8); | |||
416 | success = true; | |||
417 | break; | |||
418 | ||||
419 | case e_ulong: | |||
420 | m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8); | |||
421 | success = true; | |||
422 | break; | |||
423 | ||||
424 | case e_slonglong: | |||
425 | m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8); | |||
426 | success = true; | |||
427 | break; | |||
428 | ||||
429 | case e_ulonglong: | |||
430 | m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8); | |||
431 | success = true; | |||
432 | break; | |||
433 | ||||
434 | case e_sint128: | |||
435 | case e_uint128: | |||
436 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128); | |||
437 | success = true; | |||
438 | break; | |||
439 | ||||
440 | case e_sint256: | |||
441 | case e_uint256: | |||
442 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256); | |||
443 | success = true; | |||
444 | break; | |||
445 | ||||
446 | case e_float: | |||
447 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
448 | m_float.convertFromAPInt(m_integer, true, | |||
449 | llvm::APFloat::rmNearestTiesToEven); | |||
450 | success = true; | |||
451 | break; | |||
452 | ||||
453 | case e_double: | |||
454 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
455 | m_float.convertFromAPInt(m_integer, true, | |||
456 | llvm::APFloat::rmNearestTiesToEven); | |||
457 | success = true; | |||
458 | break; | |||
459 | ||||
460 | case e_long_double: | |||
461 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
462 | : llvm::APFloat::x87DoubleExtended()); | |||
463 | m_float.convertFromAPInt(m_integer, true, | |||
464 | llvm::APFloat::rmNearestTiesToEven); | |||
465 | success = true; | |||
466 | break; | |||
467 | } | |||
468 | break; | |||
469 | ||||
470 | case e_uint: | |||
471 | switch (type) { | |||
472 | case e_void: | |||
473 | case e_sint: | |||
474 | break; | |||
475 | case e_uint: | |||
476 | success = true; | |||
477 | break; | |||
478 | case e_slong: | |||
479 | m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8); | |||
480 | success = true; | |||
481 | break; | |||
482 | ||||
483 | case e_ulong: | |||
484 | m_integer = m_integer.zextOrTrunc(sizeof(ulong_t) * 8); | |||
485 | success = true; | |||
486 | break; | |||
487 | ||||
488 | case e_slonglong: | |||
489 | m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8); | |||
490 | success = true; | |||
491 | break; | |||
492 | ||||
493 | case e_ulonglong: | |||
494 | m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8); | |||
495 | success = true; | |||
496 | break; | |||
497 | ||||
498 | case e_sint128: | |||
499 | case e_uint128: | |||
500 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128); | |||
501 | success = true; | |||
502 | break; | |||
503 | ||||
504 | case e_sint256: | |||
505 | case e_uint256: | |||
506 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256); | |||
507 | success = true; | |||
508 | break; | |||
509 | ||||
510 | case e_float: | |||
511 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
512 | m_float.convertFromAPInt(m_integer, false, | |||
513 | llvm::APFloat::rmNearestTiesToEven); | |||
514 | success = true; | |||
515 | break; | |||
516 | ||||
517 | case e_double: | |||
518 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
519 | m_float.convertFromAPInt(m_integer, false, | |||
520 | llvm::APFloat::rmNearestTiesToEven); | |||
521 | success = true; | |||
522 | break; | |||
523 | ||||
524 | case e_long_double: | |||
525 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
526 | : llvm::APFloat::x87DoubleExtended()); | |||
527 | m_float.convertFromAPInt(m_integer, false, | |||
528 | llvm::APFloat::rmNearestTiesToEven); | |||
529 | success = true; | |||
530 | break; | |||
531 | } | |||
532 | break; | |||
533 | ||||
534 | case e_slong: | |||
535 | switch (type) { | |||
536 | case e_void: | |||
537 | case e_sint: | |||
538 | case e_uint: | |||
539 | break; | |||
540 | case e_slong: | |||
541 | success = true; | |||
542 | break; | |||
543 | case e_ulong: | |||
544 | m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8); | |||
545 | success = true; | |||
546 | break; | |||
547 | ||||
548 | case e_slonglong: | |||
549 | m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8); | |||
550 | success = true; | |||
551 | break; | |||
552 | ||||
553 | case e_ulonglong: | |||
554 | m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8); | |||
555 | success = true; | |||
556 | break; | |||
557 | ||||
558 | case e_sint128: | |||
559 | case e_uint128: | |||
560 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128); | |||
561 | success = true; | |||
562 | break; | |||
563 | ||||
564 | case e_sint256: | |||
565 | case e_uint256: | |||
566 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256); | |||
567 | success = true; | |||
568 | break; | |||
569 | ||||
570 | case e_float: | |||
571 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
572 | m_float.convertFromAPInt(m_integer, true, | |||
573 | llvm::APFloat::rmNearestTiesToEven); | |||
574 | success = true; | |||
575 | break; | |||
576 | ||||
577 | case e_double: | |||
578 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
579 | m_float.convertFromAPInt(m_integer, true, | |||
580 | llvm::APFloat::rmNearestTiesToEven); | |||
581 | success = true; | |||
582 | break; | |||
583 | ||||
584 | case e_long_double: | |||
585 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
586 | : llvm::APFloat::x87DoubleExtended()); | |||
587 | m_float.convertFromAPInt(m_integer, true, | |||
588 | llvm::APFloat::rmNearestTiesToEven); | |||
589 | success = true; | |||
590 | break; | |||
591 | } | |||
592 | break; | |||
593 | ||||
594 | case e_ulong: | |||
595 | switch (type) { | |||
596 | case e_void: | |||
597 | case e_sint: | |||
598 | case e_uint: | |||
599 | case e_slong: | |||
600 | break; | |||
601 | case e_ulong: | |||
602 | success = true; | |||
603 | break; | |||
604 | case e_slonglong: | |||
605 | m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8); | |||
606 | success = true; | |||
607 | break; | |||
608 | ||||
609 | case e_ulonglong: | |||
610 | m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8); | |||
611 | success = true; | |||
612 | break; | |||
613 | ||||
614 | case e_sint128: | |||
615 | case e_uint128: | |||
616 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128); | |||
617 | success = true; | |||
618 | break; | |||
619 | ||||
620 | case e_sint256: | |||
621 | case e_uint256: | |||
622 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256); | |||
623 | success = true; | |||
624 | break; | |||
625 | ||||
626 | case e_float: | |||
627 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
628 | m_float.convertFromAPInt(m_integer, false, | |||
629 | llvm::APFloat::rmNearestTiesToEven); | |||
630 | success = true; | |||
631 | break; | |||
632 | ||||
633 | case e_double: | |||
634 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
635 | m_float.convertFromAPInt(m_integer, false, | |||
636 | llvm::APFloat::rmNearestTiesToEven); | |||
637 | success = true; | |||
638 | break; | |||
639 | ||||
640 | case e_long_double: | |||
641 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
642 | : llvm::APFloat::x87DoubleExtended()); | |||
643 | m_float.convertFromAPInt(m_integer, false, | |||
644 | llvm::APFloat::rmNearestTiesToEven); | |||
645 | success = true; | |||
646 | break; | |||
647 | } | |||
648 | break; | |||
649 | ||||
650 | case e_slonglong: | |||
651 | switch (type) { | |||
652 | case e_void: | |||
653 | case e_sint: | |||
654 | case e_uint: | |||
655 | case e_slong: | |||
656 | case e_ulong: | |||
657 | break; | |||
658 | case e_slonglong: | |||
659 | success = true; | |||
660 | break; | |||
661 | case e_ulonglong: | |||
662 | m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8); | |||
663 | success = true; | |||
664 | break; | |||
665 | ||||
666 | case e_sint128: | |||
667 | case e_uint128: | |||
668 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128); | |||
669 | success = true; | |||
670 | break; | |||
671 | ||||
672 | case e_sint256: | |||
673 | case e_uint256: | |||
674 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256); | |||
675 | success = true; | |||
676 | break; | |||
677 | ||||
678 | case e_float: | |||
679 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
680 | m_float.convertFromAPInt(m_integer, true, | |||
681 | llvm::APFloat::rmNearestTiesToEven); | |||
682 | success = true; | |||
683 | break; | |||
684 | ||||
685 | case e_double: | |||
686 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
687 | m_float.convertFromAPInt(m_integer, true, | |||
688 | llvm::APFloat::rmNearestTiesToEven); | |||
689 | success = true; | |||
690 | break; | |||
691 | ||||
692 | case e_long_double: | |||
693 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
694 | : llvm::APFloat::x87DoubleExtended()); | |||
695 | m_float.convertFromAPInt(m_integer, true, | |||
696 | llvm::APFloat::rmNearestTiesToEven); | |||
697 | success = true; | |||
698 | break; | |||
699 | } | |||
700 | break; | |||
701 | ||||
702 | case e_ulonglong: | |||
703 | switch (type) { | |||
704 | case e_void: | |||
705 | case e_sint: | |||
706 | case e_uint: | |||
707 | case e_slong: | |||
708 | case e_ulong: | |||
709 | case e_slonglong: | |||
710 | break; | |||
711 | case e_ulonglong: | |||
712 | success = true; | |||
713 | break; | |||
714 | case e_sint128: | |||
715 | case e_uint128: | |||
716 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128); | |||
717 | success = true; | |||
718 | break; | |||
719 | ||||
720 | case e_sint256: | |||
721 | case e_uint256: | |||
722 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256); | |||
723 | success = true; | |||
724 | break; | |||
725 | ||||
726 | case e_float: | |||
727 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
728 | m_float.convertFromAPInt(m_integer, false, | |||
729 | llvm::APFloat::rmNearestTiesToEven); | |||
730 | success = true; | |||
731 | break; | |||
732 | ||||
733 | case e_double: | |||
734 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
735 | m_float.convertFromAPInt(m_integer, false, | |||
736 | llvm::APFloat::rmNearestTiesToEven); | |||
737 | success = true; | |||
738 | break; | |||
739 | ||||
740 | case e_long_double: | |||
741 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
742 | : llvm::APFloat::x87DoubleExtended()); | |||
743 | m_float.convertFromAPInt(m_integer, false, | |||
744 | llvm::APFloat::rmNearestTiesToEven); | |||
745 | success = true; | |||
746 | break; | |||
747 | } | |||
748 | break; | |||
749 | ||||
750 | case e_sint128: | |||
751 | switch (type) { | |||
752 | case e_void: | |||
753 | case e_sint: | |||
754 | case e_uint: | |||
755 | case e_slong: | |||
756 | case e_ulong: | |||
757 | case e_slonglong: | |||
758 | case e_ulonglong: | |||
759 | break; | |||
760 | case e_sint128: | |||
761 | success = true; | |||
762 | break; | |||
763 | case e_uint128: | |||
764 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128); | |||
765 | success = true; | |||
766 | break; | |||
767 | ||||
768 | case e_sint256: | |||
769 | case e_uint256: | |||
770 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256); | |||
771 | success = true; | |||
772 | break; | |||
773 | ||||
774 | case e_float: | |||
775 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
776 | m_float.convertFromAPInt(m_integer, true, | |||
777 | llvm::APFloat::rmNearestTiesToEven); | |||
778 | success = true; | |||
779 | break; | |||
780 | ||||
781 | case e_double: | |||
782 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
783 | m_float.convertFromAPInt(m_integer, true, | |||
784 | llvm::APFloat::rmNearestTiesToEven); | |||
785 | success = true; | |||
786 | break; | |||
787 | ||||
788 | case e_long_double: | |||
789 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
790 | : llvm::APFloat::x87DoubleExtended()); | |||
791 | m_float.convertFromAPInt(m_integer, true, | |||
792 | llvm::APFloat::rmNearestTiesToEven); | |||
793 | success = true; | |||
794 | break; | |||
795 | } | |||
796 | break; | |||
797 | ||||
798 | case e_uint128: | |||
799 | switch (type) { | |||
800 | case e_void: | |||
801 | case e_sint: | |||
802 | case e_uint: | |||
803 | case e_slong: | |||
804 | case e_ulong: | |||
805 | case e_slonglong: | |||
806 | case e_ulonglong: | |||
807 | case e_sint128: | |||
808 | break; | |||
809 | case e_uint128: | |||
810 | success = true; | |||
811 | break; | |||
812 | case e_sint256: | |||
813 | case e_uint256: | |||
814 | m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256); | |||
815 | success = true; | |||
816 | break; | |||
817 | ||||
818 | case e_float: | |||
819 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
820 | m_float.convertFromAPInt(m_integer, false, | |||
821 | llvm::APFloat::rmNearestTiesToEven); | |||
822 | success = true; | |||
823 | break; | |||
824 | ||||
825 | case e_double: | |||
826 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
827 | m_float.convertFromAPInt(m_integer, false, | |||
828 | llvm::APFloat::rmNearestTiesToEven); | |||
829 | success = true; | |||
830 | break; | |||
831 | ||||
832 | case e_long_double: | |||
833 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
834 | : llvm::APFloat::x87DoubleExtended()); | |||
835 | m_float.convertFromAPInt(m_integer, false, | |||
836 | llvm::APFloat::rmNearestTiesToEven); | |||
837 | success = true; | |||
838 | break; | |||
839 | } | |||
840 | break; | |||
841 | ||||
842 | case e_sint256: | |||
843 | switch (type) { | |||
844 | case e_void: | |||
845 | case e_sint: | |||
846 | case e_uint: | |||
847 | case e_slong: | |||
848 | case e_ulong: | |||
849 | case e_slonglong: | |||
850 | case e_ulonglong: | |||
851 | case e_sint128: | |||
852 | case e_uint128: | |||
853 | break; | |||
854 | case e_sint256: | |||
855 | success = true; | |||
856 | break; | |||
857 | case e_uint256: | |||
858 | m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256); | |||
859 | success = true; | |||
860 | break; | |||
861 | ||||
862 | case e_float: | |||
863 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
864 | m_float.convertFromAPInt(m_integer, true, | |||
865 | llvm::APFloat::rmNearestTiesToEven); | |||
866 | success = true; | |||
867 | break; | |||
868 | ||||
869 | case e_double: | |||
870 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
871 | m_float.convertFromAPInt(m_integer, true, | |||
872 | llvm::APFloat::rmNearestTiesToEven); | |||
873 | success = true; | |||
874 | break; | |||
875 | ||||
876 | case e_long_double: | |||
877 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
878 | : llvm::APFloat::x87DoubleExtended()); | |||
879 | m_float.convertFromAPInt(m_integer, true, | |||
880 | llvm::APFloat::rmNearestTiesToEven); | |||
881 | success = true; | |||
882 | break; | |||
883 | } | |||
884 | break; | |||
885 | ||||
886 | case e_uint256: | |||
887 | switch (type) { | |||
888 | case e_void: | |||
889 | case e_sint: | |||
890 | case e_uint: | |||
891 | case e_slong: | |||
892 | case e_ulong: | |||
893 | case e_slonglong: | |||
894 | case e_ulonglong: | |||
895 | case e_sint128: | |||
896 | case e_uint128: | |||
897 | case e_sint256: | |||
898 | break; | |||
899 | case e_uint256: | |||
900 | success = true; | |||
901 | break; | |||
902 | case e_float: | |||
903 | m_float = llvm::APFloat(llvm::APFloat::IEEEsingle()); | |||
904 | m_float.convertFromAPInt(m_integer, false, | |||
905 | llvm::APFloat::rmNearestTiesToEven); | |||
906 | success = true; | |||
907 | break; | |||
908 | ||||
909 | case e_double: | |||
910 | m_float = llvm::APFloat(llvm::APFloat::IEEEdouble()); | |||
911 | m_float.convertFromAPInt(m_integer, false, | |||
912 | llvm::APFloat::rmNearestTiesToEven); | |||
913 | success = true; | |||
914 | break; | |||
915 | ||||
916 | case e_long_double: | |||
917 | m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
918 | : llvm::APFloat::x87DoubleExtended()); | |||
919 | m_float.convertFromAPInt(m_integer, false, | |||
920 | llvm::APFloat::rmNearestTiesToEven); | |||
921 | success = true; | |||
922 | break; | |||
923 | } | |||
924 | break; | |||
925 | ||||
926 | case e_float: | |||
927 | switch (type) { | |||
928 | case e_void: | |||
929 | case e_sint: | |||
930 | case e_uint: | |||
931 | case e_slong: | |||
932 | case e_ulong: | |||
933 | case e_slonglong: | |||
934 | case e_ulonglong: | |||
935 | case e_sint128: | |||
936 | case e_uint128: | |||
937 | case e_sint256: | |||
938 | case e_uint256: | |||
939 | break; | |||
940 | case e_float: | |||
941 | success = true; | |||
942 | break; | |||
943 | case e_double: | |||
944 | m_float = llvm::APFloat((double_t)m_float.convertToFloat()); | |||
945 | success = true; | |||
946 | break; | |||
947 | ||||
948 | case e_long_double: { | |||
949 | bool ignore; | |||
950 | m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
951 | : llvm::APFloat::x87DoubleExtended(), | |||
952 | llvm::APFloat::rmNearestTiesToEven, &ignore); | |||
953 | success = true; | |||
954 | break; | |||
955 | } | |||
956 | } | |||
957 | break; | |||
958 | ||||
959 | case e_double: | |||
960 | switch (type) { | |||
961 | case e_void: | |||
962 | case e_sint: | |||
963 | case e_uint: | |||
964 | case e_slong: | |||
965 | case e_ulong: | |||
966 | case e_slonglong: | |||
967 | case e_ulonglong: | |||
968 | case e_sint128: | |||
969 | case e_uint128: | |||
970 | case e_sint256: | |||
971 | case e_uint256: | |||
972 | case e_float: | |||
973 | break; | |||
974 | case e_double: | |||
975 | success = true; | |||
976 | break; | |||
977 | case e_long_double: { | |||
978 | bool ignore; | |||
979 | m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad() | |||
980 | : llvm::APFloat::x87DoubleExtended(), | |||
981 | llvm::APFloat::rmNearestTiesToEven, &ignore); | |||
982 | success = true; | |||
983 | break; | |||
984 | } | |||
985 | } | |||
986 | break; | |||
987 | ||||
988 | case e_long_double: | |||
989 | switch (type) { | |||
990 | case e_void: | |||
991 | case e_sint: | |||
992 | case e_uint: | |||
993 | case e_slong: | |||
994 | case e_ulong: | |||
995 | case e_slonglong: | |||
996 | case e_ulonglong: | |||
997 | case e_sint128: | |||
998 | case e_uint128: | |||
999 | case e_sint256: | |||
1000 | case e_uint256: | |||
1001 | case e_float: | |||
1002 | case e_double: | |||
1003 | break; | |||
1004 | case e_long_double: | |||
1005 | success = true; | |||
1006 | break; | |||
1007 | } | |||
1008 | break; | |||
1009 | } | |||
1010 | ||||
1011 | if (success) | |||
1012 | m_type = type; | |||
1013 | return success; | |||
1014 | } | |||
1015 | ||||
1016 | const char *Scalar::GetValueTypeAsCString(Scalar::Type type) { | |||
1017 | switch (type) { | |||
1018 | case e_void: | |||
1019 | return "void"; | |||
1020 | case e_sint: | |||
1021 | return "int"; | |||
1022 | case e_uint: | |||
1023 | return "unsigned int"; | |||
1024 | case e_slong: | |||
1025 | return "long"; | |||
1026 | case e_ulong: | |||
1027 | return "unsigned long"; | |||
1028 | case e_slonglong: | |||
1029 | return "long long"; | |||
1030 | case e_ulonglong: | |||
1031 | return "unsigned long long"; | |||
1032 | case e_float: | |||
1033 | return "float"; | |||
1034 | case e_double: | |||
1035 | return "double"; | |||
1036 | case e_long_double: | |||
1037 | return "long double"; | |||
1038 | case e_sint128: | |||
1039 | return "int128_t"; | |||
1040 | case e_uint128: | |||
1041 | return "uint128_t"; | |||
1042 | case e_sint256: | |||
1043 | return "int256_t"; | |||
1044 | case e_uint256: | |||
1045 | return "uint256_t"; | |||
1046 | } | |||
1047 | return "???"; | |||
1048 | } | |||
1049 | ||||
1050 | Scalar::Type | |||
1051 | Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) { | |||
1052 | if (byte_size <= sizeof(sint_t)) | |||
1053 | return e_sint; | |||
1054 | if (byte_size <= sizeof(slong_t)) | |||
1055 | return e_slong; | |||
1056 | if (byte_size <= sizeof(slonglong_t)) | |||
1057 | return e_slonglong; | |||
1058 | return e_void; | |||
1059 | } | |||
1060 | ||||
1061 | Scalar::Type | |||
1062 | Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) { | |||
1063 | if (byte_size <= sizeof(uint_t)) | |||
1064 | return e_uint; | |||
1065 | if (byte_size <= sizeof(ulong_t)) | |||
1066 | return e_ulong; | |||
1067 | if (byte_size <= sizeof(ulonglong_t)) | |||
1068 | return e_ulonglong; | |||
1069 | return e_void; | |||
1070 | } | |||
1071 | ||||
1072 | Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) { | |||
1073 | if (byte_size == sizeof(float_t)) | |||
1074 | return e_float; | |||
1075 | if (byte_size == sizeof(double_t)) | |||
1076 | return e_double; | |||
1077 | if (byte_size == sizeof(long_double_t)) | |||
1078 | return e_long_double; | |||
1079 | return e_void; | |||
1080 | } | |||
1081 | ||||
1082 | bool Scalar::MakeSigned() { | |||
1083 | bool success = false; | |||
1084 | ||||
1085 | switch (m_type) { | |||
1086 | case e_void: | |||
1087 | break; | |||
1088 | case e_sint: | |||
1089 | success = true; | |||
1090 | break; | |||
1091 | case e_uint: | |||
1092 | m_type = e_sint; | |||
1093 | success = true; | |||
1094 | break; | |||
1095 | case e_slong: | |||
1096 | success = true; | |||
1097 | break; | |||
1098 | case e_ulong: | |||
1099 | m_type = e_slong; | |||
1100 | success = true; | |||
1101 | break; | |||
1102 | case e_slonglong: | |||
1103 | success = true; | |||
1104 | break; | |||
1105 | case e_ulonglong: | |||
1106 | m_type = e_slonglong; | |||
1107 | success = true; | |||
1108 | break; | |||
1109 | case e_sint128: | |||
1110 | success = true; | |||
1111 | break; | |||
1112 | case e_uint128: | |||
1113 | m_type = e_sint128; | |||
1114 | success = true; | |||
1115 | break; | |||
1116 | case e_sint256: | |||
1117 | success = true; | |||
1118 | break; | |||
1119 | case e_uint256: | |||
1120 | m_type = e_sint256; | |||
1121 | success = true; | |||
1122 | break; | |||
1123 | case e_float: | |||
1124 | success = true; | |||
1125 | break; | |||
1126 | case e_double: | |||
1127 | success = true; | |||
1128 | break; | |||
1129 | case e_long_double: | |||
1130 | success = true; | |||
1131 | break; | |||
1132 | } | |||
1133 | ||||
1134 | return success; | |||
1135 | } | |||
1136 | ||||
1137 | bool Scalar::MakeUnsigned() { | |||
1138 | bool success = false; | |||
1139 | ||||
1140 | switch (m_type) { | |||
1141 | case e_void: | |||
1142 | break; | |||
1143 | case e_sint: | |||
1144 | m_type = e_uint; | |||
1145 | success = true; | |||
1146 | break; | |||
1147 | case e_uint: | |||
1148 | success = true; | |||
1149 | break; | |||
1150 | case e_slong: | |||
1151 | m_type = e_ulong; | |||
1152 | success = true; | |||
1153 | break; | |||
1154 | case e_ulong: | |||
1155 | success = true; | |||
1156 | break; | |||
1157 | case e_slonglong: | |||
1158 | m_type = e_ulonglong; | |||
1159 | success = true; | |||
1160 | break; | |||
1161 | case e_ulonglong: | |||
1162 | success = true; | |||
1163 | break; | |||
1164 | case e_sint128: | |||
1165 | m_type = e_uint128; | |||
1166 | success = true; | |||
1167 | break; | |||
1168 | case e_uint128: | |||
1169 | success = true; | |||
1170 | break; | |||
1171 | case e_sint256: | |||
1172 | m_type = e_uint256; | |||
1173 | success = true; | |||
1174 | break; | |||
1175 | case e_uint256: | |||
1176 | success = true; | |||
1177 | break; | |||
1178 | case e_float: | |||
1179 | success = true; | |||
1180 | break; | |||
1181 | case e_double: | |||
1182 | success = true; | |||
1183 | break; | |||
1184 | case e_long_double: | |||
1185 | success = true; | |||
1186 | break; | |||
1187 | } | |||
1188 | ||||
1189 | return success; | |||
1190 | } | |||
1191 | ||||
1192 | signed char Scalar::SChar(char fail_value) const { | |||
1193 | switch (m_type) { | |||
1194 | case e_void: | |||
1195 | break; | |||
1196 | case e_sint: | |||
1197 | case e_uint: | |||
1198 | case e_slong: | |||
1199 | case e_ulong: | |||
1200 | case e_slonglong: | |||
1201 | case e_ulonglong: | |||
1202 | case e_sint128: | |||
1203 | case e_uint128: | |||
1204 | case e_sint256: | |||
1205 | case e_uint256: | |||
1206 | return (schar_t)(m_integer.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue(); | |||
1207 | case e_float: | |||
1208 | return (schar_t)m_float.convertToFloat(); | |||
1209 | case e_double: | |||
1210 | return (schar_t)m_float.convertToDouble(); | |||
1211 | case e_long_double: | |||
1212 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1213 | return (schar_t)(ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue(); | |||
1214 | } | |||
1215 | return fail_value; | |||
1216 | } | |||
1217 | ||||
1218 | unsigned char Scalar::UChar(unsigned char fail_value) const { | |||
1219 | switch (m_type) { | |||
1220 | case e_void: | |||
1221 | break; | |||
1222 | case e_sint: | |||
1223 | case e_uint: | |||
1224 | case e_slong: | |||
1225 | case e_ulong: | |||
1226 | case e_slonglong: | |||
1227 | case e_ulonglong: | |||
1228 | case e_sint128: | |||
1229 | case e_uint128: | |||
1230 | case e_sint256: | |||
1231 | case e_uint256: | |||
1232 | return (uchar_t)(m_integer.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue(); | |||
1233 | case e_float: | |||
1234 | return (uchar_t)m_float.convertToFloat(); | |||
1235 | case e_double: | |||
1236 | return (uchar_t)m_float.convertToDouble(); | |||
1237 | case e_long_double: | |||
1238 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1239 | return (uchar_t)(ldbl_val.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue(); | |||
1240 | } | |||
1241 | return fail_value; | |||
1242 | } | |||
1243 | ||||
1244 | short Scalar::SShort(short fail_value) const { | |||
1245 | switch (m_type) { | |||
1246 | case e_void: | |||
1247 | break; | |||
1248 | case e_sint: | |||
1249 | case e_uint: | |||
1250 | case e_slong: | |||
1251 | case e_ulong: | |||
1252 | case e_slonglong: | |||
1253 | case e_ulonglong: | |||
1254 | case e_sint128: | |||
1255 | case e_uint128: | |||
1256 | case e_sint256: | |||
1257 | case e_uint256: | |||
1258 | return (sshort_t)(m_integer.sextOrTrunc(sizeof(sshort_t) * 8)) | |||
1259 | .getSExtValue(); | |||
1260 | case e_float: | |||
1261 | return (sshort_t)m_float.convertToFloat(); | |||
1262 | case e_double: | |||
1263 | return (sshort_t)m_float.convertToDouble(); | |||
1264 | case e_long_double: | |||
1265 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1266 | return (sshort_t)(ldbl_val.sextOrTrunc(sizeof(sshort_t) * 8)) | |||
1267 | .getSExtValue(); | |||
1268 | } | |||
1269 | return fail_value; | |||
1270 | } | |||
1271 | ||||
1272 | unsigned short Scalar::UShort(unsigned short fail_value) const { | |||
1273 | switch (m_type) { | |||
1274 | case e_void: | |||
1275 | break; | |||
1276 | case e_sint: | |||
1277 | case e_uint: | |||
1278 | case e_slong: | |||
1279 | case e_ulong: | |||
1280 | case e_slonglong: | |||
1281 | case e_ulonglong: | |||
1282 | case e_sint128: | |||
1283 | case e_uint128: | |||
1284 | case e_sint256: | |||
1285 | case e_uint256: | |||
1286 | return (ushort_t)(m_integer.zextOrTrunc(sizeof(ushort_t) * 8)) | |||
1287 | .getZExtValue(); | |||
1288 | case e_float: | |||
1289 | return (ushort_t)m_float.convertToFloat(); | |||
1290 | case e_double: | |||
1291 | return (ushort_t)m_float.convertToDouble(); | |||
1292 | case e_long_double: | |||
1293 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1294 | return (ushort_t)(ldbl_val.zextOrTrunc(sizeof(ushort_t) * 8)) | |||
1295 | .getZExtValue(); | |||
1296 | } | |||
1297 | return fail_value; | |||
1298 | } | |||
1299 | ||||
1300 | int Scalar::SInt(int fail_value) const { | |||
1301 | switch (m_type) { | |||
1302 | case e_void: | |||
1303 | break; | |||
1304 | case e_sint: | |||
1305 | case e_uint: | |||
1306 | case e_slong: | |||
1307 | case e_ulong: | |||
1308 | case e_slonglong: | |||
1309 | case e_ulonglong: | |||
1310 | case e_sint128: | |||
1311 | case e_uint128: | |||
1312 | case e_sint256: | |||
1313 | case e_uint256: | |||
1314 | return (sint_t)(m_integer.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue(); | |||
1315 | case e_float: | |||
1316 | return (sint_t)m_float.convertToFloat(); | |||
1317 | case e_double: | |||
1318 | return (sint_t)m_float.convertToDouble(); | |||
1319 | case e_long_double: | |||
1320 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1321 | return (sint_t)(ldbl_val.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue(); | |||
1322 | } | |||
1323 | return fail_value; | |||
1324 | } | |||
1325 | ||||
1326 | unsigned int Scalar::UInt(unsigned int fail_value) const { | |||
1327 | switch (m_type) { | |||
1328 | case e_void: | |||
1329 | break; | |||
1330 | case e_sint: | |||
1331 | case e_uint: | |||
1332 | case e_slong: | |||
1333 | case e_ulong: | |||
1334 | case e_slonglong: | |||
1335 | case e_ulonglong: | |||
1336 | case e_sint128: | |||
1337 | case e_uint128: | |||
1338 | case e_sint256: | |||
1339 | case e_uint256: | |||
1340 | return (uint_t)(m_integer.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue(); | |||
1341 | case e_float: | |||
1342 | return (uint_t)m_float.convertToFloat(); | |||
1343 | case e_double: | |||
1344 | return (uint_t)m_float.convertToDouble(); | |||
1345 | case e_long_double: | |||
1346 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1347 | return (uint_t)(ldbl_val.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue(); | |||
1348 | } | |||
1349 | return fail_value; | |||
1350 | } | |||
1351 | ||||
1352 | long Scalar::SLong(long fail_value) const { | |||
1353 | switch (m_type) { | |||
1354 | case e_void: | |||
1355 | break; | |||
1356 | case e_sint: | |||
1357 | case e_uint: | |||
1358 | case e_slong: | |||
1359 | case e_ulong: | |||
1360 | case e_slonglong: | |||
1361 | case e_ulonglong: | |||
1362 | case e_sint128: | |||
1363 | case e_uint128: | |||
1364 | case e_sint256: | |||
1365 | case e_uint256: | |||
1366 | return (slong_t)(m_integer.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue(); | |||
1367 | case e_float: | |||
1368 | return (slong_t)m_float.convertToFloat(); | |||
1369 | case e_double: | |||
1370 | return (slong_t)m_float.convertToDouble(); | |||
1371 | case e_long_double: | |||
1372 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1373 | return (slong_t)(ldbl_val.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue(); | |||
1374 | } | |||
1375 | return fail_value; | |||
1376 | } | |||
1377 | ||||
1378 | unsigned long Scalar::ULong(unsigned long fail_value) const { | |||
1379 | switch (m_type) { | |||
1380 | case e_void: | |||
1381 | break; | |||
1382 | case e_sint: | |||
1383 | case e_uint: | |||
1384 | case e_slong: | |||
1385 | case e_ulong: | |||
1386 | case e_slonglong: | |||
1387 | case e_ulonglong: | |||
1388 | case e_sint128: | |||
1389 | case e_uint128: | |||
1390 | case e_sint256: | |||
1391 | case e_uint256: | |||
1392 | return (ulong_t)(m_integer.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue(); | |||
1393 | case e_float: | |||
1394 | return (ulong_t)m_float.convertToFloat(); | |||
1395 | case e_double: | |||
1396 | return (ulong_t)m_float.convertToDouble(); | |||
1397 | case e_long_double: | |||
1398 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1399 | return (ulong_t)(ldbl_val.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue(); | |||
1400 | } | |||
1401 | return fail_value; | |||
1402 | } | |||
1403 | ||||
1404 | long long Scalar::SLongLong(long long fail_value) const { | |||
1405 | switch (m_type) { | |||
1406 | case e_void: | |||
1407 | break; | |||
1408 | case e_sint: | |||
1409 | case e_uint: | |||
1410 | case e_slong: | |||
1411 | case e_ulong: | |||
1412 | case e_slonglong: | |||
1413 | case e_ulonglong: | |||
1414 | case e_sint128: | |||
1415 | case e_uint128: | |||
1416 | case e_sint256: | |||
1417 | case e_uint256: | |||
1418 | return (slonglong_t)(m_integer.sextOrTrunc(sizeof(slonglong_t) * 8)) | |||
1419 | .getSExtValue(); | |||
1420 | case e_float: | |||
1421 | return (slonglong_t)m_float.convertToFloat(); | |||
1422 | case e_double: | |||
1423 | return (slonglong_t)m_float.convertToDouble(); | |||
1424 | case e_long_double: | |||
1425 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1426 | return (slonglong_t)(ldbl_val.sextOrTrunc(sizeof(slonglong_t) * 8)) | |||
1427 | .getSExtValue(); | |||
1428 | } | |||
1429 | return fail_value; | |||
1430 | } | |||
1431 | ||||
1432 | unsigned long long Scalar::ULongLong(unsigned long long fail_value) const { | |||
1433 | switch (m_type) { | |||
1434 | case e_void: | |||
1435 | break; | |||
1436 | case e_sint: | |||
1437 | case e_uint: | |||
1438 | case e_slong: | |||
1439 | case e_ulong: | |||
1440 | case e_slonglong: | |||
1441 | case e_ulonglong: | |||
1442 | case e_sint128: | |||
1443 | case e_uint128: | |||
1444 | case e_sint256: | |||
1445 | case e_uint256: | |||
1446 | return (ulonglong_t)(m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8)) | |||
1447 | .getZExtValue(); | |||
1448 | case e_float: | |||
1449 | return (ulonglong_t)m_float.convertToFloat(); | |||
1450 | case e_double: { | |||
1451 | double d_val = m_float.convertToDouble(); | |||
1452 | llvm::APInt rounded_double = | |||
1453 | llvm::APIntOps::RoundDoubleToAPInt(d_val, sizeof(ulonglong_t) * 8); | |||
1454 | return (ulonglong_t)(rounded_double.zextOrTrunc(sizeof(ulonglong_t) * 8)) | |||
1455 | .getZExtValue(); | |||
1456 | } | |||
1457 | case e_long_double: | |||
1458 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1459 | return (ulonglong_t)(ldbl_val.zextOrTrunc(sizeof(ulonglong_t) * 8)) | |||
1460 | .getZExtValue(); | |||
1461 | } | |||
1462 | return fail_value; | |||
1463 | } | |||
1464 | ||||
1465 | llvm::APInt Scalar::SInt128(llvm::APInt &fail_value) const { | |||
1466 | switch (m_type) { | |||
1467 | case e_void: | |||
1468 | break; | |||
1469 | case e_sint: | |||
1470 | case e_uint: | |||
1471 | case e_slong: | |||
1472 | case e_ulong: | |||
1473 | case e_slonglong: | |||
1474 | case e_ulonglong: | |||
1475 | case e_sint128: | |||
1476 | case e_uint128: | |||
1477 | case e_sint256: | |||
1478 | case e_uint256: | |||
1479 | return m_integer; | |||
1480 | case e_float: | |||
1481 | case e_double: | |||
1482 | case e_long_double: | |||
1483 | return m_float.bitcastToAPInt(); | |||
1484 | } | |||
1485 | return fail_value; | |||
1486 | } | |||
1487 | ||||
1488 | llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const { | |||
1489 | switch (m_type) { | |||
1490 | case e_void: | |||
1491 | break; | |||
1492 | case e_sint: | |||
1493 | case e_uint: | |||
1494 | case e_slong: | |||
1495 | case e_ulong: | |||
1496 | case e_slonglong: | |||
1497 | case e_ulonglong: | |||
1498 | case e_sint128: | |||
1499 | case e_uint128: | |||
1500 | case e_sint256: | |||
1501 | case e_uint256: | |||
1502 | return m_integer; | |||
1503 | case e_float: | |||
1504 | case e_double: | |||
1505 | case e_long_double: | |||
1506 | return m_float.bitcastToAPInt(); | |||
1507 | } | |||
1508 | return fail_value; | |||
1509 | } | |||
1510 | ||||
1511 | llvm::APInt Scalar::SInt256(llvm::APInt &fail_value) const { | |||
1512 | switch (m_type) { | |||
1513 | case e_void: | |||
1514 | break; | |||
1515 | case e_sint: | |||
1516 | case e_uint: | |||
1517 | case e_slong: | |||
1518 | case e_ulong: | |||
1519 | case e_slonglong: | |||
1520 | case e_ulonglong: | |||
1521 | case e_sint128: | |||
1522 | case e_uint128: | |||
1523 | case e_sint256: | |||
1524 | case e_uint256: | |||
1525 | return m_integer; | |||
1526 | case e_float: | |||
1527 | case e_double: | |||
1528 | case e_long_double: | |||
1529 | return m_float.bitcastToAPInt(); | |||
1530 | } | |||
1531 | return fail_value; | |||
1532 | } | |||
1533 | ||||
1534 | llvm::APInt Scalar::UInt256(const llvm::APInt &fail_value) const { | |||
1535 | switch (m_type) { | |||
1536 | case e_void: | |||
1537 | break; | |||
1538 | case e_sint: | |||
1539 | case e_uint: | |||
1540 | case e_slong: | |||
1541 | case e_ulong: | |||
1542 | case e_slonglong: | |||
1543 | case e_ulonglong: | |||
1544 | case e_sint128: | |||
1545 | case e_uint128: | |||
1546 | case e_sint256: | |||
1547 | case e_uint256: | |||
1548 | return m_integer; | |||
1549 | case e_float: | |||
1550 | case e_double: | |||
1551 | case e_long_double: | |||
1552 | return m_float.bitcastToAPInt(); | |||
1553 | } | |||
1554 | return fail_value; | |||
1555 | } | |||
1556 | ||||
1557 | float Scalar::Float(float fail_value) const { | |||
1558 | switch (m_type) { | |||
1559 | case e_void: | |||
1560 | break; | |||
1561 | case e_sint: | |||
1562 | case e_uint: | |||
1563 | case e_slong: | |||
1564 | case e_ulong: | |||
1565 | case e_slonglong: | |||
1566 | case e_ulonglong: | |||
1567 | case e_sint128: | |||
1568 | case e_uint128: | |||
1569 | case e_sint256: | |||
1570 | case e_uint256: | |||
1571 | return llvm::APIntOps::RoundAPIntToFloat(m_integer); | |||
1572 | case e_float: | |||
1573 | return m_float.convertToFloat(); | |||
1574 | case e_double: | |||
1575 | return (float_t)m_float.convertToDouble(); | |||
1576 | case e_long_double: | |||
1577 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1578 | return ldbl_val.bitsToFloat(); | |||
1579 | } | |||
1580 | return fail_value; | |||
1581 | } | |||
1582 | ||||
1583 | double Scalar::Double(double fail_value) const { | |||
1584 | switch (m_type) { | |||
1585 | case e_void: | |||
1586 | break; | |||
1587 | case e_sint: | |||
1588 | case e_uint: | |||
1589 | case e_slong: | |||
1590 | case e_ulong: | |||
1591 | case e_slonglong: | |||
1592 | case e_ulonglong: | |||
1593 | case e_sint128: | |||
1594 | case e_uint128: | |||
1595 | case e_sint256: | |||
1596 | case e_uint256: | |||
1597 | return llvm::APIntOps::RoundAPIntToDouble(m_integer); | |||
1598 | case e_float: | |||
1599 | return (double_t)m_float.convertToFloat(); | |||
1600 | case e_double: | |||
1601 | return m_float.convertToDouble(); | |||
1602 | case e_long_double: | |||
1603 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1604 | return ldbl_val.bitsToFloat(); | |||
1605 | } | |||
1606 | return fail_value; | |||
1607 | } | |||
1608 | ||||
1609 | long double Scalar::LongDouble(long double fail_value) const { | |||
1610 | switch (m_type) { | |||
1611 | case e_void: | |||
1612 | break; | |||
1613 | case e_sint: | |||
1614 | case e_uint: | |||
1615 | case e_slong: | |||
1616 | case e_ulong: | |||
1617 | case e_slonglong: | |||
1618 | case e_ulonglong: | |||
1619 | case e_sint128: | |||
1620 | case e_uint128: | |||
1621 | case e_sint256: | |||
1622 | case e_uint256: | |||
1623 | return (long_double_t)llvm::APIntOps::RoundAPIntToDouble(m_integer); | |||
1624 | case e_float: | |||
1625 | return (long_double_t)m_float.convertToFloat(); | |||
1626 | case e_double: | |||
1627 | return (long_double_t)m_float.convertToDouble(); | |||
1628 | case e_long_double: | |||
1629 | llvm::APInt ldbl_val = m_float.bitcastToAPInt(); | |||
1630 | return (long_double_t)ldbl_val.bitsToDouble(); | |||
1631 | } | |||
1632 | return fail_value; | |||
1633 | } | |||
1634 | ||||
1635 | Scalar &Scalar::operator+=(const Scalar &rhs) { | |||
1636 | Scalar temp_value; | |||
1637 | const Scalar *a; | |||
1638 | const Scalar *b; | |||
1639 | if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) != | |||
1640 | Scalar::e_void) { | |||
1641 | switch (m_type) { | |||
1642 | case e_void: | |||
1643 | break; | |||
1644 | case e_sint: | |||
1645 | case e_uint: | |||
1646 | case e_slong: | |||
1647 | case e_ulong: | |||
1648 | case e_slonglong: | |||
1649 | case e_ulonglong: | |||
1650 | case e_sint128: | |||
1651 | case e_uint128: | |||
1652 | case e_sint256: | |||
1653 | case e_uint256: | |||
1654 | m_integer = a->m_integer + b->m_integer; | |||
1655 | break; | |||
1656 | ||||
1657 | case e_float: | |||
1658 | case e_double: | |||
1659 | case e_long_double: | |||
1660 | m_float = a->m_float + b->m_float; | |||
1661 | break; | |||
1662 | } | |||
1663 | } | |||
1664 | return *this; | |||
1665 | } | |||
1666 | ||||
1667 | Scalar &Scalar::operator<<=(const Scalar &rhs) { | |||
1668 | switch (m_type) { | |||
1669 | case e_void: | |||
1670 | case e_float: | |||
1671 | case e_double: | |||
1672 | case e_long_double: | |||
1673 | m_type = e_void; | |||
1674 | break; | |||
1675 | ||||
1676 | case e_sint: | |||
1677 | case e_uint: | |||
1678 | case e_slong: | |||
1679 | case e_ulong: | |||
1680 | case e_slonglong: | |||
1681 | case e_ulonglong: | |||
1682 | case e_sint128: | |||
1683 | case e_uint128: | |||
1684 | case e_sint256: | |||
1685 | case e_uint256: | |||
1686 | switch (rhs.m_type) { | |||
1687 | case e_void: | |||
1688 | case e_float: | |||
1689 | case e_double: | |||
1690 | case e_long_double: | |||
1691 | m_type = e_void; | |||
1692 | break; | |||
1693 | case e_sint: | |||
1694 | case e_uint: | |||
1695 | case e_slong: | |||
1696 | case e_ulong: | |||
1697 | case e_slonglong: | |||
1698 | case e_ulonglong: | |||
1699 | case e_sint128: | |||
1700 | case e_uint128: | |||
1701 | case e_sint256: | |||
1702 | case e_uint256: | |||
1703 | m_integer = m_integer << rhs.m_integer; | |||
1704 | break; | |||
1705 | } | |||
1706 | break; | |||
1707 | } | |||
1708 | return *this; | |||
1709 | } | |||
1710 | ||||
1711 | bool Scalar::ShiftRightLogical(const Scalar &rhs) { | |||
1712 | switch (m_type) { | |||
1713 | case e_void: | |||
1714 | case e_float: | |||
1715 | case e_double: | |||
1716 | case e_long_double: | |||
1717 | m_type = e_void; | |||
1718 | break; | |||
1719 | ||||
1720 | case e_sint: | |||
1721 | case e_uint: | |||
1722 | case e_slong: | |||
1723 | case e_ulong: | |||
1724 | case e_slonglong: | |||
1725 | case e_ulonglong: | |||
1726 | case e_sint128: | |||
1727 | case e_uint128: | |||
1728 | case e_sint256: | |||
1729 | case e_uint256: | |||
1730 | switch (rhs.m_type) { | |||
1731 | case e_void: | |||
1732 | case e_float: | |||
1733 | case e_double: | |||
1734 | case e_long_double: | |||
1735 | m_type = e_void; | |||
1736 | break; | |||
1737 | case e_sint: | |||
1738 | case e_uint: | |||
1739 | case e_slong: | |||
1740 | case e_ulong: | |||
1741 | case e_slonglong: | |||
1742 | case e_ulonglong: | |||
1743 | case e_sint128: | |||
1744 | case e_uint128: | |||
1745 | case e_sint256: | |||
1746 | case e_uint256: | |||
1747 | m_integer = m_integer.lshr(rhs.m_integer); | |||
1748 | break; | |||
1749 | } | |||
1750 | break; | |||
1751 | } | |||
1752 | return m_type != e_void; | |||
1753 | } | |||
1754 | ||||
1755 | Scalar &Scalar::operator>>=(const Scalar &rhs) { | |||
1756 | switch (m_type) { | |||
1757 | case e_void: | |||
1758 | case e_float: | |||
1759 | case e_double: | |||
1760 | case e_long_double: | |||
1761 | m_type = e_void; | |||
1762 | break; | |||
1763 | ||||
1764 | case e_sint: | |||
1765 | case e_uint: | |||
1766 | case e_slong: | |||
1767 | case e_ulong: | |||
1768 | case e_slonglong: | |||
1769 | case e_ulonglong: | |||
1770 | case e_sint128: | |||
1771 | case e_uint128: | |||
1772 | case e_sint256: | |||
1773 | case e_uint256: | |||
1774 | switch (rhs.m_type) { | |||
1775 | case e_void: | |||
1776 | case e_float: | |||
1777 | case e_double: | |||
1778 | case e_long_double: | |||
1779 | m_type = e_void; | |||
1780 | break; | |||
1781 | case e_sint: | |||
1782 | case e_uint: | |||
1783 | case e_slong: | |||
1784 | case e_ulong: | |||
1785 | case e_slonglong: | |||
1786 | case e_ulonglong: | |||
1787 | case e_sint128: | |||
1788 | case e_uint128: | |||
1789 | case e_sint256: | |||
1790 | case e_uint256: | |||
1791 | m_integer = m_integer.ashr(rhs.m_integer); | |||
1792 | break; | |||
1793 | } | |||
1794 | break; | |||
1795 | } | |||
1796 | return *this; | |||
1797 | } | |||
1798 | ||||
1799 | Scalar &Scalar::operator&=(const Scalar &rhs) { | |||
1800 | switch (m_type) { | |||
1801 | case e_void: | |||
1802 | case e_float: | |||
1803 | case e_double: | |||
1804 | case e_long_double: | |||
1805 | m_type = e_void; | |||
1806 | break; | |||
1807 | ||||
1808 | case e_sint: | |||
1809 | case e_uint: | |||
1810 | case e_slong: | |||
1811 | case e_ulong: | |||
1812 | case e_slonglong: | |||
1813 | case e_ulonglong: | |||
1814 | case e_sint128: | |||
1815 | case e_uint128: | |||
1816 | case e_sint256: | |||
1817 | case e_uint256: | |||
1818 | switch (rhs.m_type) { | |||
1819 | case e_void: | |||
1820 | case e_float: | |||
1821 | case e_double: | |||
1822 | case e_long_double: | |||
1823 | m_type = e_void; | |||
1824 | break; | |||
1825 | case e_sint: | |||
1826 | case e_uint: | |||
1827 | case e_slong: | |||
1828 | case e_ulong: | |||
1829 | case e_slonglong: | |||
1830 | case e_ulonglong: | |||
1831 | case e_sint128: | |||
1832 | case e_uint128: | |||
1833 | case e_sint256: | |||
1834 | case e_uint256: | |||
1835 | m_integer &= rhs.m_integer; | |||
1836 | break; | |||
1837 | } | |||
1838 | break; | |||
1839 | } | |||
1840 | return *this; | |||
1841 | } | |||
1842 | ||||
1843 | bool Scalar::AbsoluteValue() { | |||
1844 | switch (m_type) { | |||
1845 | case e_void: | |||
1846 | break; | |||
1847 | ||||
1848 | case e_sint: | |||
1849 | case e_slong: | |||
1850 | case e_slonglong: | |||
1851 | case e_sint128: | |||
1852 | case e_sint256: | |||
1853 | if (m_integer.isNegative()) | |||
1854 | m_integer = -m_integer; | |||
1855 | return true; | |||
1856 | ||||
1857 | case e_uint: | |||
1858 | case e_ulong: | |||
1859 | case e_ulonglong: | |||
1860 | return true; | |||
1861 | case e_uint128: | |||
1862 | case e_uint256: | |||
1863 | case e_float: | |||
1864 | case e_double: | |||
1865 | case e_long_double: | |||
1866 | m_float.clearSign(); | |||
1867 | return true; | |||
1868 | } | |||
1869 | return false; | |||
1870 | } | |||
1871 | ||||
1872 | bool Scalar::UnaryNegate() { | |||
1873 | switch (m_type) { | |||
1874 | case e_void: | |||
1875 | break; | |||
1876 | case e_sint: | |||
1877 | case e_uint: | |||
1878 | case e_slong: | |||
1879 | case e_ulong: | |||
1880 | case e_slonglong: | |||
1881 | case e_ulonglong: | |||
1882 | case e_sint128: | |||
1883 | case e_uint128: | |||
1884 | case e_sint256: | |||
1885 | case e_uint256: | |||
1886 | m_integer = -m_integer; | |||
1887 | return true; | |||
1888 | case e_float: | |||
1889 | case e_double: | |||
1890 | case e_long_double: | |||
1891 | m_float.changeSign(); | |||
1892 | return true; | |||
1893 | } | |||
1894 | return false; | |||
1895 | } | |||
1896 | ||||
1897 | bool Scalar::OnesComplement() { | |||
1898 | switch (m_type) { | |||
1899 | case e_sint: | |||
1900 | case e_uint: | |||
1901 | case e_slong: | |||
1902 | case e_ulong: | |||
1903 | case e_slonglong: | |||
1904 | case e_ulonglong: | |||
1905 | case e_sint128: | |||
1906 | case e_uint128: | |||
1907 | case e_sint256: | |||
1908 | case e_uint256: | |||
1909 | m_integer = ~m_integer; | |||
1910 | return true; | |||
1911 | ||||
1912 | case e_void: | |||
1913 | case e_float: | |||
1914 | case e_double: | |||
1915 | case e_long_double: | |||
1916 | break; | |||
1917 | } | |||
1918 | return false; | |||
1919 | } | |||
1920 | ||||
1921 | const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) { | |||
1922 | Scalar result; | |||
1923 | Scalar temp_value; | |||
1924 | const Scalar *a; | |||
1925 | const Scalar *b; | |||
1926 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
1927 | Scalar::e_void) { | |||
1928 | switch (result.m_type) { | |||
1929 | case Scalar::e_void: | |||
1930 | break; | |||
1931 | case Scalar::e_sint: | |||
1932 | case Scalar::e_uint: | |||
1933 | case Scalar::e_slong: | |||
1934 | case Scalar::e_ulong: | |||
1935 | case Scalar::e_slonglong: | |||
1936 | case Scalar::e_ulonglong: | |||
1937 | case Scalar::e_sint128: | |||
1938 | case Scalar::e_uint128: | |||
1939 | case Scalar::e_sint256: | |||
1940 | case Scalar::e_uint256: | |||
1941 | result.m_integer = a->m_integer + b->m_integer; | |||
1942 | break; | |||
1943 | case Scalar::e_float: | |||
1944 | case Scalar::e_double: | |||
1945 | case Scalar::e_long_double: | |||
1946 | result.m_float = a->m_float + b->m_float; | |||
1947 | break; | |||
1948 | } | |||
1949 | } | |||
1950 | return result; | |||
1951 | } | |||
1952 | ||||
1953 | const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) { | |||
1954 | Scalar result; | |||
1955 | Scalar temp_value; | |||
1956 | const Scalar *a; | |||
1957 | const Scalar *b; | |||
1958 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
1959 | Scalar::e_void) { | |||
1960 | switch (result.m_type) { | |||
1961 | case Scalar::e_void: | |||
1962 | break; | |||
1963 | case Scalar::e_sint: | |||
1964 | case Scalar::e_uint: | |||
1965 | case Scalar::e_slong: | |||
1966 | case Scalar::e_ulong: | |||
1967 | case Scalar::e_slonglong: | |||
1968 | case Scalar::e_ulonglong: | |||
1969 | case Scalar::e_sint128: | |||
1970 | case Scalar::e_uint128: | |||
1971 | case Scalar::e_sint256: | |||
1972 | case Scalar::e_uint256: | |||
1973 | result.m_integer = a->m_integer - b->m_integer; | |||
1974 | break; | |||
1975 | case Scalar::e_float: | |||
1976 | case Scalar::e_double: | |||
1977 | case Scalar::e_long_double: | |||
1978 | result.m_float = a->m_float - b->m_float; | |||
1979 | break; | |||
1980 | } | |||
1981 | } | |||
1982 | return result; | |||
1983 | } | |||
1984 | ||||
1985 | const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) { | |||
1986 | Scalar result; | |||
1987 | Scalar temp_value; | |||
1988 | const Scalar *a; | |||
1989 | const Scalar *b; | |||
1990 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
1991 | Scalar::e_void) { | |||
1992 | switch (result.m_type) { | |||
1993 | case Scalar::e_void: | |||
1994 | break; | |||
1995 | case Scalar::e_sint: | |||
1996 | case Scalar::e_slong: | |||
1997 | case Scalar::e_slonglong: | |||
1998 | case Scalar::e_sint128: | |||
1999 | case Scalar::e_sint256: | |||
2000 | if (b->m_integer != 0) { | |||
2001 | result.m_integer = a->m_integer.sdiv(b->m_integer); | |||
2002 | return result; | |||
2003 | } | |||
2004 | break; | |||
2005 | case Scalar::e_uint: | |||
2006 | case Scalar::e_ulong: | |||
2007 | case Scalar::e_ulonglong: | |||
2008 | case Scalar::e_uint128: | |||
2009 | case Scalar::e_uint256: | |||
2010 | if (b->m_integer != 0) { | |||
2011 | result.m_integer = a->m_integer.udiv(b->m_integer); | |||
2012 | return result; | |||
2013 | } | |||
2014 | break; | |||
2015 | case Scalar::e_float: | |||
2016 | case Scalar::e_double: | |||
2017 | case Scalar::e_long_double: | |||
2018 | if (!b->m_float.isZero()) { | |||
2019 | result.m_float = a->m_float / b->m_float; | |||
2020 | return result; | |||
2021 | } | |||
2022 | break; | |||
2023 | } | |||
2024 | } | |||
2025 | // For division only, the only way it should make it here is if a promotion | |||
2026 | // failed, or if we are trying to do a divide by zero. | |||
2027 | result.m_type = Scalar::e_void; | |||
2028 | return result; | |||
2029 | } | |||
2030 | ||||
2031 | const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) { | |||
2032 | Scalar result; | |||
2033 | Scalar temp_value; | |||
2034 | const Scalar *a; | |||
2035 | const Scalar *b; | |||
2036 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
2037 | Scalar::e_void) { | |||
2038 | switch (result.m_type) { | |||
2039 | case Scalar::e_void: | |||
2040 | break; | |||
2041 | case Scalar::e_sint: | |||
2042 | case Scalar::e_uint: | |||
2043 | case Scalar::e_slong: | |||
2044 | case Scalar::e_ulong: | |||
2045 | case Scalar::e_slonglong: | |||
2046 | case Scalar::e_ulonglong: | |||
2047 | case Scalar::e_sint128: | |||
2048 | case Scalar::e_uint128: | |||
2049 | case Scalar::e_sint256: | |||
2050 | case Scalar::e_uint256: | |||
2051 | result.m_integer = a->m_integer * b->m_integer; | |||
2052 | break; | |||
2053 | case Scalar::e_float: | |||
2054 | case Scalar::e_double: | |||
2055 | case Scalar::e_long_double: | |||
2056 | result.m_float = a->m_float * b->m_float; | |||
2057 | break; | |||
2058 | } | |||
2059 | } | |||
2060 | return result; | |||
2061 | } | |||
2062 | ||||
2063 | const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) { | |||
2064 | Scalar result; | |||
2065 | Scalar temp_value; | |||
2066 | const Scalar *a; | |||
2067 | const Scalar *b; | |||
2068 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
2069 | Scalar::e_void) { | |||
2070 | switch (result.m_type) { | |||
2071 | case Scalar::e_sint: | |||
2072 | case Scalar::e_uint: | |||
2073 | case Scalar::e_slong: | |||
2074 | case Scalar::e_ulong: | |||
2075 | case Scalar::e_slonglong: | |||
2076 | case Scalar::e_ulonglong: | |||
2077 | case Scalar::e_sint128: | |||
2078 | case Scalar::e_uint128: | |||
2079 | case Scalar::e_sint256: | |||
2080 | case Scalar::e_uint256: | |||
2081 | result.m_integer = a->m_integer & b->m_integer; | |||
2082 | break; | |||
2083 | case Scalar::e_void: | |||
2084 | case Scalar::e_float: | |||
2085 | case Scalar::e_double: | |||
2086 | case Scalar::e_long_double: | |||
2087 | // No bitwise AND on floats, doubles of long doubles | |||
2088 | result.m_type = Scalar::e_void; | |||
2089 | break; | |||
2090 | } | |||
2091 | } | |||
2092 | return result; | |||
2093 | } | |||
2094 | ||||
2095 | const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) { | |||
2096 | Scalar result; | |||
2097 | Scalar temp_value; | |||
2098 | const Scalar *a; | |||
2099 | const Scalar *b; | |||
2100 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
2101 | Scalar::e_void) { | |||
2102 | switch (result.m_type) { | |||
2103 | case Scalar::e_sint: | |||
2104 | case Scalar::e_uint: | |||
2105 | case Scalar::e_slong: | |||
2106 | case Scalar::e_ulong: | |||
2107 | case Scalar::e_slonglong: | |||
2108 | case Scalar::e_ulonglong: | |||
2109 | case Scalar::e_sint128: | |||
2110 | case Scalar::e_uint128: | |||
2111 | case Scalar::e_sint256: | |||
2112 | case Scalar::e_uint256: | |||
2113 | result.m_integer = a->m_integer | b->m_integer; | |||
2114 | break; | |||
2115 | ||||
2116 | case Scalar::e_void: | |||
2117 | case Scalar::e_float: | |||
2118 | case Scalar::e_double: | |||
2119 | case Scalar::e_long_double: | |||
2120 | // No bitwise AND on floats, doubles of long doubles | |||
2121 | result.m_type = Scalar::e_void; | |||
2122 | break; | |||
2123 | } | |||
2124 | } | |||
2125 | return result; | |||
2126 | } | |||
2127 | ||||
2128 | const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) { | |||
2129 | Scalar result; | |||
2130 | Scalar temp_value; | |||
2131 | const Scalar *a; | |||
2132 | const Scalar *b; | |||
2133 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
2134 | Scalar::e_void) { | |||
2135 | switch (result.m_type) { | |||
2136 | default: | |||
2137 | break; | |||
2138 | case Scalar::e_void: | |||
2139 | break; | |||
2140 | case Scalar::e_sint: | |||
2141 | case Scalar::e_slong: | |||
2142 | case Scalar::e_slonglong: | |||
2143 | case Scalar::e_sint128: | |||
2144 | case Scalar::e_sint256: | |||
2145 | if (b->m_integer != 0) { | |||
2146 | result.m_integer = a->m_integer.srem(b->m_integer); | |||
2147 | return result; | |||
2148 | } | |||
2149 | break; | |||
2150 | case Scalar::e_uint: | |||
2151 | case Scalar::e_ulong: | |||
2152 | case Scalar::e_ulonglong: | |||
2153 | case Scalar::e_uint128: | |||
2154 | case Scalar::e_uint256: | |||
2155 | if (b->m_integer != 0) { | |||
2156 | result.m_integer = a->m_integer.urem(b->m_integer); | |||
2157 | return result; | |||
2158 | } | |||
2159 | break; | |||
2160 | } | |||
2161 | } | |||
2162 | result.m_type = Scalar::e_void; | |||
2163 | return result; | |||
2164 | } | |||
2165 | ||||
2166 | const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) { | |||
2167 | Scalar result; | |||
2168 | Scalar temp_value; | |||
2169 | const Scalar *a; | |||
2170 | const Scalar *b; | |||
2171 | if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != | |||
2172 | Scalar::e_void) { | |||
2173 | switch (result.m_type) { | |||
2174 | case Scalar::e_sint: | |||
2175 | case Scalar::e_uint: | |||
2176 | case Scalar::e_slong: | |||
2177 | case Scalar::e_ulong: | |||
2178 | case Scalar::e_slonglong: | |||
2179 | case Scalar::e_ulonglong: | |||
2180 | case Scalar::e_sint128: | |||
2181 | case Scalar::e_uint128: | |||
2182 | case Scalar::e_sint256: | |||
2183 | case Scalar::e_uint256: | |||
2184 | result.m_integer = a->m_integer ^ b->m_integer; | |||
2185 | break; | |||
2186 | ||||
2187 | case Scalar::e_void: | |||
2188 | case Scalar::e_float: | |||
2189 | case Scalar::e_double: | |||
2190 | case Scalar::e_long_double: | |||
2191 | // No bitwise AND on floats, doubles of long doubles | |||
2192 | result.m_type = Scalar::e_void; | |||
2193 | break; | |||
2194 | } | |||
2195 | } | |||
2196 | return result; | |||
2197 | } | |||
2198 | ||||
2199 | const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) { | |||
2200 | Scalar result = lhs; | |||
2201 | result <<= rhs; | |||
2202 | return result; | |||
2203 | } | |||
2204 | ||||
2205 | const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) { | |||
2206 | Scalar result = lhs; | |||
2207 | result >>= rhs; | |||
2208 | return result; | |||
2209 | } | |||
2210 | ||||
2211 | Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding, | |||
2212 | size_t byte_size) { | |||
2213 | Status error; | |||
2214 | if (value_str == nullptr || value_str[0] == '\0') { | |||
2215 | error.SetErrorString("Invalid c-string value string."); | |||
2216 | return error; | |||
2217 | } | |||
2218 | switch (encoding) { | |||
2219 | case eEncodingInvalid: | |||
2220 | error.SetErrorString("Invalid encoding."); | |||
2221 | break; | |||
2222 | ||||
2223 | case eEncodingUint: | |||
2224 | if (byte_size <= sizeof(uint64_t)) { | |||
2225 | uint64_t uval64; | |||
2226 | if (!llvm::to_integer(value_str, uval64)) | |||
2227 | error.SetErrorStringWithFormat( | |||
2228 | "'%s' is not a valid unsigned integer string value", value_str); | |||
2229 | else if (!UIntValueIsValidForSize(uval64, byte_size)) | |||
2230 | error.SetErrorStringWithFormat("value 0x%" PRIx64"l" "x" | |||
2231 | " is too large to fit in a %" PRIu64"l" "u" | |||
2232 | " byte unsigned integer value", | |||
2233 | uval64, (uint64_t)byte_size); | |||
2234 | else { | |||
2235 | m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size); | |||
2236 | switch (m_type) { | |||
2237 | case e_uint: | |||
2238 | m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false); | |||
2239 | break; | |||
2240 | case e_ulong: | |||
2241 | m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false); | |||
2242 | break; | |||
2243 | case e_ulonglong: | |||
2244 | m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false); | |||
2245 | break; | |||
2246 | default: | |||
2247 | error.SetErrorStringWithFormat( | |||
2248 | "unsupported unsigned integer byte size: %" PRIu64"l" "u" "", | |||
2249 | (uint64_t)byte_size); | |||
2250 | break; | |||
2251 | } | |||
2252 | } | |||
2253 | } else { | |||
2254 | error.SetErrorStringWithFormat( | |||
2255 | "unsupported unsigned integer byte size: %" PRIu64"l" "u" "", | |||
2256 | (uint64_t)byte_size); | |||
2257 | return error; | |||
2258 | } | |||
2259 | break; | |||
2260 | ||||
2261 | case eEncodingSint: | |||
2262 | if (byte_size <= sizeof(int64_t)) { | |||
2263 | int64_t sval64; | |||
2264 | if (!llvm::to_integer(value_str, sval64)) | |||
2265 | error.SetErrorStringWithFormat( | |||
2266 | "'%s' is not a valid signed integer string value", value_str); | |||
2267 | else if (!SIntValueIsValidForSize(sval64, byte_size)) | |||
2268 | error.SetErrorStringWithFormat("value 0x%" PRIx64"l" "x" | |||
2269 | " is too large to fit in a %" PRIu64"l" "u" | |||
2270 | " byte signed integer value", | |||
2271 | sval64, (uint64_t)byte_size); | |||
2272 | else { | |||
2273 | m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size); | |||
2274 | switch (m_type) { | |||
2275 | case e_sint: | |||
2276 | m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true); | |||
2277 | break; | |||
2278 | case e_slong: | |||
2279 | m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true); | |||
2280 | break; | |||
2281 | case e_slonglong: | |||
2282 | m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true); | |||
2283 | break; | |||
2284 | default: | |||
2285 | error.SetErrorStringWithFormat( | |||
2286 | "unsupported signed integer byte size: %" PRIu64"l" "u" "", | |||
2287 | (uint64_t)byte_size); | |||
2288 | break; | |||
2289 | } | |||
2290 | } | |||
2291 | } else { | |||
2292 | error.SetErrorStringWithFormat( | |||
2293 | "unsupported signed integer byte size: %" PRIu64"l" "u" "", | |||
2294 | (uint64_t)byte_size); | |||
2295 | return error; | |||
2296 | } | |||
2297 | break; | |||
2298 | ||||
2299 | case eEncodingIEEE754: | |||
2300 | static float f_val; | |||
2301 | static double d_val; | |||
2302 | static long double l_val; | |||
2303 | if (byte_size == sizeof(float)) { | |||
2304 | if (::sscanf(value_str, "%f", &f_val) == 1) { | |||
2305 | m_float = llvm::APFloat(f_val); | |||
2306 | m_type = e_float; | |||
2307 | } else | |||
2308 | error.SetErrorStringWithFormat("'%s' is not a valid float string value", | |||
2309 | value_str); | |||
2310 | } else if (byte_size == sizeof(double)) { | |||
2311 | if (::sscanf(value_str, "%lf", &d_val) == 1) { | |||
2312 | m_float = llvm::APFloat(d_val); | |||
2313 | m_type = e_double; | |||
2314 | } else | |||
2315 | error.SetErrorStringWithFormat("'%s' is not a valid float string value", | |||
2316 | value_str); | |||
2317 | } else if (byte_size == sizeof(long double)) { | |||
2318 | if (::sscanf(value_str, "%Lf", &l_val) == 1) { | |||
2319 | m_float = | |||
2320 | llvm::APFloat(llvm::APFloat::x87DoubleExtended(), | |||
2321 | llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, | |||
2322 | ((type128 *)&l_val)->x)); | |||
2323 | m_type = e_long_double; | |||
2324 | } else | |||
2325 | error.SetErrorStringWithFormat("'%s' is not a valid float string value", | |||
2326 | value_str); | |||
2327 | } else { | |||
2328 | error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64"l" "u" "", | |||
2329 | (uint64_t)byte_size); | |||
2330 | return error; | |||
2331 | } | |||
2332 | break; | |||
2333 | ||||
2334 | case eEncodingVector: | |||
2335 | error.SetErrorString("vector encoding unsupported."); | |||
2336 | break; | |||
2337 | } | |||
2338 | if (error.Fail()) | |||
2339 | m_type = e_void; | |||
2340 | ||||
2341 | return error; | |||
2342 | } | |||
2343 | ||||
2344 | Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding, | |||
2345 | size_t byte_size) { | |||
2346 | Status error; | |||
2347 | ||||
2348 | type128 int128; | |||
2349 | type256 int256; | |||
2350 | switch (encoding) { | |||
2351 | case lldb::eEncodingInvalid: | |||
2352 | error.SetErrorString("invalid encoding"); | |||
2353 | break; | |||
2354 | case lldb::eEncodingVector: | |||
2355 | error.SetErrorString("vector encoding unsupported"); | |||
2356 | break; | |||
2357 | case lldb::eEncodingUint: { | |||
2358 | lldb::offset_t offset = 0; | |||
2359 | ||||
2360 | switch (byte_size) { | |||
2361 | case 1: | |||
2362 | operator=((uint8_t)data.GetU8(&offset)); | |||
2363 | break; | |||
2364 | case 2: | |||
2365 | operator=((uint16_t)data.GetU16(&offset)); | |||
2366 | break; | |||
2367 | case 4: | |||
2368 | operator=((uint32_t)data.GetU32(&offset)); | |||
2369 | break; | |||
2370 | case 8: | |||
2371 | operator=((uint64_t)data.GetU64(&offset)); | |||
2372 | break; | |||
2373 | case 16: | |||
2374 | if (data.GetByteOrder() == eByteOrderBig) { | |||
2375 | int128.x[1] = (uint64_t)data.GetU64(&offset); | |||
2376 | int128.x[0] = (uint64_t)data.GetU64(&offset); | |||
2377 | } else { | |||
2378 | int128.x[0] = (uint64_t)data.GetU64(&offset); | |||
2379 | int128.x[1] = (uint64_t)data.GetU64(&offset); | |||
2380 | } | |||
2381 | operator=(llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, int128.x)); | |||
2382 | break; | |||
2383 | case 32: | |||
2384 | if (data.GetByteOrder() == eByteOrderBig) { | |||
2385 | int256.x[3] = (uint64_t)data.GetU64(&offset); | |||
2386 | int256.x[2] = (uint64_t)data.GetU64(&offset); | |||
2387 | int256.x[1] = (uint64_t)data.GetU64(&offset); | |||
2388 | int256.x[0] = (uint64_t)data.GetU64(&offset); | |||
2389 | } else { | |||
2390 | int256.x[0] = (uint64_t)data.GetU64(&offset); | |||
2391 | int256.x[1] = (uint64_t)data.GetU64(&offset); | |||
2392 | int256.x[2] = (uint64_t)data.GetU64(&offset); | |||
2393 | int256.x[3] = (uint64_t)data.GetU64(&offset); | |||
2394 | } | |||
2395 | operator=(llvm::APInt(BITWIDTH_INT256256, NUM_OF_WORDS_INT2564, int256.x)); | |||
2396 | break; | |||
2397 | default: | |||
2398 | error.SetErrorStringWithFormat( | |||
2399 | "unsupported unsigned integer byte size: %" PRIu64"l" "u" "", | |||
2400 | (uint64_t)byte_size); | |||
2401 | break; | |||
2402 | } | |||
2403 | } break; | |||
2404 | case lldb::eEncodingSint: { | |||
2405 | lldb::offset_t offset = 0; | |||
2406 | ||||
2407 | switch (byte_size) { | |||
2408 | case 1: | |||
2409 | operator=((int8_t)data.GetU8(&offset)); | |||
2410 | break; | |||
2411 | case 2: | |||
2412 | operator=((int16_t)data.GetU16(&offset)); | |||
2413 | break; | |||
2414 | case 4: | |||
2415 | operator=((int32_t)data.GetU32(&offset)); | |||
2416 | break; | |||
2417 | case 8: | |||
2418 | operator=((int64_t)data.GetU64(&offset)); | |||
2419 | break; | |||
2420 | case 16: | |||
2421 | if (data.GetByteOrder() == eByteOrderBig) { | |||
2422 | int128.x[1] = (uint64_t)data.GetU64(&offset); | |||
2423 | int128.x[0] = (uint64_t)data.GetU64(&offset); | |||
2424 | } else { | |||
2425 | int128.x[0] = (uint64_t)data.GetU64(&offset); | |||
2426 | int128.x[1] = (uint64_t)data.GetU64(&offset); | |||
2427 | } | |||
2428 | operator=(llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, int128.x)); | |||
2429 | break; | |||
2430 | case 32: | |||
2431 | if (data.GetByteOrder() == eByteOrderBig) { | |||
2432 | int256.x[3] = (uint64_t)data.GetU64(&offset); | |||
2433 | int256.x[2] = (uint64_t)data.GetU64(&offset); | |||
2434 | int256.x[1] = (uint64_t)data.GetU64(&offset); | |||
2435 | int256.x[0] = (uint64_t)data.GetU64(&offset); | |||
2436 | } else { | |||
2437 | int256.x[0] = (uint64_t)data.GetU64(&offset); | |||
2438 | int256.x[1] = (uint64_t)data.GetU64(&offset); | |||
2439 | int256.x[2] = (uint64_t)data.GetU64(&offset); | |||
2440 | int256.x[3] = (uint64_t)data.GetU64(&offset); | |||
2441 | } | |||
2442 | operator=(llvm::APInt(BITWIDTH_INT256256, NUM_OF_WORDS_INT2564, int256.x)); | |||
2443 | break; | |||
2444 | default: | |||
2445 | error.SetErrorStringWithFormat( | |||
2446 | "unsupported signed integer byte size: %" PRIu64"l" "u" "", | |||
2447 | (uint64_t)byte_size); | |||
2448 | break; | |||
2449 | } | |||
2450 | } break; | |||
2451 | case lldb::eEncodingIEEE754: { | |||
2452 | lldb::offset_t offset = 0; | |||
2453 | ||||
2454 | if (byte_size == sizeof(float)) | |||
2455 | operator=((float)data.GetFloat(&offset)); | |||
2456 | else if (byte_size == sizeof(double)) | |||
2457 | operator=((double)data.GetDouble(&offset)); | |||
2458 | else if (byte_size == sizeof(long double)) | |||
2459 | operator=((long double)data.GetLongDouble(&offset)); | |||
2460 | else | |||
2461 | error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64"l" "u" "", | |||
2462 | (uint64_t)byte_size); | |||
2463 | } break; | |||
2464 | } | |||
2465 | ||||
2466 | return error; | |||
2467 | } | |||
2468 | ||||
2469 | bool Scalar::SignExtend(uint32_t sign_bit_pos) { | |||
2470 | const uint32_t max_bit_pos = GetByteSize() * 8; | |||
2471 | ||||
2472 | if (sign_bit_pos < max_bit_pos) { | |||
2473 | switch (m_type) { | |||
2474 | case Scalar::e_void: | |||
2475 | case Scalar::e_float: | |||
2476 | case Scalar::e_double: | |||
2477 | case Scalar::e_long_double: | |||
2478 | return false; | |||
2479 | ||||
2480 | case Scalar::e_sint: | |||
2481 | case Scalar::e_uint: | |||
2482 | case Scalar::e_slong: | |||
2483 | case Scalar::e_ulong: | |||
2484 | case Scalar::e_slonglong: | |||
2485 | case Scalar::e_ulonglong: | |||
2486 | case Scalar::e_sint128: | |||
2487 | case Scalar::e_uint128: | |||
2488 | case Scalar::e_sint256: | |||
2489 | case Scalar::e_uint256: | |||
2490 | if (max_bit_pos == sign_bit_pos) | |||
2491 | return true; | |||
2492 | else if (sign_bit_pos < (max_bit_pos - 1)) { | |||
2493 | llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1); | |||
2494 | llvm::APInt bitwize_and = m_integer & sign_bit; | |||
2495 | if (bitwize_and.getBoolValue()) { | |||
2496 | const llvm::APInt mask = | |||
2497 | ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1); | |||
2498 | m_integer |= mask; | |||
2499 | } | |||
2500 | return true; | |||
2501 | } | |||
2502 | break; | |||
2503 | } | |||
2504 | } | |||
2505 | return false; | |||
2506 | } | |||
2507 | ||||
2508 | size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len, | |||
2509 | lldb::ByteOrder dst_byte_order, | |||
2510 | Status &error) const { | |||
2511 | // Get a data extractor that points to the native scalar data | |||
2512 | DataExtractor data; | |||
2513 | if (!GetData(data)) { | |||
| ||||
2514 | error.SetErrorString("invalid scalar value"); | |||
2515 | return 0; | |||
2516 | } | |||
2517 | ||||
2518 | const size_t src_len = data.GetByteSize(); | |||
2519 | ||||
2520 | // Prepare a memory buffer that contains some or all of the register value | |||
2521 | const size_t bytes_copied = | |||
2522 | data.CopyByteOrderedData(0, // src offset | |||
2523 | src_len, // src length | |||
2524 | dst, // dst buffer | |||
2525 | dst_len, // dst length | |||
2526 | dst_byte_order); // dst byte order | |||
2527 | if (bytes_copied == 0) | |||
2528 | error.SetErrorString("failed to copy data"); | |||
2529 | ||||
2530 | return bytes_copied; | |||
2531 | } | |||
2532 | ||||
2533 | bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) { | |||
2534 | if (bit_size == 0) | |||
2535 | return true; | |||
2536 | ||||
2537 | switch (m_type) { | |||
2538 | case Scalar::e_void: | |||
2539 | case Scalar::e_float: | |||
2540 | case Scalar::e_double: | |||
2541 | case Scalar::e_long_double: | |||
2542 | break; | |||
2543 | ||||
2544 | case Scalar::e_sint: | |||
2545 | case Scalar::e_slong: | |||
2546 | case Scalar::e_slonglong: | |||
2547 | case Scalar::e_sint128: | |||
2548 | case Scalar::e_sint256: | |||
2549 | m_integer = m_integer.ashr(bit_offset) | |||
2550 | .sextOrTrunc(bit_size) | |||
2551 | .sextOrSelf(8 * GetByteSize()); | |||
2552 | return true; | |||
2553 | ||||
2554 | case Scalar::e_uint: | |||
2555 | case Scalar::e_ulong: | |||
2556 | case Scalar::e_ulonglong: | |||
2557 | case Scalar::e_uint128: | |||
2558 | case Scalar::e_uint256: | |||
2559 | m_integer = m_integer.lshr(bit_offset) | |||
2560 | .zextOrTrunc(bit_size) | |||
2561 | .zextOrSelf(8 * GetByteSize()); | |||
2562 | return true; | |||
2563 | } | |||
2564 | return false; | |||
2565 | } | |||
2566 | ||||
2567 | bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) { | |||
2568 | // If either entry is void then we can just compare the types | |||
2569 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2570 | return lhs.m_type == rhs.m_type; | |||
2571 | ||||
2572 | Scalar temp_value; | |||
2573 | const Scalar *a; | |||
2574 | const Scalar *b; | |||
2575 | llvm::APFloat::cmpResult result; | |||
2576 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2577 | case Scalar::e_void: | |||
2578 | break; | |||
2579 | case Scalar::e_sint: | |||
2580 | case Scalar::e_uint: | |||
2581 | case Scalar::e_slong: | |||
2582 | case Scalar::e_ulong: | |||
2583 | case Scalar::e_slonglong: | |||
2584 | case Scalar::e_ulonglong: | |||
2585 | case Scalar::e_sint128: | |||
2586 | case Scalar::e_uint128: | |||
2587 | case Scalar::e_sint256: | |||
2588 | case Scalar::e_uint256: | |||
2589 | return a->m_integer == b->m_integer; | |||
2590 | case Scalar::e_float: | |||
2591 | case Scalar::e_double: | |||
2592 | case Scalar::e_long_double: | |||
2593 | result = a->m_float.compare(b->m_float); | |||
2594 | if (result == llvm::APFloat::cmpEqual) | |||
2595 | return true; | |||
2596 | } | |||
2597 | return false; | |||
2598 | } | |||
2599 | ||||
2600 | bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) { | |||
2601 | // If either entry is void then we can just compare the types | |||
2602 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2603 | return lhs.m_type != rhs.m_type; | |||
2604 | ||||
2605 | Scalar | |||
2606 | temp_value; // A temp value that might get a copy of either promoted value | |||
2607 | const Scalar *a; | |||
2608 | const Scalar *b; | |||
2609 | llvm::APFloat::cmpResult result; | |||
2610 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2611 | case Scalar::e_void: | |||
2612 | break; | |||
2613 | case Scalar::e_sint: | |||
2614 | case Scalar::e_uint: | |||
2615 | case Scalar::e_slong: | |||
2616 | case Scalar::e_ulong: | |||
2617 | case Scalar::e_slonglong: | |||
2618 | case Scalar::e_ulonglong: | |||
2619 | case Scalar::e_sint128: | |||
2620 | case Scalar::e_uint128: | |||
2621 | case Scalar::e_sint256: | |||
2622 | case Scalar::e_uint256: | |||
2623 | return a->m_integer != b->m_integer; | |||
2624 | case Scalar::e_float: | |||
2625 | case Scalar::e_double: | |||
2626 | case Scalar::e_long_double: | |||
2627 | result = a->m_float.compare(b->m_float); | |||
2628 | if (result != llvm::APFloat::cmpEqual) | |||
2629 | return true; | |||
2630 | } | |||
2631 | return true; | |||
2632 | } | |||
2633 | ||||
2634 | bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) { | |||
2635 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2636 | return false; | |||
2637 | ||||
2638 | Scalar temp_value; | |||
2639 | const Scalar *a; | |||
2640 | const Scalar *b; | |||
2641 | llvm::APFloat::cmpResult result; | |||
2642 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2643 | case Scalar::e_void: | |||
2644 | break; | |||
2645 | case Scalar::e_sint: | |||
2646 | case Scalar::e_slong: | |||
2647 | case Scalar::e_slonglong: | |||
2648 | case Scalar::e_sint128: | |||
2649 | case Scalar::e_sint256: | |||
2650 | return a->m_integer.slt(b->m_integer); | |||
2651 | case Scalar::e_uint: | |||
2652 | case Scalar::e_ulong: | |||
2653 | case Scalar::e_ulonglong: | |||
2654 | case Scalar::e_uint128: | |||
2655 | case Scalar::e_uint256: | |||
2656 | return a->m_integer.ult(b->m_integer); | |||
2657 | case Scalar::e_float: | |||
2658 | case Scalar::e_double: | |||
2659 | case Scalar::e_long_double: | |||
2660 | result = a->m_float.compare(b->m_float); | |||
2661 | if (result == llvm::APFloat::cmpLessThan) | |||
2662 | return true; | |||
2663 | } | |||
2664 | return false; | |||
2665 | } | |||
2666 | ||||
2667 | bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) { | |||
2668 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2669 | return false; | |||
2670 | ||||
2671 | Scalar temp_value; | |||
2672 | const Scalar *a; | |||
2673 | const Scalar *b; | |||
2674 | llvm::APFloat::cmpResult result; | |||
2675 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2676 | case Scalar::e_void: | |||
2677 | break; | |||
2678 | case Scalar::e_sint: | |||
2679 | case Scalar::e_slong: | |||
2680 | case Scalar::e_slonglong: | |||
2681 | case Scalar::e_sint128: | |||
2682 | case Scalar::e_sint256: | |||
2683 | return a->m_integer.sle(b->m_integer); | |||
2684 | case Scalar::e_uint: | |||
2685 | case Scalar::e_ulong: | |||
2686 | case Scalar::e_ulonglong: | |||
2687 | case Scalar::e_uint128: | |||
2688 | case Scalar::e_uint256: | |||
2689 | return a->m_integer.ule(b->m_integer); | |||
2690 | case Scalar::e_float: | |||
2691 | case Scalar::e_double: | |||
2692 | case Scalar::e_long_double: | |||
2693 | result = a->m_float.compare(b->m_float); | |||
2694 | if (result == llvm::APFloat::cmpLessThan || | |||
2695 | result == llvm::APFloat::cmpEqual) | |||
2696 | return true; | |||
2697 | } | |||
2698 | return false; | |||
2699 | } | |||
2700 | ||||
2701 | bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) { | |||
2702 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2703 | return false; | |||
2704 | ||||
2705 | Scalar temp_value; | |||
2706 | const Scalar *a; | |||
2707 | const Scalar *b; | |||
2708 | llvm::APFloat::cmpResult result; | |||
2709 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2710 | case Scalar::e_void: | |||
2711 | break; | |||
2712 | case Scalar::e_sint: | |||
2713 | case Scalar::e_slong: | |||
2714 | case Scalar::e_slonglong: | |||
2715 | case Scalar::e_sint128: | |||
2716 | case Scalar::e_sint256: | |||
2717 | return a->m_integer.sgt(b->m_integer); | |||
2718 | case Scalar::e_uint: | |||
2719 | case Scalar::e_ulong: | |||
2720 | case Scalar::e_ulonglong: | |||
2721 | case Scalar::e_uint128: | |||
2722 | case Scalar::e_uint256: | |||
2723 | return a->m_integer.ugt(b->m_integer); | |||
2724 | case Scalar::e_float: | |||
2725 | case Scalar::e_double: | |||
2726 | case Scalar::e_long_double: | |||
2727 | result = a->m_float.compare(b->m_float); | |||
2728 | if (result == llvm::APFloat::cmpGreaterThan) | |||
2729 | return true; | |||
2730 | } | |||
2731 | return false; | |||
2732 | } | |||
2733 | ||||
2734 | bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) { | |||
2735 | if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) | |||
2736 | return false; | |||
2737 | ||||
2738 | Scalar temp_value; | |||
2739 | const Scalar *a; | |||
2740 | const Scalar *b; | |||
2741 | llvm::APFloat::cmpResult result; | |||
2742 | switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { | |||
2743 | case Scalar::e_void: | |||
2744 | break; | |||
2745 | case Scalar::e_sint: | |||
2746 | case Scalar::e_slong: | |||
2747 | case Scalar::e_slonglong: | |||
2748 | case Scalar::e_sint128: | |||
2749 | case Scalar::e_sint256: | |||
2750 | return a->m_integer.sge(b->m_integer); | |||
2751 | case Scalar::e_uint: | |||
2752 | case Scalar::e_ulong: | |||
2753 | case Scalar::e_ulonglong: | |||
2754 | case Scalar::e_uint128: | |||
2755 | case Scalar::e_uint256: | |||
2756 | return a->m_integer.uge(b->m_integer); | |||
2757 | case Scalar::e_float: | |||
2758 | case Scalar::e_double: | |||
2759 | case Scalar::e_long_double: | |||
2760 | result = a->m_float.compare(b->m_float); | |||
2761 | if (result == llvm::APFloat::cmpGreaterThan || | |||
2762 | result == llvm::APFloat::cmpEqual) | |||
2763 | return true; | |||
2764 | } | |||
2765 | return false; | |||
2766 | } | |||
2767 | ||||
2768 | bool Scalar::ClearBit(uint32_t bit) { | |||
2769 | switch (m_type) { | |||
2770 | case e_void: | |||
2771 | break; | |||
2772 | case e_sint: | |||
2773 | case e_uint: | |||
2774 | case e_slong: | |||
2775 | case e_ulong: | |||
2776 | case e_slonglong: | |||
2777 | case e_ulonglong: | |||
2778 | case e_sint128: | |||
2779 | case e_uint128: | |||
2780 | case e_sint256: | |||
2781 | case e_uint256: | |||
2782 | m_integer.clearBit(bit); | |||
2783 | return true; | |||
2784 | case e_float: | |||
2785 | case e_double: | |||
2786 | case e_long_double: | |||
2787 | break; | |||
2788 | } | |||
2789 | return false; | |||
2790 | } | |||
2791 | ||||
2792 | bool Scalar::SetBit(uint32_t bit) { | |||
2793 | switch (m_type) { | |||
2794 | case e_void: | |||
2795 | break; | |||
2796 | case e_sint: | |||
2797 | case e_uint: | |||
2798 | case e_slong: | |||
2799 | case e_ulong: | |||
2800 | case e_slonglong: | |||
2801 | case e_ulonglong: | |||
2802 | case e_sint128: | |||
2803 | case e_uint128: | |||
2804 | case e_sint256: | |||
2805 | case e_uint256: | |||
2806 | m_integer.setBit(bit); | |||
2807 | return true; | |||
2808 | case e_float: | |||
2809 | case e_double: | |||
2810 | case e_long_double: | |||
2811 | break; | |||
2812 | } | |||
2813 | return false; | |||
2814 | } |
1 | //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===// |
2 | // |
3 | // The LLVM Compiler Infrastructure |
4 | // |
5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | /// |
10 | /// \file |
11 | /// This file implements a class to represent arbitrary precision |
12 | /// integral constant values and operations on them. |
13 | /// |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #ifndef LLVM_ADT_APINT_H |
17 | #define LLVM_ADT_APINT_H |
18 | |
19 | #include "llvm/Support/Compiler.h" |
20 | #include "llvm/Support/MathExtras.h" |
21 | #include <cassert> |
22 | #include <climits> |
23 | #include <cstring> |
24 | #include <string> |
25 | |
26 | namespace llvm { |
27 | class FoldingSetNodeID; |
28 | class StringRef; |
29 | class hash_code; |
30 | class raw_ostream; |
31 | |
32 | template <typename T> class SmallVectorImpl; |
33 | template <typename T> class ArrayRef; |
34 | template <typename T> class Optional; |
35 | |
36 | class APInt; |
37 | |
38 | inline APInt operator-(APInt); |
39 | |
40 | //===----------------------------------------------------------------------===// |
41 | // APInt Class |
42 | //===----------------------------------------------------------------------===// |
43 | |
44 | /// Class for arbitrary precision integers. |
45 | /// |
46 | /// APInt is a functional replacement for common case unsigned integer type like |
47 | /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width |
48 | /// integer sizes and large integer value types such as 3-bits, 15-bits, or more |
49 | /// than 64-bits of precision. APInt provides a variety of arithmetic operators |
50 | /// and methods to manipulate integer values of any bit-width. It supports both |
51 | /// the typical integer arithmetic and comparison operations as well as bitwise |
52 | /// manipulation. |
53 | /// |
54 | /// The class has several invariants worth noting: |
55 | /// * All bit, byte, and word positions are zero-based. |
56 | /// * Once the bit width is set, it doesn't change except by the Truncate, |
57 | /// SignExtend, or ZeroExtend operations. |
58 | /// * All binary operators must be on APInt instances of the same bit width. |
59 | /// Attempting to use these operators on instances with different bit |
60 | /// widths will yield an assertion. |
61 | /// * The value is stored canonically as an unsigned value. For operations |
62 | /// where it makes a difference, there are both signed and unsigned variants |
63 | /// of the operation. For example, sdiv and udiv. However, because the bit |
64 | /// widths must be the same, operations such as Mul and Add produce the same |
65 | /// results regardless of whether the values are interpreted as signed or |
66 | /// not. |
67 | /// * In general, the class tries to follow the style of computation that LLVM |
68 | /// uses in its IR. This simplifies its use for LLVM. |
69 | /// |
70 | class LLVM_NODISCARD[[clang::warn_unused_result]] APInt { |
71 | public: |
72 | typedef uint64_t WordType; |
73 | |
74 | /// This enum is used to hold the constants we needed for APInt. |
75 | enum : unsigned { |
76 | /// Byte size of a word. |
77 | APINT_WORD_SIZE = sizeof(WordType), |
78 | /// Bits in a word. |
79 | APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT8 |
80 | }; |
81 | |
82 | enum class Rounding { |
83 | DOWN, |
84 | TOWARD_ZERO, |
85 | UP, |
86 | }; |
87 | |
88 | static const WordType WORDTYPE_MAX = ~WordType(0); |
89 | |
90 | private: |
91 | /// This union is used to store the integer value. When the |
92 | /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal. |
93 | union { |
94 | uint64_t VAL; ///< Used to store the <= 64 bits integer value. |
95 | uint64_t *pVal; ///< Used to store the >64 bits integer value. |
96 | } U; |
97 | |
98 | unsigned BitWidth; ///< The number of bits in this APInt. |
99 | |
100 | friend struct DenseMapAPIntKeyInfo; |
101 | |
102 | friend class APSInt; |
103 | |
104 | /// Fast internal constructor |
105 | /// |
106 | /// This constructor is used only internally for speed of construction of |
107 | /// temporaries. It is unsafe for general use so it is not public. |
108 | APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { |
109 | U.pVal = val; |
110 | } |
111 | |
112 | /// Determine if this APInt just has one word to store value. |
113 | /// |
114 | /// \returns true if the number of bits <= 64, false otherwise. |
115 | bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } |
116 | |
117 | /// Determine which word a bit is in. |
118 | /// |
119 | /// \returns the word position for the specified bit position. |
120 | static unsigned whichWord(unsigned bitPosition) { |
121 | return bitPosition / APINT_BITS_PER_WORD; |
122 | } |
123 | |
124 | /// Determine which bit in a word a bit is in. |
125 | /// |
126 | /// \returns the bit position in a word for the specified bit position |
127 | /// in the APInt. |
128 | static unsigned whichBit(unsigned bitPosition) { |
129 | return bitPosition % APINT_BITS_PER_WORD; |
130 | } |
131 | |
132 | /// Get a single bit mask. |
133 | /// |
134 | /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set |
135 | /// This method generates and returns a uint64_t (word) mask for a single |
136 | /// bit at a specific bit position. This is used to mask the bit in the |
137 | /// corresponding word. |
138 | static uint64_t maskBit(unsigned bitPosition) { |
139 | return 1ULL << whichBit(bitPosition); |
140 | } |
141 | |
142 | /// Clear unused high order bits |
143 | /// |
144 | /// This method is used internally to clear the top "N" bits in the high order |
145 | /// word that are not used by the APInt. This is needed after the most |
146 | /// significant word is assigned a value to ensure that those bits are |
147 | /// zero'd out. |
148 | APInt &clearUnusedBits() { |
149 | // Compute how many bits are used in the final word |
150 | unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1; |
151 | |
152 | // Mask out the high bits. |
153 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); |
154 | if (isSingleWord()) |
155 | U.VAL &= mask; |
156 | else |
157 | U.pVal[getNumWords() - 1] &= mask; |
158 | return *this; |
159 | } |
160 | |
161 | /// Get the word corresponding to a bit position |
162 | /// \returns the corresponding word for the specified bit position. |
163 | uint64_t getWord(unsigned bitPosition) const { |
164 | return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; |
165 | } |
166 | |
167 | /// Utility method to change the bit width of this APInt to new bit width, |
168 | /// allocating and/or deallocating as necessary. There is no guarantee on the |
169 | /// value of any bits upon return. Caller should populate the bits after. |
170 | void reallocate(unsigned NewBitWidth); |
171 | |
172 | /// Convert a char array into an APInt |
173 | /// |
174 | /// \param radix 2, 8, 10, 16, or 36 |
175 | /// Converts a string into a number. The string must be non-empty |
176 | /// and well-formed as a number of the given base. The bit-width |
177 | /// must be sufficient to hold the result. |
178 | /// |
179 | /// This is used by the constructors that take string arguments. |
180 | /// |
181 | /// StringRef::getAsInteger is superficially similar but (1) does |
182 | /// not assume that the string is well-formed and (2) grows the |
183 | /// result to hold the input. |
184 | void fromString(unsigned numBits, StringRef str, uint8_t radix); |
185 | |
186 | /// An internal division function for dividing APInts. |
187 | /// |
188 | /// This is used by the toString method to divide by the radix. It simply |
189 | /// provides a more convenient form of divide for internal use since KnuthDiv |
190 | /// has specific constraints on its inputs. If those constraints are not met |
191 | /// then it provides a simpler form of divide. |
192 | static void divide(const WordType *LHS, unsigned lhsWords, |
193 | const WordType *RHS, unsigned rhsWords, WordType *Quotient, |
194 | WordType *Remainder); |
195 | |
196 | /// out-of-line slow case for inline constructor |
197 | void initSlowCase(uint64_t val, bool isSigned); |
198 | |
199 | /// shared code between two array constructors |
200 | void initFromArray(ArrayRef<uint64_t> array); |
201 | |
202 | /// out-of-line slow case for inline copy constructor |
203 | void initSlowCase(const APInt &that); |
204 | |
205 | /// out-of-line slow case for shl |
206 | void shlSlowCase(unsigned ShiftAmt); |
207 | |
208 | /// out-of-line slow case for lshr. |
209 | void lshrSlowCase(unsigned ShiftAmt); |
210 | |
211 | /// out-of-line slow case for ashr. |
212 | void ashrSlowCase(unsigned ShiftAmt); |
213 | |
214 | /// out-of-line slow case for operator= |
215 | void AssignSlowCase(const APInt &RHS); |
216 | |
217 | /// out-of-line slow case for operator== |
218 | bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
219 | |
220 | /// out-of-line slow case for countLeadingZeros |
221 | unsigned countLeadingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
222 | |
223 | /// out-of-line slow case for countLeadingOnes. |
224 | unsigned countLeadingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
225 | |
226 | /// out-of-line slow case for countTrailingZeros. |
227 | unsigned countTrailingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
228 | |
229 | /// out-of-line slow case for countTrailingOnes |
230 | unsigned countTrailingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
231 | |
232 | /// out-of-line slow case for countPopulation |
233 | unsigned countPopulationSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
234 | |
235 | /// out-of-line slow case for intersects. |
236 | bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
237 | |
238 | /// out-of-line slow case for isSubsetOf. |
239 | bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
240 | |
241 | /// out-of-line slow case for setBits. |
242 | void setBitsSlowCase(unsigned loBit, unsigned hiBit); |
243 | |
244 | /// out-of-line slow case for flipAllBits. |
245 | void flipAllBitsSlowCase(); |
246 | |
247 | /// out-of-line slow case for operator&=. |
248 | void AndAssignSlowCase(const APInt& RHS); |
249 | |
250 | /// out-of-line slow case for operator|=. |
251 | void OrAssignSlowCase(const APInt& RHS); |
252 | |
253 | /// out-of-line slow case for operator^=. |
254 | void XorAssignSlowCase(const APInt& RHS); |
255 | |
256 | /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal |
257 | /// to, or greater than RHS. |
258 | int compare(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
259 | |
260 | /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal |
261 | /// to, or greater than RHS. |
262 | int compareSigned(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
263 | |
264 | public: |
265 | /// \name Constructors |
266 | /// @{ |
267 | |
268 | /// Create a new APInt of numBits width, initialized as val. |
269 | /// |
270 | /// If isSigned is true then val is treated as if it were a signed value |
271 | /// (i.e. as an int64_t) and the appropriate sign extension to the bit width |
272 | /// will be done. Otherwise, no sign extension occurs (high order bits beyond |
273 | /// the range of val are zero filled). |
274 | /// |
275 | /// \param numBits the bit width of the constructed APInt |
276 | /// \param val the initial value of the APInt |
277 | /// \param isSigned how to treat signedness of val |
278 | APInt(unsigned numBits, uint64_t val, bool isSigned = false) |
279 | : BitWidth(numBits) { |
280 | assert(BitWidth && "bitwidth too small")((BitWidth && "bitwidth too small") ? static_cast< void> (0) : __assert_fail ("BitWidth && \"bitwidth too small\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 280, __PRETTY_FUNCTION__)); |
281 | if (isSingleWord()) { |
282 | U.VAL = val; |
283 | clearUnusedBits(); |
284 | } else { |
285 | initSlowCase(val, isSigned); |
286 | } |
287 | } |
288 | |
289 | /// Construct an APInt of numBits width, initialized as bigVal[]. |
290 | /// |
291 | /// Note that bigVal.size() can be smaller or larger than the corresponding |
292 | /// bit width but any extraneous bits will be dropped. |
293 | /// |
294 | /// \param numBits the bit width of the constructed APInt |
295 | /// \param bigVal a sequence of words to form the initial value of the APInt |
296 | APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); |
297 | |
298 | /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but |
299 | /// deprecated because this constructor is prone to ambiguity with the |
300 | /// APInt(unsigned, uint64_t, bool) constructor. |
301 | /// |
302 | /// If this overload is ever deleted, care should be taken to prevent calls |
303 | /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool) |
304 | /// constructor. |
305 | APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); |
306 | |
307 | /// Construct an APInt from a string representation. |
308 | /// |
309 | /// This constructor interprets the string \p str in the given radix. The |
310 | /// interpretation stops when the first character that is not suitable for the |
311 | /// radix is encountered, or the end of the string. Acceptable radix values |
312 | /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the |
313 | /// string to require more bits than numBits. |
314 | /// |
315 | /// \param numBits the bit width of the constructed APInt |
316 | /// \param str the string to be interpreted |
317 | /// \param radix the radix to use for the conversion |
318 | APInt(unsigned numBits, StringRef str, uint8_t radix); |
319 | |
320 | /// Simply makes *this a copy of that. |
321 | /// Copy Constructor. |
322 | APInt(const APInt &that) : BitWidth(that.BitWidth) { |
323 | if (isSingleWord()) |
324 | U.VAL = that.U.VAL; |
325 | else |
326 | initSlowCase(that); |
327 | } |
328 | |
329 | /// Move Constructor. |
330 | APInt(APInt &&that) : BitWidth(that.BitWidth) { |
331 | memcpy(&U, &that.U, sizeof(U)); |
332 | that.BitWidth = 0; |
333 | } |
334 | |
335 | /// Destructor. |
336 | ~APInt() { |
337 | if (needsCleanup()) |
338 | delete[] U.pVal; |
339 | } |
340 | |
341 | /// Default constructor that creates an uninteresting APInt |
342 | /// representing a 1-bit zero value. |
343 | /// |
344 | /// This is useful for object deserialization (pair this with the static |
345 | /// method Read). |
346 | explicit APInt() : BitWidth(1) { U.VAL = 0; } |
347 | |
348 | /// Returns whether this instance allocated memory. |
349 | bool needsCleanup() const { return !isSingleWord(); } |
350 | |
351 | /// Used to insert APInt objects, or objects that contain APInt objects, into |
352 | /// FoldingSets. |
353 | void Profile(FoldingSetNodeID &id) const; |
354 | |
355 | /// @} |
356 | /// \name Value Tests |
357 | /// @{ |
358 | |
359 | /// Determine sign of this APInt. |
360 | /// |
361 | /// This tests the high bit of this APInt to determine if it is set. |
362 | /// |
363 | /// \returns true if this APInt is negative, false otherwise |
364 | bool isNegative() const { return (*this)[BitWidth - 1]; } |
365 | |
366 | /// Determine if this APInt Value is non-negative (>= 0) |
367 | /// |
368 | /// This tests the high bit of the APInt to determine if it is unset. |
369 | bool isNonNegative() const { return !isNegative(); } |
370 | |
371 | /// Determine if sign bit of this APInt is set. |
372 | /// |
373 | /// This tests the high bit of this APInt to determine if it is set. |
374 | /// |
375 | /// \returns true if this APInt has its sign bit set, false otherwise. |
376 | bool isSignBitSet() const { return (*this)[BitWidth-1]; } |
377 | |
378 | /// Determine if sign bit of this APInt is clear. |
379 | /// |
380 | /// This tests the high bit of this APInt to determine if it is clear. |
381 | /// |
382 | /// \returns true if this APInt has its sign bit clear, false otherwise. |
383 | bool isSignBitClear() const { return !isSignBitSet(); } |
384 | |
385 | /// Determine if this APInt Value is positive. |
386 | /// |
387 | /// This tests if the value of this APInt is positive (> 0). Note |
388 | /// that 0 is not a positive value. |
389 | /// |
390 | /// \returns true if this APInt is positive. |
391 | bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } |
392 | |
393 | /// Determine if all bits are set |
394 | /// |
395 | /// This checks to see if the value has all bits of the APInt are set or not. |
396 | bool isAllOnesValue() const { |
397 | if (isSingleWord()) |
398 | return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); |
399 | return countTrailingOnesSlowCase() == BitWidth; |
400 | } |
401 | |
402 | /// Determine if all bits are clear |
403 | /// |
404 | /// This checks to see if the value has all bits of the APInt are clear or |
405 | /// not. |
406 | bool isNullValue() const { return !*this; } |
407 | |
408 | /// Determine if this is a value of 1. |
409 | /// |
410 | /// This checks to see if the value of this APInt is one. |
411 | bool isOneValue() const { |
412 | if (isSingleWord()) |
413 | return U.VAL == 1; |
414 | return countLeadingZerosSlowCase() == BitWidth - 1; |
415 | } |
416 | |
417 | /// Determine if this is the largest unsigned value. |
418 | /// |
419 | /// This checks to see if the value of this APInt is the maximum unsigned |
420 | /// value for the APInt's bit width. |
421 | bool isMaxValue() const { return isAllOnesValue(); } |
422 | |
423 | /// Determine if this is the largest signed value. |
424 | /// |
425 | /// This checks to see if the value of this APInt is the maximum signed |
426 | /// value for the APInt's bit width. |
427 | bool isMaxSignedValue() const { |
428 | if (isSingleWord()) |
429 | return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); |
430 | return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; |
431 | } |
432 | |
433 | /// Determine if this is the smallest unsigned value. |
434 | /// |
435 | /// This checks to see if the value of this APInt is the minimum unsigned |
436 | /// value for the APInt's bit width. |
437 | bool isMinValue() const { return isNullValue(); } |
438 | |
439 | /// Determine if this is the smallest signed value. |
440 | /// |
441 | /// This checks to see if the value of this APInt is the minimum signed |
442 | /// value for the APInt's bit width. |
443 | bool isMinSignedValue() const { |
444 | if (isSingleWord()) |
445 | return U.VAL == (WordType(1) << (BitWidth - 1)); |
446 | return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; |
447 | } |
448 | |
449 | /// Check if this APInt has an N-bits unsigned integer value. |
450 | bool isIntN(unsigned N) const { |
451 | assert(N && "N == 0 ???")((N && "N == 0 ???") ? static_cast<void> (0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 451, __PRETTY_FUNCTION__)); |
452 | return getActiveBits() <= N; |
453 | } |
454 | |
455 | /// Check if this APInt has an N-bits signed integer value. |
456 | bool isSignedIntN(unsigned N) const { |
457 | assert(N && "N == 0 ???")((N && "N == 0 ???") ? static_cast<void> (0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 457, __PRETTY_FUNCTION__)); |
458 | return getMinSignedBits() <= N; |
459 | } |
460 | |
461 | /// Check if this APInt's value is a power of two greater than zero. |
462 | /// |
463 | /// \returns true if the argument APInt value is a power of two > 0. |
464 | bool isPowerOf2() const { |
465 | if (isSingleWord()) |
466 | return isPowerOf2_64(U.VAL); |
467 | return countPopulationSlowCase() == 1; |
468 | } |
469 | |
470 | /// Check if the APInt's value is returned by getSignMask. |
471 | /// |
472 | /// \returns true if this is the value returned by getSignMask. |
473 | bool isSignMask() const { return isMinSignedValue(); } |
474 | |
475 | /// Convert APInt to a boolean value. |
476 | /// |
477 | /// This converts the APInt to a boolean value as a test against zero. |
478 | bool getBoolValue() const { return !!*this; } |
479 | |
480 | /// If this value is smaller than the specified limit, return it, otherwise |
481 | /// return the limit value. This causes the value to saturate to the limit. |
482 | uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX(18446744073709551615UL)) const { |
483 | return ugt(Limit) ? Limit : getZExtValue(); |
484 | } |
485 | |
486 | /// Check if the APInt consists of a repeated bit pattern. |
487 | /// |
488 | /// e.g. 0x01010101 satisfies isSplat(8). |
489 | /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit |
490 | /// width without remainder. |
491 | bool isSplat(unsigned SplatSizeInBits) const; |
492 | |
493 | /// \returns true if this APInt value is a sequence of \param numBits ones |
494 | /// starting at the least significant bit with the remainder zero. |
495 | bool isMask(unsigned numBits) const { |
496 | assert(numBits != 0 && "numBits must be non-zero")((numBits != 0 && "numBits must be non-zero") ? static_cast <void> (0) : __assert_fail ("numBits != 0 && \"numBits must be non-zero\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 496, __PRETTY_FUNCTION__)); |
497 | assert(numBits <= BitWidth && "numBits out of range")((numBits <= BitWidth && "numBits out of range") ? static_cast<void> (0) : __assert_fail ("numBits <= BitWidth && \"numBits out of range\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 497, __PRETTY_FUNCTION__)); |
498 | if (isSingleWord()) |
499 | return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); |
500 | unsigned Ones = countTrailingOnesSlowCase(); |
501 | return (numBits == Ones) && |
502 | ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
503 | } |
504 | |
505 | /// \returns true if this APInt is a non-empty sequence of ones starting at |
506 | /// the least significant bit with the remainder zero. |
507 | /// Ex. isMask(0x0000FFFFU) == true. |
508 | bool isMask() const { |
509 | if (isSingleWord()) |
510 | return isMask_64(U.VAL); |
511 | unsigned Ones = countTrailingOnesSlowCase(); |
512 | return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
513 | } |
514 | |
515 | /// Return true if this APInt value contains a sequence of ones with |
516 | /// the remainder zero. |
517 | bool isShiftedMask() const { |
518 | if (isSingleWord()) |
519 | return isShiftedMask_64(U.VAL); |
520 | unsigned Ones = countPopulationSlowCase(); |
521 | unsigned LeadZ = countLeadingZerosSlowCase(); |
522 | return (Ones + LeadZ + countTrailingZeros()) == BitWidth; |
523 | } |
524 | |
525 | /// @} |
526 | /// \name Value Generators |
527 | /// @{ |
528 | |
529 | /// Gets maximum unsigned value of APInt for specific bit width. |
530 | static APInt getMaxValue(unsigned numBits) { |
531 | return getAllOnesValue(numBits); |
532 | } |
533 | |
534 | /// Gets maximum signed value of APInt for a specific bit width. |
535 | static APInt getSignedMaxValue(unsigned numBits) { |
536 | APInt API = getAllOnesValue(numBits); |
537 | API.clearBit(numBits - 1); |
538 | return API; |
539 | } |
540 | |
541 | /// Gets minimum unsigned value of APInt for a specific bit width. |
542 | static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } |
543 | |
544 | /// Gets minimum signed value of APInt for a specific bit width. |
545 | static APInt getSignedMinValue(unsigned numBits) { |
546 | APInt API(numBits, 0); |
547 | API.setBit(numBits - 1); |
548 | return API; |
549 | } |
550 | |
551 | /// Get the SignMask for a specific bit width. |
552 | /// |
553 | /// This is just a wrapper function of getSignedMinValue(), and it helps code |
554 | /// readability when we want to get a SignMask. |
555 | static APInt getSignMask(unsigned BitWidth) { |
556 | return getSignedMinValue(BitWidth); |
557 | } |
558 | |
559 | /// Get the all-ones value. |
560 | /// |
561 | /// \returns the all-ones value for an APInt of the specified bit-width. |
562 | static APInt getAllOnesValue(unsigned numBits) { |
563 | return APInt(numBits, WORDTYPE_MAX, true); |
564 | } |
565 | |
566 | /// Get the '0' value. |
567 | /// |
568 | /// \returns the '0' value for an APInt of the specified bit-width. |
569 | static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); } |
570 | |
571 | /// Compute an APInt containing numBits highbits from this APInt. |
572 | /// |
573 | /// Get an APInt with the same BitWidth as this APInt, just zero mask |
574 | /// the low bits and right shift to the least significant bit. |
575 | /// |
576 | /// \returns the high "numBits" bits of this APInt. |
577 | APInt getHiBits(unsigned numBits) const; |
578 | |
579 | /// Compute an APInt containing numBits lowbits from this APInt. |
580 | /// |
581 | /// Get an APInt with the same BitWidth as this APInt, just zero mask |
582 | /// the high bits. |
583 | /// |
584 | /// \returns the low "numBits" bits of this APInt. |
585 | APInt getLoBits(unsigned numBits) const; |
586 | |
587 | /// Return an APInt with exactly one bit set in the result. |
588 | static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { |
589 | APInt Res(numBits, 0); |
590 | Res.setBit(BitNo); |
591 | return Res; |
592 | } |
593 | |
594 | /// Get a value with a block of bits set. |
595 | /// |
596 | /// Constructs an APInt value that has a contiguous range of bits set. The |
597 | /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other |
598 | /// bits will be zero. For example, with parameters(32, 0, 16) you would get |
599 | /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For |
600 | /// example, with parameters (32, 28, 4), you would get 0xF000000F. |
601 | /// |
602 | /// \param numBits the intended bit width of the result |
603 | /// \param loBit the index of the lowest bit set. |
604 | /// \param hiBit the index of the highest bit set. |
605 | /// |
606 | /// \returns An APInt value with the requested bits set. |
607 | static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { |
608 | APInt Res(numBits, 0); |
609 | Res.setBits(loBit, hiBit); |
610 | return Res; |
611 | } |
612 | |
613 | /// Get a value with upper bits starting at loBit set. |
614 | /// |
615 | /// Constructs an APInt value that has a contiguous range of bits set. The |
616 | /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other |
617 | /// bits will be zero. For example, with parameters(32, 12) you would get |
618 | /// 0xFFFFF000. |
619 | /// |
620 | /// \param numBits the intended bit width of the result |
621 | /// \param loBit the index of the lowest bit to set. |
622 | /// |
623 | /// \returns An APInt value with the requested bits set. |
624 | static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { |
625 | APInt Res(numBits, 0); |
626 | Res.setBitsFrom(loBit); |
627 | return Res; |
628 | } |
629 | |
630 | /// Get a value with high bits set |
631 | /// |
632 | /// Constructs an APInt value that has the top hiBitsSet bits set. |
633 | /// |
634 | /// \param numBits the bitwidth of the result |
635 | /// \param hiBitsSet the number of high-order bits set in the result. |
636 | static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { |
637 | APInt Res(numBits, 0); |
638 | Res.setHighBits(hiBitsSet); |
639 | return Res; |
640 | } |
641 | |
642 | /// Get a value with low bits set |
643 | /// |
644 | /// Constructs an APInt value that has the bottom loBitsSet bits set. |
645 | /// |
646 | /// \param numBits the bitwidth of the result |
647 | /// \param loBitsSet the number of low-order bits set in the result. |
648 | static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { |
649 | APInt Res(numBits, 0); |
650 | Res.setLowBits(loBitsSet); |
651 | return Res; |
652 | } |
653 | |
654 | /// Return a value containing V broadcasted over NewLen bits. |
655 | static APInt getSplat(unsigned NewLen, const APInt &V); |
656 | |
657 | /// Determine if two APInts have the same value, after zero-extending |
658 | /// one of them (if needed!) to ensure that the bit-widths match. |
659 | static bool isSameValue(const APInt &I1, const APInt &I2) { |
660 | if (I1.getBitWidth() == I2.getBitWidth()) |
661 | return I1 == I2; |
662 | |
663 | if (I1.getBitWidth() > I2.getBitWidth()) |
664 | return I1 == I2.zext(I1.getBitWidth()); |
665 | |
666 | return I1.zext(I2.getBitWidth()) == I2; |
667 | } |
668 | |
669 | /// Overload to compute a hash_code for an APInt value. |
670 | friend hash_code hash_value(const APInt &Arg); |
671 | |
672 | /// This function returns a pointer to the internal storage of the APInt. |
673 | /// This is useful for writing out the APInt in binary form without any |
674 | /// conversions. |
675 | const uint64_t *getRawData() const { |
676 | if (isSingleWord()) |
677 | return &U.VAL; |
678 | return &U.pVal[0]; |
679 | } |
680 | |
681 | /// @} |
682 | /// \name Unary Operators |
683 | /// @{ |
684 | |
685 | /// Postfix increment operator. |
686 | /// |
687 | /// Increments *this by 1. |
688 | /// |
689 | /// \returns a new APInt value representing the original value of *this. |
690 | const APInt operator++(int) { |
691 | APInt API(*this); |
692 | ++(*this); |
693 | return API; |
694 | } |
695 | |
696 | /// Prefix increment operator. |
697 | /// |
698 | /// \returns *this incremented by one |
699 | APInt &operator++(); |
700 | |
701 | /// Postfix decrement operator. |
702 | /// |
703 | /// Decrements *this by 1. |
704 | /// |
705 | /// \returns a new APInt value representing the original value of *this. |
706 | const APInt operator--(int) { |
707 | APInt API(*this); |
708 | --(*this); |
709 | return API; |
710 | } |
711 | |
712 | /// Prefix decrement operator. |
713 | /// |
714 | /// \returns *this decremented by one. |
715 | APInt &operator--(); |
716 | |
717 | /// Logical negation operator. |
718 | /// |
719 | /// Performs logical negation operation on this APInt. |
720 | /// |
721 | /// \returns true if *this is zero, false otherwise. |
722 | bool operator!() const { |
723 | if (isSingleWord()) |
724 | return U.VAL == 0; |
725 | return countLeadingZerosSlowCase() == BitWidth; |
726 | } |
727 | |
728 | /// @} |
729 | /// \name Assignment Operators |
730 | /// @{ |
731 | |
732 | /// Copy assignment operator. |
733 | /// |
734 | /// \returns *this after assignment of RHS. |
735 | APInt &operator=(const APInt &RHS) { |
736 | // If the bitwidths are the same, we can avoid mucking with memory |
737 | if (isSingleWord() && RHS.isSingleWord()) { |
738 | U.VAL = RHS.U.VAL; |
739 | BitWidth = RHS.BitWidth; |
740 | return clearUnusedBits(); |
741 | } |
742 | |
743 | AssignSlowCase(RHS); |
744 | return *this; |
745 | } |
746 | |
747 | /// Move assignment operator. |
748 | APInt &operator=(APInt &&that) { |
749 | #ifdef _MSC_VER |
750 | // The MSVC std::shuffle implementation still does self-assignment. |
751 | if (this == &that) |
752 | return *this; |
753 | #endif |
754 | assert(this != &that && "Self-move not supported")((this != &that && "Self-move not supported") ? static_cast <void> (0) : __assert_fail ("this != &that && \"Self-move not supported\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 754, __PRETTY_FUNCTION__)); |
755 | if (!isSingleWord()) |
756 | delete[] U.pVal; |
757 | |
758 | // Use memcpy so that type based alias analysis sees both VAL and pVal |
759 | // as modified. |
760 | memcpy(&U, &that.U, sizeof(U)); |
761 | |
762 | BitWidth = that.BitWidth; |
763 | that.BitWidth = 0; |
764 | |
765 | return *this; |
766 | } |
767 | |
768 | /// Assignment operator. |
769 | /// |
770 | /// The RHS value is assigned to *this. If the significant bits in RHS exceed |
771 | /// the bit width, the excess bits are truncated. If the bit width is larger |
772 | /// than 64, the value is zero filled in the unspecified high order bits. |
773 | /// |
774 | /// \returns *this after assignment of RHS value. |
775 | APInt &operator=(uint64_t RHS) { |
776 | if (isSingleWord()) { |
777 | U.VAL = RHS; |
778 | clearUnusedBits(); |
779 | } else { |
780 | U.pVal[0] = RHS; |
781 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
782 | } |
783 | return *this; |
784 | } |
785 | |
786 | /// Bitwise AND assignment operator. |
787 | /// |
788 | /// Performs a bitwise AND operation on this APInt and RHS. The result is |
789 | /// assigned to *this. |
790 | /// |
791 | /// \returns *this after ANDing with RHS. |
792 | APInt &operator&=(const APInt &RHS) { |
793 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")((BitWidth == RHS.BitWidth && "Bit widths must be the same" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 793, __PRETTY_FUNCTION__)); |
794 | if (isSingleWord()) |
795 | U.VAL &= RHS.U.VAL; |
796 | else |
797 | AndAssignSlowCase(RHS); |
798 | return *this; |
799 | } |
800 | |
801 | /// Bitwise AND assignment operator. |
802 | /// |
803 | /// Performs a bitwise AND operation on this APInt and RHS. RHS is |
804 | /// logically zero-extended or truncated to match the bit-width of |
805 | /// the LHS. |
806 | APInt &operator&=(uint64_t RHS) { |
807 | if (isSingleWord()) { |
808 | U.VAL &= RHS; |
809 | return *this; |
810 | } |
811 | U.pVal[0] &= RHS; |
812 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
813 | return *this; |
814 | } |
815 | |
816 | /// Bitwise OR assignment operator. |
817 | /// |
818 | /// Performs a bitwise OR operation on this APInt and RHS. The result is |
819 | /// assigned *this; |
820 | /// |
821 | /// \returns *this after ORing with RHS. |
822 | APInt &operator|=(const APInt &RHS) { |
823 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")((BitWidth == RHS.BitWidth && "Bit widths must be the same" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 823, __PRETTY_FUNCTION__)); |
824 | if (isSingleWord()) |
825 | U.VAL |= RHS.U.VAL; |
826 | else |
827 | OrAssignSlowCase(RHS); |
828 | return *this; |
829 | } |
830 | |
831 | /// Bitwise OR assignment operator. |
832 | /// |
833 | /// Performs a bitwise OR operation on this APInt and RHS. RHS is |
834 | /// logically zero-extended or truncated to match the bit-width of |
835 | /// the LHS. |
836 | APInt &operator|=(uint64_t RHS) { |
837 | if (isSingleWord()) { |
838 | U.VAL |= RHS; |
839 | clearUnusedBits(); |
840 | } else { |
841 | U.pVal[0] |= RHS; |
842 | } |
843 | return *this; |
844 | } |
845 | |
846 | /// Bitwise XOR assignment operator. |
847 | /// |
848 | /// Performs a bitwise XOR operation on this APInt and RHS. The result is |
849 | /// assigned to *this. |
850 | /// |
851 | /// \returns *this after XORing with RHS. |
852 | APInt &operator^=(const APInt &RHS) { |
853 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")((BitWidth == RHS.BitWidth && "Bit widths must be the same" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 853, __PRETTY_FUNCTION__)); |
854 | if (isSingleWord()) |
855 | U.VAL ^= RHS.U.VAL; |
856 | else |
857 | XorAssignSlowCase(RHS); |
858 | return *this; |
859 | } |
860 | |
861 | /// Bitwise XOR assignment operator. |
862 | /// |
863 | /// Performs a bitwise XOR operation on this APInt and RHS. RHS is |
864 | /// logically zero-extended or truncated to match the bit-width of |
865 | /// the LHS. |
866 | APInt &operator^=(uint64_t RHS) { |
867 | if (isSingleWord()) { |
868 | U.VAL ^= RHS; |
869 | clearUnusedBits(); |
870 | } else { |
871 | U.pVal[0] ^= RHS; |
872 | } |
873 | return *this; |
874 | } |
875 | |
876 | /// Multiplication assignment operator. |
877 | /// |
878 | /// Multiplies this APInt by RHS and assigns the result to *this. |
879 | /// |
880 | /// \returns *this |
881 | APInt &operator*=(const APInt &RHS); |
882 | APInt &operator*=(uint64_t RHS); |
883 | |
884 | /// Addition assignment operator. |
885 | /// |
886 | /// Adds RHS to *this and assigns the result to *this. |
887 | /// |
888 | /// \returns *this |
889 | APInt &operator+=(const APInt &RHS); |
890 | APInt &operator+=(uint64_t RHS); |
891 | |
892 | /// Subtraction assignment operator. |
893 | /// |
894 | /// Subtracts RHS from *this and assigns the result to *this. |
895 | /// |
896 | /// \returns *this |
897 | APInt &operator-=(const APInt &RHS); |
898 | APInt &operator-=(uint64_t RHS); |
899 | |
900 | /// Left-shift assignment function. |
901 | /// |
902 | /// Shifts *this left by shiftAmt and assigns the result to *this. |
903 | /// |
904 | /// \returns *this after shifting left by ShiftAmt |
905 | APInt &operator<<=(unsigned ShiftAmt) { |
906 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")((ShiftAmt <= BitWidth && "Invalid shift amount") ? static_cast<void> (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 906, __PRETTY_FUNCTION__)); |
907 | if (isSingleWord()) { |
908 | if (ShiftAmt == BitWidth) |
909 | U.VAL = 0; |
910 | else |
911 | U.VAL <<= ShiftAmt; |
912 | return clearUnusedBits(); |
913 | } |
914 | shlSlowCase(ShiftAmt); |
915 | return *this; |
916 | } |
917 | |
918 | /// Left-shift assignment function. |
919 | /// |
920 | /// Shifts *this left by shiftAmt and assigns the result to *this. |
921 | /// |
922 | /// \returns *this after shifting left by ShiftAmt |
923 | APInt &operator<<=(const APInt &ShiftAmt); |
924 | |
925 | /// @} |
926 | /// \name Binary Operators |
927 | /// @{ |
928 | |
929 | /// Multiplication operator. |
930 | /// |
931 | /// Multiplies this APInt by RHS and returns the result. |
932 | APInt operator*(const APInt &RHS) const; |
933 | |
934 | /// Left logical shift operator. |
935 | /// |
936 | /// Shifts this APInt left by \p Bits and returns the result. |
937 | APInt operator<<(unsigned Bits) const { return shl(Bits); } |
938 | |
939 | /// Left logical shift operator. |
940 | /// |
941 | /// Shifts this APInt left by \p Bits and returns the result. |
942 | APInt operator<<(const APInt &Bits) const { return shl(Bits); } |
943 | |
944 | /// Arithmetic right-shift function. |
945 | /// |
946 | /// Arithmetic right-shift this APInt by shiftAmt. |
947 | APInt ashr(unsigned ShiftAmt) const { |
948 | APInt R(*this); |
949 | R.ashrInPlace(ShiftAmt); |
950 | return R; |
951 | } |
952 | |
953 | /// Arithmetic right-shift this APInt by ShiftAmt in place. |
954 | void ashrInPlace(unsigned ShiftAmt) { |
955 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")((ShiftAmt <= BitWidth && "Invalid shift amount") ? static_cast<void> (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 955, __PRETTY_FUNCTION__)); |
956 | if (isSingleWord()) { |
957 | int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); |
958 | if (ShiftAmt == BitWidth) |
959 | U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit. |
960 | else |
961 | U.VAL = SExtVAL >> ShiftAmt; |
962 | clearUnusedBits(); |
963 | return; |
964 | } |
965 | ashrSlowCase(ShiftAmt); |
966 | } |
967 | |
968 | /// Logical right-shift function. |
969 | /// |
970 | /// Logical right-shift this APInt by shiftAmt. |
971 | APInt lshr(unsigned shiftAmt) const { |
972 | APInt R(*this); |
973 | R.lshrInPlace(shiftAmt); |
974 | return R; |
975 | } |
976 | |
977 | /// Logical right-shift this APInt by ShiftAmt in place. |
978 | void lshrInPlace(unsigned ShiftAmt) { |
979 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")((ShiftAmt <= BitWidth && "Invalid shift amount") ? static_cast<void> (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 979, __PRETTY_FUNCTION__)); |
980 | if (isSingleWord()) { |
981 | if (ShiftAmt == BitWidth) |
982 | U.VAL = 0; |
983 | else |
984 | U.VAL >>= ShiftAmt; |
985 | return; |
986 | } |
987 | lshrSlowCase(ShiftAmt); |
988 | } |
989 | |
990 | /// Left-shift function. |
991 | /// |
992 | /// Left-shift this APInt by shiftAmt. |
993 | APInt shl(unsigned shiftAmt) const { |
994 | APInt R(*this); |
995 | R <<= shiftAmt; |
996 | return R; |
997 | } |
998 | |
999 | /// Rotate left by rotateAmt. |
1000 | APInt rotl(unsigned rotateAmt) const; |
1001 | |
1002 | /// Rotate right by rotateAmt. |
1003 | APInt rotr(unsigned rotateAmt) const; |
1004 | |
1005 | /// Arithmetic right-shift function. |
1006 | /// |
1007 | /// Arithmetic right-shift this APInt by shiftAmt. |
1008 | APInt ashr(const APInt &ShiftAmt) const { |
1009 | APInt R(*this); |
1010 | R.ashrInPlace(ShiftAmt); |
1011 | return R; |
1012 | } |
1013 | |
1014 | /// Arithmetic right-shift this APInt by shiftAmt in place. |
1015 | void ashrInPlace(const APInt &shiftAmt); |
1016 | |
1017 | /// Logical right-shift function. |
1018 | /// |
1019 | /// Logical right-shift this APInt by shiftAmt. |
1020 | APInt lshr(const APInt &ShiftAmt) const { |
1021 | APInt R(*this); |
1022 | R.lshrInPlace(ShiftAmt); |
1023 | return R; |
1024 | } |
1025 | |
1026 | /// Logical right-shift this APInt by ShiftAmt in place. |
1027 | void lshrInPlace(const APInt &ShiftAmt); |
1028 | |
1029 | /// Left-shift function. |
1030 | /// |
1031 | /// Left-shift this APInt by shiftAmt. |
1032 | APInt shl(const APInt &ShiftAmt) const { |
1033 | APInt R(*this); |
1034 | R <<= ShiftAmt; |
1035 | return R; |
1036 | } |
1037 | |
1038 | /// Rotate left by rotateAmt. |
1039 | APInt rotl(const APInt &rotateAmt) const; |
1040 | |
1041 | /// Rotate right by rotateAmt. |
1042 | APInt rotr(const APInt &rotateAmt) const; |
1043 | |
1044 | /// Unsigned division operation. |
1045 | /// |
1046 | /// Perform an unsigned divide operation on this APInt by RHS. Both this and |
1047 | /// RHS are treated as unsigned quantities for purposes of this division. |
1048 | /// |
1049 | /// \returns a new APInt value containing the division result, rounded towards |
1050 | /// zero. |
1051 | APInt udiv(const APInt &RHS) const; |
1052 | APInt udiv(uint64_t RHS) const; |
1053 | |
1054 | /// Signed division function for APInt. |
1055 | /// |
1056 | /// Signed divide this APInt by APInt RHS. |
1057 | /// |
1058 | /// The result is rounded towards zero. |
1059 | APInt sdiv(const APInt &RHS) const; |
1060 | APInt sdiv(int64_t RHS) const; |
1061 | |
1062 | /// Unsigned remainder operation. |
1063 | /// |
1064 | /// Perform an unsigned remainder operation on this APInt with RHS being the |
1065 | /// divisor. Both this and RHS are treated as unsigned quantities for purposes |
1066 | /// of this operation. Note that this is a true remainder operation and not a |
1067 | /// modulo operation because the sign follows the sign of the dividend which |
1068 | /// is *this. |
1069 | /// |
1070 | /// \returns a new APInt value containing the remainder result |
1071 | APInt urem(const APInt &RHS) const; |
1072 | uint64_t urem(uint64_t RHS) const; |
1073 | |
1074 | /// Function for signed remainder operation. |
1075 | /// |
1076 | /// Signed remainder operation on APInt. |
1077 | APInt srem(const APInt &RHS) const; |
1078 | int64_t srem(int64_t RHS) const; |
1079 | |
1080 | /// Dual division/remainder interface. |
1081 | /// |
1082 | /// Sometimes it is convenient to divide two APInt values and obtain both the |
1083 | /// quotient and remainder. This function does both operations in the same |
1084 | /// computation making it a little more efficient. The pair of input arguments |
1085 | /// may overlap with the pair of output arguments. It is safe to call |
1086 | /// udivrem(X, Y, X, Y), for example. |
1087 | static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1088 | APInt &Remainder); |
1089 | static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
1090 | uint64_t &Remainder); |
1091 | |
1092 | static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1093 | APInt &Remainder); |
1094 | static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, |
1095 | int64_t &Remainder); |
1096 | |
1097 | // Operations that return overflow indicators. |
1098 | APInt sadd_ov(const APInt &RHS, bool &Overflow) const; |
1099 | APInt uadd_ov(const APInt &RHS, bool &Overflow) const; |
1100 | APInt ssub_ov(const APInt &RHS, bool &Overflow) const; |
1101 | APInt usub_ov(const APInt &RHS, bool &Overflow) const; |
1102 | APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; |
1103 | APInt smul_ov(const APInt &RHS, bool &Overflow) const; |
1104 | APInt umul_ov(const APInt &RHS, bool &Overflow) const; |
1105 | APInt sshl_ov(const APInt &Amt, bool &Overflow) const; |
1106 | APInt ushl_ov(const APInt &Amt, bool &Overflow) const; |
1107 | |
1108 | /// Array-indexing support. |
1109 | /// |
1110 | /// \returns the bit value at bitPosition |
1111 | bool operator[](unsigned bitPosition) const { |
1112 | assert(bitPosition < getBitWidth() && "Bit position out of bounds!")((bitPosition < getBitWidth() && "Bit position out of bounds!" ) ? static_cast<void> (0) : __assert_fail ("bitPosition < getBitWidth() && \"Bit position out of bounds!\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1112, __PRETTY_FUNCTION__)); |
1113 | return (maskBit(bitPosition) & getWord(bitPosition)) != 0; |
1114 | } |
1115 | |
1116 | /// @} |
1117 | /// \name Comparison Operators |
1118 | /// @{ |
1119 | |
1120 | /// Equality operator. |
1121 | /// |
1122 | /// Compares this APInt with RHS for the validity of the equality |
1123 | /// relationship. |
1124 | bool operator==(const APInt &RHS) const { |
1125 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths")((BitWidth == RHS.BitWidth && "Comparison requires equal bit widths" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Comparison requires equal bit widths\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1125, __PRETTY_FUNCTION__)); |
1126 | if (isSingleWord()) |
1127 | return U.VAL == RHS.U.VAL; |
1128 | return EqualSlowCase(RHS); |
1129 | } |
1130 | |
1131 | /// Equality operator. |
1132 | /// |
1133 | /// Compares this APInt with a uint64_t for the validity of the equality |
1134 | /// relationship. |
1135 | /// |
1136 | /// \returns true if *this == Val |
1137 | bool operator==(uint64_t Val) const { |
1138 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; |
1139 | } |
1140 | |
1141 | /// Equality comparison. |
1142 | /// |
1143 | /// Compares this APInt with RHS for the validity of the equality |
1144 | /// relationship. |
1145 | /// |
1146 | /// \returns true if *this == Val |
1147 | bool eq(const APInt &RHS) const { return (*this) == RHS; } |
1148 | |
1149 | /// Inequality operator. |
1150 | /// |
1151 | /// Compares this APInt with RHS for the validity of the inequality |
1152 | /// relationship. |
1153 | /// |
1154 | /// \returns true if *this != Val |
1155 | bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } |
1156 | |
1157 | /// Inequality operator. |
1158 | /// |
1159 | /// Compares this APInt with a uint64_t for the validity of the inequality |
1160 | /// relationship. |
1161 | /// |
1162 | /// \returns true if *this != Val |
1163 | bool operator!=(uint64_t Val) const { return !((*this) == Val); } |
1164 | |
1165 | /// Inequality comparison |
1166 | /// |
1167 | /// Compares this APInt with RHS for the validity of the inequality |
1168 | /// relationship. |
1169 | /// |
1170 | /// \returns true if *this != Val |
1171 | bool ne(const APInt &RHS) const { return !((*this) == RHS); } |
1172 | |
1173 | /// Unsigned less than comparison |
1174 | /// |
1175 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1176 | /// the validity of the less-than relationship. |
1177 | /// |
1178 | /// \returns true if *this < RHS when both are considered unsigned. |
1179 | bool ult(const APInt &RHS) const { return compare(RHS) < 0; } |
1180 | |
1181 | /// Unsigned less than comparison |
1182 | /// |
1183 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1184 | /// the validity of the less-than relationship. |
1185 | /// |
1186 | /// \returns true if *this < RHS when considered unsigned. |
1187 | bool ult(uint64_t RHS) const { |
1188 | // Only need to check active bits if not a single word. |
1189 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; |
1190 | } |
1191 | |
1192 | /// Signed less than comparison |
1193 | /// |
1194 | /// Regards both *this and RHS as signed quantities and compares them for |
1195 | /// validity of the less-than relationship. |
1196 | /// |
1197 | /// \returns true if *this < RHS when both are considered signed. |
1198 | bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } |
1199 | |
1200 | /// Signed less than comparison |
1201 | /// |
1202 | /// Regards both *this as a signed quantity and compares it with RHS for |
1203 | /// the validity of the less-than relationship. |
1204 | /// |
1205 | /// \returns true if *this < RHS when considered signed. |
1206 | bool slt(int64_t RHS) const { |
1207 | return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative() |
1208 | : getSExtValue() < RHS; |
1209 | } |
1210 | |
1211 | /// Unsigned less or equal comparison |
1212 | /// |
1213 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1214 | /// validity of the less-or-equal relationship. |
1215 | /// |
1216 | /// \returns true if *this <= RHS when both are considered unsigned. |
1217 | bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } |
1218 | |
1219 | /// Unsigned less or equal comparison |
1220 | /// |
1221 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1222 | /// the validity of the less-or-equal relationship. |
1223 | /// |
1224 | /// \returns true if *this <= RHS when considered unsigned. |
1225 | bool ule(uint64_t RHS) const { return !ugt(RHS); } |
1226 | |
1227 | /// Signed less or equal comparison |
1228 | /// |
1229 | /// Regards both *this and RHS as signed quantities and compares them for |
1230 | /// validity of the less-or-equal relationship. |
1231 | /// |
1232 | /// \returns true if *this <= RHS when both are considered signed. |
1233 | bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } |
1234 | |
1235 | /// Signed less or equal comparison |
1236 | /// |
1237 | /// Regards both *this as a signed quantity and compares it with RHS for the |
1238 | /// validity of the less-or-equal relationship. |
1239 | /// |
1240 | /// \returns true if *this <= RHS when considered signed. |
1241 | bool sle(uint64_t RHS) const { return !sgt(RHS); } |
1242 | |
1243 | /// Unsigned greather than comparison |
1244 | /// |
1245 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1246 | /// the validity of the greater-than relationship. |
1247 | /// |
1248 | /// \returns true if *this > RHS when both are considered unsigned. |
1249 | bool ugt(const APInt &RHS) const { return !ule(RHS); } |
1250 | |
1251 | /// Unsigned greater than comparison |
1252 | /// |
1253 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1254 | /// the validity of the greater-than relationship. |
1255 | /// |
1256 | /// \returns true if *this > RHS when considered unsigned. |
1257 | bool ugt(uint64_t RHS) const { |
1258 | // Only need to check active bits if not a single word. |
1259 | return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; |
1260 | } |
1261 | |
1262 | /// Signed greather than comparison |
1263 | /// |
1264 | /// Regards both *this and RHS as signed quantities and compares them for the |
1265 | /// validity of the greater-than relationship. |
1266 | /// |
1267 | /// \returns true if *this > RHS when both are considered signed. |
1268 | bool sgt(const APInt &RHS) const { return !sle(RHS); } |
1269 | |
1270 | /// Signed greater than comparison |
1271 | /// |
1272 | /// Regards both *this as a signed quantity and compares it with RHS for |
1273 | /// the validity of the greater-than relationship. |
1274 | /// |
1275 | /// \returns true if *this > RHS when considered signed. |
1276 | bool sgt(int64_t RHS) const { |
1277 | return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative() |
1278 | : getSExtValue() > RHS; |
1279 | } |
1280 | |
1281 | /// Unsigned greater or equal comparison |
1282 | /// |
1283 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1284 | /// validity of the greater-or-equal relationship. |
1285 | /// |
1286 | /// \returns true if *this >= RHS when both are considered unsigned. |
1287 | bool uge(const APInt &RHS) const { return !ult(RHS); } |
1288 | |
1289 | /// Unsigned greater or equal comparison |
1290 | /// |
1291 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1292 | /// the validity of the greater-or-equal relationship. |
1293 | /// |
1294 | /// \returns true if *this >= RHS when considered unsigned. |
1295 | bool uge(uint64_t RHS) const { return !ult(RHS); } |
1296 | |
1297 | /// Signed greater or equal comparison |
1298 | /// |
1299 | /// Regards both *this and RHS as signed quantities and compares them for |
1300 | /// validity of the greater-or-equal relationship. |
1301 | /// |
1302 | /// \returns true if *this >= RHS when both are considered signed. |
1303 | bool sge(const APInt &RHS) const { return !slt(RHS); } |
1304 | |
1305 | /// Signed greater or equal comparison |
1306 | /// |
1307 | /// Regards both *this as a signed quantity and compares it with RHS for |
1308 | /// the validity of the greater-or-equal relationship. |
1309 | /// |
1310 | /// \returns true if *this >= RHS when considered signed. |
1311 | bool sge(int64_t RHS) const { return !slt(RHS); } |
1312 | |
1313 | /// This operation tests if there are any pairs of corresponding bits |
1314 | /// between this APInt and RHS that are both set. |
1315 | bool intersects(const APInt &RHS) const { |
1316 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")((BitWidth == RHS.BitWidth && "Bit widths must be the same" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1316, __PRETTY_FUNCTION__)); |
1317 | if (isSingleWord()) |
1318 | return (U.VAL & RHS.U.VAL) != 0; |
1319 | return intersectsSlowCase(RHS); |
1320 | } |
1321 | |
1322 | /// This operation checks that all bits set in this APInt are also set in RHS. |
1323 | bool isSubsetOf(const APInt &RHS) const { |
1324 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")((BitWidth == RHS.BitWidth && "Bit widths must be the same" ) ? static_cast<void> (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1324, __PRETTY_FUNCTION__)); |
1325 | if (isSingleWord()) |
1326 | return (U.VAL & ~RHS.U.VAL) == 0; |
1327 | return isSubsetOfSlowCase(RHS); |
1328 | } |
1329 | |
1330 | /// @} |
1331 | /// \name Resizing Operators |
1332 | /// @{ |
1333 | |
1334 | /// Truncate to new width. |
1335 | /// |
1336 | /// Truncate the APInt to a specified width. It is an error to specify a width |
1337 | /// that is greater than or equal to the current width. |
1338 | APInt trunc(unsigned width) const; |
1339 | |
1340 | /// Sign extend to a new width. |
1341 | /// |
1342 | /// This operation sign extends the APInt to a new width. If the high order |
1343 | /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. |
1344 | /// It is an error to specify a width that is less than or equal to the |
1345 | /// current width. |
1346 | APInt sext(unsigned width) const; |
1347 | |
1348 | /// Zero extend to a new width. |
1349 | /// |
1350 | /// This operation zero extends the APInt to a new width. The high order bits |
1351 | /// are filled with 0 bits. It is an error to specify a width that is less |
1352 | /// than or equal to the current width. |
1353 | APInt zext(unsigned width) const; |
1354 | |
1355 | /// Sign extend or truncate to width |
1356 | /// |
1357 | /// Make this APInt have the bit width given by \p width. The value is sign |
1358 | /// extended, truncated, or left alone to make it that width. |
1359 | APInt sextOrTrunc(unsigned width) const; |
1360 | |
1361 | /// Zero extend or truncate to width |
1362 | /// |
1363 | /// Make this APInt have the bit width given by \p width. The value is zero |
1364 | /// extended, truncated, or left alone to make it that width. |
1365 | APInt zextOrTrunc(unsigned width) const; |
1366 | |
1367 | /// Sign extend or truncate to width |
1368 | /// |
1369 | /// Make this APInt have the bit width given by \p width. The value is sign |
1370 | /// extended, or left alone to make it that width. |
1371 | APInt sextOrSelf(unsigned width) const; |
1372 | |
1373 | /// Zero extend or truncate to width |
1374 | /// |
1375 | /// Make this APInt have the bit width given by \p width. The value is zero |
1376 | /// extended, or left alone to make it that width. |
1377 | APInt zextOrSelf(unsigned width) const; |
1378 | |
1379 | /// @} |
1380 | /// \name Bit Manipulation Operators |
1381 | /// @{ |
1382 | |
1383 | /// Set every bit to 1. |
1384 | void setAllBits() { |
1385 | if (isSingleWord()) |
1386 | U.VAL = WORDTYPE_MAX; |
1387 | else |
1388 | // Set all the bits in all the words. |
1389 | memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); |
1390 | // Clear the unused ones |
1391 | clearUnusedBits(); |
1392 | } |
1393 | |
1394 | /// Set a given bit to 1. |
1395 | /// |
1396 | /// Set the given bit to 1 whose position is given as "bitPosition". |
1397 | void setBit(unsigned BitPosition) { |
1398 | assert(BitPosition <= BitWidth && "BitPosition out of range")((BitPosition <= BitWidth && "BitPosition out of range" ) ? static_cast<void> (0) : __assert_fail ("BitPosition <= BitWidth && \"BitPosition out of range\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1398, __PRETTY_FUNCTION__)); |
1399 | WordType Mask = maskBit(BitPosition); |
1400 | if (isSingleWord()) |
1401 | U.VAL |= Mask; |
1402 | else |
1403 | U.pVal[whichWord(BitPosition)] |= Mask; |
1404 | } |
1405 | |
1406 | /// Set the sign bit to 1. |
1407 | void setSignBit() { |
1408 | setBit(BitWidth - 1); |
1409 | } |
1410 | |
1411 | /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1. |
1412 | void setBits(unsigned loBit, unsigned hiBit) { |
1413 | assert(hiBit <= BitWidth && "hiBit out of range")((hiBit <= BitWidth && "hiBit out of range") ? static_cast <void> (0) : __assert_fail ("hiBit <= BitWidth && \"hiBit out of range\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1413, __PRETTY_FUNCTION__)); |
1414 | assert(loBit <= BitWidth && "loBit out of range")((loBit <= BitWidth && "loBit out of range") ? static_cast <void> (0) : __assert_fail ("loBit <= BitWidth && \"loBit out of range\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1414, __PRETTY_FUNCTION__)); |
1415 | assert(loBit <= hiBit && "loBit greater than hiBit")((loBit <= hiBit && "loBit greater than hiBit") ? static_cast <void> (0) : __assert_fail ("loBit <= hiBit && \"loBit greater than hiBit\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1415, __PRETTY_FUNCTION__)); |
1416 | if (loBit == hiBit) |
1417 | return; |
1418 | if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { |
1419 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); |
1420 | mask <<= loBit; |
1421 | if (isSingleWord()) |
1422 | U.VAL |= mask; |
1423 | else |
1424 | U.pVal[0] |= mask; |
1425 | } else { |
1426 | setBitsSlowCase(loBit, hiBit); |
1427 | } |
1428 | } |
1429 | |
1430 | /// Set the top bits starting from loBit. |
1431 | void setBitsFrom(unsigned loBit) { |
1432 | return setBits(loBit, BitWidth); |
1433 | } |
1434 | |
1435 | /// Set the bottom loBits bits. |
1436 | void setLowBits(unsigned loBits) { |
1437 | return setBits(0, loBits); |
1438 | } |
1439 | |
1440 | /// Set the top hiBits bits. |
1441 | void setHighBits(unsigned hiBits) { |
1442 | return setBits(BitWidth - hiBits, BitWidth); |
1443 | } |
1444 | |
1445 | /// Set every bit to 0. |
1446 | void clearAllBits() { |
1447 | if (isSingleWord()) |
1448 | U.VAL = 0; |
1449 | else |
1450 | memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); |
1451 | } |
1452 | |
1453 | /// Set a given bit to 0. |
1454 | /// |
1455 | /// Set the given bit to 0 whose position is given as "bitPosition". |
1456 | void clearBit(unsigned BitPosition) { |
1457 | assert(BitPosition <= BitWidth && "BitPosition out of range")((BitPosition <= BitWidth && "BitPosition out of range" ) ? static_cast<void> (0) : __assert_fail ("BitPosition <= BitWidth && \"BitPosition out of range\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1457, __PRETTY_FUNCTION__)); |
1458 | WordType Mask = ~maskBit(BitPosition); |
1459 | if (isSingleWord()) |
1460 | U.VAL &= Mask; |
1461 | else |
1462 | U.pVal[whichWord(BitPosition)] &= Mask; |
1463 | } |
1464 | |
1465 | /// Set the sign bit to 0. |
1466 | void clearSignBit() { |
1467 | clearBit(BitWidth - 1); |
1468 | } |
1469 | |
1470 | /// Toggle every bit to its opposite value. |
1471 | void flipAllBits() { |
1472 | if (isSingleWord()) { |
1473 | U.VAL ^= WORDTYPE_MAX; |
1474 | clearUnusedBits(); |
1475 | } else { |
1476 | flipAllBitsSlowCase(); |
1477 | } |
1478 | } |
1479 | |
1480 | /// Toggles a given bit to its opposite value. |
1481 | /// |
1482 | /// Toggle a given bit to its opposite value whose position is given |
1483 | /// as "bitPosition". |
1484 | void flipBit(unsigned bitPosition); |
1485 | |
1486 | /// Negate this APInt in place. |
1487 | void negate() { |
1488 | flipAllBits(); |
1489 | ++(*this); |
1490 | } |
1491 | |
1492 | /// Insert the bits from a smaller APInt starting at bitPosition. |
1493 | void insertBits(const APInt &SubBits, unsigned bitPosition); |
1494 | |
1495 | /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits). |
1496 | APInt extractBits(unsigned numBits, unsigned bitPosition) const; |
1497 | |
1498 | /// @} |
1499 | /// \name Value Characterization Functions |
1500 | /// @{ |
1501 | |
1502 | /// Return the number of bits in the APInt. |
1503 | unsigned getBitWidth() const { return BitWidth; } |
1504 | |
1505 | /// Get the number of words. |
1506 | /// |
1507 | /// Here one word's bitwidth equals to that of uint64_t. |
1508 | /// |
1509 | /// \returns the number of words to hold the integer value of this APInt. |
1510 | unsigned getNumWords() const { return getNumWords(BitWidth); } |
1511 | |
1512 | /// Get the number of words. |
1513 | /// |
1514 | /// *NOTE* Here one word's bitwidth equals to that of uint64_t. |
1515 | /// |
1516 | /// \returns the number of words to hold the integer value with a given bit |
1517 | /// width. |
1518 | static unsigned getNumWords(unsigned BitWidth) { |
1519 | return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
1520 | } |
1521 | |
1522 | /// Compute the number of active bits in the value |
1523 | /// |
1524 | /// This function returns the number of active bits which is defined as the |
1525 | /// bit width minus the number of leading zeros. This is used in several |
1526 | /// computations to see how "wide" the value is. |
1527 | unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); } |
1528 | |
1529 | /// Compute the number of active words in the value of this APInt. |
1530 | /// |
1531 | /// This is used in conjunction with getActiveData to extract the raw value of |
1532 | /// the APInt. |
1533 | unsigned getActiveWords() const { |
1534 | unsigned numActiveBits = getActiveBits(); |
1535 | return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; |
1536 | } |
1537 | |
1538 | /// Get the minimum bit size for this signed APInt |
1539 | /// |
1540 | /// Computes the minimum bit width for this APInt while considering it to be a |
1541 | /// signed (and probably negative) value. If the value is not negative, this |
1542 | /// function returns the same value as getActiveBits()+1. Otherwise, it |
1543 | /// returns the smallest bit width that will retain the negative value. For |
1544 | /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so |
1545 | /// for -1, this function will always return 1. |
1546 | unsigned getMinSignedBits() const { |
1547 | if (isNegative()) |
1548 | return BitWidth - countLeadingOnes() + 1; |
1549 | return getActiveBits() + 1; |
1550 | } |
1551 | |
1552 | /// Get zero extended value |
1553 | /// |
1554 | /// This method attempts to return the value of this APInt as a zero extended |
1555 | /// uint64_t. The bitwidth must be <= 64 or the value must fit within a |
1556 | /// uint64_t. Otherwise an assertion will result. |
1557 | uint64_t getZExtValue() const { |
1558 | if (isSingleWord()) |
1559 | return U.VAL; |
1560 | assert(getActiveBits() <= 64 && "Too many bits for uint64_t")((getActiveBits() <= 64 && "Too many bits for uint64_t" ) ? static_cast<void> (0) : __assert_fail ("getActiveBits() <= 64 && \"Too many bits for uint64_t\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1560, __PRETTY_FUNCTION__)); |
1561 | return U.pVal[0]; |
1562 | } |
1563 | |
1564 | /// Get sign extended value |
1565 | /// |
1566 | /// This method attempts to return the value of this APInt as a sign extended |
1567 | /// int64_t. The bit width must be <= 64 or the value must fit within an |
1568 | /// int64_t. Otherwise an assertion will result. |
1569 | int64_t getSExtValue() const { |
1570 | if (isSingleWord()) |
1571 | return SignExtend64(U.VAL, BitWidth); |
1572 | assert(getMinSignedBits() <= 64 && "Too many bits for int64_t")((getMinSignedBits() <= 64 && "Too many bits for int64_t" ) ? static_cast<void> (0) : __assert_fail ("getMinSignedBits() <= 64 && \"Too many bits for int64_t\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/APInt.h" , 1572, __PRETTY_FUNCTION__)); |
1573 | return int64_t(U.pVal[0]); |
1574 | } |
1575 | |
1576 | /// Get bits required for string value. |
1577 | /// |
1578 | /// This method determines how many bits are required to hold the APInt |
1579 | /// equivalent of the string given by \p str. |
1580 | static unsigned getBitsNeeded(StringRef str, uint8_t radix); |
1581 | |
1582 | /// The APInt version of the countLeadingZeros functions in |
1583 | /// MathExtras.h. |
1584 | /// |
1585 | /// It counts the number of zeros from the most significant bit to the first |
1586 | /// one bit. |
1587 | /// |
1588 | /// \returns BitWidth if the value is zero, otherwise returns the number of |
1589 | /// zeros from the most significant bit to the first one bits. |
1590 | unsigned countLeadingZeros() const { |
1591 | if (isSingleWord()) { |
1592 | unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; |
1593 | return llvm::countLeadingZeros(U.VAL) - unusedBits; |
1594 | } |
1595 | return countLeadingZerosSlowCase(); |
1596 | } |
1597 | |
1598 | /// Count the number of leading one bits. |
1599 | /// |
1600 | /// This function is an APInt version of the countLeadingOnes |
1601 | /// functions in MathExtras.h. It counts the number of ones from the most |
1602 | /// significant bit to the first zero bit. |
1603 | /// |
1604 | /// \returns 0 if the high order bit is not set, otherwise returns the number |
1605 | /// of 1 bits from the most significant to the least |
1606 | unsigned countLeadingOnes() const { |
1607 | if (isSingleWord()) |
1608 | return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); |
1609 | return countLeadingOnesSlowCase(); |
1610 | } |
1611 | |
1612 | /// Computes the number of leading bits of this APInt that are equal to its |
1613 | /// sign bit. |
1614 | unsigned getNumSignBits() const { |
1615 | return isNegative() ? countLeadingOnes() : countLeadingZeros(); |
1616 | } |
1617 | |
1618 | /// Count the number of trailing zero bits. |
1619 | /// |
1620 | /// This function is an APInt version of the countTrailingZeros |
1621 | /// functions in MathExtras.h. It counts the number of zeros from the least |
1622 | /// significant bit to the first set bit. |
1623 | /// |
1624 | /// \returns BitWidth if the value is zero, otherwise returns the number of |
1625 | /// zeros from the least significant bit to the first one bit. |
1626 | unsigned countTrailingZeros() const { |
1627 | if (isSingleWord()) |
1628 | return std::min(unsigned(llvm::countTrailingZeros(U.VAL)), BitWidth); |
1629 | return countTrailingZerosSlowCase(); |
1630 | } |
1631 | |
1632 | /// Count the number of trailing one bits. |
1633 | /// |
1634 | /// This function is an APInt version of the countTrailingOnes |
1635 | /// functions in MathExtras.h. It counts the number of ones from the least |
1636 | /// significant bit to the first zero bit. |
1637 | /// |
1638 | /// \returns BitWidth if the value is all ones, otherwise returns the number |
1639 | /// of ones from the least significant bit to the first zero bit. |
1640 | unsigned countTrailingOnes() const { |
1641 | if (isSingleWord()) |
1642 | return llvm::countTrailingOnes(U.VAL); |
1643 | return countTrailingOnesSlowCase(); |
1644 | } |
1645 | |
1646 | /// Count the number of bits set. |
1647 | /// |
1648 | /// This function is an APInt version of the countPopulation functions |
1649 | /// in MathExtras.h. It counts the number of 1 bits in the APInt value. |
1650 | /// |
1651 | /// \returns 0 if the value is zero, otherwise returns the number of set bits. |
1652 | unsigned countPopulation() const { |
1653 | if (isSingleWord()) |
1654 | return llvm::countPopulation(U.VAL); |
1655 | return countPopulationSlowCase(); |
1656 | } |
1657 | |
1658 | /// @} |
1659 | /// \name Conversion Functions |
1660 | /// @{ |
1661 | void print(raw_ostream &OS, bool isSigned) const; |
1662 | |
1663 | /// Converts an APInt to a string and append it to Str. Str is commonly a |
1664 | /// SmallString. |
1665 | void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, |
1666 | bool formatAsCLiteral = false) const; |
1667 | |
1668 | /// Considers the APInt to be unsigned and converts it into a string in the |
1669 | /// radix given. The radix can be 2, 8, 10 16, or 36. |
1670 | void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1671 | toString(Str, Radix, false, false); |
1672 | } |
1673 | |
1674 | /// Considers the APInt to be signed and converts it into a string in the |
1675 | /// radix given. The radix can be 2, 8, 10, 16, or 36. |
1676 | void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1677 | toString(Str, Radix, true, false); |
1678 | } |
1679 | |
1680 | /// Return the APInt as a std::string. |
1681 | /// |
1682 | /// Note that this is an inefficient method. It is better to pass in a |
1683 | /// SmallVector/SmallString to the methods above to avoid thrashing the heap |
1684 | /// for the string. |
1685 | std::string toString(unsigned Radix, bool Signed) const; |
1686 | |
1687 | /// \returns a byte-swapped representation of this APInt Value. |
1688 | APInt byteSwap() const; |
1689 | |
1690 | /// \returns the value with the bit representation reversed of this APInt |
1691 | /// Value. |
1692 | APInt reverseBits() const; |
1693 | |
1694 | /// Converts this APInt to a double value. |
1695 | double roundToDouble(bool isSigned) const; |
1696 | |
1697 | /// Converts this unsigned APInt to a double value. |
1698 | double roundToDouble() const { return roundToDouble(false); } |
1699 | |
1700 | /// Converts this signed APInt to a double value. |
1701 | double signedRoundToDouble() const { return roundToDouble(true); } |
1702 | |
1703 | /// Converts APInt bits to a double |
1704 | /// |
1705 | /// The conversion does not do a translation from integer to double, it just |
1706 | /// re-interprets the bits as a double. Note that it is valid to do this on |
1707 | /// any bit width. Exactly 64 bits will be translated. |
1708 | double bitsToDouble() const { |
1709 | return BitsToDouble(getWord(0)); |
1710 | } |
1711 | |
1712 | /// Converts APInt bits to a double |
1713 | /// |
1714 | /// The conversion does not do a translation from integer to float, it just |
1715 | /// re-interprets the bits as a float. Note that it is valid to do this on |
1716 | /// any bit width. Exactly 32 bits will be translated. |
1717 | float bitsToFloat() const { |
1718 | return BitsToFloat(getWord(0)); |
1719 | } |
1720 | |
1721 | /// Converts a double to APInt bits. |
1722 | /// |
1723 | /// The conversion does not do a translation from double to integer, it just |
1724 | /// re-interprets the bits of the double. |
1725 | static APInt doubleToBits(double V) { |
1726 | return APInt(sizeof(double) * CHAR_BIT8, DoubleToBits(V)); |
1727 | } |
1728 | |
1729 | /// Converts a float to APInt bits. |
1730 | /// |
1731 | /// The conversion does not do a translation from float to integer, it just |
1732 | /// re-interprets the bits of the float. |
1733 | static APInt floatToBits(float V) { |
1734 | return APInt(sizeof(float) * CHAR_BIT8, FloatToBits(V)); |
1735 | } |
1736 | |
1737 | /// @} |
1738 | /// \name Mathematics Operations |
1739 | /// @{ |
1740 | |
1741 | /// \returns the floor log base 2 of this APInt. |
1742 | unsigned logBase2() const { return getActiveBits() - 1; } |
1743 | |
1744 | /// \returns the ceil log base 2 of this APInt. |
1745 | unsigned ceilLogBase2() const { |
1746 | APInt temp(*this); |
1747 | --temp; |
1748 | return temp.getActiveBits(); |
1749 | } |
1750 | |
1751 | /// \returns the nearest log base 2 of this APInt. Ties round up. |
1752 | /// |
1753 | /// NOTE: When we have a BitWidth of 1, we define: |
1754 | /// |
1755 | /// log2(0) = UINT32_MAX |
1756 | /// log2(1) = 0 |
1757 | /// |
1758 | /// to get around any mathematical concerns resulting from |
1759 | /// referencing 2 in a space where 2 does no exist. |
1760 | unsigned nearestLogBase2() const { |
1761 | // Special case when we have a bitwidth of 1. If VAL is 1, then we |
1762 | // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to |
1763 | // UINT32_MAX. |
1764 | if (BitWidth == 1) |
1765 | return U.VAL - 1; |
1766 | |
1767 | // Handle the zero case. |
1768 | if (isNullValue()) |
1769 | return UINT32_MAX(4294967295U); |
1770 | |
1771 | // The non-zero case is handled by computing: |
1772 | // |
1773 | // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1]. |
1774 | // |
1775 | // where x[i] is referring to the value of the ith bit of x. |
1776 | unsigned lg = logBase2(); |
1777 | return lg + unsigned((*this)[lg - 1]); |
1778 | } |
1779 | |
1780 | /// \returns the log base 2 of this APInt if its an exact power of two, -1 |
1781 | /// otherwise |
1782 | int32_t exactLogBase2() const { |
1783 | if (!isPowerOf2()) |
1784 | return -1; |
1785 | return logBase2(); |
1786 | } |
1787 | |
1788 | /// Compute the square root |
1789 | APInt sqrt() const; |
1790 | |
1791 | /// Get the absolute value; |
1792 | /// |
1793 | /// If *this is < 0 then return -(*this), otherwise *this; |
1794 | APInt abs() const { |
1795 | if (isNegative()) |
1796 | return -(*this); |
1797 | return *this; |
1798 | } |
1799 | |
1800 | /// \returns the multiplicative inverse for a given modulo. |
1801 | APInt multiplicativeInverse(const APInt &modulo) const; |
1802 | |
1803 | /// @} |
1804 | /// \name Support for division by constant |
1805 | /// @{ |
1806 | |
1807 | /// Calculate the magic number for signed division by a constant. |
1808 | struct ms; |
1809 | ms magic() const; |
1810 | |
1811 | /// Calculate the magic number for unsigned division by a constant. |
1812 | struct mu; |
1813 | mu magicu(unsigned LeadingZeros = 0) const; |
1814 | |
1815 | /// @} |
1816 | /// \name Building-block Operations for APInt and APFloat |
1817 | /// @{ |
1818 | |
1819 | // These building block operations operate on a representation of arbitrary |
1820 | // precision, two's-complement, bignum integer values. They should be |
1821 | // sufficient to implement APInt and APFloat bignum requirements. Inputs are |
1822 | // generally a pointer to the base of an array of integer parts, representing |
1823 | // an unsigned bignum, and a count of how many parts there are. |
1824 | |
1825 | /// Sets the least significant part of a bignum to the input value, and zeroes |
1826 | /// out higher parts. |
1827 | static void tcSet(WordType *, WordType, unsigned); |
1828 | |
1829 | /// Assign one bignum to another. |
1830 | static void tcAssign(WordType *, const WordType *, unsigned); |
1831 | |
1832 | /// Returns true if a bignum is zero, false otherwise. |
1833 | static bool tcIsZero(const WordType *, unsigned); |
1834 | |
1835 | /// Extract the given bit of a bignum; returns 0 or 1. Zero-based. |
1836 | static int tcExtractBit(const WordType *, unsigned bit); |
1837 | |
1838 | /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to |
1839 | /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least |
1840 | /// significant bit of DST. All high bits above srcBITS in DST are |
1841 | /// zero-filled. |
1842 | static void tcExtract(WordType *, unsigned dstCount, |
1843 | const WordType *, unsigned srcBits, |
1844 | unsigned srcLSB); |
1845 | |
1846 | /// Set the given bit of a bignum. Zero-based. |
1847 | static void tcSetBit(WordType *, unsigned bit); |
1848 | |
1849 | /// Clear the given bit of a bignum. Zero-based. |
1850 | static void tcClearBit(WordType *, unsigned bit); |
1851 | |
1852 | /// Returns the bit number of the least or most significant set bit of a |
1853 | /// number. If the input number has no bits set -1U is returned. |
1854 | static unsigned tcLSB(const WordType *, unsigned n); |
1855 | static unsigned tcMSB(const WordType *parts, unsigned n); |
1856 | |
1857 | /// Negate a bignum in-place. |
1858 | static void tcNegate(WordType *, unsigned); |
1859 | |
1860 | /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag. |
1861 | static WordType tcAdd(WordType *, const WordType *, |
1862 | WordType carry, unsigned); |
1863 | /// DST += RHS. Returns the carry flag. |
1864 | static WordType tcAddPart(WordType *, WordType, unsigned); |
1865 | |
1866 | /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag. |
1867 | static WordType tcSubtract(WordType *, const WordType *, |
1868 | WordType carry, unsigned); |
1869 | /// DST -= RHS. Returns the carry flag. |
1870 | static WordType tcSubtractPart(WordType *, WordType, unsigned); |
1871 | |
1872 | /// DST += SRC * MULTIPLIER + PART if add is true |
1873 | /// DST = SRC * MULTIPLIER + PART if add is false |
1874 | /// |
1875 | /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must |
1876 | /// start at the same point, i.e. DST == SRC. |
1877 | /// |
1878 | /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned. |
1879 | /// Otherwise DST is filled with the least significant DSTPARTS parts of the |
1880 | /// result, and if all of the omitted higher parts were zero return zero, |
1881 | /// otherwise overflow occurred and return one. |
1882 | static int tcMultiplyPart(WordType *dst, const WordType *src, |
1883 | WordType multiplier, WordType carry, |
1884 | unsigned srcParts, unsigned dstParts, |
1885 | bool add); |
1886 | |
1887 | /// DST = LHS * RHS, where DST has the same width as the operands and is |
1888 | /// filled with the least significant parts of the result. Returns one if |
1889 | /// overflow occurred, otherwise zero. DST must be disjoint from both |
1890 | /// operands. |
1891 | static int tcMultiply(WordType *, const WordType *, const WordType *, |
1892 | unsigned); |
1893 | |
1894 | /// DST = LHS * RHS, where DST has width the sum of the widths of the |
1895 | /// operands. No overflow occurs. DST must be disjoint from both operands. |
1896 | static void tcFullMultiply(WordType *, const WordType *, |
1897 | const WordType *, unsigned, unsigned); |
1898 | |
1899 | /// If RHS is zero LHS and REMAINDER are left unchanged, return one. |
1900 | /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set |
1901 | /// REMAINDER to the remainder, return zero. i.e. |
1902 | /// |
1903 | /// OLD_LHS = RHS * LHS + REMAINDER |
1904 | /// |
1905 | /// SCRATCH is a bignum of the same size as the operands and result for use by |
1906 | /// the routine; its contents need not be initialized and are destroyed. LHS, |
1907 | /// REMAINDER and SCRATCH must be distinct. |
1908 | static int tcDivide(WordType *lhs, const WordType *rhs, |
1909 | WordType *remainder, WordType *scratch, |
1910 | unsigned parts); |
1911 | |
1912 | /// Shift a bignum left Count bits. Shifted in bits are zero. There are no |
1913 | /// restrictions on Count. |
1914 | static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); |
1915 | |
1916 | /// Shift a bignum right Count bits. Shifted in bits are zero. There are no |
1917 | /// restrictions on Count. |
1918 | static void tcShiftRight(WordType *, unsigned Words, unsigned Count); |
1919 | |
1920 | /// The obvious AND, OR and XOR and complement operations. |
1921 | static void tcAnd(WordType *, const WordType *, unsigned); |
1922 | static void tcOr(WordType *, const WordType *, unsigned); |
1923 | static void tcXor(WordType *, const WordType *, unsigned); |
1924 | static void tcComplement(WordType *, unsigned); |
1925 | |
1926 | /// Comparison (unsigned) of two bignums. |
1927 | static int tcCompare(const WordType *, const WordType *, unsigned); |
1928 | |
1929 | /// Increment a bignum in-place. Return the carry flag. |
1930 | static WordType tcIncrement(WordType *dst, unsigned parts) { |
1931 | return tcAddPart(dst, 1, parts); |
1932 | } |
1933 | |
1934 | /// Decrement a bignum in-place. Return the borrow flag. |
1935 | static WordType tcDecrement(WordType *dst, unsigned parts) { |
1936 | return tcSubtractPart(dst, 1, parts); |
1937 | } |
1938 | |
1939 | /// Set the least significant BITS and clear the rest. |
1940 | static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); |
1941 | |
1942 | /// debug method |
1943 | void dump() const; |
1944 | |
1945 | /// @} |
1946 | }; |
1947 | |
1948 | /// Magic data for optimising signed division by a constant. |
1949 | struct APInt::ms { |
1950 | APInt m; ///< magic number |
1951 | unsigned s; ///< shift amount |
1952 | }; |
1953 | |
1954 | /// Magic data for optimising unsigned division by a constant. |
1955 | struct APInt::mu { |
1956 | APInt m; ///< magic number |
1957 | bool a; ///< add indicator |
1958 | unsigned s; ///< shift amount |
1959 | }; |
1960 | |
1961 | inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } |
1962 | |
1963 | inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } |
1964 | |
1965 | /// Unary bitwise complement operator. |
1966 | /// |
1967 | /// \returns an APInt that is the bitwise complement of \p v. |
1968 | inline APInt operator~(APInt v) { |
1969 | v.flipAllBits(); |
1970 | return v; |
1971 | } |
1972 | |
1973 | inline APInt operator&(APInt a, const APInt &b) { |
1974 | a &= b; |
1975 | return a; |
1976 | } |
1977 | |
1978 | inline APInt operator&(const APInt &a, APInt &&b) { |
1979 | b &= a; |
1980 | return std::move(b); |
1981 | } |
1982 | |
1983 | inline APInt operator&(APInt a, uint64_t RHS) { |
1984 | a &= RHS; |
1985 | return a; |
1986 | } |
1987 | |
1988 | inline APInt operator&(uint64_t LHS, APInt b) { |
1989 | b &= LHS; |
1990 | return b; |
1991 | } |
1992 | |
1993 | inline APInt operator|(APInt a, const APInt &b) { |
1994 | a |= b; |
1995 | return a; |
1996 | } |
1997 | |
1998 | inline APInt operator|(const APInt &a, APInt &&b) { |
1999 | b |= a; |
2000 | return std::move(b); |
2001 | } |
2002 | |
2003 | inline APInt operator|(APInt a, uint64_t RHS) { |
2004 | a |= RHS; |
2005 | return a; |
2006 | } |
2007 | |
2008 | inline APInt operator|(uint64_t LHS, APInt b) { |
2009 | b |= LHS; |
2010 | return b; |
2011 | } |
2012 | |
2013 | inline APInt operator^(APInt a, const APInt &b) { |
2014 | a ^= b; |
2015 | return a; |
2016 | } |
2017 | |
2018 | inline APInt operator^(const APInt &a, APInt &&b) { |
2019 | b ^= a; |
2020 | return std::move(b); |
2021 | } |
2022 | |
2023 | inline APInt operator^(APInt a, uint64_t RHS) { |
2024 | a ^= RHS; |
2025 | return a; |
2026 | } |
2027 | |
2028 | inline APInt operator^(uint64_t LHS, APInt b) { |
2029 | b ^= LHS; |
2030 | return b; |
2031 | } |
2032 | |
2033 | inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { |
2034 | I.print(OS, true); |
2035 | return OS; |
2036 | } |
2037 | |
2038 | inline APInt operator-(APInt v) { |
2039 | v.negate(); |
2040 | return v; |
2041 | } |
2042 | |
2043 | inline APInt operator+(APInt a, const APInt &b) { |
2044 | a += b; |
2045 | return a; |
2046 | } |
2047 | |
2048 | inline APInt operator+(const APInt &a, APInt &&b) { |
2049 | b += a; |
2050 | return std::move(b); |
2051 | } |
2052 | |
2053 | inline APInt operator+(APInt a, uint64_t RHS) { |
2054 | a += RHS; |
2055 | return a; |
2056 | } |
2057 | |
2058 | inline APInt operator+(uint64_t LHS, APInt b) { |
2059 | b += LHS; |
2060 | return b; |
2061 | } |
2062 | |
2063 | inline APInt operator-(APInt a, const APInt &b) { |
2064 | a -= b; |
2065 | return a; |
2066 | } |
2067 | |
2068 | inline APInt operator-(const APInt &a, APInt &&b) { |
2069 | b.negate(); |
2070 | b += a; |
2071 | return std::move(b); |
2072 | } |
2073 | |
2074 | inline APInt operator-(APInt a, uint64_t RHS) { |
2075 | a -= RHS; |
2076 | return a; |
2077 | } |
2078 | |
2079 | inline APInt operator-(uint64_t LHS, APInt b) { |
2080 | b.negate(); |
2081 | b += LHS; |
2082 | return b; |
2083 | } |
2084 | |
2085 | inline APInt operator*(APInt a, uint64_t RHS) { |
2086 | a *= RHS; |
2087 | return a; |
2088 | } |
2089 | |
2090 | inline APInt operator*(uint64_t LHS, APInt b) { |
2091 | b *= LHS; |
2092 | return b; |
2093 | } |
2094 | |
2095 | |
2096 | namespace APIntOps { |
2097 | |
2098 | /// Determine the smaller of two APInts considered to be signed. |
2099 | inline const APInt &smin(const APInt &A, const APInt &B) { |
2100 | return A.slt(B) ? A : B; |
2101 | } |
2102 | |
2103 | /// Determine the larger of two APInts considered to be signed. |
2104 | inline const APInt &smax(const APInt &A, const APInt &B) { |
2105 | return A.sgt(B) ? A : B; |
2106 | } |
2107 | |
2108 | /// Determine the smaller of two APInts considered to be signed. |
2109 | inline const APInt &umin(const APInt &A, const APInt &B) { |
2110 | return A.ult(B) ? A : B; |
2111 | } |
2112 | |
2113 | /// Determine the larger of two APInts considered to be unsigned. |
2114 | inline const APInt &umax(const APInt &A, const APInt &B) { |
2115 | return A.ugt(B) ? A : B; |
2116 | } |
2117 | |
2118 | /// Compute GCD of two unsigned APInt values. |
2119 | /// |
2120 | /// This function returns the greatest common divisor of the two APInt values |
2121 | /// using Stein's algorithm. |
2122 | /// |
2123 | /// \returns the greatest common divisor of A and B. |
2124 | APInt GreatestCommonDivisor(APInt A, APInt B); |
2125 | |
2126 | /// Converts the given APInt to a double value. |
2127 | /// |
2128 | /// Treats the APInt as an unsigned value for conversion purposes. |
2129 | inline double RoundAPIntToDouble(const APInt &APIVal) { |
2130 | return APIVal.roundToDouble(); |
2131 | } |
2132 | |
2133 | /// Converts the given APInt to a double value. |
2134 | /// |
2135 | /// Treats the APInt as a signed value for conversion purposes. |
2136 | inline double RoundSignedAPIntToDouble(const APInt &APIVal) { |
2137 | return APIVal.signedRoundToDouble(); |
2138 | } |
2139 | |
2140 | /// Converts the given APInt to a float vlalue. |
2141 | inline float RoundAPIntToFloat(const APInt &APIVal) { |
2142 | return float(RoundAPIntToDouble(APIVal)); |
2143 | } |
2144 | |
2145 | /// Converts the given APInt to a float value. |
2146 | /// |
2147 | /// Treast the APInt as a signed value for conversion purposes. |
2148 | inline float RoundSignedAPIntToFloat(const APInt &APIVal) { |
2149 | return float(APIVal.signedRoundToDouble()); |
2150 | } |
2151 | |
2152 | /// Converts the given double value into a APInt. |
2153 | /// |
2154 | /// This function convert a double value to an APInt value. |
2155 | APInt RoundDoubleToAPInt(double Double, unsigned width); |
2156 | |
2157 | /// Converts a float value into a APInt. |
2158 | /// |
2159 | /// Converts a float value into an APInt value. |
2160 | inline APInt RoundFloatToAPInt(float Float, unsigned width) { |
2161 | return RoundDoubleToAPInt(double(Float), width); |
2162 | } |
2163 | |
2164 | /// Return A unsign-divided by B, rounded by the given rounding mode. |
2165 | APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2166 | |
2167 | /// Return A sign-divided by B, rounded by the given rounding mode. |
2168 | APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2169 | |
2170 | /// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range |
2171 | /// (e.g. 32 for i32). |
2172 | /// This function finds the smallest number n, such that |
2173 | /// (a) n >= 0 and q(n) = 0, or |
2174 | /// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all |
2175 | /// integers, belong to two different intervals [Rk, Rk+R), |
2176 | /// where R = 2^BW, and k is an integer. |
2177 | /// The idea here is to find when q(n) "overflows" 2^BW, while at the |
2178 | /// same time "allowing" subtraction. In unsigned modulo arithmetic a |
2179 | /// subtraction (treated as addition of negated numbers) would always |
2180 | /// count as an overflow, but here we want to allow values to decrease |
2181 | /// and increase as long as they are within the same interval. |
2182 | /// Specifically, adding of two negative numbers should not cause an |
2183 | /// overflow (as long as the magnitude does not exceed the bith width). |
2184 | /// On the other hand, given a positive number, adding a negative |
2185 | /// number to it can give a negative result, which would cause the |
2186 | /// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is |
2187 | /// treated as a special case of an overflow. |
2188 | /// |
2189 | /// This function returns None if after finding k that minimizes the |
2190 | /// positive solution to q(n) = kR, both solutions are contained between |
2191 | /// two consecutive integers. |
2192 | /// |
2193 | /// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation |
2194 | /// in arithmetic modulo 2^BW, and treating the values as signed) by the |
2195 | /// virtue of *signed* overflow. This function will *not* find such an n, |
2196 | /// however it may find a value of n satisfying the inequalities due to |
2197 | /// an *unsigned* overflow (if the values are treated as unsigned). |
2198 | /// To find a solution for a signed overflow, treat it as a problem of |
2199 | /// finding an unsigned overflow with a range with of BW-1. |
2200 | /// |
2201 | /// The returned value may have a different bit width from the input |
2202 | /// coefficients. |
2203 | Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
2204 | unsigned RangeWidth); |
2205 | } // End of APIntOps namespace |
2206 | |
2207 | // See friend declaration above. This additional declaration is required in |
2208 | // order to compile LLVM with IBM xlC compiler. |
2209 | hash_code hash_value(const APInt &Arg); |
2210 | } // End of llvm namespace |
2211 | |
2212 | #endif |