SkUtils.cpp revision 3fdc7d6dd13b510de09cf29ffd3fe36adf89d541
1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#include "SkUtils.h" 11#include "SkLazyFnPtr.h" 12 13#if 0 14#define assign_16_longs(dst, value) \ 15 do { \ 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; \ 24 } while (0) 25#else 26#define assign_16_longs(dst, value) \ 27 do { \ 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; \ 36 } while (0) 37#endif 38 39/////////////////////////////////////////////////////////////////////////////// 40 41static void sk_memset16_portable(uint16_t dst[], uint16_t value, int count) { 42 SkASSERT(dst != NULL && count >= 0); 43 44 if (count <= 0) { 45 return; 46 } 47 48 // not sure if this helps to short-circuit on small values of count 49 if (count < 8) { 50 do { 51 *dst++ = (uint16_t)value; 52 } while (--count != 0); 53 return; 54 } 55 56 // ensure we're on a long boundary 57 if ((size_t)dst & 2) { 58 *dst++ = (uint16_t)value; 59 count -= 1; 60 } 61 62 uint32_t value32 = ((uint32_t)value << 16) | value; 63 64 // handle the bulk with our unrolled macro 65 { 66 int sixteenlongs = count >> 5; 67 if (sixteenlongs) { 68 uint32_t* dst32 = (uint32_t*)dst; 69 do { 70 assign_16_longs(dst32, value32); 71 } while (--sixteenlongs != 0); 72 dst = (uint16_t*)dst32; 73 count &= 31; 74 } 75 } 76 77 // handle (most) of the rest 78 { 79 int longs = count >> 1; 80 if (longs) { 81 do { 82 *(uint32_t*)dst = value32; 83 dst += 2; 84 } while (--longs != 0); 85 } 86 } 87 88 // cleanup a possible trailing short 89 if (count & 1) { 90 *dst = (uint16_t)value; 91 } 92} 93 94static void sk_memset32_portable(uint32_t dst[], uint32_t value, int count) { 95 SkASSERT(dst != NULL && count >= 0); 96 97 int sixteenlongs = count >> 4; 98 if (sixteenlongs) { 99 do { 100 assign_16_longs(dst, value); 101 } while (--sixteenlongs != 0); 102 count &= 15; 103 } 104 105 if (count) { 106 do { 107 *dst++ = value; 108 } while (--count != 0); 109 } 110} 111 112static void sk_memcpy32_portable(uint32_t dst[], const uint32_t src[], int count) { 113 memcpy(dst, src, count * sizeof(uint32_t)); 114} 115 116static SkMemset16Proc choose_memset16() { 117 SkMemset16Proc proc = SkMemset16GetPlatformProc(); 118 return proc ? proc : sk_memset16_portable; 119} 120 121void sk_memset16(uint16_t dst[], uint16_t value, int count) { 122 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset16Proc, choice); 123 return choice.get(choose_memset16)(dst, value, count); 124} 125 126static SkMemset32Proc choose_memset32() { 127 SkMemset32Proc proc = SkMemset32GetPlatformProc(); 128 return proc ? proc : sk_memset32_portable; 129} 130 131void sk_memset32(uint32_t dst[], uint32_t value, int count) { 132 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemset32Proc, choice); 133 return choice.get(choose_memset32)(dst, value, count); 134} 135 136static SkMemcpy32Proc choose_memcpy32() { 137 SkMemcpy32Proc proc = SkMemcpy32GetPlatformProc(); 138 return proc ? proc : sk_memcpy32_portable; 139} 140 141void sk_memcpy32(uint32_t dst[], const uint32_t src[], int count) { 142 SK_DECLARE_STATIC_LAZY_FN_PTR(SkMemcpy32Proc, choice); 143 return choice.get(choose_memcpy32)(dst, src, count); 144} 145 146/////////////////////////////////////////////////////////////////////////////// 147 148/* 0xxxxxxx 1 total 149 10xxxxxx // never a leading byte 150 110xxxxx 2 total 151 1110xxxx 3 total 152 11110xxx 4 total 153 154 11 10 01 01 xx xx xx xx 0... 155 0xE5XX0000 156 0xE5 << 24 157*/ 158 159#ifdef SK_DEBUG 160 static void assert_utf8_leadingbyte(unsigned c) { 161 SkASSERT(c <= 0xF7); // otherwise leading byte is too big (more than 4 bytes) 162 SkASSERT((c & 0xC0) != 0x80); // can't begin with a middle char 163 } 164 165 int SkUTF8_LeadByteToCount(unsigned c) { 166 assert_utf8_leadingbyte(c); 167 return (((0xE5 << 24) >> (c >> 4 << 1)) & 3) + 1; 168 } 169#else 170 #define assert_utf8_leadingbyte(c) 171#endif 172 173int SkUTF8_CountUnichars(const char utf8[]) { 174 SkASSERT(utf8); 175 176 int count = 0; 177 178 for (;;) { 179 int c = *(const uint8_t*)utf8; 180 if (c == 0) { 181 break; 182 } 183 utf8 += SkUTF8_LeadByteToCount(c); 184 count += 1; 185 } 186 return count; 187} 188 189int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) { 190 SkASSERT(NULL != utf8 || 0 == byteLength); 191 192 int count = 0; 193 const char* stop = utf8 + byteLength; 194 195 while (utf8 < stop) { 196 utf8 += SkUTF8_LeadByteToCount(*(const uint8_t*)utf8); 197 count += 1; 198 } 199 return count; 200} 201 202SkUnichar SkUTF8_ToUnichar(const char utf8[]) { 203 SkASSERT(NULL != utf8); 204 205 const uint8_t* p = (const uint8_t*)utf8; 206 int c = *p; 207 int hic = c << 24; 208 209 assert_utf8_leadingbyte(c); 210 211 if (hic < 0) { 212 uint32_t mask = (uint32_t)~0x3F; 213 hic <<= 1; 214 do { 215 c = (c << 6) | (*++p & 0x3F); 216 mask <<= 5; 217 } while ((hic <<= 1) < 0); 218 c &= ~mask; 219 } 220 return c; 221} 222 223SkUnichar SkUTF8_NextUnichar(const char** ptr) { 224 SkASSERT(NULL != ptr && NULL != *ptr); 225 226 const uint8_t* p = (const uint8_t*)*ptr; 227 int c = *p; 228 int hic = c << 24; 229 230 assert_utf8_leadingbyte(c); 231 232 if (hic < 0) { 233 uint32_t mask = (uint32_t)~0x3F; 234 hic <<= 1; 235 do { 236 c = (c << 6) | (*++p & 0x3F); 237 mask <<= 5; 238 } while ((hic <<= 1) < 0); 239 c &= ~mask; 240 } 241 *ptr = (char*)p + 1; 242 return c; 243} 244 245SkUnichar SkUTF8_PrevUnichar(const char** ptr) { 246 SkASSERT(NULL != ptr && NULL != *ptr); 247 248 const char* p = *ptr; 249 250 if (*--p & 0x80) { 251 while (*--p & 0x40) { 252 ; 253 } 254 } 255 256 *ptr = (char*)p; 257 return SkUTF8_NextUnichar(&p); 258} 259 260size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[]) { 261 if ((uint32_t)uni > 0x10FFFF) { 262 SkDEBUGFAIL("bad unichar"); 263 return 0; 264 } 265 266 if (uni <= 127) { 267 if (utf8) { 268 *utf8 = (char)uni; 269 } 270 return 1; 271 } 272 273 char tmp[4]; 274 char* p = tmp; 275 size_t count = 1; 276 277 SkDEBUGCODE(SkUnichar orig = uni;) 278 279 while (uni > 0x7F >> count) { 280 *p++ = (char)(0x80 | (uni & 0x3F)); 281 uni >>= 6; 282 count += 1; 283 } 284 285 if (utf8) { 286 p = tmp; 287 utf8 += count; 288 while (p < tmp + count - 1) { 289 *--utf8 = *p++; 290 } 291 *--utf8 = (char)(~(0xFF >> count) | uni); 292 } 293 294 SkASSERT(utf8 == NULL || orig == SkUTF8_ToUnichar(utf8)); 295 return count; 296} 297 298/////////////////////////////////////////////////////////////////////////////// 299 300int SkUTF16_CountUnichars(const uint16_t src[]) { 301 SkASSERT(src); 302 303 int count = 0; 304 unsigned c; 305 while ((c = *src++) != 0) { 306 SkASSERT(!SkUTF16_IsLowSurrogate(c)); 307 if (SkUTF16_IsHighSurrogate(c)) { 308 c = *src++; 309 SkASSERT(SkUTF16_IsLowSurrogate(c)); 310 } 311 count += 1; 312 } 313 return count; 314} 315 316int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues) { 317 SkASSERT(src); 318 319 const uint16_t* stop = src + numberOf16BitValues; 320 int count = 0; 321 while (src < stop) { 322 unsigned c = *src++; 323 SkASSERT(!SkUTF16_IsLowSurrogate(c)); 324 if (SkUTF16_IsHighSurrogate(c)) { 325 SkASSERT(src < stop); 326 c = *src++; 327 SkASSERT(SkUTF16_IsLowSurrogate(c)); 328 } 329 count += 1; 330 } 331 return count; 332} 333 334SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr) { 335 SkASSERT(srcPtr && *srcPtr); 336 337 const uint16_t* src = *srcPtr; 338 SkUnichar c = *src++; 339 340 SkASSERT(!SkUTF16_IsLowSurrogate(c)); 341 if (SkUTF16_IsHighSurrogate(c)) { 342 unsigned c2 = *src++; 343 SkASSERT(SkUTF16_IsLowSurrogate(c2)); 344 345 // c = ((c & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000 346 // c = (((c & 0x3FF) + 64) << 10) + (c2 & 0x3FF) 347 c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00); 348 } 349 *srcPtr = src; 350 return c; 351} 352 353SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr) { 354 SkASSERT(srcPtr && *srcPtr); 355 356 const uint16_t* src = *srcPtr; 357 SkUnichar c = *--src; 358 359 SkASSERT(!SkUTF16_IsHighSurrogate(c)); 360 if (SkUTF16_IsLowSurrogate(c)) { 361 unsigned c2 = *--src; 362 SkASSERT(SkUTF16_IsHighSurrogate(c2)); 363 c = (c2 << 10) + c + (0x10000 - (0xD800 << 10) - 0xDC00); 364 } 365 *srcPtr = src; 366 return c; 367} 368 369size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[]) { 370 SkASSERT((unsigned)uni <= 0x10FFFF); 371 372 int extra = (uni > 0xFFFF); 373 374 if (dst) { 375 if (extra) { 376 // dst[0] = SkToU16(0xD800 | ((uni - 0x10000) >> 10)); 377 // dst[0] = SkToU16(0xD800 | ((uni >> 10) - 64)); 378 dst[0] = SkToU16((0xD800 - 64) + (uni >> 10)); 379 dst[1] = SkToU16(0xDC00 | (uni & 0x3FF)); 380 381 SkASSERT(SkUTF16_IsHighSurrogate(dst[0])); 382 SkASSERT(SkUTF16_IsLowSurrogate(dst[1])); 383 } else { 384 dst[0] = SkToU16(uni); 385 SkASSERT(!SkUTF16_IsHighSurrogate(dst[0])); 386 SkASSERT(!SkUTF16_IsLowSurrogate(dst[0])); 387 } 388 } 389 return 1 + extra; 390} 391 392size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues, 393 char utf8[]) { 394 SkASSERT(numberOf16BitValues >= 0); 395 if (numberOf16BitValues <= 0) { 396 return 0; 397 } 398 399 SkASSERT(utf16 != NULL); 400 401 const uint16_t* stop = utf16 + numberOf16BitValues; 402 size_t size = 0; 403 404 if (utf8 == NULL) { // just count 405 while (utf16 < stop) { 406 size += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), NULL); 407 } 408 } else { 409 char* start = utf8; 410 while (utf16 < stop) { 411 utf8 += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), utf8); 412 } 413 size = utf8 - start; 414 } 415 return size; 416} 417