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.
11 #include "SkLazyFnPtr.h"
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 sk_memcpy32_portable(uint32_t dst[], const uint32_t src[], int count) {
113 memcpy(dst, src, count * sizeof(uint32_t));
117 // These three methods technically need external linkage to be passed as template parameters.
118 // Since they can't be static, we hide them in an anonymous namespace instead.
120 SkMemset16Proc choose_memset16() {
121 SkMemset16Proc proc = SkMemset16GetPlatformProc();
122 return proc ? proc : sk_memset16_portable;
125 SkMemset32Proc choose_memset32() {
126 SkMemset32Proc proc = SkMemset32GetPlatformProc();
127 return proc ? proc : sk_memset32_portable;
130 SkMemcpy32Proc choose_memcpy32() {
131 SkMemcpy32Proc proc = SkMemcpy32GetPlatformProc();
132 return proc ? proc : sk_memcpy32_portable;
137 void sk_memset16(uint16_t dst[], uint16_t value, int count) {
138 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset16Proc, proc, choose_memset16);
139 proc.get()(dst, value, count);
142 void sk_memset32(uint32_t dst[], uint32_t value, int count) {
143 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset32Proc, proc, choose_memset32);
144 proc.get()(dst, value, count);
147 void sk_memcpy32(uint32_t dst[], const uint32_t src[], int count) {
148 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemcpy32Proc, proc, choose_memcpy32);
149 proc.get()(dst, src, count);
152 ///////////////////////////////////////////////////////////////////////////////
155 10xxxxxx // never a leading byte
160 11 10 01 01 xx xx xx xx 0...
166 static void assert_utf8_leadingbyte(unsigned c) {
167 SkASSERT(c <= 0xF7); // otherwise leading byte is too big (more than 4 bytes)
168 SkASSERT((c & 0xC0) != 0x80); // can't begin with a middle char
171 int SkUTF8_LeadByteToCount(unsigned c) {
172 assert_utf8_leadingbyte(c);
173 return (((0xE5 << 24) >> (c >> 4 << 1)) & 3) + 1;
176 #define assert_utf8_leadingbyte(c)
179 int SkUTF8_CountUnichars(const char utf8[]) {
185 int c = *(const uint8_t*)utf8;
189 utf8 += SkUTF8_LeadByteToCount(c);
195 int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
196 SkASSERT(utf8 || 0 == byteLength);
199 const char* stop = utf8 + byteLength;
201 while (utf8 < stop) {
202 utf8 += SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
208 SkUnichar SkUTF8_ToUnichar(const char utf8[]) {
211 const uint8_t* p = (const uint8_t*)utf8;
215 assert_utf8_leadingbyte(c);
218 uint32_t mask = (uint32_t)~0x3F;
221 c = (c << 6) | (*++p & 0x3F);
223 } while ((hic <<= 1) < 0);
229 SkUnichar SkUTF8_NextUnichar(const char** ptr) {
230 SkASSERT(ptr && *ptr);
232 const uint8_t* p = (const uint8_t*)*ptr;
236 assert_utf8_leadingbyte(c);
239 uint32_t mask = (uint32_t)~0x3F;
242 c = (c << 6) | (*++p & 0x3F);
244 } while ((hic <<= 1) < 0);
251 SkUnichar SkUTF8_PrevUnichar(const char** ptr) {
252 SkASSERT(ptr && *ptr);
254 const char* p = *ptr;
257 while (*--p & 0x40) {
263 return SkUTF8_NextUnichar(&p);
266 size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[]) {
267 if ((uint32_t)uni > 0x10FFFF) {
268 SkDEBUGFAIL("bad unichar");
283 SkDEBUGCODE(SkUnichar orig = uni;)
285 while (uni > 0x7F >> count) {
286 *p++ = (char)(0x80 | (uni & 0x3F));
294 while (p < tmp + count - 1) {
297 *--utf8 = (char)(~(0xFF >> count) | uni);
300 SkASSERT(utf8 == NULL || orig == SkUTF8_ToUnichar(utf8));
304 ///////////////////////////////////////////////////////////////////////////////
306 int SkUTF16_CountUnichars(const uint16_t src[]) {
311 while ((c = *src++) != 0) {
312 SkASSERT(!SkUTF16_IsLowSurrogate(c));
313 if (SkUTF16_IsHighSurrogate(c)) {
315 SkASSERT(SkUTF16_IsLowSurrogate(c));
322 int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues) {
325 const uint16_t* stop = src + numberOf16BitValues;
329 SkASSERT(!SkUTF16_IsLowSurrogate(c));
330 if (SkUTF16_IsHighSurrogate(c)) {
331 SkASSERT(src < stop);
333 SkASSERT(SkUTF16_IsLowSurrogate(c));
340 SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr) {
341 SkASSERT(srcPtr && *srcPtr);
343 const uint16_t* src = *srcPtr;
344 SkUnichar c = *src++;
346 SkASSERT(!SkUTF16_IsLowSurrogate(c));
347 if (SkUTF16_IsHighSurrogate(c)) {
348 unsigned c2 = *src++;
349 SkASSERT(SkUTF16_IsLowSurrogate(c2));
351 // c = ((c & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000
352 // c = (((c & 0x3FF) + 64) << 10) + (c2 & 0x3FF)
353 c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00);
359 SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr) {
360 SkASSERT(srcPtr && *srcPtr);
362 const uint16_t* src = *srcPtr;
363 SkUnichar c = *--src;
365 SkASSERT(!SkUTF16_IsHighSurrogate(c));
366 if (SkUTF16_IsLowSurrogate(c)) {
367 unsigned c2 = *--src;
368 SkASSERT(SkUTF16_IsHighSurrogate(c2));
369 c = (c2 << 10) + c + (0x10000 - (0xD800 << 10) - 0xDC00);
375 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[]) {
376 SkASSERT((unsigned)uni <= 0x10FFFF);
378 int extra = (uni > 0xFFFF);
382 // dst[0] = SkToU16(0xD800 | ((uni - 0x10000) >> 10));
383 // dst[0] = SkToU16(0xD800 | ((uni >> 10) - 64));
384 dst[0] = SkToU16((0xD800 - 64) + (uni >> 10));
385 dst[1] = SkToU16(0xDC00 | (uni & 0x3FF));
387 SkASSERT(SkUTF16_IsHighSurrogate(dst[0]));
388 SkASSERT(SkUTF16_IsLowSurrogate(dst[1]));
390 dst[0] = SkToU16(uni);
391 SkASSERT(!SkUTF16_IsHighSurrogate(dst[0]));
392 SkASSERT(!SkUTF16_IsLowSurrogate(dst[0]));
398 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
400 SkASSERT(numberOf16BitValues >= 0);
401 if (numberOf16BitValues <= 0) {
405 SkASSERT(utf16 != NULL);
407 const uint16_t* stop = utf16 + numberOf16BitValues;
410 if (utf8 == NULL) { // just count
411 while (utf16 < stop) {
412 size += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), NULL);
416 while (utf16 < stop) {
417 utf8 += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), utf8);