utils: add endian_inl.h

moves the following to this header:
- htole*() definitions from bit_writer.c
- __BIG_ENDIAN__ fallback define from bit_reader_inl.h

Change-Id: I7fff59543f08a70bf8f9ddac849b72ed290471b1
This commit is contained in:
James Zern 2014-07-03 10:41:37 -07:00
parent 58ab622437
commit d5104b1ff6
5 changed files with 60 additions and 39 deletions

View File

@ -246,10 +246,13 @@ all: ex $(EXTRA_EXAMPLES)
$(EX_FORMAT_DEC_OBJS): %.o: %.h
# special dependencies for tree.c/vp8.c/bit_reader.c <-> bit_reader_inl.h
src/dec/tree.o: src/utils/bit_reader_inl.h
src/dec/vp8.o: src/utils/bit_reader_inl.h
src/utils/bit_reader.o: src/utils/bit_reader_inl.h
# special dependencies:
# tree.c/vp8.c/bit_reader.c <-> bit_reader_inl.h, endian_inl.h
# bit_writer.c <-> endian_inl.h
src/dec/tree.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h
src/dec/vp8.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h
src/utils/bit_reader.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h
src/utils/bit_writer.o: src/utils/endian_inl.h
%.o: %.c $(HDRS)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

View File

@ -13,6 +13,7 @@ COMMON_SOURCES += bit_reader.h
COMMON_SOURCES += bit_reader_inl.h
COMMON_SOURCES += color_cache.c
COMMON_SOURCES += color_cache.h
COMMON_SOURCES += endian_inl.h
COMMON_SOURCES += filters.c
COMMON_SOURCES += filters.h
COMMON_SOURCES += huffman.c

View File

@ -17,6 +17,7 @@
#define WEBP_UTILS_BIT_READER_INL_H_
#include "./bit_reader.h"
#include "./endian_inl.h"
#ifdef __cplusplus
extern "C" {
@ -35,12 +36,6 @@ typedef uint16_t lbit_t;
typedef uint8_t lbit_t;
#endif
// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
#if !defined(__BIG_ENDIAN__) && defined(__BYTE_ORDER__) && \
(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define __BIG_ENDIAN__
#endif
// gcc 4.3 has builtin functions for swap32/swap64
#if defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))

View File

@ -17,6 +17,7 @@
#include <stdlib.h>
#include "./bit_writer.h"
#include "./endian_inl.h"
#include "./utils.h"
//------------------------------------------------------------------------------
@ -204,35 +205,6 @@ void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
#define VP8L_WRITER_BITS (VP8L_WRITER_BYTES * 8)
#define VP8L_WRITER_MAX_BITS (8 * (int)sizeof(vp8l_atype_t))
// endian-specific htoleXX() definition
// TODO(skal): move this to config.h, and collect all the endian-related code
// in a proper .h file
#if defined(_WIN32)
#if !defined(_M_PPC)
#define htole32(x) (x)
#define htole16(x) (x)
#else // PPC is BIG_ENDIAN
#include <stdlib.h>
#define htole32(x) (_byteswap_ulong((unsigned long)(x)))
#define htole16(x) (_byteswap_ushort((unsigned short)(x)))
#endif // _M_PPC
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || \
defined(__DragonFly__)
#include <sys/endian.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define htole32 OSSwapHostToLittleInt32
#define htole16 OSSwapHostToLittleInt16
#elif defined(__native_client__) && !defined(__GLIBC__)
// NaCl without glibc is assumed to be little-endian
#define htole32(x) (x)
#define htole16(x) (x)
#elif defined(__QNX__)
#include <net/netbyte.h>
#else // pretty much all linux and/or glibc
#include <endian.h>
#endif
// Returns 1 on success.
static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
uint8_t* allocated_buf;

50
src/utils/endian_inl.h Normal file
View File

@ -0,0 +1,50 @@
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Endian related functions.
#ifndef WEBP_UTILS_ENDIAN_INL_H_
#define WEBP_UTILS_ENDIAN_INL_H_
// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
#if !defined(__BIG_ENDIAN__) && defined(__BYTE_ORDER__) && \
(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define __BIG_ENDIAN__
#endif
// endian-specific htoleXX() definition
// TODO(skal): add a test for htoleXX() in endian.h and others in autoconf or
// remove it and replace with a generic implementation.
#if defined(_WIN32)
#if !defined(_M_PPC)
#define htole32(x) (x)
#define htole16(x) (x)
#else // PPC is BIG_ENDIAN
#include <stdlib.h>
#define htole32(x) (_byteswap_ulong((unsigned long)(x)))
#define htole16(x) (_byteswap_ushort((unsigned short)(x)))
#endif // _M_PPC
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || \
defined(__DragonFly__)
#include <sys/endian.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define htole32 OSSwapHostToLittleInt32
#define htole16 OSSwapHostToLittleInt16
#elif defined(__native_client__) && !defined(__GLIBC__)
// NaCl without glibc is assumed to be little-endian
#define htole32(x) (x)
#define htole16(x) (x)
#elif defined(__QNX__)
#include <net/netbyte.h>
#else // pretty much all linux and/or glibc
#include <endian.h>
#endif
#endif // WEBP_UTILS_ENDIAN_INL_H_