File: Value.h

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (899 lines) | stat: -rw-r--r-- 22,807 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899

#ifndef VALUE_H_20191119_
#define VALUE_H_20191119_

#include "API.h"
#include <array>
#include <cinttypes>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <sstream>

#include <QString>
#include <QVariant>

#ifdef _MSC_VER
extern "C" EDB_EXPORT void __fastcall long_double_to_double(const void *src, double *dest);
EDB_EXPORT void convert_real64_to_real80(const void *src, void *dst);
#endif

namespace edb {

namespace v1 {
EDB_EXPORT bool debuggeeIs32Bit();
}

namespace detail {

template <class Integer>
using IsInteger = typename std::enable_if<std::is_integral<Integer>::value>::type;

template <class T1, class T2>
using PromoteType = typename std::conditional<
	sizeof(T1) >= sizeof(T2),
	typename std::make_unsigned<T1>::type,
	typename std::make_unsigned<T2>::type>::type;

template <size_t N>
class value_type_large {
public:
	using T = uint64_t[N / 64];

public:
	// all defaulted to help ensure that this is a trivially-copyable type
	value_type_large()                         = default;
	value_type_large(const value_type_large &) = default;
	value_type_large &operator=(const value_type_large &) = default;
	value_type_large(value_type_large &&)                 = default;
	value_type_large &operator=(value_type_large &&) = default;
	~value_type_large()                              = default;

public:
	template <class U, class = typename std::enable_if<!std::is_arithmetic<U>::value>::type>
	explicit value_type_large(const U &data, size_t offset = 0) {

		static_assert(sizeof(data) >= sizeof(T), "value_type can only be constructed from large enough variable");
		static_assert(std::is_trivially_copyable<U>::value, "value_type can only be constructed from trivially copiable data");

		Q_ASSERT(sizeof(data) - offset >= sizeof(T)); // check bounds, this can't be done at compile time

		auto dataStart = reinterpret_cast<const char *>(&data);
		std::memcpy(&value_, dataStart + offset, sizeof(value_));
	}

public:
	template <class U>
	void load(const U &n) {
		static_assert(sizeof(T) >= sizeof(n), "Value to load is too large.");
		std::memcpy(&value_, &n, sizeof(n));
	}

public:
	bool operator==(const value_type_large &rhs) const { return std::memcmp(value_, rhs.value_, sizeof(T)) == 0; }
	bool operator!=(const value_type_large &rhs) const { return std::memcmp(value_, rhs.value_, sizeof(T)) != 0; }

public:
	QString toHexString() const {
		char buf[sizeof(T) * 2 + 1];
		char *p = buf;

		for (auto it = std::rbegin(value_); it != std::rend(value_); ++it) {
			p += sprintf(p, "%016" PRIx64, *it);
		}

		return QString::fromLatin1(buf);
	}

public:
	template <class U>
	static value_type_large fromZeroExtended(const U &data) {

		static_assert(sizeof(data) <= sizeof(T), "It doesn't make sense to expand a larger type into a smaller type");

		value_type_large created;

		auto dataStart = reinterpret_cast<const char *>(&data);
		auto target    = reinterpret_cast<char *>(&created.value_);

		std::memcpy(target, dataStart, sizeof(data));
		std::memset(target + sizeof(data), 0, sizeof(T) - sizeof(data));

		return created;
	}

private:
	T value_ = {};
};

template <class T>
class value_type {
	template <class U>
	friend class value_type;

public:
	using InnerValueType = T;

public:
	// all defaulted to help ensure that this is a trivially-copyable type
	value_type()                   = default;
	value_type(const value_type &) = default;
	value_type &operator=(const value_type &) = default;
	value_type(value_type &&)                 = default;
	value_type &operator=(value_type &&) = default;
	~value_type()                        = default;

public:
	template <class Integer, class = IsInteger<Integer>>
	value_type(Integer n)
		: value_(n) {
		// NOTE(eteran): this is allowed to truncate like assigning a uint64_t to a uint32_t
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator=(const Integer &rhs) {
		value_ = rhs;
		// NOTE(eteran): this is allowed to truncate like assigning a uint64_t to a uint32_t
		return *this;
	}

public:
	template <class U>
	explicit value_type(const value_type<U> &other)
		: value_(other.value_) {
		// NOTE(eteran): this is allowed to truncate like assigning a uint64_t to a uint32_t
	}

	template <class U>
	value_type &operator=(const value_type<U> &rhs) {
		value_ = rhs.value_;
		// NOTE(eteran): this is allowed to truncate like assigning a uint64_t to a uint32_t
		return *this;
	}

public:
	template <class U, class = typename std::enable_if<!std::is_arithmetic<U>::value>::type>
	explicit value_type(const U &data, size_t offset = 0) {

		static_assert(sizeof(data) >= sizeof(T), "value_type can only be constructed from large enough variable");
		static_assert(std::is_trivially_copyable<U>::value, "value_type can only be constructed from trivially copiable data");

		Q_ASSERT(sizeof(data) - offset >= sizeof(T)); // check bounds, this can't be done at compile time

		auto dataStart = reinterpret_cast<const char *>(&data);
		std::memcpy(&value_, dataStart + offset, sizeof(value_));
	}

public:
	template <class U>
	void load(const U &n) {
		static_assert(sizeof(T) >= sizeof(n), "Value to load is too large.");
		std::memcpy(&value_, &n, sizeof(n));
	}

public:
	static value_type fromString(const QString &str, bool *ok = nullptr, int base = 10, bool isSigned = false) {

		const qulonglong v = isSigned ? static_cast<qulonglong>(str.toLongLong(ok, base)) : str.toULongLong(ok, base);

		if (ok && !*ok) {
			return 0;
		}

		// Check that the result fits into the underlying type
		value_type result(v);
		if (result == v) {
			return result;
		}

		if (ok) {
			*ok = false;
		}

		return 0;
	}

	static value_type fromHexString(const QString &str, bool *ok = nullptr) {
		return fromString(str, ok, 16);
	}

	static value_type fromSignedString(const QString &str, bool *ok = nullptr) {
		return fromString(str, ok, 10, true);
	}

	static value_type fromCString(const QString &str, bool *ok = nullptr) {
		return fromString(str, ok, 0);
	}

	template <class U>
	static value_type fromZeroExtended(const U &data) {
		value_type created;

		static_assert(sizeof(data) <= sizeof(T), "It doesn't make sense to expand a larger type into a smaller type");

		auto dataStart = reinterpret_cast<const char *>(&data);
		auto target    = reinterpret_cast<char *>(&created.value_);

		std::memcpy(target, dataStart, sizeof(data));
		std::memset(target + sizeof(data), 0, sizeof(T) - sizeof(data));

		return created;
	}

public:
	void swap(value_type &other) {
		using std::swap;
		swap(value_, other.value_);
	}

public:
	bool negative() const {
		return typename std::make_signed<T>::type(value_) < 0;
	}

public:
	explicit operator bool() const { return value_ != 0; }
	bool operator!() const { return !value_; }
	operator T() const { return value_; }
	T toUint() const { return value_; }
	T &asUint() { return value_; }

public:
	value_type operator++(int) {
		T v(value_);
		++value_;
		return v;
	}

	value_type &operator++() {
		++value_;
		return *this;
	}

	value_type operator--(int) {
		T v(value_);
		--value_;
		return v;
	}

	value_type &operator--() {
		--value_;
		return *this;
	}

public:
	template <class U>
	value_type &operator+=(const value_type<U> &rhs) {
		value_ += rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator-=(const value_type<U> &rhs) {
		value_ -= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator*=(const value_type<U> &rhs) {
		value_ *= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator/=(const value_type<U> &rhs) {
		value_ /= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator%=(const value_type<U> &rhs) {
		value_ %= rhs.value_;
		return *this;
	}

public:
	template <class U>
	value_type &operator&=(const value_type<U> &rhs) {
		value_ &= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator|=(const value_type<U> &rhs) {
		value_ |= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator^=(const value_type<U> &rhs) {
		value_ ^= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator>>=(const value_type<U> &rhs) {
		value_ >>= rhs.value_;
		return *this;
	}

	template <class U>
	value_type &operator<<=(const value_type<U> &rhs) {
		value_ <<= rhs.value_;
		return *this;
	}

public:
	template <class Integer, class = IsInteger<Integer>>
	value_type &operator+=(Integer n) {
		value_ += n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator-=(Integer n) {
		value_ -= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator*=(Integer n) {
		value_ *= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator/=(Integer n) {
		value_ /= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator%=(Integer n) {
		value_ %= n;
		return *this;
	}

public:
	template <class Integer, class = IsInteger<Integer>>
	value_type &operator&=(Integer n) {
		value_ &= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator|=(Integer n) {
		value_ |= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator^=(Integer n) {
		value_ ^= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator>>=(Integer n) {
		value_ >>= n;
		return *this;
	}

	template <class Integer, class = IsInteger<Integer>>
	value_type &operator<<=(Integer n) {
		value_ <<= n;
		return *this;
	}

public:
	QString toPointerString(bool createdFromNativePointer = true) const {
		if (edb::v1::debuggeeIs32Bit()) {
			return "0x" + value_type<uint32_t>(value_).toHexString();
		} else {
			if (!createdFromNativePointer) { // then we don't know value of upper dword
				return "0x????????" + value_type<uint32_t>(value_).toHexString();
			} else {
				return "0x" + toHexString();
			}
		}
	}

	QString toHexString() const {
		std::ostringstream ss;
		ss << std::setw(sizeof(value_) * 2) << std::setfill('0') << std::hex << +value_; // + to prevent printing uint8_t as a character
		return QString::fromStdString(ss.str());
	}

	QString unsignedToString() const {
		return toString();
	}

	QString signedToString() const {
		return QString("%1").arg(typename std::make_signed<T>::type(value_));
	}

	QString toString() const {
		return QString("%1").arg(value_);
	}

	QVariant toQVariant() const {
		return QVariant::fromValue(value_);
	}

public:
	value_type signExtended(size_t valueLength) const {
		value_type result(value_);

		if (valueLength == sizeof(value_)) {
			return result;
		}

		// if the sign bit is set
		if (value_ & (1ull << (valueLength * 8 - 1))) {
			// start with all bits set
			result.value_ = -1ll;

			// overwrite the lower <valueLength> bytes with the original value
			std::memcpy(&result.value_, &value_, valueLength);
		}

		return result;
	}

public:
	void normalize() {
		if (edb::v1::debuggeeIs32Bit()) {
			value_ &= 0xffffffffull;
		}
	}

public:
	T value_ = {};
};

// iostream operators
template <class T>
std::istream &operator>>(std::istream &os, value_type<T> &val) {
	os >> val.asUint();
	return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, value_type<T> &val) {
	os << val.toUint();
	return os;
}

// operators for value_type, Integer
template <class T, class Integer, class = IsInteger<Integer>>
bool operator==(const value_type<T> &lhs, Integer rhs) {
	using U = typename std::make_unsigned<Integer>::type;
	return lhs.value_ == static_cast<U>(rhs);
}

template <class T, class Integer, class = IsInteger<Integer>>
bool operator!=(const value_type<T> &lhs, Integer rhs) {
	using U = typename std::make_unsigned<Integer>::type;
	return lhs.value_ != static_cast<U>(rhs);
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator+(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r += rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator-(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r -= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator*(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r *= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator/(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r /= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator%(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r %= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator&(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r &= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator|(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r |= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator^(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r ^= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator>>(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r >>= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator<<(const value_type<T> &lhs, Integer rhs) -> value_type<T> {
	value_type<T> r(lhs);
	r <<= rhs;
	return r;
}

// operators for Integer, value_type
template <class T, class Integer, class = IsInteger<Integer>>
bool operator==(Integer lhs, const value_type<T> &rhs) {
	return rhs == lhs;
}

template <class T, class Integer, class = IsInteger<Integer>>
bool operator!=(Integer lhs, const value_type<T> &rhs) {
	return rhs != lhs;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator+(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r += rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator-(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r -= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator*(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r *= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator/(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r /= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator%(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r %= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator&(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r &= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator|(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r |= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator^(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r ^= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator>>(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r >>= rhs;
	return r;
}

template <class T, class Integer, class = IsInteger<Integer>>
auto operator<<(Integer lhs, const value_type<T> &rhs) -> value_type<PromoteType<T, Integer>> {

	using U = value_type<PromoteType<T, Integer>>;

	value_type<U> r(lhs);
	r <<= rhs;
	return r;
}

// operators for value_type, value_type
template <class T1, class T2>
bool operator==(const value_type<T1> &lhs, const value_type<T2> &rhs) {
	return lhs.value_ == rhs.value_;
}

template <class T1, class T2>
bool operator!=(const value_type<T1> &lhs, const value_type<T2> &rhs) {
	return lhs.value_ != rhs.value_;
}

template <class T1, class T2>
auto operator+(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r += rhs;
	return r;
}

template <class T1, class T2>
auto operator-(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r -= rhs;
	return r;
}

template <class T1, class T2>
auto operator*(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r *= rhs;
	return r;
}

template <class T1, class T2>
auto operator/(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r /= rhs;
	return r;
}

template <class T1, class T2>
auto operator%(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r %= rhs;
	return r;
}

template <class T1, class T2>
auto operator&(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r &= rhs;
	return r;
}

template <class T1, class T2>
auto operator|(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r |= rhs;
	return r;
}

template <class T1, class T2>
auto operator^(const value_type<T1> &lhs, const value_type<T2> &rhs) -> value_type<PromoteType<T1, T2>> {

	using U = value_type<PromoteType<T1, T2>>;

	value_type<U> r(lhs);
	r ^= rhs;
	return r;
}

struct value_type80 {
public:
	using T = uint8_t[10];

public:
	// all defaulted to help ensure that this is a trivially-copyable type
	value_type80()                     = default;
	value_type80(const value_type80 &) = default;
	value_type80 &operator=(const value_type80 &) = default;
	value_type80(value_type80 &&)                 = default;
	value_type80 &operator=(value_type80 &&) = default;
	~value_type80()                          = default;

public:
	template <class U>
	explicit value_type80(const U &data, size_t offset = 0) {
#ifdef _MSC_VER
		if (std::is_same<U, long double>::value && sizeof(U) < sizeof(T)) {
			T temp;
			convert_real64_to_real80(&data, &temp);

			Q_ASSERT(sizeof(temp) - offset >= sizeof(T)); // check bounds, this can't be done at compile time

			auto dataStart = reinterpret_cast<const char *>(&temp);
			std::memcpy(&value_, dataStart + offset, sizeof(value_));
			return;
		}
#else
		static_assert(sizeof(data) >= sizeof(T), "ValueBase can only be constructed from large enough variable");
#endif
		static_assert(std::is_trivially_copyable<U>::value, "ValueBase can only be constructed from trivially copiable data");

		Q_ASSERT(sizeof(data) - offset >= sizeof(T)); // check bounds, this can't be done at compile time

		auto dataStart = reinterpret_cast<const char *>(&data);
		std::memcpy(&value_, dataStart + offset, sizeof(value_));
	}

public:
	bool negative() const {
		return value_[9] & 0x80;
	}

	value_type<uint16_t> exponent() const {
		value_type<uint16_t> e(value_, 8);
		e &= 0x7fff;
		return e;
	}

	value_type<uint64_t> mantissa() const {
		value_type<uint64_t> m(value_, 0);
		return m;
	}

	bool normalized() const {
		return value_[7] & 0x80;
	}

public:
	long double toFloatValue() const {
#ifdef _MSC_VER
		double d;
		long_double_to_double(&value_, &d);
		return d;
#else
		long double float80val;
		std::memcpy(&float80val, &value_, sizeof(value_));
		return float80val;
#endif
	}

public:
	QString toHexString() const {
		char buf[32];
		snprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
				 value_[9],
				 value_[8],
				 value_[7],
				 value_[6],
				 value_[5],
				 value_[4],
				 value_[3],
				 value_[2],
				 value_[1],
				 value_[0]);

		return QString::fromLatin1(buf);
	}

public:
	bool operator==(const value_type80 &rhs) const { return std::memcmp(value_, rhs.value_, 10) == 0; }
	bool operator!=(const value_type80 &rhs) const { return std::memcmp(value_, rhs.value_, 10) != 0; }

private:
	T value_ = {};
};

static_assert(sizeof(value_type80) * 8 == 80, "value_type80 size is broken!");

}

// GPR on x86
using value8  = detail::value_type<uint8_t>;
using value16 = detail::value_type<uint16_t>;
using value32 = detail::value_type<uint32_t>;

// MMX/GPR(x86_64)
using value64 = detail::value_type<uint64_t>;

// We support registers and addresses of 64-bits
using address_t = value64;
using reg_t     = value64;

// FPU
using value80 = detail::value_type80;

// SSE
using value128 = detail::value_type_large<128>;

// AVX
using value256 = detail::value_type_large<256>;

// AVX512
using value512 = detail::value_type_large<512>;

static_assert(std::is_standard_layout<value8>::value &&
				  std::is_standard_layout<value16>::value &&
				  std::is_standard_layout<value32>::value &&
				  std::is_standard_layout<value64>::value &&
				  std::is_standard_layout<value80>::value &&
				  std::is_standard_layout<value128>::value &&
				  std::is_standard_layout<value256>::value &&
				  std::is_standard_layout<value512>::value,
			  "Fixed-sized values are intended to have standard layout");

static_assert(std::is_trivially_copyable<value8>::value &&
				  std::is_trivially_copyable<value16>::value &&
				  std::is_trivially_copyable<value32>::value &&
				  std::is_trivially_copyable<value64>::value &&
				  std::is_trivially_copyable<value80>::value &&
				  std::is_trivially_copyable<value128>::value &&
				  std::is_trivially_copyable<value256>::value &&
				  std::is_trivially_copyable<value512>::value,
			  "Fixed-sized values are intended to be trivially copyable");

}

#endif