Flutter Engine
The Flutter Engine
SkEndian.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkEndian_DEFINED
9#define SkEndian_DEFINED
10
13
14#include <cstdint>
15
16/** \file SkEndian.h
17
18 Macros and helper functions for handling 16 and 32 bit values in
19 big and little endian formats.
20*/
21
22#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN)
23 #error "can't have both LENDIAN and BENDIAN defined"
24#endif
25
26#if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN)
27 #error "need either LENDIAN or BENDIAN defined"
28#endif
29
30/** Swap the two bytes in the low 16bits of the parameters.
31 e.g. 0x1234 -> 0x3412
32*/
33static inline uint16_t SkEndianSwap16(uint16_t value) {
34 return static_cast<uint16_t>((value >> 8) | ((value & 0xFF) << 8));
35}
36
37template<uint16_t N> struct SkTEndianSwap16 {
38 static const uint16_t value = static_cast<uint16_t>((N >> 8) | ((N & 0xFF) << 8));
39};
40
41/** Vector version of SkEndianSwap16(), which swaps the
42 low two bytes of each value in the array.
43*/
44static inline void SkEndianSwap16s(uint16_t array[], int count) {
45 SkASSERT(count == 0 || array != nullptr);
46
47 while (--count >= 0) {
48 *array = SkEndianSwap16(*array);
49 array += 1;
50 }
51}
52
53/** Reverse all 4 bytes in a 32bit value.
54 e.g. 0x12345678 -> 0x78563412
55*/
56static constexpr uint32_t SkEndianSwap32(uint32_t value) {
57 return ((value & 0xFF) << 24) |
58 ((value & 0xFF00) << 8) |
59 ((value & 0xFF0000) >> 8) |
60 (value >> 24);
61}
62
63template<uint32_t N> struct SkTEndianSwap32 {
64 static const uint32_t value = ((N & 0xFF) << 24) |
65 ((N & 0xFF00) << 8) |
66 ((N & 0xFF0000) >> 8) |
67 (N >> 24);
68};
69
70/** Vector version of SkEndianSwap32(), which swaps the
71 bytes of each value in the array.
72*/
73static inline void SkEndianSwap32s(uint32_t array[], int count) {
74 SkASSERT(count == 0 || array != nullptr);
75
76 while (--count >= 0) {
77 *array = SkEndianSwap32(*array);
78 array += 1;
79 }
80}
81
82/** Reverse all 8 bytes in a 64bit value.
83 e.g. 0x1122334455667788 -> 0x8877665544332211
84*/
85static inline uint64_t SkEndianSwap64(uint64_t value) {
86 return (((value & 0x00000000000000FFULL) << (8*7)) |
87 ((value & 0x000000000000FF00ULL) << (8*5)) |
88 ((value & 0x0000000000FF0000ULL) << (8*3)) |
89 ((value & 0x00000000FF000000ULL) << (8*1)) |
90 ((value & 0x000000FF00000000ULL) >> (8*1)) |
91 ((value & 0x0000FF0000000000ULL) >> (8*3)) |
92 ((value & 0x00FF000000000000ULL) >> (8*5)) |
93 ((value) >> (8*7)));
94}
95template<uint64_t N> struct SkTEndianSwap64 {
96 static const uint64_t value = (((N & 0x00000000000000FFULL) << (8*7)) |
97 ((N & 0x000000000000FF00ULL) << (8*5)) |
98 ((N & 0x0000000000FF0000ULL) << (8*3)) |
99 ((N & 0x00000000FF000000ULL) << (8*1)) |
100 ((N & 0x000000FF00000000ULL) >> (8*1)) |
101 ((N & 0x0000FF0000000000ULL) >> (8*3)) |
102 ((N & 0x00FF000000000000ULL) >> (8*5)) |
103 ((N) >> (8*7)));
104};
105
106/** Vector version of SkEndianSwap64(), which swaps the
107 bytes of each value in the array.
108*/
109static inline void SkEndianSwap64s(uint64_t array[], int count) {
110 SkASSERT(count == 0 || array != nullptr);
111
112 while (--count >= 0) {
113 *array = SkEndianSwap64(*array);
114 array += 1;
115 }
116}
117
118// Static casts are used here for otherwise no-op macros to make sure they return the same type as
119// SkEndianSwap* functions.
120#ifdef SK_CPU_LENDIAN
121 #define SkEndian_SwapBE16(n) SkEndianSwap16(n)
122 #define SkEndian_SwapBE32(n) SkEndianSwap32(n)
123 #define SkEndian_SwapBE64(n) SkEndianSwap64(n)
124 #define SkEndian_SwapLE16(n) static_cast<uint16_t>(n)
125 #define SkEndian_SwapLE32(n) static_cast<uint32_t>(n)
126 #define SkEndian_SwapLE64(n) static_cast<uint64_t>(n)
127
128 #define SkTEndian_SwapBE16(n) SkTEndianSwap16<n>::value
129 #define SkTEndian_SwapBE32(n) SkTEndianSwap32<n>::value
130 #define SkTEndian_SwapBE64(n) SkTEndianSwap64<n>::value
131 #define SkTEndian_SwapLE16(n) (n)
132 #define SkTEndian_SwapLE32(n) (n)
133 #define SkTEndian_SwapLE64(n) (n)
134#else // SK_CPU_BENDIAN
135 #define SkEndian_SwapBE16(n) static_cast<uint16_t>(n)
136 #define SkEndian_SwapBE32(n) static_cast<uint32_t>(n)
137 #define SkEndian_SwapBE64(n) static_cast<uint64_t>(n)
138 #define SkEndian_SwapLE16(n) SkEndianSwap16(n)
139 #define SkEndian_SwapLE32(n) SkEndianSwap32(n)
140 #define SkEndian_SwapLE64(n) SkEndianSwap64(n)
141
142 #define SkTEndian_SwapBE16(n) (n)
143 #define SkTEndian_SwapBE32(n) (n)
144 #define SkTEndian_SwapBE64(n) (n)
145 #define SkTEndian_SwapLE16(n) SkTEndianSwap16<n>::value
146 #define SkTEndian_SwapLE32(n) SkTEndianSwap32<n>::value
147 #define SkTEndian_SwapLE64(n) SkTEndianSwap64<n>::value
148#endif
149
150// When a bytestream is embedded in a 32-bit word, how far we need to
151// shift the word to extract each byte from the low 8 bits by anding with 0xff.
152#ifdef SK_CPU_LENDIAN
153 #define SkEndian_Byte0Shift 0
154 #define SkEndian_Byte1Shift 8
155 #define SkEndian_Byte2Shift 16
156 #define SkEndian_Byte3Shift 24
157#else // SK_CPU_BENDIAN
158 #define SkEndian_Byte0Shift 24
159 #define SkEndian_Byte1Shift 16
160 #define SkEndian_Byte2Shift 8
161 #define SkEndian_Byte3Shift 0
162#endif
163
164
165#if defined(SK_UINT8_BITFIELD_LENDIAN) && defined(SK_UINT8_BITFIELD_BENDIAN)
166 #error "can't have both bitfield LENDIAN and BENDIAN defined"
167#endif
168
169#if !defined(SK_UINT8_BITFIELD_LENDIAN) && !defined(SK_UINT8_BITFIELD_BENDIAN)
170 #ifdef SK_CPU_LENDIAN
171 #define SK_UINT8_BITFIELD_LENDIAN
172 #else
173 #define SK_UINT8_BITFIELD_BENDIAN
174 #endif
175#endif
176
177#ifdef SK_UINT8_BITFIELD_LENDIAN
178 #define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \
179 SK_OT_BYTE f0 : 1; \
180 SK_OT_BYTE f1 : 1; \
181 SK_OT_BYTE f2 : 1; \
182 SK_OT_BYTE f3 : 1; \
183 SK_OT_BYTE f4 : 1; \
184 SK_OT_BYTE f5 : 1; \
185 SK_OT_BYTE f6 : 1; \
186 SK_OT_BYTE f7 : 1;
187#else
188 #define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \
189 SK_OT_BYTE f7 : 1; \
190 SK_OT_BYTE f6 : 1; \
191 SK_OT_BYTE f5 : 1; \
192 SK_OT_BYTE f4 : 1; \
193 SK_OT_BYTE f3 : 1; \
194 SK_OT_BYTE f2 : 1; \
195 SK_OT_BYTE f1 : 1; \
196 SK_OT_BYTE f0 : 1;
197#endif
198
199#endif
int count
Definition: FontMgrTest.cpp:50
#define SkASSERT(cond)
Definition: SkAssert.h:116
static uint64_t SkEndianSwap64(uint64_t value)
Definition: SkEndian.h:85
static uint16_t SkEndianSwap16(uint16_t value)
Definition: SkEndian.h:33
static void SkEndianSwap64s(uint64_t array[], int count)
Definition: SkEndian.h:109
static void SkEndianSwap16s(uint16_t array[], int count)
Definition: SkEndian.h:44
static void SkEndianSwap32s(uint32_t array[], int count)
Definition: SkEndian.h:73
static constexpr uint32_t SkEndianSwap32(uint32_t value)
Definition: SkEndian.h:56
#define N
Definition: beziers.cpp:19
uint8_t value
static const uint16_t value
Definition: SkEndian.h:38
static const uint32_t value
Definition: SkEndian.h:64
static const uint64_t value
Definition: SkEndian.h:96