epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkEndian_DEFINED |
| 11 | #define SkEndian_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | /** \file SkEndian.h |
| 16 | |
| 17 | Macros and helper functions for handling 16 and 32 bit values in |
| 18 | big and little endian formats. |
| 19 | */ |
| 20 | |
| 21 | #if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN) |
| 22 | #error "can't have both LENDIAN and BENDIAN defined" |
| 23 | #endif |
| 24 | |
| 25 | #if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN) |
| 26 | #error "need either LENDIAN or BENDIAN defined" |
| 27 | #endif |
| 28 | |
| 29 | /** Swap the two bytes in the low 16bits of the parameters. |
| 30 | e.g. 0x1234 -> 0x3412 |
| 31 | */ |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 32 | static inline uint16_t SkEndianSwap16(U16CPU value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | SkASSERT(value == (uint16_t)value); |
| 34 | return (uint16_t)((value >> 8) | (value << 8)); |
| 35 | } |
| 36 | |
| 37 | /** Vector version of SkEndianSwap16(), which swaps the |
| 38 | low two bytes of each value in the array. |
| 39 | */ |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 40 | static inline void SkEndianSwap16s(uint16_t array[], int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | SkASSERT(count == 0 || array != NULL); |
| 42 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 43 | while (--count >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | *array = SkEndianSwap16(*array); |
| 45 | array += 1; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** Reverse all 4 bytes in a 32bit value. |
| 50 | e.g. 0x12345678 -> 0x78563412 |
| 51 | */ |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 52 | static inline uint32_t SkEndianSwap32(uint32_t value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | return ((value & 0xFF) << 24) | |
| 54 | ((value & 0xFF00) << 8) | |
| 55 | ((value & 0xFF0000) >> 8) | |
| 56 | (value >> 24); |
| 57 | } |
| 58 | |
| 59 | /** Vector version of SkEndianSwap16(), which swaps the |
| 60 | bytes of each value in the array. |
| 61 | */ |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 62 | static inline void SkEndianSwap32s(uint32_t array[], int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | SkASSERT(count == 0 || array != NULL); |
| 64 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 65 | while (--count >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | *array = SkEndianSwap32(*array); |
| 67 | array += 1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #ifdef SK_CPU_LENDIAN |
| 72 | #define SkEndian_SwapBE16(n) SkEndianSwap16(n) |
| 73 | #define SkEndian_SwapBE32(n) SkEndianSwap32(n) |
| 74 | #define SkEndian_SwapLE16(n) (n) |
| 75 | #define SkEndian_SwapLE32(n) (n) |
| 76 | #else // SK_CPU_BENDIAN |
| 77 | #define SkEndian_SwapBE16(n) (n) |
| 78 | #define SkEndian_SwapBE32(n) (n) |
| 79 | #define SkEndian_SwapLE16(n) SkEndianSwap16(n) |
| 80 | #define SkEndian_SwapLE32(n) SkEndianSwap32(n) |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | #endif |
| 85 | |