2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkEndian_DEFINED
|
|
|
|
#define SkEndian_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
/** \file SkEndian.h
|
|
|
|
|
|
|
|
Macros and helper functions for handling 16 and 32 bit values in
|
|
|
|
big and little endian formats.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN)
|
|
|
|
#error "can't have both LENDIAN and BENDIAN defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN)
|
|
|
|
#error "need either LENDIAN or BENDIAN defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Swap the two bytes in the low 16bits of the parameters.
|
|
|
|
e.g. 0x1234 -> 0x3412
|
|
|
|
*/
|
2014-04-23 19:19:44 +00:00
|
|
|
static inline uint16_t SkEndianSwap16(uint16_t value) {
|
2012-02-02 19:15:21 +00:00
|
|
|
return static_cast<uint16_t>((value >> 8) | (value << 8));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-04-23 19:19:44 +00:00
|
|
|
|
2012-02-02 19:15:21 +00:00
|
|
|
template<uint16_t N> struct SkTEndianSwap16 {
|
|
|
|
static const uint16_t value = static_cast<uint16_t>((N >> 8) | ((N & 0xFF) << 8));
|
|
|
|
};
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Vector version of SkEndianSwap16(), which swaps the
|
|
|
|
low two bytes of each value in the array.
|
|
|
|
*/
|
2011-04-11 19:01:12 +00:00
|
|
|
static inline void SkEndianSwap16s(uint16_t array[], int count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(count == 0 || array != NULL);
|
|
|
|
|
2011-04-11 19:01:12 +00:00
|
|
|
while (--count >= 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
*array = SkEndianSwap16(*array);
|
|
|
|
array += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reverse all 4 bytes in a 32bit value.
|
|
|
|
e.g. 0x12345678 -> 0x78563412
|
|
|
|
*/
|
2011-04-11 19:01:12 +00:00
|
|
|
static inline uint32_t SkEndianSwap32(uint32_t value) {
|
2014-04-23 19:19:44 +00:00
|
|
|
return ((value & 0xFF) << 24) |
|
|
|
|
((value & 0xFF00) << 8) |
|
|
|
|
((value & 0xFF0000) >> 8) |
|
2008-12-17 15:59:43 +00:00
|
|
|
(value >> 24);
|
|
|
|
}
|
2014-04-23 19:19:44 +00:00
|
|
|
|
2012-02-02 19:15:21 +00:00
|
|
|
template<uint32_t N> struct SkTEndianSwap32 {
|
|
|
|
static const uint32_t value = ((N & 0xFF) << 24) |
|
|
|
|
((N & 0xFF00) << 8) |
|
|
|
|
((N & 0xFF0000) >> 8) |
|
|
|
|
(N >> 24);
|
|
|
|
};
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-05-03 17:35:39 +00:00
|
|
|
/** Vector version of SkEndianSwap32(), which swaps the
|
2008-12-17 15:59:43 +00:00
|
|
|
bytes of each value in the array.
|
|
|
|
*/
|
2011-04-11 19:01:12 +00:00
|
|
|
static inline void SkEndianSwap32s(uint32_t array[], int count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(count == 0 || array != NULL);
|
|
|
|
|
2011-04-11 19:01:12 +00:00
|
|
|
while (--count >= 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
*array = SkEndianSwap32(*array);
|
|
|
|
array += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:35:39 +00:00
|
|
|
/** Reverse all 8 bytes in a 64bit value.
|
|
|
|
e.g. 0x1122334455667788 -> 0x8877665544332211
|
|
|
|
*/
|
|
|
|
static inline uint64_t SkEndianSwap64(uint64_t value) {
|
|
|
|
return (((value & 0x00000000000000FFULL) << (8*7)) |
|
|
|
|
((value & 0x000000000000FF00ULL) << (8*5)) |
|
|
|
|
((value & 0x0000000000FF0000ULL) << (8*3)) |
|
|
|
|
((value & 0x00000000FF000000ULL) << (8*1)) |
|
|
|
|
((value & 0x000000FF00000000ULL) >> (8*1)) |
|
|
|
|
((value & 0x0000FF0000000000ULL) >> (8*3)) |
|
|
|
|
((value & 0x00FF000000000000ULL) >> (8*5)) |
|
|
|
|
((value) >> (8*7)));
|
|
|
|
}
|
|
|
|
template<uint64_t N> struct SkTEndianSwap64 {
|
|
|
|
static const uint64_t value = (((N & 0x00000000000000FFULL) << (8*7)) |
|
|
|
|
((N & 0x000000000000FF00ULL) << (8*5)) |
|
|
|
|
((N & 0x0000000000FF0000ULL) << (8*3)) |
|
|
|
|
((N & 0x00000000FF000000ULL) << (8*1)) |
|
|
|
|
((N & 0x000000FF00000000ULL) >> (8*1)) |
|
|
|
|
((N & 0x0000FF0000000000ULL) >> (8*3)) |
|
|
|
|
((N & 0x00FF000000000000ULL) >> (8*5)) |
|
|
|
|
((N) >> (8*7)));
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Vector version of SkEndianSwap64(), which swaps the
|
|
|
|
bytes of each value in the array.
|
|
|
|
*/
|
|
|
|
static inline void SkEndianSwap64s(uint64_t array[], int count) {
|
|
|
|
SkASSERT(count == 0 || array != NULL);
|
|
|
|
|
|
|
|
while (--count >= 0) {
|
|
|
|
*array = SkEndianSwap64(*array);
|
|
|
|
array += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_CPU_LENDIAN
|
|
|
|
#define SkEndian_SwapBE16(n) SkEndianSwap16(n)
|
|
|
|
#define SkEndian_SwapBE32(n) SkEndianSwap32(n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkEndian_SwapBE64(n) SkEndianSwap64(n)
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkEndian_SwapLE16(n) (n)
|
|
|
|
#define SkEndian_SwapLE32(n) (n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkEndian_SwapLE64(n) (n)
|
2012-02-02 19:15:21 +00:00
|
|
|
|
|
|
|
#define SkTEndian_SwapBE16(n) SkTEndianSwap16<n>::value
|
|
|
|
#define SkTEndian_SwapBE32(n) SkTEndianSwap32<n>::value
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkTEndian_SwapBE64(n) SkTEndianSwap64<n>::value
|
2012-02-02 19:15:21 +00:00
|
|
|
#define SkTEndian_SwapLE16(n) (n)
|
|
|
|
#define SkTEndian_SwapLE32(n) (n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkTEndian_SwapLE64(n) (n)
|
2008-12-17 15:59:43 +00:00
|
|
|
#else // SK_CPU_BENDIAN
|
|
|
|
#define SkEndian_SwapBE16(n) (n)
|
|
|
|
#define SkEndian_SwapBE32(n) (n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkEndian_SwapBE64(n) (n)
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkEndian_SwapLE16(n) SkEndianSwap16(n)
|
|
|
|
#define SkEndian_SwapLE32(n) SkEndianSwap32(n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkEndian_SwapLE64(n) SkEndianSwap64(n)
|
2012-02-02 19:15:21 +00:00
|
|
|
|
|
|
|
#define SkTEndian_SwapBE16(n) (n)
|
|
|
|
#define SkTEndian_SwapBE32(n) (n)
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkTEndian_SwapBE64(n) (n)
|
2012-02-02 19:15:21 +00:00
|
|
|
#define SkTEndian_SwapLE16(n) SkTEndianSwap16<n>::value
|
|
|
|
#define SkTEndian_SwapLE32(n) SkTEndianSwap32<n>::value
|
2013-05-03 17:35:39 +00:00
|
|
|
#define SkTEndian_SwapLE64(n) SkTEndianSwap64<n>::value
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-28 18:22:01 +00:00
|
|
|
// When a bytestream is embedded in a 32-bit word, how far we need to
|
|
|
|
// shift the word to extract each byte from the low 8 bits by anding with 0xff.
|
|
|
|
#ifdef SK_CPU_LENDIAN
|
|
|
|
#define SkEndian_Byte0Shift 0
|
|
|
|
#define SkEndian_Byte1Shift 8
|
|
|
|
#define SkEndian_Byte2Shift 16
|
|
|
|
#define SkEndian_Byte3Shift 24
|
|
|
|
#else // SK_CPU_BENDIAN
|
|
|
|
#define SkEndian_Byte0Shift 24
|
|
|
|
#define SkEndian_Byte1Shift 16
|
|
|
|
#define SkEndian_Byte2Shift 8
|
|
|
|
#define SkEndian_Byte3Shift 0
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-02-02 19:15:21 +00:00
|
|
|
|
|
|
|
#if defined(SK_UINT8_BITFIELD_LENDIAN) && defined(SK_UINT8_BITFIELD_BENDIAN)
|
|
|
|
#error "can't have both bitfield LENDIAN and BENDIAN defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(SK_UINT8_BITFIELD_LENDIAN) && !defined(SK_UINT8_BITFIELD_BENDIAN)
|
|
|
|
#ifdef SK_CPU_LENDIAN
|
|
|
|
#define SK_UINT8_BITFIELD_LENDIAN
|
|
|
|
#else
|
|
|
|
#define SK_UINT8_BITFIELD_BENDIAN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SK_UINT8_BITFIELD_LENDIAN
|
|
|
|
#define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \
|
|
|
|
SK_OT_BYTE f0 : 1; \
|
|
|
|
SK_OT_BYTE f1 : 1; \
|
|
|
|
SK_OT_BYTE f2 : 1; \
|
|
|
|
SK_OT_BYTE f3 : 1; \
|
|
|
|
SK_OT_BYTE f4 : 1; \
|
|
|
|
SK_OT_BYTE f5 : 1; \
|
|
|
|
SK_OT_BYTE f6 : 1; \
|
|
|
|
SK_OT_BYTE f7 : 1;
|
|
|
|
#else
|
|
|
|
#define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \
|
|
|
|
SK_OT_BYTE f7 : 1; \
|
|
|
|
SK_OT_BYTE f6 : 1; \
|
|
|
|
SK_OT_BYTE f5 : 1; \
|
|
|
|
SK_OT_BYTE f4 : 1; \
|
|
|
|
SK_OT_BYTE f3 : 1; \
|
|
|
|
SK_OT_BYTE f2 : 1; \
|
|
|
|
SK_OT_BYTE f1 : 1; \
|
|
|
|
SK_OT_BYTE f0 : 1;
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|