xref: /aosp_15_r20/external/deqp/external/vulkancts/vkscserver/vksEndian.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _VKSENDIAN_HPP
2 #define _VKSENDIAN_HPP
3 
4 /*-------------------------------------------------------------------------
5  * Vulkan CTS Framework
6  * --------------------
7  *
8  * Copyright (c) 2021 The Khronos Group Inc.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  *-------------------------------------------------------------------------*/
23 
24 #include "vksCommon.hpp"
25 
26 #include <deInt32.h>
27 
28 namespace vksc_server
29 {
30 
IsBigEndian()31 constexpr bool IsBigEndian()
32 {
33     return (DE_ENDIANNESS) == (DE_BIG_ENDIAN);
34 }
35 
ReverseBytes64(u64 n)36 constexpr u64 ReverseBytes64(u64 n)
37 {
38     return (((n & u64{0xFF00000000000000}) >> 56) | ((n & u64{0x00FF000000000000}) >> 40) |
39             ((n & u64{0x0000FF0000000000}) >> 24) | ((n & u64{0x000000FF00000000}) >> 8) |
40             ((n & u64{0x00000000FF000000}) << 8) | ((n & u64{0x0000000000FF0000}) << 24) |
41             ((n & u64{0x000000000000FF00}) << 40) | ((n & u64{0x00000000000000FF}) << 56));
42 }
43 
44 #if DE_ENDIANNESS == DE_LITTLE_ENDIAN
HostToNetwork16(u16 host)45 inline u16 HostToNetwork16(u16 host)
46 {
47     return deReverseBytes16(host);
48 }
HostToNetwork32(u32 host)49 inline u32 HostToNetwork32(u32 host)
50 {
51     return deReverseBytes32(host);
52 }
HostToNetwork64(u64 host)53 inline u64 HostToNetwork64(u64 host)
54 {
55     return ReverseBytes64(host);
56 }
57 #elif DE_ENDIANNESS == DE_BIG_ENDIAN
HostToNetwork16(u16 host)58 inline u16 HostToNetwork16(u16 host)
59 {
60     return host;
61 }
HostToNetwork32(u32 host)62 inline u32 HostToNetwork32(u32 host)
63 {
64     return host;
65 }
HostToNetwork64(u64 host)66 inline u64 HostToNetwork64(u64 host)
67 {
68     return host;
69 }
70 #endif
71 
NetworkToHost16(u16 net)72 inline u16 NetworkToHost16(u16 net)
73 {
74     return HostToNetwork16(net);
75 }
NetworkToHost32(u32 net)76 inline u32 NetworkToHost32(u32 net)
77 {
78     return HostToNetwork32(net);
79 }
NetworkToHost64(u64 net)80 inline u64 NetworkToHost64(u64 net)
81 {
82     return HostToNetwork64(net);
83 }
84 
85 } // namespace vksc_server
86 
87 #endif // _VKSENDIAN_HPP
88