Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkFixed.h
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 // Now look for ASM overrides for our portable versions (should consider putting this in its own file)
87
88 #ifdef SkLONGLONG
89     inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b)
90     {
91         return (SkFixed)((int64_t)a * b >> 16);
92     }
93     #define SkFixedMul(a,b)     SkFixedMul_longlong(a,b)
94 #endif
95
96 #if defined(SK_CPU_ARM32)
97     /* This guy does not handle NaN or other obscurities, but is faster than
98        than (int)(x*65536).  When built on Android with -Os, needs forcing
99        to inline or we lose the speed benefit.
100     */
101     SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
102     {
103         int32_t y, z;
104         asm("movs    %1, %3, lsl #1         \n"
105             "mov     %2, #0x8E              \n"
106             "sub     %1, %2, %1, lsr #24    \n"
107             "mov     %2, %3, lsl #8         \n"
108             "orr     %2, %2, #0x80000000    \n"
109             "mov     %1, %2, lsr %1         \n"
110             "it cs                          \n"
111             "rsbcs   %1, %1, #0             \n"
112             : "=r"(x), "=&r"(y), "=&r"(z)
113             : "r"(x)
114             : "cc"
115             );
116         return y;
117     }
118     inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
119     {
120         int32_t t;
121         asm("smull  %0, %2, %1, %3          \n"
122             "mov    %0, %0, lsr #16         \n"
123             "orr    %0, %0, %2, lsl #16     \n"
124             : "=r"(x), "=&r"(y), "=r"(t)
125             : "r"(x), "1"(y)
126             :
127             );
128         return x;
129     }
130     #undef SkFixedMul
131     #define SkFixedMul(x, y)        SkFixedMul_arm(x, y)
132
133     #undef SkFloatToFixed
134     #define SkFloatToFixed(x)  SkFloatToFixed_arm(x)
135 #endif
136
137 #ifndef SkFixedMul
138     #define SkFixedMul(x, y)    SkFixedMul_portable(x, y)
139 #endif
140
141 ///////////////////////////////////////////////////////////////////////////////
142
143 typedef int64_t SkFixed48;
144
145 #define SkIntToFixed48(x)       ((SkFixed48)(x) << 48)
146 #define SkFixed48ToInt(x)       ((int)((x) >> 48))
147 #define SkFixedToFixed48(x)     ((SkFixed48)(x) << 32)
148 #define SkFixed48ToFixed(x)     ((SkFixed)((x) >> 32))
149 #define SkFloatToFixed48(x)     ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f)))
150
151 #define SkScalarToFixed48(x)    SkFloatToFixed48(x)
152
153 #endif