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 SkFixed_DEFINED
9 #define SkFixed_DEFINED
10
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/base/SkDebug.h"
13 #include "include/private/base/SkMath.h" // IWYU pragma: keep
14 #include "include/private/base/SkTPin.h" // IWYU pragma: keep
15
16 #include <cstdint>
17
18 /** \file SkFixed.h
19
20 Types and macros for 16.16 fixed point
21 */
22
23 /** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
24 */
25 typedef int32_t SkFixed;
26 #define SK_Fixed1 (1 << 16)
27 #define SK_FixedHalf (1 << 15)
28 #define SK_FixedQuarter (1 << 14)
29 #define SK_FixedMax (0x7FFFFFFF)
30 #define SK_FixedMin (-SK_FixedMax)
31 #define SK_FixedPI (0x3243F)
32 #define SK_FixedSqrt2 (92682)
33 #define SK_FixedTanPIOver8 (0x6A0A)
34 #define SK_FixedRoot2Over2 (0xB505)
35
36 // NOTE: SkFixedToFloat is exact. SkFloatToFixed seems to lack a rounding step. For all fixed-point
37 // values, this version is as accurate as possible for (fixed -> float -> fixed). Rounding reduces
38 // accuracy if the intermediate floats are in the range that only holds integers (adding 0.5f to an
39 // odd integer then snaps to nearest even). Using double for the rounding math gives maximum
40 // accuracy for (float -> fixed -> float), but that's usually overkill.
41 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
42 #define SkFloatToFixed(x) sk_float_saturate2int((x) * SK_Fixed1)
43
44 #ifdef SK_DEBUG
SkFloatToFixed_Check(float x)45 static inline SkFixed SkFloatToFixed_Check(float x) {
46 int64_t n64 = (int64_t)(x * SK_Fixed1);
47 SkFixed n32 = (SkFixed)n64;
48 SkASSERT(n64 == n32);
49 return n32;
50 }
51 #else
52 #define SkFloatToFixed_Check(x) SkFloatToFixed(x)
53 #endif
54
55 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
56 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
57
58 /** Converts an integer to a SkFixed, asserting that the result does not overflow
59 a 32 bit signed integer
60 */
61 #ifdef SK_DEBUG
SkIntToFixed(int n)62 inline SkFixed SkIntToFixed(int n)
63 {
64 SkASSERT(n >= -32768 && n <= 32767);
65 // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
66 // shifting.
67 return (SkFixed)( (unsigned)n << 16 );
68 }
69 #else
70 // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
71 // shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
72 // debug version).
73 #define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
74 #endif
75
76 #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
77 #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
78 #define SkFixedFloorToInt(x) ((x) >> 16)
79
SkFixedRoundToFixed(SkFixed x)80 static inline SkFixed SkFixedRoundToFixed(SkFixed x) {
81 return (SkFixed)( (uint32_t)(x + SK_FixedHalf) & 0xFFFF0000 );
82 }
SkFixedCeilToFixed(SkFixed x)83 static inline SkFixed SkFixedCeilToFixed(SkFixed x) {
84 return (SkFixed)( (uint32_t)(x + SK_Fixed1 - 1) & 0xFFFF0000 );
85 }
SkFixedFloorToFixed(SkFixed x)86 static inline SkFixed SkFixedFloorToFixed(SkFixed x) {
87 return (SkFixed)( (uint32_t)x & 0xFFFF0000 );
88 }
89
90 #define SkFixedAve(a, b) (((a) + (b)) >> 1)
91
92 // The divide may exceed 32 bits. Clamp to a signed 32 bit result.
93 #define SkFixedDiv(numer, denom) \
94 SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)(numer), 16) / (denom)), SK_MinS32, SK_MaxS32))
95
SkFixedMul(SkFixed a,SkFixed b)96 static inline SkFixed SkFixedMul(SkFixed a, SkFixed b) {
97 return (SkFixed)((int64_t)a * b >> 16);
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////
101 // Platform-specific alternatives to our portable versions.
102
103 // The VCVT float-to-fixed instruction is part of the VFPv3 instruction set.
104 #if defined(__ARM_VFPV3__)
105 #include <cstring>
106
107 /* This does not handle NaN or other obscurities, but is faster than
108 than (int)(x*65536). When built on Android with -Os, needs forcing
109 to inline or we lose the speed benefit.
110 */
SkFloatToFixed_arm(float x)111 SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
112 {
113 int32_t y;
114 asm("vcvt.s32.f32 %0, %0, #16": "+w"(x));
115 std::memcpy(&y, &x, sizeof(y));
116 return y;
117 }
118 #undef SkFloatToFixed
119 #define SkFloatToFixed(x) SkFloatToFixed_arm(x)
120 #endif
121
122 ///////////////////////////////////////////////////////////////////////////////
123
124 #define SkFixedToScalar(x) SkFixedToFloat(x)
125 #define SkScalarToFixed(x) SkFloatToFixed(x)
126
127 ///////////////////////////////////////////////////////////////////////////////
128
129 typedef int64_t SkFixed3232; // 32.32
130
131 #define SkFixed3232Max SK_MaxS64
132 #define SkFixed3232Min (-SkFixed3232Max)
133
134 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32))
135 #define SkFixed3232ToInt(x) ((int)((x) >> 32))
136 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16))
137 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16))
138 #define SkFloatToFixed3232(x) sk_float_saturate2int64((x) * (65536.0f * 65536.0f))
139 #define SkFixed3232ToFloat(x) (x * (1 / (65536.0f * 65536.0f)))
140
141 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
142
143 #endif
144