2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkFixed_DEFINED
|
|
|
|
#define SkFixed_DEFINED
|
|
|
|
|
2011-09-27 17:38:17 +00:00
|
|
|
#include "SkTypes.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** \file SkFixed.h
|
|
|
|
|
|
|
|
Types and macros for 16.16 fixed point
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
|
|
|
|
*/
|
|
|
|
typedef int32_t SkFixed;
|
|
|
|
#define SK_Fixed1 (1 << 16)
|
|
|
|
#define SK_FixedHalf (1 << 15)
|
|
|
|
#define SK_FixedMax (0x7FFFFFFF)
|
2011-05-09 22:32:52 +00:00
|
|
|
#define SK_FixedMin (-SK_FixedMax)
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SK_FixedNaN ((int) 0x80000000)
|
|
|
|
#define SK_FixedPI (0x3243F)
|
|
|
|
#define SK_FixedSqrt2 (92682)
|
|
|
|
#define SK_FixedTanPIOver8 (0x6A0A)
|
|
|
|
#define SK_FixedRoot2Over2 (0xB505)
|
|
|
|
|
2012-06-11 21:21:26 +00:00
|
|
|
#define SkFixedToFloat(x) ((x) * 1.5258789e-5f)
|
2011-01-11 18:32:13 +00:00
|
|
|
#if 1
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1))
|
2011-01-11 18:32:13 +00:00
|
|
|
#else
|
|
|
|
// pins over/under flows to max/min int32 (slower than just a cast)
|
|
|
|
static inline SkFixed SkFloatToFixed(float x) {
|
|
|
|
int64_t n = x * SK_Fixed1;
|
|
|
|
return (SkFixed)n;
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-03 12:45:14 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
static inline SkFixed SkFloatToFixed_Check(float x) {
|
2012-08-03 13:55:37 +00:00
|
|
|
int64_t n64 = (int64_t)(x * SK_Fixed1);
|
2012-08-03 12:45:14 +00:00
|
|
|
SkFixed n32 = (SkFixed)n64;
|
|
|
|
SkASSERT(n64 == n32);
|
|
|
|
return n32;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define SkFloatToFixed_Check(x) SkFloatToFixed(x)
|
|
|
|
#endif
|
|
|
|
|
2012-06-11 21:21:26 +00:00
|
|
|
#define SkFixedToDouble(x) ((x) * 1.5258789e-5)
|
|
|
|
#define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** 32 bit signed integer used to represent fractions values with 30 bits to the right of the decimal point
|
|
|
|
*/
|
|
|
|
typedef int32_t SkFract;
|
|
|
|
#define SK_Fract1 (1 << 30)
|
|
|
|
#define Sk_FracHalf (1 << 29)
|
|
|
|
#define SK_FractPIOver180 (0x11DF46A)
|
|
|
|
|
2012-06-11 21:21:26 +00:00
|
|
|
#define SkFractToFloat(x) ((float)(x) * 0.00000000093132257f)
|
|
|
|
#define SkFloatToFract(x) ((SkFract)((x) * SK_Fract1))
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Converts an integer to a SkFixed, asserting that the result does not overflow
|
|
|
|
a 32 bit signed integer
|
|
|
|
*/
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
inline SkFixed SkIntToFixed(int n)
|
|
|
|
{
|
|
|
|
SkASSERT(n >= -32768 && n <= 32767);
|
|
|
|
return n << 16;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// force the cast to SkFixed to ensure that the answer is signed (like the debug version)
|
|
|
|
#define SkIntToFixed(n) (SkFixed)((n) << 16)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Converts a SkFixed to a SkFract, asserting that the result does not overflow
|
|
|
|
a 32 bit signed integer
|
|
|
|
*/
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
inline SkFract SkFixedToFract(SkFixed x)
|
|
|
|
{
|
|
|
|
SkASSERT(x >= (-2 << 16) && x <= (2 << 16) - 1);
|
|
|
|
return x << 14;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define SkFixedToFract(x) ((x) << 14)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Returns the signed fraction of a SkFixed
|
|
|
|
*/
|
|
|
|
inline SkFixed SkFixedFraction(SkFixed x)
|
|
|
|
{
|
|
|
|
SkFixed mask = x >> 31 << 16;
|
|
|
|
return (x & 0xFFFF) | mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Converts a SkFract to a SkFixed
|
|
|
|
*/
|
|
|
|
#define SkFractToFixed(x) ((x) >> 14)
|
2011-08-01 20:49:45 +00:00
|
|
|
|
|
|
|
#define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
|
|
|
|
#define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
|
|
|
|
#define SkFixedFloorToInt(x) ((x) >> 16)
|
|
|
|
|
|
|
|
#define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000)
|
|
|
|
#define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000)
|
|
|
|
#define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000)
|
|
|
|
|
|
|
|
// DEPRECATED
|
|
|
|
#define SkFixedFloor(x) SkFixedFloorToInt(x)
|
|
|
|
#define SkFixedCeil(x) SkFixedCeilToInt(x)
|
|
|
|
#define SkFixedRound(x) SkFixedRoundToInt(x)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkFixedAbs(x) SkAbs32(x)
|
|
|
|
#define SkFixedAve(a, b) (((a) + (b)) >> 1)
|
|
|
|
|
2013-08-15 13:43:23 +00:00
|
|
|
SkFixed SkFixedMul_portable(SkFixed, SkFixed);
|
|
|
|
SkFract SkFractMul_portable(SkFract, SkFract);
|
|
|
|
inline SkFixed SkFixedSquare_portable(SkFixed value)
|
|
|
|
{
|
|
|
|
uint32_t a = SkAbs32(value);
|
|
|
|
uint32_t ah = a >> 16;
|
|
|
|
uint32_t al = a & 0xFFFF;
|
|
|
|
SkFixed result = ah * a + al * ah + (al * al >> 16);
|
|
|
|
if (result >= 0)
|
|
|
|
return result;
|
|
|
|
else // Overflow.
|
|
|
|
return SK_FixedMax;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16)
|
|
|
|
SkFixed SkFixedDivInt(int32_t numer, int32_t denom);
|
|
|
|
SkFixed SkFixedMod(SkFixed numer, SkFixed denom);
|
|
|
|
#define SkFixedInvert(n) SkDivBits(SK_Fixed1, n, 16)
|
|
|
|
SkFixed SkFixedFastInvert(SkFixed n);
|
|
|
|
#define SkFixedSqrt(n) SkSqrtBits(n, 23)
|
|
|
|
SkFixed SkFixedMean(SkFixed a, SkFixed b); //*< returns sqrt(x*y)
|
|
|
|
int SkFixedMulCommon(SkFixed, int , int bias); // internal used by SkFixedMulFloor, SkFixedMulCeil, SkFixedMulRound
|
|
|
|
|
|
|
|
#define SkFractDiv(numer, denom) SkDivBits(numer, denom, 30)
|
|
|
|
#define SkFractSqrt(n) SkSqrtBits(n, 30)
|
|
|
|
|
|
|
|
SkFixed SkFixedSinCos(SkFixed radians, SkFixed* cosValueOrNull);
|
|
|
|
#define SkFixedSin(radians) SkFixedSinCos(radians, NULL)
|
|
|
|
inline SkFixed SkFixedCos(SkFixed radians)
|
|
|
|
{
|
|
|
|
SkFixed cosValue;
|
|
|
|
(void)SkFixedSinCos(radians, &cosValue);
|
|
|
|
return cosValue;
|
|
|
|
}
|
|
|
|
SkFixed SkFixedTan(SkFixed radians);
|
|
|
|
SkFixed SkFixedASin(SkFixed);
|
|
|
|
SkFixed SkFixedACos(SkFixed);
|
|
|
|
SkFixed SkFixedATan2(SkFixed y, SkFixed x);
|
|
|
|
SkFixed SkFixedExp(SkFixed);
|
|
|
|
SkFixed SkFixedLog(SkFixed);
|
|
|
|
|
|
|
|
#define SK_FixedNearlyZero (SK_Fixed1 >> 12)
|
|
|
|
|
|
|
|
inline bool SkFixedNearlyZero(SkFixed x, SkFixed tolerance = SK_FixedNearlyZero)
|
|
|
|
{
|
|
|
|
SkASSERT(tolerance > 0);
|
|
|
|
return SkAbs32(x) < tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Now look for ASM overrides for our portable versions (should consider putting this in its own file)
|
|
|
|
|
2013-08-15 13:43:23 +00:00
|
|
|
#ifdef SkLONGLONG
|
|
|
|
inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b)
|
|
|
|
{
|
|
|
|
return (SkFixed)((SkLONGLONG)a * b >> 16);
|
|
|
|
}
|
|
|
|
inline SkFract SkFractMul_longlong(SkFract a, SkFract b)
|
|
|
|
{
|
|
|
|
return (SkFract)((SkLONGLONG)a * b >> 30);
|
|
|
|
}
|
|
|
|
inline SkFixed SkFixedSquare_longlong(SkFixed value)
|
|
|
|
{
|
|
|
|
return (SkFixed)((SkLONGLONG)value * value >> 16);
|
|
|
|
}
|
|
|
|
#define SkFixedMul(a,b) SkFixedMul_longlong(a,b)
|
|
|
|
#define SkFractMul(a,b) SkFractMul_longlong(a,b)
|
|
|
|
#define SkFixedSquare(a) SkFixedSquare_longlong(a)
|
|
|
|
#endif
|
|
|
|
|
2013-07-29 19:30:00 +00:00
|
|
|
#if defined(SK_CPU_ARM)
|
2008-12-17 15:59:43 +00:00
|
|
|
/* This guy does not handle NaN or other obscurities, but is faster than
|
2013-07-18 19:11:30 +00:00
|
|
|
than (int)(x*65536)
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
inline SkFixed SkFloatToFixed_arm(float x)
|
|
|
|
{
|
2013-12-16 23:51:11 +00:00
|
|
|
int32_t y, z;
|
2008-12-17 15:59:43 +00:00
|
|
|
asm("movs %1, %3, lsl #1 \n"
|
|
|
|
"mov %2, #0x8E \n"
|
|
|
|
"sub %1, %2, %1, lsr #24 \n"
|
|
|
|
"mov %2, %3, lsl #8 \n"
|
|
|
|
"orr %2, %2, #0x80000000 \n"
|
|
|
|
"mov %1, %2, lsr %1 \n"
|
2013-07-29 19:30:00 +00:00
|
|
|
"it cs \n"
|
2008-12-17 15:59:43 +00:00
|
|
|
"rsbcs %1, %1, #0 \n"
|
|
|
|
: "=r"(x), "=&r"(y), "=&r"(z)
|
|
|
|
: "r"(x)
|
|
|
|
: "cc"
|
|
|
|
);
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
|
|
|
|
{
|
2013-12-16 23:51:11 +00:00
|
|
|
int32_t t;
|
2008-12-17 15:59:43 +00:00
|
|
|
asm("smull %0, %2, %1, %3 \n"
|
|
|
|
"mov %0, %0, lsr #16 \n"
|
|
|
|
"orr %0, %0, %2, lsl #16 \n"
|
|
|
|
: "=r"(x), "=&r"(y), "=r"(t)
|
|
|
|
: "r"(x), "1"(y)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
inline SkFixed SkFixedMulAdd_arm(SkFixed x, SkFixed y, SkFixed a)
|
|
|
|
{
|
2013-12-16 23:51:11 +00:00
|
|
|
int32_t t;
|
2008-12-17 15:59:43 +00:00
|
|
|
asm("smull %0, %3, %1, %4 \n"
|
|
|
|
"add %0, %2, %0, lsr #16 \n"
|
|
|
|
"add %0, %0, %3, lsl #16 \n"
|
|
|
|
: "=r"(x), "=&r"(y), "=&r"(a), "=r"(t)
|
|
|
|
: "%r"(x), "1"(y), "2"(a)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
inline SkFixed SkFractMul_arm(SkFixed x, SkFixed y)
|
|
|
|
{
|
2013-12-16 23:51:11 +00:00
|
|
|
int32_t t;
|
2008-12-17 15:59:43 +00:00
|
|
|
asm("smull %0, %2, %1, %3 \n"
|
|
|
|
"mov %0, %0, lsr #30 \n"
|
|
|
|
"orr %0, %0, %2, lsl #2 \n"
|
|
|
|
: "=r"(x), "=&r"(y), "=r"(t)
|
|
|
|
: "r"(x), "1"(y)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#undef SkFixedMul
|
|
|
|
#undef SkFractMul
|
|
|
|
#define SkFixedMul(x, y) SkFixedMul_arm(x, y)
|
|
|
|
#define SkFractMul(x, y) SkFractMul_arm(x, y)
|
|
|
|
#define SkFixedMulAdd(x, y, a) SkFixedMulAdd_arm(x, y, a)
|
|
|
|
|
|
|
|
#undef SkFloatToFixed
|
|
|
|
#define SkFloatToFixed(x) SkFloatToFixed_arm(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/////////////////////// Now define our macros to the portable versions if they weren't overridden
|
|
|
|
|
|
|
|
#ifndef SkFixedSquare
|
|
|
|
#define SkFixedSquare(x) SkFixedSquare_portable(x)
|
|
|
|
#endif
|
2013-08-15 13:43:23 +00:00
|
|
|
#ifndef SkFixedMul
|
|
|
|
#define SkFixedMul(x, y) SkFixedMul_portable(x, y)
|
|
|
|
#endif
|
|
|
|
#ifndef SkFractMul
|
|
|
|
#define SkFractMul(x, y) SkFractMul_portable(x, y)
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkFixedMulAdd
|
|
|
|
#define SkFixedMulAdd(x, y, a) (SkFixedMul(x, y) + (a))
|
|
|
|
#endif
|
|
|
|
|
2012-03-07 21:47:41 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef int64_t SkFixed48;
|
|
|
|
|
|
|
|
#define SkIntToFixed48(x) ((SkFixed48)(x) << 48)
|
|
|
|
#define SkFixed48ToInt(x) ((int)((x) >> 48))
|
|
|
|
#define SkFixedToFixed48(x) ((SkFixed48)(x) << 32)
|
|
|
|
#define SkFixed48ToFixed(x) ((SkFixed)((x) >> 32))
|
|
|
|
#define SkFloatToFixed48(x) ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f)))
|
|
|
|
|
2013-12-17 16:44:46 +00:00
|
|
|
#define SkScalarToFixed48(x) SkFloatToFixed48(x)
|
2012-03-07 21:47:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|