reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkEndian_DEFINED |
| 18 | #define SkEndian_DEFINED |
| 19 | |
| 20 | #include "SkTypes.h" |
| 21 | |
| 22 | /** \file SkEndian.h |
| 23 | |
| 24 | Macros and helper functions for handling 16 and 32 bit values in |
| 25 | big and little endian formats. |
| 26 | */ |
| 27 | |
| 28 | #if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN) |
| 29 | #error "can't have both LENDIAN and BENDIAN defined" |
| 30 | #endif |
| 31 | |
| 32 | #if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN) |
| 33 | #error "need either LENDIAN or BENDIAN defined" |
| 34 | #endif |
| 35 | |
| 36 | /** Swap the two bytes in the low 16bits of the parameters. |
| 37 | e.g. 0x1234 -> 0x3412 |
| 38 | */ |
| 39 | inline uint16_t SkEndianSwap16(U16CPU value) |
| 40 | { |
| 41 | SkASSERT(value == (uint16_t)value); |
| 42 | return (uint16_t)((value >> 8) | (value << 8)); |
| 43 | } |
| 44 | |
| 45 | /** Vector version of SkEndianSwap16(), which swaps the |
| 46 | low two bytes of each value in the array. |
| 47 | */ |
| 48 | inline void SkEndianSwap16s(uint16_t array[], int count) |
| 49 | { |
| 50 | SkASSERT(count == 0 || array != NULL); |
| 51 | |
| 52 | while (--count >= 0) |
| 53 | { |
| 54 | *array = SkEndianSwap16(*array); |
| 55 | array += 1; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** Reverse all 4 bytes in a 32bit value. |
| 60 | e.g. 0x12345678 -> 0x78563412 |
| 61 | */ |
| 62 | inline uint32_t SkEndianSwap32(uint32_t value) |
| 63 | { |
| 64 | return ((value & 0xFF) << 24) | |
| 65 | ((value & 0xFF00) << 8) | |
| 66 | ((value & 0xFF0000) >> 8) | |
| 67 | (value >> 24); |
| 68 | } |
| 69 | |
| 70 | /** Vector version of SkEndianSwap16(), which swaps the |
| 71 | bytes of each value in the array. |
| 72 | */ |
| 73 | inline void SkEndianSwap32s(uint32_t array[], int count) |
| 74 | { |
| 75 | SkASSERT(count == 0 || array != NULL); |
| 76 | |
| 77 | while (--count >= 0) |
| 78 | { |
| 79 | *array = SkEndianSwap32(*array); |
| 80 | array += 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #ifdef SK_CPU_LENDIAN |
| 85 | #define SkEndian_SwapBE16(n) SkEndianSwap16(n) |
| 86 | #define SkEndian_SwapBE32(n) SkEndianSwap32(n) |
| 87 | #define SkEndian_SwapLE16(n) (n) |
| 88 | #define SkEndian_SwapLE32(n) (n) |
| 89 | #else // SK_CPU_BENDIAN |
| 90 | #define SkEndian_SwapBE16(n) (n) |
| 91 | #define SkEndian_SwapBE32(n) (n) |
| 92 | #define SkEndian_SwapLE16(n) SkEndianSwap16(n) |
| 93 | #define SkEndian_SwapLE32(n) SkEndianSwap32(n) |
| 94 | #endif |
| 95 | |
| 96 | |
| 97 | #endif |
| 98 | |