Qt 大小端位元組序的處理

一杯清酒邀明月發表於2024-03-13

1、Endian Order
  什麼是大小端位元組序(Endian Order)?以四位元組十六進位制整型數0x12345678為例,按照書寫習慣,從左到右,高位在前,低位在後,我們稱0x12為最高有效位元組,0x78為最低有效位元組,在儲存這個數字時,需要四位元組的地址空間,比如說從0x00到0x03,地址空間是從低地址0x00到高地址0x03的一段連續地址,那麼,低地址儲存最高有效位元組的形式為大端(Big Endian),反之,高地址儲存最高有效位元組的形式為小端(Little Endian),也就是說,大端位元組序時,地址0x00儲存數字0x12,0x01儲存0x34,0x02儲存0x56,0x03儲存0x78,反之,小端位元組序時,地址0x00儲存數字0x78,0x01儲存0x56,0x02儲存0x34,0x03儲存0x12,在實際使用中,大端位元組序符合我們的書寫習慣。另外,對於一個位元組來說,其中的八個位元位也有類似的大小端位元序,常見的最高有效位MSB(Most Significant Bit)和最低有效位LSB(Least Significant Bit)就是從這裡來的。由於資料傳輸是逐位元組的,最小單位為位元組,所以我們需要關心的是大小端位元組序,網路位元組序為大端位元組序,大小端位元序可以忽略。

2、qbswap
  對於給定的處理器來說,其大小端位元組序是已知的,Qt在標頭檔案qendian.h提供了若干對大小端進行處理的介面。首先是位元組逆序函式qbswap_helper,因為在標頭檔案中定義,所以必須指定為inline,以及模板函式qbswap,其中的轉換結果dest不要求位元組對齊,實現方式如下:

 1 inline void qbswap_helper(const void *src, void *dest, int size)
 2 {
 3     for (int i = 0; i < size ; ++i)
 4         static_cast<uchar *>(dest)[i] = static_cast<const uchar *>(src)[size - 1 - i];
 5 }
 6 
 7 template <typename T> inline void qbswap(const T src, void *dest)
 8 {
 9     qbswap_helper(&src, dest, sizeof(T));
10 }

3、考慮位元組對齊

  考慮到型別安全和位元組對齊安全,提供了qToUnalignedqFromUnaligned兩個函式,以及六個模板特化函式qbswap,如下:

 1 template <typename T> Q_ALWAYS_INLINE void qToUnaligned(const T src, void *dest)
 2 {
 3     const size_t size = sizeof(T);
 4 #if QT_HAS_BUILTIN(__builtin_memcpy)
 5     __builtin_memcpy
 6 #else
 7     memcpy
 8 #endif
 9             (dest, &src, size);
10 }
11 
12 template <typename T> Q_ALWAYS_INLINE T qFromUnaligned(const void *src)
13 {
14     T dest;
15     const size_t size = sizeof(T);
16 #if QT_HAS_BUILTIN(__builtin_memcpy)
17     __builtin_memcpy
18 #else
19     memcpy
20 #endif
21             (&dest, src, size);
22     return dest;
23 }
24 
25 #if (defined(Q_CC_GNU) && Q_CC_GNU >= 403) || QT_HAS_BUILTIN(__builtin_bswap32)
26 template <> inline void qbswap<quint64>(quint64 source, void *dest)
27 {
28     qToUnaligned<quint64>(__builtin_bswap64(source), dest);
29 }
30 template <> inline void qbswap<quint32>(quint32 source, void *dest)
31 {
32     qToUnaligned<quint32>(__builtin_bswap32(source), dest);
33 }
34 #else
35 #endif // GCC & Clang intrinsics
36 
37 #if (defined(Q_CC_GNU) && Q_CC_GNU >= 408) || QT_HAS_BUILTIN(__builtin_bswap16)
38 template <> inline void qbswap<quint16>(quint16 source, void *dest)
39 {
40     qToUnaligned<quint16>(__builtin_bswap16(source), dest);
41 }
42 #else
43 #endif // GCC & Clang intrinsics
44 
45 // signed specializations
46 template <> inline void qbswap<qint64>(qint64 source, void *dest)
47 {
48     qbswap<quint64>(quint64(source), dest);
49 }
50 
51 template <> inline void qbswap<qint32>(qint32 source, void *dest)
52 {
53     qbswap<quint32>(quint32(source), dest);
54 }
55 
56 template <> inline void qbswap<qint16>(qint16 source, void *dest)
57 {
58     qbswap<quint16>(quint16(source), dest);
59 }

4、不考慮位元組對齊

  如果不考慮位元組對齊問題,使用另一個版本的過載函式qbswap更方便,同樣有六個特化模板,如下:

 1 template <typename T> T qbswap(T source);
 2 
 3 #if (defined(Q_CC_GNU) && Q_CC_GNU >= 403) || QT_HAS_BUILTIN(__builtin_bswap32)
 4 template <> inline quint64 qbswap<quint64>(quint64 source)
 5 {
 6     return __builtin_bswap64(source);
 7 }
 8 template <> inline quint32 qbswap<quint32>(quint32 source)
 9 {
10     return __builtin_bswap32(source);
11 }
12 #else
13 template <> inline quint64 qbswap<quint64>(quint64 source)
14 {
15     return 0
16         | ((source & Q_UINT64_C(0x00000000000000ff)) << 56)
17         | ((source & Q_UINT64_C(0x000000000000ff00)) << 40)
18         | ((source & Q_UINT64_C(0x0000000000ff0000)) << 24)
19         | ((source & Q_UINT64_C(0x00000000ff000000)) << 8)
20         | ((source & Q_UINT64_C(0x000000ff00000000)) >> 8)
21         | ((source & Q_UINT64_C(0x0000ff0000000000)) >> 24)
22         | ((source & Q_UINT64_C(0x00ff000000000000)) >> 40)
23         | ((source & Q_UINT64_C(0xff00000000000000)) >> 56);
24 }
25 
26 template <> inline quint32 qbswap<quint32>(quint32 source)
27 {
28     return 0
29         | ((source & 0x000000ff) << 24)
30         | ((source & 0x0000ff00) << 8)
31         | ((source & 0x00ff0000) >> 8)
32         | ((source & 0xff000000) >> 24);
33 }
34 #endif // GCC & Clang intrinsics
35 
36 #if (defined(Q_CC_GNU) && Q_CC_GNU >= 408) || QT_HAS_BUILTIN(__builtin_bswap16)
37 template <> inline quint16 qbswap<quint16>(quint16 source)
38 {
39     return __builtin_bswap16(source);
40 }
41 #else
42 template <> inline quint16 qbswap<quint16>(quint16 source)
43 {
44     return quint16( 0
45                     | ((source & 0x00ff) << 8)
46                     | ((source & 0xff00) >> 8) );
47 }
48 #endif // GCC & Clang intrinsics
49 
50 
51 // signed specializations
52 template <> inline qint64 qbswap<qint64>(qint64 source)
53 {
54     return qbswap<quint64>(quint64(source));
55 }
56 
57 template <> inline qint32 qbswap<qint32>(qint32 source)
58 {
59     return qbswap<quint32>(quint32(source));
60 }
61 
62 template <> inline qint16 qbswap<qint16>(qint16 source)
63 {
64     return qbswap<quint16>(quint16(source));
65 }
66 }

5、8 Bits

  對於8個二進位制位的型別來說,是不需要考慮大小端的,如下:

1 template <> inline quint8 qbswap<quint8>(quint8 source)
2 {
3     return source;
4 }
5 
6 template <> inline qint8 qbswap<qint8>(qint8 source)
7 {
8     return source;
9 }

6、大小端轉換

  大小端轉換涉及如下六個函式:

 1 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
 2 
 3 template <typename T> inline T qToBigEndian(T source)
 4 { return source; }
 5 template <typename T> inline T qFromBigEndian(T source)
 6 { return source; }
 7 template <typename T> inline T qToLittleEndian(T source)
 8 { return qbswap<T>(source); }
 9 template <typename T> inline T qFromLittleEndian(T source)
10 { return qbswap<T>(source); }
11 template <typename T> inline void qToBigEndian(T src, void *dest)
12 { qToUnaligned<T>(src, dest); }
13 template <typename T> inline void qToLittleEndian(T src, void *dest)
14 { qbswap<T>(src, dest); }
15 #else // Q_LITTLE_ENDIAN
16 
17 template <typename T> inline T qToBigEndian(T source)
18 { return qbswap<T>(source); }
19 template <typename T> inline T qFromBigEndian(T source)
20 { return qbswap<T>(source); }
21 template <typename T> inline T qToLittleEndian(T source)
22 { return source; }
23 template <typename T> inline T qFromLittleEndian(T source)
24 { return source; }
25 template <typename T> inline void qToBigEndian(T src, void *dest)
26 { qbswap<T>(src, dest); }
27 template <typename T> inline void qToLittleEndian(T src, void *dest)
28 { qToUnaligned<T>(src, dest); }
29 
30 #endif // Q_BYTE_ORDER == Q_BIG_ENDIAN

  如果不考慮位元組對齊,還有下面幾個特化模板。

 1 template <typename T> inline T qFromLittleEndian(const void *src)
 2 {
 3     return qFromLittleEndian(qFromUnaligned<T>(src));
 4 }
 5 
 6 template <> inline quint8 qFromLittleEndian<quint8>(const void *src)
 7 { return static_cast<const quint8 *>(src)[0]; }
 8 template <> inline qint8 qFromLittleEndian<qint8>(const void *src)
 9 { return static_cast<const qint8 *>(src)[0]; }
10 
11 template <class T> inline T qFromBigEndian(const void *src)
12 {
13     return qFromBigEndian(qFromUnaligned<T>(src));
14 }
15 
16 template <> inline quint8 qFromBigEndian<quint8>(const void *src)
17 { return static_cast<const quint8 *>(src)[0]; }
18 template <> inline qint8 qFromBigEndian<qint8>(const void *src)
19 { return static_cast<const qint8 *>(src)[0]; }

相關文章