blob: 17a238649c361a46548ff4b8b79931bf9675b89a [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
Yaowu Xuf883b422016-08-30 14:01:10 -070014#ifndef AOM_UTIL_ENDIAN_INL_H_
15#define AOM_UTIL_ENDIAN_INL_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
17#include <stdlib.h>
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "./aom_config.h"
19#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020
21#if defined(__GNUC__)
22#define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
23#define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
24#else
25#define LOCAL_GCC_VERSION 0
26#define LOCAL_GCC_PREREQ(maj, min) 0
27#endif
28
29// handle clang compatibility
30#ifndef __has_builtin
31#define __has_builtin(x) 0
32#endif
33
34// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
35#if !defined(WORDS_BIGENDIAN) && \
36 (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
37 (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
38#define WORDS_BIGENDIAN
39#endif
40
41#if defined(WORDS_BIGENDIAN)
42#define HToLE32 BSwap32
43#define HToLE16 BSwap16
44#define HToBE64(x) (x)
45#define HToBE32(x) (x)
46#else
47#define HToLE32(x) (x)
48#define HToLE16(x) (x)
49#define HToBE64(X) BSwap64(X)
50#define HToBE32(X) BSwap32(X)
51#endif
52
53#if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
54#define HAVE_BUILTIN_BSWAP16
55#endif
56
57#if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
58#define HAVE_BUILTIN_BSWAP32
59#endif
60
61#if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
62#define HAVE_BUILTIN_BSWAP64
63#endif
64
65#if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
66 defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
Yaowu Xuf883b422016-08-30 14:01:10 -070067#define AOM_USE_MIPS32_R2
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#endif
69
70static INLINE uint16_t BSwap16(uint16_t x) {
71#if defined(HAVE_BUILTIN_BSWAP16)
72 return __builtin_bswap16(x);
73#elif defined(_MSC_VER)
74 return _byteswap_ushort(x);
75#else
76 // gcc will recognize a 'rorw $8, ...' here:
77 return (x >> 8) | ((x & 0xff) << 8);
78#endif // HAVE_BUILTIN_BSWAP16
79}
80
81static INLINE uint32_t BSwap32(uint32_t x) {
Yaowu Xuf883b422016-08-30 14:01:10 -070082#if defined(AOM_USE_MIPS32_R2)
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 uint32_t ret;
84 __asm__ volatile(
85 "wsbh %[ret], %[x] \n\t"
86 "rotr %[ret], %[ret], 16 \n\t"
87 : [ret] "=r"(ret)
88 : [x] "r"(x));
89 return ret;
90#elif defined(HAVE_BUILTIN_BSWAP32)
91 return __builtin_bswap32(x);
92#elif defined(__i386__) || defined(__x86_64__)
93 uint32_t swapped_bytes;
94 __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
95 return swapped_bytes;
96#elif defined(_MSC_VER)
97 return (uint32_t)_byteswap_ulong(x);
98#else
99 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
100#endif // HAVE_BUILTIN_BSWAP32
101}
102
103static INLINE uint64_t BSwap64(uint64_t x) {
104#if defined(HAVE_BUILTIN_BSWAP64)
105 return __builtin_bswap64(x);
106#elif defined(__x86_64__)
107 uint64_t swapped_bytes;
108 __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
109 return swapped_bytes;
110#elif defined(_MSC_VER)
111 return (uint64_t)_byteswap_uint64(x);
112#else // generic code for swapping 64-bit values (suggested by bdb@)
113 x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
114 x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
115 x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
116 return x;
117#endif // HAVE_BUILTIN_BSWAP64
118}
119
Yaowu Xuf883b422016-08-30 14:01:10 -0700120#endif // AOM_UTIL_ENDIAN_INL_H_