3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
14 #define assign_16_longs(dst, value) \
16 (dst)[0] = value; (dst)[1] = value; \
17 (dst)[2] = value; (dst)[3] = value; \
18 (dst)[4] = value; (dst)[5] = value; \
19 (dst)[6] = value; (dst)[7] = value; \
20 (dst)[8] = value; (dst)[9] = value; \
21 (dst)[10] = value; (dst)[11] = value; \
22 (dst)[12] = value; (dst)[13] = value; \
23 (dst)[14] = value; (dst)[15] = value; \
26 #define assign_16_longs(dst, value) \
28 *(dst)++ = value; *(dst)++ = value; \
29 *(dst)++ = value; *(dst)++ = value; \
30 *(dst)++ = value; *(dst)++ = value; \
31 *(dst)++ = value; *(dst)++ = value; \
32 *(dst)++ = value; *(dst)++ = value; \
33 *(dst)++ = value; *(dst)++ = value; \
34 *(dst)++ = value; *(dst)++ = value; \
35 *(dst)++ = value; *(dst)++ = value; \
39 ///////////////////////////////////////////////////////////////////////////////
41 static void sk_memset16_portable(uint16_t dst[], uint16_t value, int count) {
42 SkASSERT(dst != NULL && count >= 0);
48 // not sure if this helps to short-circuit on small values of count
51 *dst++ = (uint16_t)value;
52 } while (--count != 0);
56 // ensure we're on a long boundary
57 if ((size_t)dst & 2) {
58 *dst++ = (uint16_t)value;
62 uint32_t value32 = ((uint32_t)value << 16) | value;
64 // handle the bulk with our unrolled macro
66 int sixteenlongs = count >> 5;
68 uint32_t* dst32 = (uint32_t*)dst;
70 assign_16_longs(dst32, value32);
71 } while (--sixteenlongs != 0);
72 dst = (uint16_t*)dst32;
77 // handle (most) of the rest
79 int longs = count >> 1;
82 *(uint32_t*)dst = value32;
84 } while (--longs != 0);
88 // cleanup a possible trailing short
90 *dst = (uint16_t)value;
94 static void sk_memset32_portable(uint32_t dst[], uint32_t value, int count) {
95 SkASSERT(dst != NULL && count >= 0);
97 int sixteenlongs = count >> 4;
100 assign_16_longs(dst, value);
101 } while (--sixteenlongs != 0);
108 } while (--count != 0);
112 static void choose_memset16(SkMemset16Proc* proc) {
113 *proc = SkMemset16GetPlatformProc();
115 *proc = &sk_memset16_portable;
119 void sk_memset16(uint16_t dst[], uint16_t value, int count) {
120 SK_DECLARE_STATIC_ONCE(once);
121 static SkMemset16Proc proc = NULL;
122 SkOnce(&once, choose_memset16, &proc);
123 SkASSERT(proc != NULL);
125 return proc(dst, value, count);
128 static void choose_memset32(SkMemset32Proc* proc) {
129 *proc = SkMemset32GetPlatformProc();
131 *proc = &sk_memset32_portable;
135 void sk_memset32(uint32_t dst[], uint32_t value, int count) {
136 SK_DECLARE_STATIC_ONCE(once);
137 static SkMemset32Proc proc = NULL;
138 SkOnce(&once, choose_memset32, &proc);
139 SkASSERT(proc != NULL);
141 return proc(dst, value, count);
144 ///////////////////////////////////////////////////////////////////////////////
147 10xxxxxx // never a leading byte
152 11 10 01 01 xx xx xx xx 0...
158 static void assert_utf8_leadingbyte(unsigned c) {
159 SkASSERT(c <= 0xF7); // otherwise leading byte is too big (more than 4 bytes)
160 SkASSERT((c & 0xC0) != 0x80); // can't begin with a middle char
163 int SkUTF8_LeadByteToCount(unsigned c) {
164 assert_utf8_leadingbyte(c);
165 return (((0xE5 << 24) >> (c >> 4 << 1)) & 3) + 1;
168 #define assert_utf8_leadingbyte(c)
171 int SkUTF8_CountUnichars(const char utf8[]) {
177 int c = *(const uint8_t*)utf8;
181 utf8 += SkUTF8_LeadByteToCount(c);
187 int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
188 SkASSERT(NULL != utf8 || 0 == byteLength);
191 const char* stop = utf8 + byteLength;
193 while (utf8 < stop) {
194 utf8 += SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
200 SkUnichar SkUTF8_ToUnichar(const char utf8[]) {
201 SkASSERT(NULL != utf8);
203 const uint8_t* p = (const uint8_t*)utf8;
207 assert_utf8_leadingbyte(c);
210 uint32_t mask = (uint32_t)~0x3F;
213 c = (c << 6) | (*++p & 0x3F);
215 } while ((hic <<= 1) < 0);
221 SkUnichar SkUTF8_NextUnichar(const char** ptr) {
222 SkASSERT(NULL != ptr && NULL != *ptr);
224 const uint8_t* p = (const uint8_t*)*ptr;
228 assert_utf8_leadingbyte(c);
231 uint32_t mask = (uint32_t)~0x3F;
234 c = (c << 6) | (*++p & 0x3F);
236 } while ((hic <<= 1) < 0);
243 SkUnichar SkUTF8_PrevUnichar(const char** ptr) {
244 SkASSERT(NULL != ptr && NULL != *ptr);
246 const char* p = *ptr;
249 while (*--p & 0x40) {
255 return SkUTF8_NextUnichar(&p);
258 size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[]) {
259 if ((uint32_t)uni > 0x10FFFF) {
260 SkDEBUGFAIL("bad unichar");
275 SkDEBUGCODE(SkUnichar orig = uni;)
277 while (uni > 0x7F >> count) {
278 *p++ = (char)(0x80 | (uni & 0x3F));
286 while (p < tmp + count - 1) {
289 *--utf8 = (char)(~(0xFF >> count) | uni);
292 SkASSERT(utf8 == NULL || orig == SkUTF8_ToUnichar(utf8));
296 ///////////////////////////////////////////////////////////////////////////////
298 int SkUTF16_CountUnichars(const uint16_t src[]) {
303 while ((c = *src++) != 0) {
304 SkASSERT(!SkUTF16_IsLowSurrogate(c));
305 if (SkUTF16_IsHighSurrogate(c)) {
307 SkASSERT(SkUTF16_IsLowSurrogate(c));
314 int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues) {
317 const uint16_t* stop = src + numberOf16BitValues;
321 SkASSERT(!SkUTF16_IsLowSurrogate(c));
322 if (SkUTF16_IsHighSurrogate(c)) {
323 SkASSERT(src < stop);
325 SkASSERT(SkUTF16_IsLowSurrogate(c));
332 SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr) {
333 SkASSERT(srcPtr && *srcPtr);
335 const uint16_t* src = *srcPtr;
336 SkUnichar c = *src++;
338 SkASSERT(!SkUTF16_IsLowSurrogate(c));
339 if (SkUTF16_IsHighSurrogate(c)) {
340 unsigned c2 = *src++;
341 SkASSERT(SkUTF16_IsLowSurrogate(c2));
343 // c = ((c & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000
344 // c = (((c & 0x3FF) + 64) << 10) + (c2 & 0x3FF)
345 c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00);
351 SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr) {
352 SkASSERT(srcPtr && *srcPtr);
354 const uint16_t* src = *srcPtr;
355 SkUnichar c = *--src;
357 SkASSERT(!SkUTF16_IsHighSurrogate(c));
358 if (SkUTF16_IsLowSurrogate(c)) {
359 unsigned c2 = *--src;
360 SkASSERT(SkUTF16_IsHighSurrogate(c2));
361 c = (c2 << 10) + c + (0x10000 - (0xD800 << 10) - 0xDC00);
367 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[]) {
368 SkASSERT((unsigned)uni <= 0x10FFFF);
370 int extra = (uni > 0xFFFF);
374 // dst[0] = SkToU16(0xD800 | ((uni - 0x10000) >> 10));
375 // dst[0] = SkToU16(0xD800 | ((uni >> 10) - 64));
376 dst[0] = SkToU16((0xD800 - 64) + (uni >> 10));
377 dst[1] = SkToU16(0xDC00 | (uni & 0x3FF));
379 SkASSERT(SkUTF16_IsHighSurrogate(dst[0]));
380 SkASSERT(SkUTF16_IsLowSurrogate(dst[1]));
382 dst[0] = SkToU16(uni);
383 SkASSERT(!SkUTF16_IsHighSurrogate(dst[0]));
384 SkASSERT(!SkUTF16_IsLowSurrogate(dst[0]));
390 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
392 SkASSERT(numberOf16BitValues >= 0);
393 if (numberOf16BitValues <= 0) {
397 SkASSERT(utf16 != NULL);
399 const uint16_t* stop = utf16 + numberOf16BitValues;
402 if (utf8 == NULL) { // just count
403 while (utf16 < stop) {
404 size += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), NULL);
408 while (utf16 < stop) {
409 utf8 += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), utf8);