xref: /aosp_15_r20/external/skia/src/base/SkUtils.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/private/base/SkAttributes.h"
12 
13 #include <cstring>
14 #include <type_traits> // is_trivially_copyable
15 
16 namespace SkHexadecimalDigits {
17     extern const char gUpper[16];  // 0-9A-F
18     extern const char gLower[16];  // 0-9a-f
19 }  // namespace SkHexadecimalDigits
20 
21 ///////////////////////////////////////////////////////////////////////////////
22 
23 // If T is an 8-byte GCC or Clang vector extension type, it would naturally pass or return in the
24 // MMX mm0 register on 32-bit x86 builds.  This has the fun side effect of clobbering any state in
25 // the x87 st0 register.  (There is no ABI governing who should preserve mm?/st? registers, so no
26 // one does!)
27 //
28 // We force-inline sk_unaligned_load() and sk_unaligned_store() to avoid that, making them safe to
29 // use for all types on all platforms, thus solving the problem once and for all!
30 
31 // A separate problem exists with 32-bit x86. The default calling convention returns values in
32 // ST0 (the x87 FPU). Unfortunately, doing so can mutate some bit patterns (signaling NaNs
33 // become quiet). If you're using these functions to pass data around as floats, but it's actually
34 // integers, that can be bad -- raster pipeline does this.
35 //
36 // With GCC and Clang, the always_inline attribute ensures we don't have a problem. MSVC, though,
37 // ignores __forceinline in debug builds, so the return-via-ST0 is always present. Switching to
38 // __vectorcall changes the functions to return in xmm0.
39 #if defined(_MSC_VER) && defined(_M_IX86)
40     #define SK_FP_SAFE_ABI __vectorcall
41 #else
42     #define SK_FP_SAFE_ABI
43 #endif
44 
45 template <typename T, typename P>
sk_unaligned_load(const P * ptr)46 static SK_ALWAYS_INLINE T SK_FP_SAFE_ABI sk_unaligned_load(const P* ptr) {
47     static_assert(std::is_trivially_copyable_v<P> || std::is_void_v<P>);
48     static_assert(std::is_trivially_copyable_v<T>);
49     T val;
50     // gcc's class-memaccess sometimes triggers when:
51     // - `T` is trivially copyable but
52     // - `T` is non-trivial (e.g. at least one eligible default constructor is
53     //    non-trivial).
54     // Use `reinterpret_cast<const void*>` to explicit suppress this warning; a
55     // trivially copyable type is safe to memcpy from/to.
56     memcpy(&val, static_cast<const void*>(ptr), sizeof(val));
57     return val;
58 }
59 
60 template <typename T, typename P>
sk_unaligned_store(P * ptr,T val)61 static SK_ALWAYS_INLINE void SK_FP_SAFE_ABI sk_unaligned_store(P* ptr, T val) {
62     static_assert(std::is_trivially_copyable<T>::value);
63     memcpy(ptr, &val, sizeof(val));
64 }
65 
66 // Copy the bytes from src into an instance of type Dst and return it.
67 template <typename Dst, typename Src>
sk_bit_cast(const Src & src)68 static SK_ALWAYS_INLINE Dst SK_FP_SAFE_ABI sk_bit_cast(const Src& src) {
69     static_assert(sizeof(Dst) == sizeof(Src));
70     static_assert(std::is_trivially_copyable<Dst>::value);
71     static_assert(std::is_trivially_copyable<Src>::value);
72     return sk_unaligned_load<Dst>(&src);
73 }
74 
75 #undef SK_FP_SAFE_ABI
76 
77 #endif
78