blob: f536ec5b8a4acbe0a90d1b36173407c1dbfe9a73 [file] [log] [blame]
Yaowu Xu9c01aa12016-09-01 14:32:49 -07001/*
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 */
Yaowu Xuc27fc142016-08-22 16:08:15 -070011//
12// Endian related functions.
13
James Zerne1cbb132018-08-22 14:10:36 -070014#ifndef AOM_AOM_UTIL_ENDIAN_INL_H_
15#define AOM_AOM_UTIL_ENDIAN_INL_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
17#include <stdlib.h>
Tom Finegan60e653d2018-05-22 11:34:58 -070018
19#include "config/aom_config.h"
20
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022
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
67#if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
68 defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
Yaowu Xuf883b422016-08-30 14:01:10 -070069#define AOM_USE_MIPS32_R2
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#endif
71
72static INLINE uint16_t BSwap16(uint16_t x) {
73#if defined(HAVE_BUILTIN_BSWAP16)
74 return __builtin_bswap16(x);
75#elif defined(_MSC_VER)
76 return _byteswap_ushort(x);
77#else
78 // gcc will recognize a 'rorw $8, ...' here:
79 return (x >> 8) | ((x & 0xff) << 8);
80#endif // HAVE_BUILTIN_BSWAP16
81}
82
83static INLINE uint32_t BSwap32(uint32_t x) {
Yaowu Xuf883b422016-08-30 14:01:10 -070084#if defined(AOM_USE_MIPS32_R2)
Yaowu Xuc27fc142016-08-22 16:08:15 -070085 uint32_t ret;
86 __asm__ volatile(
87 "wsbh %[ret], %[x] \n\t"
88 "rotr %[ret], %[ret], 16 \n\t"
89 : [ret] "=r"(ret)
90 : [x] "r"(x));
91 return ret;
92#elif defined(HAVE_BUILTIN_BSWAP32)
93 return __builtin_bswap32(x);
94#elif defined(__i386__) || defined(__x86_64__)
95 uint32_t swapped_bytes;
96 __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
97 return swapped_bytes;
98#elif defined(_MSC_VER)
99 return (uint32_t)_byteswap_ulong(x);
100#else
101 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
102#endif // HAVE_BUILTIN_BSWAP32
103}
104
105static INLINE uint64_t BSwap64(uint64_t x) {
106#if defined(HAVE_BUILTIN_BSWAP64)
107 return __builtin_bswap64(x);
108#elif defined(__x86_64__)
109 uint64_t swapped_bytes;
110 __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
111 return swapped_bytes;
112#elif defined(_MSC_VER)
113 return (uint64_t)_byteswap_uint64(x);
114#else // generic code for swapping 64-bit values (suggested by bdb@)
115 x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
116 x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
117 x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
118 return x;
119#endif // HAVE_BUILTIN_BSWAP64
120}
121
James Zerne1cbb132018-08-22 14:10:36 -0700122#endif // AOM_AOM_UTIL_ENDIAN_INL_H_