blob: cf2ee109149f02780e290d71d71df7f173eae771 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkUtils_DEFINED
9#define SkUtils_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFontTypes.h"
12#include "src/core/SkOpts.h"
13#include "src/utils/SkUTF.h"
mtkleinbdb34d02015-07-31 14:02:36 -070014
Mike Klein57bc1e72017-01-04 14:44:20 -050015/** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer.
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 @param buffer The memory to have value copied into it
Mike Klein57bc1e72017-01-04 14:44:20 -050017 @param value The value to be copied into buffer
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 @param count The number of times value should be copied into the buffer.
19*/
Mike Klein87db0012017-05-23 12:20:38 -040020static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) {
21 SkOpts::memset16(buffer, value, count);
22}
23static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) {
24 SkOpts::memset32(buffer, value, count);
25}
26static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) {
27 SkOpts::memset64(buffer, value, count);
28}
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
30///////////////////////////////////////////////////////////////////////////////
31
Hal Canaryf107a2f2018-07-25 16:52:48 -040032// Unlike the functions in SkUTF.h, these two functions do not take an array
33// length parameter. When possible, use SkUTF::NextUTF{8,16} instead.
34SkUnichar SkUTF8_NextUnichar(const char**);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035SkUnichar SkUTF16_NextUnichar(const uint16_t**);
Hal Canaryc9b6dda2018-06-19 09:53:26 -040036
Hal Canaryf107a2f2018-07-25 16:52:48 -040037///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
Ben Wagnerad031f52018-08-20 13:45:57 -040039static inline bool SkUTF16_IsLeadingSurrogate(uint16_t c) { return ((c) & 0xFC00) == 0xD800; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
Ben Wagnerad031f52018-08-20 13:45:57 -040041static inline bool SkUTF16_IsTrailingSurrogate (uint16_t c) { return ((c) & 0xFC00) == 0xDC00; }
Hal Canaryf107a2f2018-07-25 16:52:48 -040042
43///////////////////////////////////////////////////////////////////////////////
44
Mike Reed64670cb2019-04-16 11:37:38 -070045static inline int SkUTFN_CountUnichars(SkTextEncoding enc, const void* utfN, size_t bytes) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040046 switch (enc) {
Mike Reed64670cb2019-04-16 11:37:38 -070047 case SkTextEncoding::kUTF8: return SkUTF::CountUTF8((const char*)utfN, bytes);
48 case SkTextEncoding::kUTF16: return SkUTF::CountUTF16((const uint16_t*)utfN, bytes);
49 case SkTextEncoding::kUTF32: return SkUTF::CountUTF32((const int32_t*)utfN, bytes);
Hal Canaryf107a2f2018-07-25 16:52:48 -040050 default: SkDEBUGFAIL("unknown text encoding"); return -1;
reed@google.com419f4332011-12-21 15:21:32 +000051 }
reed@google.com419f4332011-12-21 15:21:32 +000052}
Hal Canaryd6e6e662017-06-17 10:38:13 -040053
Mike Reed64670cb2019-04-16 11:37:38 -070054static inline SkUnichar SkUTFN_Next(SkTextEncoding enc, const void** ptr, const void* stop) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040055 switch (enc) {
Mike Reed64670cb2019-04-16 11:37:38 -070056 case SkTextEncoding::kUTF8:
Hal Canaryf107a2f2018-07-25 16:52:48 -040057 return SkUTF::NextUTF8((const char**)ptr, (const char*)stop);
Mike Reed64670cb2019-04-16 11:37:38 -070058 case SkTextEncoding::kUTF16:
Hal Canaryf107a2f2018-07-25 16:52:48 -040059 return SkUTF::NextUTF16((const uint16_t**)ptr, (const uint16_t*)stop);
Mike Reed64670cb2019-04-16 11:37:38 -070060 case SkTextEncoding::kUTF32:
Hal Canaryf107a2f2018-07-25 16:52:48 -040061 return SkUTF::NextUTF32((const int32_t**)ptr, (const int32_t*)stop);
62 default: SkDEBUGFAIL("unknown text encoding"); return -1;
63 }
64}
65
66///////////////////////////////////////////////////////////////////////////////
67
Hal Canaryd6e6e662017-06-17 10:38:13 -040068namespace SkHexadecimalDigits {
69 extern const char gUpper[16]; // 0-9A-F
70 extern const char gLower[16]; // 0-9a-f
71}
72
Mike Klein7a177b42019-06-17 17:17:47 -050073///////////////////////////////////////////////////////////////////////////////
74
75// If T is an 8-byte GCC or Clang vector extension type, it would naturally
76// pass or return in the MMX mm0 register on 32-bit x86 builds. This has the
77// fun side effect of clobbering any state in the x87 st0 register. (There is
78// no ABI governing who should preserve mm?/st? registers, so no one does!)
79//
80// We force-inline sk_unaligned_load() and sk_unaligned_store() to avoid that,
81// making them safe to use for all types on all platforms, thus solving the
82// problem once and for all!
83
84template <typename T, typename P>
85static SK_ALWAYS_INLINE T sk_unaligned_load(const P* ptr) {
86 // TODO: static_assert desirable things about T here so as not to be totally abused.
87 T val;
88 memcpy(&val, ptr, sizeof(val));
89 return val;
90}
91
92template <typename T, typename P>
93static SK_ALWAYS_INLINE void sk_unaligned_store(P* ptr, T val) {
94 // TODO: ditto
95 memcpy(ptr, &val, sizeof(val));
96}
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098#endif