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