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 ( 39 ((n & u64{0xFF00000000000000}) >> 56) | 40 ((n & u64{0x00FF000000000000}) >> 40) | 41 ((n & u64{0x0000FF0000000000}) >> 24) | 42 ((n & u64{0x000000FF00000000}) >> 8) | 43 ((n & u64{0x00000000FF000000}) << 8) | 44 ((n & u64{0x0000000000FF0000}) << 24) | 45 ((n & u64{0x000000000000FF00}) << 40) | 46 ((n & u64{0x00000000000000FF}) << 56) 47 ); 48 } 49 50 #if DE_ENDIANNESS == DE_LITTLE_ENDIAN HostToNetwork16(u16 host)51 inline u16 HostToNetwork16 (u16 host) { return deReverseBytes16(host); } HostToNetwork32(u32 host)52 inline u32 HostToNetwork32 (u32 host) { return deReverseBytes32(host); } HostToNetwork64(u64 host)53 inline u64 HostToNetwork64 (u64 host) { return ReverseBytes64(host); } 54 #elif DE_ENDIANNESS == DE_BIG_ENDIAN HostToNetwork16(u16 host)55 inline u16 HostToNetwork16 (u16 host) { return host; } HostToNetwork32(u32 host)56 inline u32 HostToNetwork32 (u32 host) { return host; } HostToNetwork64(u64 host)57 inline u64 HostToNetwork64 (u64 host) { return host; } 58 #endif 59 NetworkToHost16(u16 net)60 inline u16 NetworkToHost16 (u16 net) { return HostToNetwork16(net); } NetworkToHost32(u32 net)61 inline u32 NetworkToHost32 (u32 net) { return HostToNetwork32(net); } NetworkToHost64(u64 net)62 inline u64 NetworkToHost64 (u64 net) { return HostToNetwork64(net); } 63 64 } 65 66 #endif // _VKSENDIAN_HPP 67