• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef COMMON_ENDIANNESS_H
13 #define COMMON_ENDIANNESS_H
14 
15 #ifdef BIG_ENDIAN
16 #define be16_to_cpu(x) (x)
17 #define be32_to_cpu(x) (x)
18 #define be64_to_cpu(x) (x)
19 
20 #define le16_to_cpu(x) ((((x)&0xff) << 8) | (((x) >> 8) & 0xff))
21 #define le32_to_cpu(x) ((le16_to_cpu((x)) << 16) | (le16_to_cpu((x) >> 16)))
22 #define le64_to_cpu(x) ((le32_to_cpu((x)) << 32) | (le32_to_cpu((x) >> 32)))
23 
24 #define le128ptr_to_cpu_lo(x) (le64_to_cpu(*(u64 *)(x)))
25 #define le128ptr_to_cpu_hi(x) (le64_to_cpu(*((u64 *)(x) + 1)))
26 
27 #define le96ptr_to_cpu_lo(x) (be32_to_cpu(*(u32 *)(x)))
28 #define le96ptr_to_cpu_hi(x)                       \
29     (((u64)(le32_to_cpu(*((u32 *)(x) + 1)))) << 32 \
30      | (be32_to_cpu(*((u32 *)(x)) + 2)))
31 
32 #define leXptr_to_cpu(bits, n)                                      \
33     ({                                                              \
34         (bits == 32) ? le32_to_cpu(*(u32 *)(n)) :                   \
35                        (bits == 64) ? le64_to_cpu(*(u64 *)(n)) : ({ \
36                            BUG("invalid X");                        \
37                            0;                                       \
38                        });                                          \
39     })
40 
41 #define set_leXptr_to_cpu(bits, n, hi, lo)  \
42     do {                                    \
43         if (bits > 64) {                    \
44             if (bits == 96) {               \
45                 lo = le96ptr_to_cpu_lo(n);  \
46                 hi = le96ptr_to_cpu_hi(n);  \
47             } else if (bits == 128) {       \
48                 lo = le128ptr_to_cpu_lo(n); \
49                 hi = le128ptr_to_cpu_hi(n); \
50             } else {                        \
51                 BUG("invalid X");           \
52             }                               \
53         } else {                            \
54             lo = 0;                         \
55             hi = leXptr_to_cpu(bits, n);    \
56         }                                   \
57     } while (0)
58 #else
59 #define le16_to_cpu(x) (x)
60 #define le32_to_cpu(x) (x)
61 #define le64_to_cpu(x) (x)
62 
63 #define be16_to_cpu(x) ((((x)&0xff) << 8) | (((x) >> 8) & 0xff))
64 #define be32_to_cpu(x) ((be16_to_cpu((x)) << 16) | (be16_to_cpu((x) >> 16)))
65 #define be64_to_cpu(x) ((be32_to_cpu((x)) << 32) | (be32_to_cpu((x) >> 32)))
66 
67 #define be128ptr_to_cpu_hi(x) (be64_to_cpu(*(u64 *)(x)))
68 #define be128ptr_to_cpu_lo(x) (be64_to_cpu(*((u64 *)(x) + 1)))
69 
70 #define be96ptr_to_cpu_hi(x) (be32_to_cpu(*(u32 *)(x)))
71 #define be96ptr_to_cpu_lo(x)                       \
72     (((u64)(be32_to_cpu(*((u32 *)(x) + 1)))) << 32 \
73      | (be32_to_cpu(*((u32 *)(x)) + 2)))
74 
75 #define beXptr_to_cpu(bits, n)                                          \
76     ({                                                                  \
77         ((bits) == 32) ? be32_to_cpu(*(u32 *)(n)) :                     \
78                          ((bits) == 64) ? be64_to_cpu(*(u64 *)(n)) : ({ \
79                              BUG("invalid X");                          \
80                              0;                                         \
81                          });                                            \
82     })
83 
84 #define set_beXptr_to_cpu(bits, n, hi, lo)  \
85     do {                                    \
86         if ((bits) > 64) {                  \
87             if ((bits) == 96) {             \
88                 lo = be96ptr_to_cpu_lo(n);  \
89                 hi = be96ptr_to_cpu_hi(n);  \
90             } else if ((bits) == 128) {     \
91                 lo = be128ptr_to_cpu_lo(n); \
92                 hi = be128ptr_to_cpu_hi(n); \
93             } else {                        \
94                 BUG("invalid X");           \
95             }                               \
96         } else {                            \
97             lo = 0;                         \
98             hi = beXptr_to_cpu((bits), n);  \
99         }                                   \
100     } while (0)
101 #endif
102 
103 #endif /* COMMON_ENDIANNESS_H */