Sync may31 release/8.0-tizen (#510)
[platform/upstream/dotnet/runtime.git] / src / coreclr / pal / inc / pal_endian.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3
4 /*++
5
6
7
8
9
10 --*/
11
12 #ifndef __PAL_ENDIAN_H__
13 #define __PAL_ENDIAN_H__
14
15 #ifdef __cplusplus
16 extern "C++" {
17 inline UINT16 SWAP16(UINT16 x)
18 {
19     return (UINT16)((x >> 8) | (x << 8));
20 }
21
22 inline UINT32 SWAP32(UINT32 x)
23 {
24     return  (x >> 24) |
25             ((x >> 8) & 0x0000FF00L) |
26             ((x & 0x0000FF00L) << 8) |
27             (x << 24);
28 }
29
30 }
31 #endif // __cplusplus
32
33 #if BIGENDIAN
34 #ifdef __cplusplus
35 extern "C++" {
36 inline UINT16 VAL16(UINT16 x)
37 {
38     return SWAP16(x);
39 }
40
41 inline UINT32 VAL32(UINT32 x)
42 {
43     return SWAP32(x);
44 }
45
46 inline UINT64 VAL64(UINT64 x)
47 {
48     return ((UINT64)VAL32(x) << 32) | VAL32(x >> 32);
49 }
50
51 inline void SwapString(WCHAR *szString)
52 {
53     unsigned i;
54     for (i = 0; szString[i] != L'\0'; i++)
55     {
56         szString[i] = VAL16(szString[i]);
57     }
58 }
59
60 inline void SwapStringLength(WCHAR *szString, ULONG StringLength)
61 {
62     unsigned i;
63     for (i = 0; i < StringLength; i++)
64     {
65         szString[i] = VAL16(szString[i]);
66     }
67 }
68
69 inline void SwapGuid(GUID *pGuid)
70 {
71     pGuid->Data1 = VAL32(pGuid->Data1);
72     pGuid->Data2 = VAL16(pGuid->Data2);
73     pGuid->Data3 = VAL16(pGuid->Data3);
74 }
75 };
76 #else // __cplusplus
77 /* C Version of VAL functionality.  Swap functions omitted for lack of use in C code */
78 #define VAL16(x)    (((x) >> 8) | ((x) << 8))
79 #define VAL32(y)    (((y) >> 24) | (((y) >> 8) & 0x0000FF00L) | (((y) & 0x0000FF00L) << 8) | ((y) << 24))
80 #define VAL64(z)    (((UINT64)VAL32(z) << 32) | VAL32((z) >> 32))
81 #endif // __cplusplus
82
83 #else // !BIGENDIAN
84
85 #define VAL16(x) x
86 #define VAL32(x) x
87 #define VAL64(x) x
88 #define SwapString(x)
89 #define SwapStringLength(x, y)
90 #define SwapGuid(x)
91
92 #endif  // !BIGENDIAN
93
94 #ifdef HOST_64BIT
95 #define VALPTR(x) VAL64(x)
96 #else
97 #define VALPTR(x) VAL32(x)
98 #endif
99
100 #ifdef HOST_ARM
101 #define LOG2_PTRSIZE    2
102 #define ALIGN_ACCESS    ((1<<LOG2_PTRSIZE)-1)
103 #endif
104
105 #ifdef HOST_RISCV64
106 #define LOG2_PTRSIZE    3
107 #define ALIGN_ACCESS    ((1<<LOG2_PTRSIZE)-1)
108 #endif
109
110 #if defined(ALIGN_ACCESS) && !defined(_MSC_VER)
111 #ifdef __cplusplus
112 extern "C++" {
113 // Get Unaligned values from a potentially unaligned object
114 inline UINT16 GET_UNALIGNED_16(const void *pObject)
115 {
116     UINT16 temp;
117     memcpy(&temp, pObject, sizeof(temp));
118     return temp;
119 }
120 inline UINT32 GET_UNALIGNED_32(const void *pObject)
121 {
122     UINT32 temp;
123     memcpy(&temp, pObject, sizeof(temp));
124     return temp;
125 }
126 inline UINT64 GET_UNALIGNED_64(const void *pObject)
127 {
128     UINT64 temp;
129     memcpy(&temp, pObject, sizeof(temp));
130     return temp;
131 }
132
133 // Set Value on an potentially unaligned object
134 inline void SET_UNALIGNED_16(void *pObject, UINT16 Value)
135 {
136     memcpy(pObject, &Value, sizeof(UINT16));
137 }
138 inline void SET_UNALIGNED_32(void *pObject, UINT32 Value)
139 {
140     memcpy(pObject, &Value, sizeof(UINT32));
141 }
142 inline void SET_UNALIGNED_64(void *pObject, UINT64 Value)
143 {
144     memcpy(pObject, &Value, sizeof(UINT64));
145 }
146 }
147 #endif // __cplusplus
148
149 #else // defined(ALIGN_ACCESS) && !defined(_MSC_VER)
150
151 // Get Unaligned values from a potentially unaligned object
152 #define GET_UNALIGNED_16(_pObject)  (*(UINT16 UNALIGNED *)(_pObject))
153 #define GET_UNALIGNED_32(_pObject)  (*(UINT32 UNALIGNED *)(_pObject))
154 #define GET_UNALIGNED_64(_pObject)  (*(UINT64 UNALIGNED *)(_pObject))
155
156 // Set Value on an potentially unaligned object
157 #define SET_UNALIGNED_16(_pObject, _Value)  (*(UNALIGNED UINT16 *)(_pObject)) = (UINT16)(_Value)
158 #define SET_UNALIGNED_32(_pObject, _Value)  (*(UNALIGNED UINT32 *)(_pObject)) = (UINT32)(_Value)
159 #define SET_UNALIGNED_64(_pObject, _Value)  (*(UNALIGNED UINT64 *)(_pObject)) = (UINT64)(_Value)
160
161 #endif // defined(ALIGN_ACCESS) && !defined(_MSC_VER)
162
163 // Get Unaligned values from a potentially unaligned object and swap the value
164 #define GET_UNALIGNED_VAL16(_pObject) VAL16(GET_UNALIGNED_16(_pObject))
165 #define GET_UNALIGNED_VAL32(_pObject) VAL32(GET_UNALIGNED_32(_pObject))
166 #define GET_UNALIGNED_VAL64(_pObject) VAL64(GET_UNALIGNED_64(_pObject))
167
168 // Set a swap Value on an potentially unaligned object
169 #define SET_UNALIGNED_VAL16(_pObject, _Value) SET_UNALIGNED_16(_pObject, VAL16((UINT16)_Value))
170 #define SET_UNALIGNED_VAL32(_pObject, _Value) SET_UNALIGNED_32(_pObject, VAL32((UINT32)_Value))
171 #define SET_UNALIGNED_VAL64(_pObject, _Value) SET_UNALIGNED_64(_pObject, VAL64((UINT64)_Value))
172
173 #endif // __PAL_ENDIAN_H__