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 "SkTypes.h"
12
13 /** \file SkFixed.h
14
15 Types and macros for 16.16 fixed point
16 */
17
18 /** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
19 */
20 typedef int32_t SkFixed;
21 #define SK_Fixed1 (1 << 16)
22 #define SK_FixedHalf (1 << 15)
23 #define SK_FixedMax (0x7FFFFFFF)
24 #define SK_FixedMin (-SK_FixedMax)
25 #define SK_FixedNaN ((int) 0x80000000)
26 #define SK_FixedPI (0x3243F)
27 #define SK_FixedSqrt2 (92682)
28 #define SK_FixedTanPIOver8 (0x6A0A)
29 #define SK_FixedRoot2Over2 (0xB505)
30
31 #define SkFixedToFloat(x) ((x) * 1.5258789e-5f)
32 #if 1
33 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1))
34 #else
35 // pins over/under flows to max/min int32 (slower than just a cast)
SkFloatToFixed(float x)36 static inline SkFixed SkFloatToFixed(float x) {
37 int64_t n = x * SK_Fixed1;
38 return (SkFixed)n;
39 }
40 #endif
41
42 #ifdef SK_DEBUG
SkFloatToFixed_Check(float x)43 static inline SkFixed SkFloatToFixed_Check(float x) {
44 int64_t n64 = (int64_t)(x * SK_Fixed1);
45 SkFixed n32 = (SkFixed)n64;
46 SkASSERT(n64 == n32);
47 return n32;
48 }
49 #else
50 #define SkFloatToFixed_Check(x) SkFloatToFixed(x)
51 #endif
52
53 #define SkFixedToDouble(x) ((x) * 1.5258789e-5)
54 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
55
56 /** Converts an integer to a SkFixed, asserting that the result does not overflow
57 a 32 bit signed integer
58 */
59 #ifdef SK_DEBUG
SkIntToFixed(int n)60 inline SkFixed SkIntToFixed(int n)
61 {
62 SkASSERT(n >= -32768 && n <= 32767);
63 return n << 16;
64 }
65 #else
66 // force the cast to SkFixed to ensure that the answer is signed (like the debug version)
67 #define SkIntToFixed(n) (SkFixed)((n) << 16)
68 #endif
69
70 #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
71 #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
72 #define SkFixedFloorToInt(x) ((x) >> 16)
73
74 #define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000)
75 #define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000)
76 #define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000)
77
78 #define SkFixedAbs(x) SkAbs32(x)
79 #define SkFixedAve(a, b) (((a) + (b)) >> 1)
80
81 SkFixed SkFixedMul_portable(SkFixed, SkFixed);
82
83 #define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16)
84
85 ///////////////////////////////////////////////////////////////////////////////
86 // TODO: move fixed sin/cos into SkCosineMapper, as that is the only caller
87 // or rewrite SkCosineMapper to not use it at all
88
89 SkFixed SkFixedSinCos(SkFixed radians, SkFixed* cosValueOrNull);
90 #define SkFixedSin(radians) SkFixedSinCos(radians, NULL)
SkFixedCos(SkFixed radians)91 static inline SkFixed SkFixedCos(SkFixed radians) {
92 SkFixed cosValue;
93 (void)SkFixedSinCos(radians, &cosValue);
94 return cosValue;
95 }
96
97 //////////////////////////////////////////////////////////////////////////////////////////////////////
98 // Now look for ASM overrides for our portable versions (should consider putting this in its own file)
99
100 #ifdef SkLONGLONG
SkFixedMul_longlong(SkFixed a,SkFixed b)101 inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b)
102 {
103 return (SkFixed)((int64_t)a * b >> 16);
104 }
105 #define SkFixedMul(a,b) SkFixedMul_longlong(a,b)
106 #endif
107
108 #if defined(SK_CPU_ARM32)
109 /* This guy does not handle NaN or other obscurities, but is faster than
110 than (int)(x*65536). When built on Android with -Os, needs forcing
111 to inline or we lose the speed benefit.
112 */
SkFloatToFixed_arm(float x)113 SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
114 {
115 int32_t y, z;
116 asm("movs %1, %3, lsl #1 \n"
117 "mov %2, #0x8E \n"
118 "sub %1, %2, %1, lsr #24 \n"
119 "mov %2, %3, lsl #8 \n"
120 "orr %2, %2, #0x80000000 \n"
121 "mov %1, %2, lsr %1 \n"
122 "it cs \n"
123 "rsbcs %1, %1, #0 \n"
124 : "=r"(x), "=&r"(y), "=&r"(z)
125 : "r"(x)
126 : "cc"
127 );
128 return y;
129 }
SkFixedMul_arm(SkFixed x,SkFixed y)130 inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
131 {
132 int32_t t;
133 asm("smull %0, %2, %1, %3 \n"
134 "mov %0, %0, lsr #16 \n"
135 "orr %0, %0, %2, lsl #16 \n"
136 : "=r"(x), "=&r"(y), "=r"(t)
137 : "r"(x), "1"(y)
138 :
139 );
140 return x;
141 }
142 #undef SkFixedMul
143 #define SkFixedMul(x, y) SkFixedMul_arm(x, y)
144
145 #undef SkFloatToFixed
146 #define SkFloatToFixed(x) SkFloatToFixed_arm(x)
147 #endif
148
149 #ifndef SkFixedMul
150 #define SkFixedMul(x, y) SkFixedMul_portable(x, y)
151 #endif
152
153 ///////////////////////////////////////////////////////////////////////////////
154
155 typedef int64_t SkFixed48;
156
157 #define SkIntToFixed48(x) ((SkFixed48)(x) << 48)
158 #define SkFixed48ToInt(x) ((int)((x) >> 48))
159 #define SkFixedToFixed48(x) ((SkFixed48)(x) << 32)
160 #define SkFixed48ToFixed(x) ((SkFixed)((x) >> 32))
161 #define SkFloatToFixed48(x) ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f)))
162
163 #define SkScalarToFixed48(x) SkFloatToFixed48(x)
164
165 #endif
166