| 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 SkUtils_DEFINED | 
|---|
| 9 | #define SkUtils_DEFINED | 
|---|
| 10 |  | 
|---|
| 11 | #include "include/core/SkFontTypes.h" | 
|---|
| 12 | #include "src/core/SkOpts.h" | 
|---|
| 13 | #include "src/utils/SkUTF.h" | 
|---|
| 14 |  | 
|---|
| 15 | /** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer. | 
|---|
| 16 | @param buffer   The memory to have value copied into it | 
|---|
| 17 | @param value    The value to be copied into buffer | 
|---|
| 18 | @param count    The number of times value should be copied into the buffer. | 
|---|
| 19 | */ | 
|---|
| 20 | static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) { | 
|---|
| 21 | SkOpts::memset16(buffer, value, count); | 
|---|
| 22 | } | 
|---|
| 23 | static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) { | 
|---|
| 24 | SkOpts::memset32(buffer, value, count); | 
|---|
| 25 | } | 
|---|
| 26 | static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) { | 
|---|
| 27 | SkOpts::memset64(buffer, value, count); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 31 |  | 
|---|
| 32 | // 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. | 
|---|
| 34 | SkUnichar SkUTF8_NextUnichar(const char**); | 
|---|
| 35 | SkUnichar SkUTF16_NextUnichar(const uint16_t**); | 
|---|
| 36 |  | 
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 38 |  | 
|---|
| 39 | static inline bool SkUTF16_IsLeadingSurrogate(uint16_t c) { return ((c) & 0xFC00) == 0xD800; } | 
|---|
| 40 |  | 
|---|
| 41 | static inline bool SkUTF16_IsTrailingSurrogate (uint16_t c) { return ((c) & 0xFC00) == 0xDC00; } | 
|---|
| 42 |  | 
|---|
| 43 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 44 |  | 
|---|
| 45 | static inline int SkUTFN_CountUnichars(SkTextEncoding enc, const void* utfN, size_t bytes) { | 
|---|
| 46 | switch (enc) { | 
|---|
| 47 | 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); | 
|---|
| 50 | default: SkDEBUGFAIL( "unknown text encoding"); return -1; | 
|---|
| 51 | } | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | static inline SkUnichar SkUTFN_Next(SkTextEncoding enc, const void** ptr, const void* stop) { | 
|---|
| 55 | switch (enc) { | 
|---|
| 56 | case SkTextEncoding::kUTF8: | 
|---|
| 57 | return SkUTF::NextUTF8((const char**)ptr, (const char*)stop); | 
|---|
| 58 | case SkTextEncoding::kUTF16: | 
|---|
| 59 | return SkUTF::NextUTF16((const uint16_t**)ptr, (const uint16_t*)stop); | 
|---|
| 60 | case SkTextEncoding::kUTF32: | 
|---|
| 61 | return SkUTF::NextUTF32((const int32_t**)ptr, (const int32_t*)stop); | 
|---|
| 62 | default: SkDEBUGFAIL( "unknown text encoding"); return -1; | 
|---|
| 63 | } | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 67 |  | 
|---|
| 68 | namespace SkHexadecimalDigits { | 
|---|
| 69 | extern const char gUpper[16];  // 0-9A-F | 
|---|
| 70 | extern const char gLower[16];  // 0-9a-f | 
|---|
| 71 | }  // namespace SkHexadecimalDigits | 
|---|
| 72 |  | 
|---|
| 73 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 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 |  | 
|---|
| 84 | template <typename T, typename P> | 
|---|
| 85 | static SK_ALWAYS_INLINE T sk_unaligned_load(const P* ptr) { | 
|---|
| 86 | static_assert(std::is_trivially_copyable<T>::value); | 
|---|
| 87 | static_assert(std::is_trivially_copyable<P>::value); | 
|---|
| 88 | T val; | 
|---|
| 89 | memcpy(&val, ptr, sizeof(val)); | 
|---|
| 90 | return val; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | template <typename T, typename P> | 
|---|
| 94 | static SK_ALWAYS_INLINE void sk_unaligned_store(P* ptr, T val) { | 
|---|
| 95 | static_assert(std::is_trivially_copyable<T>::value); | 
|---|
| 96 | static_assert(std::is_trivially_copyable<P>::value); | 
|---|
| 97 | memcpy(ptr, &val, sizeof(val)); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | template <typename Dst, typename Src> | 
|---|
| 101 | static SK_ALWAYS_INLINE Dst sk_bit_cast(const Src& src) { | 
|---|
| 102 | static_assert(sizeof(Dst) == sizeof(Src)); | 
|---|
| 103 | return sk_unaligned_load<Dst>(&src); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | #endif | 
|---|
| 107 |  | 
|---|