|
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) |
|
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 |
|
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 |
|
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) |
|
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 |
|
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_ARM) |
|
109 /* This guy does not handle NaN or other obscurities, but is faster than |
|
110 than (int)(x*65536) |
|
111 */ |
|
112 inline SkFixed SkFloatToFixed_arm(float x) |
|
113 { |
|
114 int32_t y, z; |
|
115 asm("movs %1, %3, lsl #1 \n" |
|
116 "mov %2, #0x8E \n" |
|
117 "sub %1, %2, %1, lsr #24 \n" |
|
118 "mov %2, %3, lsl #8 \n" |
|
119 "orr %2, %2, #0x80000000 \n" |
|
120 "mov %1, %2, lsr %1 \n" |
|
121 "it cs \n" |
|
122 "rsbcs %1, %1, #0 \n" |
|
123 : "=r"(x), "=&r"(y), "=&r"(z) |
|
124 : "r"(x) |
|
125 : "cc" |
|
126 ); |
|
127 return y; |
|
128 } |
|
129 inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y) |
|
130 { |
|
131 int32_t t; |
|
132 asm("smull %0, %2, %1, %3 \n" |
|
133 "mov %0, %0, lsr #16 \n" |
|
134 "orr %0, %0, %2, lsl #16 \n" |
|
135 : "=r"(x), "=&r"(y), "=r"(t) |
|
136 : "r"(x), "1"(y) |
|
137 : |
|
138 ); |
|
139 return x; |
|
140 } |
|
141 #undef SkFixedMul |
|
142 #define SkFixedMul(x, y) SkFixedMul_arm(x, y) |
|
143 |
|
144 #undef SkFloatToFixed |
|
145 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) |
|
146 #endif |
|
147 |
|
148 #ifndef SkFixedMul |
|
149 #define SkFixedMul(x, y) SkFixedMul_portable(x, y) |
|
150 #endif |
|
151 |
|
152 /////////////////////////////////////////////////////////////////////////////// |
|
153 |
|
154 typedef int64_t SkFixed48; |
|
155 |
|
156 #define SkIntToFixed48(x) ((SkFixed48)(x) << 48) |
|
157 #define SkFixed48ToInt(x) ((int)((x) >> 48)) |
|
158 #define SkFixedToFixed48(x) ((SkFixed48)(x) << 32) |
|
159 #define SkFixed48ToFixed(x) ((SkFixed)((x) >> 32)) |
|
160 #define SkFloatToFixed48(x) ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f))) |
|
161 |
|
162 #define SkScalarToFixed48(x) SkFloatToFixed48(x) |
|
163 |
|
164 #endif |