blob: d7dd562cf15b9655ae48e1d4b9cf283a81196f6c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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.comad917992011-04-11 19:01:12 +000032static inline uint16_t SkEndianSwap16(U16CPU value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 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.comad917992011-04-11 19:01:12 +000040static inline void SkEndianSwap16s(uint16_t array[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 SkASSERT(count == 0 || array != NULL);
42
reed@google.comad917992011-04-11 19:01:12 +000043 while (--count >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 *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.comad917992011-04-11 19:01:12 +000052static inline uint32_t SkEndianSwap32(uint32_t value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 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.comad917992011-04-11 19:01:12 +000062static inline void SkEndianSwap32s(uint32_t array[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 SkASSERT(count == 0 || array != NULL);
64
reed@google.comad917992011-04-11 19:01:12 +000065 while (--count >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 *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