summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/swap.h130
1 files changed, 91 insertions, 39 deletions
diff --git a/src/common/swap.h b/src/common/swap.h
index 466096f58..97aacb4dc 100644
--- a/src/common/swap.h
+++ b/src/common/swap.h
@@ -645,64 +645,116 @@ protected:
645 } 645 }
646}; 646};
647 647
648#if COMMON_LITTLE_ENDIAN 648struct SwapTag {}; // Use the different endianness from the system
649using u16_le = u16; 649struct KeepTag {}; // Use the same endianness as the system
650using u32_le = u32; 650
651using u64_le = u64; 651template <typename T, typename Tag>
652struct AddEndian;
652 653
653using s16_le = s16; 654// KeepTag specializations
654using s32_le = s32;
655using s64_le = s64;
656 655
657template <typename T> 656template <typename T>
658using enum_le = std::enable_if_t<std::is_enum_v<T>, T>; 657struct AddEndian<T, KeepTag> {
658 using type = T;
659};
660
661// SwapTag specializations
659 662
660using float_le = float; 663template <>
661using double_le = double; 664struct AddEndian<u8, SwapTag> {
665 using type = u8;
666};
662 667
663using u64_be = swap_struct_t<u64, swap_64_t<u64>>; 668template <>
664using s64_be = swap_struct_t<s64, swap_64_t<s64>>; 669struct AddEndian<u16, SwapTag> {
670 using type = swap_struct_t<u16, swap_16_t<u16>>;
671};
665 672
666using u32_be = swap_struct_t<u32, swap_32_t<u32>>; 673template <>
667using s32_be = swap_struct_t<s32, swap_32_t<s32>>; 674struct AddEndian<u32, SwapTag> {
675 using type = swap_struct_t<u32, swap_32_t<u32>>;
676};
668 677
669using u16_be = swap_struct_t<u16, swap_16_t<u16>>; 678template <>
670using s16_be = swap_struct_t<s16, swap_16_t<s16>>; 679struct AddEndian<u64, SwapTag> {
680 using type = swap_struct_t<u64, swap_64_t<u64>>;
681};
682
683template <>
684struct AddEndian<s8, SwapTag> {
685 using type = s8;
686};
687
688template <>
689struct AddEndian<s16, SwapTag> {
690 using type = swap_struct_t<s16, swap_16_t<s16>>;
691};
692
693template <>
694struct AddEndian<s32, SwapTag> {
695 using type = swap_struct_t<s32, swap_32_t<s32>>;
696};
697
698template <>
699struct AddEndian<s64, SwapTag> {
700 using type = swap_struct_t<s64, swap_64_t<s64>>;
701};
702
703template <>
704struct AddEndian<float, SwapTag> {
705 using type = swap_struct_t<float, swap_float_t<float>>;
706};
707
708template <>
709struct AddEndian<double, SwapTag> {
710 using type = swap_struct_t<double, swap_double_t<double>>;
711};
671 712
672template <typename T> 713template <typename T>
673using enum_be = swap_enum_t<T>; 714struct AddEndian<T, SwapTag> {
715 static_assert(std::is_enum_v<T>);
716 using type = swap_enum_t<T>;
717};
718
719// Alias LETag/BETag as KeepTag/SwapTag depending on the system
720#if COMMON_LITTLE_ENDIAN
721
722using LETag = KeepTag;
723using BETag = SwapTag;
674 724
675using float_be = swap_struct_t<float, swap_float_t<float>>;
676using double_be = swap_struct_t<double, swap_double_t<double>>;
677#else 725#else
678 726
679using u64_le = swap_struct_t<u64, swap_64_t<u64>>; 727using BETag = KeepTag;
680using s64_le = swap_struct_t<s64, swap_64_t<s64>>; 728using LETag = SwapTag;
681 729
682using u32_le = swap_struct_t<u32, swap_32_t<u32>>; 730#endif
683using s32_le = swap_struct_t<s32, swap_32_t<s32>>;
684 731
685using u16_le = swap_struct_t<u16, swap_16_t<u16>>; 732// Aliases for LE types
686using s16_le = swap_struct_t<s16, swap_16_t<s16>>; 733using u16_le = AddEndian<u16, LETag>::type;
734using u32_le = AddEndian<u32, LETag>::type;
735using u64_le = AddEndian<u64, LETag>::type;
736
737using s16_le = AddEndian<s16, LETag>::type;
738using s32_le = AddEndian<s32, LETag>::type;
739using s64_le = AddEndian<s64, LETag>::type;
687 740
688template <typename T> 741template <typename T>
689using enum_le = swap_enum_t<T>; 742using enum_le = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, LETag>::type>;
690 743
691using float_le = swap_struct_t<float, swap_float_t<float>>; 744using float_le = AddEndian<float, LETag>::type;
692using double_le = swap_struct_t<double, swap_double_t<double>>; 745using double_le = AddEndian<double, LETag>::type;
693 746
694using u16_be = u16; 747// Aliases for BE types
695using u32_be = u32; 748using u16_be = AddEndian<u16, BETag>::type;
696using u64_be = u64; 749using u32_be = AddEndian<u32, BETag>::type;
750using u64_be = AddEndian<u64, BETag>::type;
697 751
698using s16_be = s16; 752using s16_be = AddEndian<s16, BETag>::type;
699using s32_be = s32; 753using s32_be = AddEndian<s32, BETag>::type;
700using s64_be = s64; 754using s64_be = AddEndian<s64, BETag>::type;
701 755
702template <typename T> 756template <typename T>
703using enum_be = std::enable_if_t<std::is_enum_v<T>, T>; 757using enum_be = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, BETag>::type>;
704
705using float_be = float;
706using double_be = double;
707 758
708#endif 759using float_be = AddEndian<float, BETag>::type;
760using double_be = AddEndian<double, BETag>::type;