1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 // 12 // Endian related functions. 13 14 #ifndef AOM_AOM_UTIL_ENDIAN_INL_H_ 15 #define AOM_AOM_UTIL_ENDIAN_INL_H_ 16 17 #include <stdlib.h> 18 19 #include "config/aom_config.h" 20 21 #include "aom/aom_integer.h" 22 23 #if defined(__GNUC__) 24 #define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__) 25 #define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min))) 26 #else 27 #define LOCAL_GCC_VERSION 0 28 #define LOCAL_GCC_PREREQ(maj, min) 0 29 #endif 30 31 // handle clang compatibility 32 #ifndef __has_builtin 33 #define __has_builtin(x) 0 34 #endif 35 36 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__) 37 #if !defined(WORDS_BIGENDIAN) && \ 38 (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \ 39 (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))) 40 #define WORDS_BIGENDIAN 41 #endif 42 43 #if defined(WORDS_BIGENDIAN) 44 #define HToLE32 BSwap32 45 #define HToLE16 BSwap16 46 #define HToBE64(x) (x) 47 #define HToBE32(x) (x) 48 #else 49 #define HToLE32(x) (x) 50 #define HToLE16(x) (x) 51 #define HToBE64(X) BSwap64(X) 52 #define HToBE32(X) BSwap32(X) 53 #endif 54 55 #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16) 56 #define HAVE_BUILTIN_BSWAP16 57 #endif 58 59 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32) 60 #define HAVE_BUILTIN_BSWAP32 61 #endif 62 63 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64) 64 #define HAVE_BUILTIN_BSWAP64 65 #endif 66 BSwap16(uint16_t x)67static inline uint16_t BSwap16(uint16_t x) { 68 #if defined(HAVE_BUILTIN_BSWAP16) 69 return __builtin_bswap16(x); 70 #elif defined(_MSC_VER) 71 return _byteswap_ushort(x); 72 #else 73 // gcc will recognize a 'rorw $8, ...' here: 74 return (x >> 8) | ((x & 0xff) << 8); 75 #endif // HAVE_BUILTIN_BSWAP16 76 } 77 BSwap32(uint32_t x)78static inline uint32_t BSwap32(uint32_t x) { 79 #if defined(HAVE_BUILTIN_BSWAP32) 80 return __builtin_bswap32(x); 81 #elif defined(__i386__) || defined(__x86_64__) 82 uint32_t swapped_bytes; 83 __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x)); 84 return swapped_bytes; 85 #elif defined(_MSC_VER) 86 return (uint32_t)_byteswap_ulong(x); 87 #else 88 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24); 89 #endif // HAVE_BUILTIN_BSWAP32 90 } 91 BSwap64(uint64_t x)92static inline uint64_t BSwap64(uint64_t x) { 93 #if defined(HAVE_BUILTIN_BSWAP64) 94 return __builtin_bswap64(x); 95 #elif defined(__x86_64__) 96 uint64_t swapped_bytes; 97 __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x)); 98 return swapped_bytes; 99 #elif defined(_MSC_VER) 100 return (uint64_t)_byteswap_uint64(x); 101 #else // generic code for swapping 64-bit values (suggested by bdb@) 102 x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32); 103 x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16); 104 x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8); 105 return x; 106 #endif // HAVE_BUILTIN_BSWAP64 107 } 108 109 #endif // AOM_AOM_UTIL_ENDIAN_INL_H_ 110