Path: blob/master/thirdparty/libwebp/src/utils/endian_inl_utils.h
9912 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Endian related functions.1011#ifndef WEBP_UTILS_ENDIAN_INL_UTILS_H_12#define WEBP_UTILS_ENDIAN_INL_UTILS_H_1314#ifdef HAVE_CONFIG_H15#include "src/webp/config.h"16#endif1718#include "src/dsp/dsp.h"19#include "src/webp/types.h"2021#if defined(WORDS_BIGENDIAN)22#define HToLE32 BSwap3223#define HToLE16 BSwap1624#else25#define HToLE32(x) (x)26#define HToLE16(x) (x)27#endif2829#if !defined(HAVE_CONFIG_H)30#if LOCAL_GCC_PREREQ(4,8) || __has_builtin(__builtin_bswap16)31#define HAVE_BUILTIN_BSWAP1632#endif33#if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap32)34#define HAVE_BUILTIN_BSWAP3235#endif36#if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap64)37#define HAVE_BUILTIN_BSWAP6438#endif39#endif // !HAVE_CONFIG_H4041static WEBP_INLINE uint16_t BSwap16(uint16_t x) {42#if defined(HAVE_BUILTIN_BSWAP16)43return __builtin_bswap16(x);44#elif defined(_MSC_VER)45return _byteswap_ushort(x);46#else47// gcc will recognize a 'rorw $8, ...' here:48return (x >> 8) | ((x & 0xff) << 8);49#endif // HAVE_BUILTIN_BSWAP1650}5152static WEBP_INLINE uint32_t BSwap32(uint32_t x) {53#if defined(WEBP_USE_MIPS32_R2)54uint32_t ret;55__asm__ volatile (56"wsbh %[ret], %[x] \n\t"57"rotr %[ret], %[ret], 16 \n\t"58: [ret]"=r"(ret)59: [x]"r"(x)60);61return ret;62#elif defined(HAVE_BUILTIN_BSWAP32)63return __builtin_bswap32(x);64#elif defined(__i386__) || defined(__x86_64__)65uint32_t swapped_bytes;66__asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));67return swapped_bytes;68#elif defined(_MSC_VER)69return (uint32_t)_byteswap_ulong(x);70#else71return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);72#endif // HAVE_BUILTIN_BSWAP3273}7475static WEBP_INLINE uint64_t BSwap64(uint64_t x) {76#if defined(HAVE_BUILTIN_BSWAP64)77return __builtin_bswap64(x);78#elif defined(__x86_64__)79uint64_t swapped_bytes;80__asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));81return swapped_bytes;82#elif defined(_MSC_VER)83return (uint64_t)_byteswap_uint64(x);84#else // generic code for swapping 64-bit values (suggested by bdb@)85x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);86x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);87x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);88return x;89#endif // HAVE_BUILTIN_BSWAP6490}9192#endif // WEBP_UTILS_ENDIAN_INL_UTILS_H_939495