blob: 6361bdaab554ec6cb51016fcbd6403802a335d57 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkFixed_DEFINED
9#define SkFixed_DEFINED
10
benjaminwagner6c71e0a2016-04-07 08:49:31 -070011#include "SkScalar.h"
Mike Kleine9f78b42016-11-22 08:57:45 -050012#include "SkSafe_math.h"
benjaminwagner70f1a6c2016-04-07 09:23:11 -070013
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000014#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
16/** \file SkFixed.h
17
18 Types and macros for 16.16 fixed point
19*/
20
21/** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
22*/
23typedef int32_t SkFixed;
24#define SK_Fixed1 (1 << 16)
25#define SK_FixedHalf (1 << 15)
26#define SK_FixedMax (0x7FFFFFFF)
bsalomon@google.comee9aa302011-05-09 22:32:52 +000027#define SK_FixedMin (-SK_FixedMax)
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#define SK_FixedPI (0x3243F)
29#define SK_FixedSqrt2 (92682)
30#define SK_FixedTanPIOver8 (0x6A0A)
31#define SK_FixedRoot2Over2 (0xB505)
32
bungemanf4fddfc2015-04-17 10:05:43 -070033#define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
fmalitae7365062016-04-07 19:27:45 -070034#define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1))
benjaminwagner70f1a6c2016-04-07 09:23:11 -070035
fmalitae7365062016-04-07 19:27:45 -070036#ifdef SK_DEBUG
37 static inline SkFixed SkFloatToFixed_Check(float x) {
38 int64_t n64 = (int64_t)(x * SK_Fixed1);
39 SkFixed n32 = (SkFixed)n64;
40 SkASSERT(n64 == n32);
41 return n32;
42 }
43#else
44 #define SkFloatToFixed_Check(x) SkFloatToFixed(x)
45#endif
reed@google.comc9af5d82012-08-03 12:45:14 +000046
fmalitae7365062016-04-07 19:27:45 -070047#define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
48#define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
reed@android.com8a1c16f2008-12-17 15:59:43 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050/** Converts an integer to a SkFixed, asserting that the result does not overflow
51 a 32 bit signed integer
52*/
53#ifdef SK_DEBUG
54 inline SkFixed SkIntToFixed(int n)
55 {
56 SkASSERT(n >= -32768 && n <= 32767);
benjaminwagner0b2a1892015-11-11 08:46:34 -080057 // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
58 // shifting.
59 return (unsigned)n << 16;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 }
61#else
benjaminwagner0b2a1892015-11-11 08:46:34 -080062 // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
63 // shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
64 // debug version).
65 #define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
reed@android.com8a1c16f2008-12-17 15:59:43 +000066#endif
67
reed@google.com1b202802011-08-01 20:49:45 +000068#define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
69#define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
70#define SkFixedFloorToInt(x) ((x) >> 16)
71
liyuqian3f490cc2016-10-20 11:23:09 -070072static inline SkFixed SkFixedRoundToFixed(SkFixed x) {
73 return (x + SK_FixedHalf) & 0xFFFF0000;
74}
75static inline SkFixed SkFixedCeilToFixed(SkFixed x) {
76 return (x + SK_Fixed1 - 1) & 0xFFFF0000;
77}
78static inline SkFixed SkFixedFloorToFixed(SkFixed x) {
79 return x & 0xFFFF0000;
80}
reed@google.com1b202802011-08-01 20:49:45 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082#define SkFixedAbs(x) SkAbs32(x)
83#define SkFixedAve(a, b) (((a) + (b)) >> 1)
84
reed15411302016-04-27 18:45:36 -070085// The divide may exceed 32 bits. Clamp to a signed 32 bit result.
86#define SkFixedDiv(numer, denom) \
liyuqian0d2c2342016-07-13 13:34:46 -070087 SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)(numer), 16) / (denom)), SK_MinS32, SK_MaxS32))
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
fmalitae7365062016-04-07 19:27:45 -070089//////////////////////////////////////////////////////////////////////////////////////////////////////
90// Now look for ASM overrides for our portable versions (should consider putting this in its own file)
91
92inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) {
93 return (SkFixed)((int64_t)a * b >> 16);
94}
95#define SkFixedMul(a,b) SkFixedMul_longlong(a,b)
96
97
98#if defined(SK_CPU_ARM32)
99 /* This guy does not handle NaN or other obscurities, but is faster than
100 than (int)(x*65536). When built on Android with -Os, needs forcing
101 to inline or we lose the speed benefit.
102 */
103 SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
104 {
Amaury Le Leyzourac0e7052017-03-07 16:58:08 -0800105 int32_t y;
106 asm("vcvt.s32.f32 %0, %0, #16": "+w"(x));
107 memcpy(&y, &x, sizeof(y));
fmalitae7365062016-04-07 19:27:45 -0700108 return y;
109 }
110 inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
111 {
112 int32_t t;
113 asm("smull %0, %2, %1, %3 \n"
114 "mov %0, %0, lsr #16 \n"
115 "orr %0, %0, %2, lsl #16 \n"
116 : "=r"(x), "=&r"(y), "=r"(t)
117 : "r"(x), "1"(y)
118 :
119 );
120 return x;
121 }
122 #undef SkFixedMul
123 #define SkFixedMul(x, y) SkFixedMul_arm(x, y)
124
125 #undef SkFloatToFixed
126 #define SkFloatToFixed(x) SkFloatToFixed_arm(x)
127#endif
128
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000129///////////////////////////////////////////////////////////////////////////////
130
benjaminwagner6c71e0a2016-04-07 08:49:31 -0700131#define SkFixedToScalar(x) SkFixedToFloat(x)
132#define SkScalarToFixed(x) SkFloatToFixed(x)
133
benjaminwagner6c71e0a2016-04-07 08:49:31 -0700134///////////////////////////////////////////////////////////////////////////////
135
mtklein17946512014-11-21 12:10:33 -0800136typedef int64_t SkFixed3232; // 32.32
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000137
Florin Malita63b61562017-02-10 10:42:49 -0500138#define SkFixed3232Max (0x7FFFFFFFFFFFFFFFLL)
139#define SkFixed3232Min (-SkFixed3232Max)
140
fmalitae7365062016-04-07 19:27:45 -0700141#define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32))
142#define SkFixed3232ToInt(x) ((int)((x) >> 32))
143#define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16))
144#define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16))
145#define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f)))
Florin Malita63b61562017-02-10 10:42:49 -0500146#define SkFixed3232ToFloat(x) (x * (1 / (65536.0f * 65536.0f)))
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000147
mtklein17946512014-11-21 12:10:33 -0800148#define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#endif