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