michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkFixed_DEFINED michael@0: #define SkFixed_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: /** \file SkFixed.h michael@0: michael@0: Types and macros for 16.16 fixed point michael@0: */ michael@0: michael@0: /** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point michael@0: */ michael@0: typedef int32_t SkFixed; michael@0: #define SK_Fixed1 (1 << 16) michael@0: #define SK_FixedHalf (1 << 15) michael@0: #define SK_FixedMax (0x7FFFFFFF) michael@0: #define SK_FixedMin (-SK_FixedMax) michael@0: #define SK_FixedNaN ((int) 0x80000000) michael@0: #define SK_FixedPI (0x3243F) michael@0: #define SK_FixedSqrt2 (92682) michael@0: #define SK_FixedTanPIOver8 (0x6A0A) michael@0: #define SK_FixedRoot2Over2 (0xB505) michael@0: michael@0: #define SkFixedToFloat(x) ((x) * 1.5258789e-5f) michael@0: #if 1 michael@0: #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) michael@0: #else michael@0: // pins over/under flows to max/min int32 (slower than just a cast) michael@0: static inline SkFixed SkFloatToFixed(float x) { michael@0: int64_t n = x * SK_Fixed1; michael@0: return (SkFixed)n; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef SK_DEBUG michael@0: static inline SkFixed SkFloatToFixed_Check(float x) { michael@0: int64_t n64 = (int64_t)(x * SK_Fixed1); michael@0: SkFixed n32 = (SkFixed)n64; michael@0: SkASSERT(n64 == n32); michael@0: return n32; michael@0: } michael@0: #else michael@0: #define SkFloatToFixed_Check(x) SkFloatToFixed(x) michael@0: #endif michael@0: michael@0: #define SkFixedToDouble(x) ((x) * 1.5258789e-5) michael@0: #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) michael@0: michael@0: /** Converts an integer to a SkFixed, asserting that the result does not overflow michael@0: a 32 bit signed integer michael@0: */ michael@0: #ifdef SK_DEBUG michael@0: inline SkFixed SkIntToFixed(int n) michael@0: { michael@0: SkASSERT(n >= -32768 && n <= 32767); michael@0: return n << 16; michael@0: } michael@0: #else michael@0: // force the cast to SkFixed to ensure that the answer is signed (like the debug version) michael@0: #define SkIntToFixed(n) (SkFixed)((n) << 16) michael@0: #endif michael@0: michael@0: #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16) michael@0: #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16) michael@0: #define SkFixedFloorToInt(x) ((x) >> 16) michael@0: michael@0: #define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000) michael@0: #define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000) michael@0: #define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000) michael@0: michael@0: #define SkFixedAbs(x) SkAbs32(x) michael@0: #define SkFixedAve(a, b) (((a) + (b)) >> 1) michael@0: michael@0: SkFixed SkFixedMul_portable(SkFixed, SkFixed); michael@0: michael@0: #define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16) michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // TODO: move fixed sin/cos into SkCosineMapper, as that is the only caller michael@0: // or rewrite SkCosineMapper to not use it at all michael@0: michael@0: SkFixed SkFixedSinCos(SkFixed radians, SkFixed* cosValueOrNull); michael@0: #define SkFixedSin(radians) SkFixedSinCos(radians, NULL) michael@0: static inline SkFixed SkFixedCos(SkFixed radians) { michael@0: SkFixed cosValue; michael@0: (void)SkFixedSinCos(radians, &cosValue); michael@0: return cosValue; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////////////////////////////// michael@0: // Now look for ASM overrides for our portable versions (should consider putting this in its own file) michael@0: michael@0: #ifdef SkLONGLONG michael@0: inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) michael@0: { michael@0: return (SkFixed)((int64_t)a * b >> 16); michael@0: } michael@0: #define SkFixedMul(a,b) SkFixedMul_longlong(a,b) michael@0: #endif michael@0: michael@0: #if defined(SK_CPU_ARM) michael@0: /* This guy does not handle NaN or other obscurities, but is faster than michael@0: than (int)(x*65536) michael@0: */ michael@0: inline SkFixed SkFloatToFixed_arm(float x) michael@0: { michael@0: int32_t y, z; michael@0: asm("movs %1, %3, lsl #1 \n" michael@0: "mov %2, #0x8E \n" michael@0: "sub %1, %2, %1, lsr #24 \n" michael@0: "mov %2, %3, lsl #8 \n" michael@0: "orr %2, %2, #0x80000000 \n" michael@0: "mov %1, %2, lsr %1 \n" michael@0: "it cs \n" michael@0: "rsbcs %1, %1, #0 \n" michael@0: : "=r"(x), "=&r"(y), "=&r"(z) michael@0: : "r"(x) michael@0: : "cc" michael@0: ); michael@0: return y; michael@0: } michael@0: inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y) michael@0: { michael@0: int32_t t; michael@0: asm("smull %0, %2, %1, %3 \n" michael@0: "mov %0, %0, lsr #16 \n" michael@0: "orr %0, %0, %2, lsl #16 \n" michael@0: : "=r"(x), "=&r"(y), "=r"(t) michael@0: : "r"(x), "1"(y) michael@0: : michael@0: ); michael@0: return x; michael@0: } michael@0: #undef SkFixedMul michael@0: #define SkFixedMul(x, y) SkFixedMul_arm(x, y) michael@0: michael@0: #undef SkFloatToFixed michael@0: #define SkFloatToFixed(x) SkFloatToFixed_arm(x) michael@0: #endif michael@0: michael@0: #ifndef SkFixedMul michael@0: #define SkFixedMul(x, y) SkFixedMul_portable(x, y) michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: typedef int64_t SkFixed48; michael@0: michael@0: #define SkIntToFixed48(x) ((SkFixed48)(x) << 48) michael@0: #define SkFixed48ToInt(x) ((int)((x) >> 48)) michael@0: #define SkFixedToFixed48(x) ((SkFixed48)(x) << 32) michael@0: #define SkFixed48ToFixed(x) ((SkFixed)((x) >> 32)) michael@0: #define SkFloatToFixed48(x) ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f))) michael@0: michael@0: #define SkScalarToFixed48(x) SkFloatToFixed48(x) michael@0: michael@0: #endif