1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_EXT_BASE_ENDIAN_H_ 18 #define INCLUDE_PERFETTO_EXT_BASE_ENDIAN_H_ 19 20 #include <stdint.h> 21 #include <stdlib.h> // For MSVC 22 23 #include "perfetto/base/build_config.h" 24 #include "perfetto/base/compiler.h" 25 26 #if !PERFETTO_IS_LITTLE_ENDIAN() 27 #error "endian.h supports only little-endian archs" 28 #endif 29 30 namespace perfetto { 31 namespace base { 32 33 #if PERFETTO_BUILDFLAG(PERFETTO_COMPILER_MSVC) HostToBE16(uint16_t x)34inline uint16_t HostToBE16(uint16_t x) { 35 return _byteswap_ushort(x); 36 } HostToBE32(uint32_t x)37inline uint32_t HostToBE32(uint32_t x) { 38 return _byteswap_ulong(x); 39 } HostToBE64(uint64_t x)40inline uint64_t HostToBE64(uint64_t x) { 41 return _byteswap_uint64(x); 42 } 43 #else 44 inline uint16_t HostToBE16(uint16_t x) { 45 return __builtin_bswap16(x); 46 } 47 inline uint32_t HostToBE32(uint32_t x) { 48 return __builtin_bswap32(x); 49 } 50 inline uint64_t HostToBE64(uint64_t x) { 51 return __builtin_bswap64(x); 52 } 53 #endif 54 55 } // namespace base 56 } // namespace perfetto 57 58 #endif // INCLUDE_PERFETTO_EXT_BASE_ENDIAN_H_ 59