• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
SkEndianSwap16(U16CPU value)39 static inline uint16_t SkEndianSwap16(U16CPU value) {
40     SkASSERT(value == (uint16_t)value);
41     return (uint16_t)((value >> 8) | (value << 8));
42 }
43 
44 /** Vector version of SkEndianSwap16(), which swaps the
45     low two bytes of each value in the array.
46 */
SkEndianSwap16s(uint16_t array[],int count)47 static inline void SkEndianSwap16s(uint16_t array[], int count) {
48     SkASSERT(count == 0 || array != NULL);
49 
50     while (--count >= 0) {
51         *array = SkEndianSwap16(*array);
52         array += 1;
53     }
54 }
55 
56 /** Reverse all 4 bytes in a 32bit value.
57     e.g. 0x12345678 -> 0x78563412
58 */
SkEndianSwap32(uint32_t value)59 static inline uint32_t SkEndianSwap32(uint32_t value) {
60     return  ((value & 0xFF) << 24) |
61             ((value & 0xFF00) << 8) |
62             ((value & 0xFF0000) >> 8) |
63             (value >> 24);
64 }
65 
66 /** Vector version of SkEndianSwap16(), which swaps the
67     bytes of each value in the array.
68 */
SkEndianSwap32s(uint32_t array[],int count)69 static inline void SkEndianSwap32s(uint32_t array[], int count) {
70     SkASSERT(count == 0 || array != NULL);
71 
72     while (--count >= 0) {
73         *array = SkEndianSwap32(*array);
74         array += 1;
75     }
76 }
77 
78 #ifdef SK_CPU_LENDIAN
79     #define SkEndian_SwapBE16(n)    SkEndianSwap16(n)
80     #define SkEndian_SwapBE32(n)    SkEndianSwap32(n)
81     #define SkEndian_SwapLE16(n)    (n)
82     #define SkEndian_SwapLE32(n)    (n)
83 #else   // SK_CPU_BENDIAN
84     #define SkEndian_SwapBE16(n)    (n)
85     #define SkEndian_SwapBE32(n)    (n)
86     #define SkEndian_SwapLE16(n)    SkEndianSwap16(n)
87     #define SkEndian_SwapLE32(n)    SkEndianSwap32(n)
88 #endif
89 
90 
91 #endif
92 
93