gfx/skia/trunk/include/core/SkFixed.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkFixed.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +/*
     1.5 + * Copyright 2006 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkFixed_DEFINED
    1.12 +#define SkFixed_DEFINED
    1.13 +
    1.14 +#include "SkTypes.h"
    1.15 +
    1.16 +/** \file SkFixed.h
    1.17 +
    1.18 +    Types and macros for 16.16 fixed point
    1.19 +*/
    1.20 +
    1.21 +/** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
    1.22 +*/
    1.23 +typedef int32_t             SkFixed;
    1.24 +#define SK_Fixed1           (1 << 16)
    1.25 +#define SK_FixedHalf        (1 << 15)
    1.26 +#define SK_FixedMax         (0x7FFFFFFF)
    1.27 +#define SK_FixedMin         (-SK_FixedMax)
    1.28 +#define SK_FixedNaN         ((int) 0x80000000)
    1.29 +#define SK_FixedPI          (0x3243F)
    1.30 +#define SK_FixedSqrt2       (92682)
    1.31 +#define SK_FixedTanPIOver8  (0x6A0A)
    1.32 +#define SK_FixedRoot2Over2  (0xB505)
    1.33 +
    1.34 +#define SkFixedToFloat(x)   ((x) * 1.5258789e-5f)
    1.35 +#if 1
    1.36 +    #define SkFloatToFixed(x)   ((SkFixed)((x) * SK_Fixed1))
    1.37 +#else
    1.38 +    // pins over/under flows to max/min int32 (slower than just a cast)
    1.39 +    static inline SkFixed SkFloatToFixed(float x) {
    1.40 +        int64_t n = x * SK_Fixed1;
    1.41 +        return (SkFixed)n;
    1.42 +    }
    1.43 +#endif
    1.44 +
    1.45 +#ifdef SK_DEBUG
    1.46 +    static inline SkFixed SkFloatToFixed_Check(float x) {
    1.47 +        int64_t n64 = (int64_t)(x * SK_Fixed1);
    1.48 +        SkFixed n32 = (SkFixed)n64;
    1.49 +        SkASSERT(n64 == n32);
    1.50 +        return n32;
    1.51 +    }
    1.52 +#else
    1.53 +    #define SkFloatToFixed_Check(x) SkFloatToFixed(x)
    1.54 +#endif
    1.55 +
    1.56 +#define SkFixedToDouble(x)  ((x) * 1.5258789e-5)
    1.57 +#define SkDoubleToFixed(x)  ((SkFixed)((x) * SK_Fixed1))
    1.58 +
    1.59 +/** Converts an integer to a SkFixed, asserting that the result does not overflow
    1.60 +    a 32 bit signed integer
    1.61 +*/
    1.62 +#ifdef SK_DEBUG
    1.63 +    inline SkFixed SkIntToFixed(int n)
    1.64 +    {
    1.65 +        SkASSERT(n >= -32768 && n <= 32767);
    1.66 +        return n << 16;
    1.67 +    }
    1.68 +#else
    1.69 +    //  force the cast to SkFixed to ensure that the answer is signed (like the debug version)
    1.70 +    #define SkIntToFixed(n)     (SkFixed)((n) << 16)
    1.71 +#endif
    1.72 +
    1.73 +#define SkFixedRoundToInt(x)    (((x) + SK_FixedHalf) >> 16)
    1.74 +#define SkFixedCeilToInt(x)     (((x) + SK_Fixed1 - 1) >> 16)
    1.75 +#define SkFixedFloorToInt(x)    ((x) >> 16)
    1.76 +
    1.77 +#define SkFixedRoundToFixed(x)  (((x) + SK_FixedHalf) & 0xFFFF0000)
    1.78 +#define SkFixedCeilToFixed(x)   (((x) + SK_Fixed1 - 1) & 0xFFFF0000)
    1.79 +#define SkFixedFloorToFixed(x)  ((x) & 0xFFFF0000)
    1.80 +
    1.81 +#define SkFixedAbs(x)       SkAbs32(x)
    1.82 +#define SkFixedAve(a, b)    (((a) + (b)) >> 1)
    1.83 +
    1.84 +SkFixed SkFixedMul_portable(SkFixed, SkFixed);
    1.85 +
    1.86 +#define SkFixedDiv(numer, denom)    SkDivBits(numer, denom, 16)
    1.87 +
    1.88 +///////////////////////////////////////////////////////////////////////////////
    1.89 +// TODO: move fixed sin/cos into SkCosineMapper, as that is the only caller
    1.90 +//       or rewrite SkCosineMapper to not use it at all
    1.91 +
    1.92 +SkFixed SkFixedSinCos(SkFixed radians, SkFixed* cosValueOrNull);
    1.93 +#define SkFixedSin(radians)         SkFixedSinCos(radians, NULL)
    1.94 +static inline SkFixed SkFixedCos(SkFixed radians) {
    1.95 +    SkFixed cosValue;
    1.96 +    (void)SkFixedSinCos(radians, &cosValue);
    1.97 +    return cosValue;
    1.98 +}
    1.99 +
   1.100 +//////////////////////////////////////////////////////////////////////////////////////////////////////
   1.101 +// Now look for ASM overrides for our portable versions (should consider putting this in its own file)
   1.102 +
   1.103 +#ifdef SkLONGLONG
   1.104 +    inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b)
   1.105 +    {
   1.106 +        return (SkFixed)((int64_t)a * b >> 16);
   1.107 +    }
   1.108 +    #define SkFixedMul(a,b)     SkFixedMul_longlong(a,b)
   1.109 +#endif
   1.110 +
   1.111 +#if defined(SK_CPU_ARM)
   1.112 +    /* This guy does not handle NaN or other obscurities, but is faster than
   1.113 +       than (int)(x*65536)
   1.114 +    */
   1.115 +    inline SkFixed SkFloatToFixed_arm(float x)
   1.116 +    {
   1.117 +        int32_t y, z;
   1.118 +        asm("movs    %1, %3, lsl #1         \n"
   1.119 +            "mov     %2, #0x8E              \n"
   1.120 +            "sub     %1, %2, %1, lsr #24    \n"
   1.121 +            "mov     %2, %3, lsl #8         \n"
   1.122 +            "orr     %2, %2, #0x80000000    \n"
   1.123 +            "mov     %1, %2, lsr %1         \n"
   1.124 +            "it cs                          \n"
   1.125 +            "rsbcs   %1, %1, #0             \n"
   1.126 +            : "=r"(x), "=&r"(y), "=&r"(z)
   1.127 +            : "r"(x)
   1.128 +            : "cc"
   1.129 +            );
   1.130 +        return y;
   1.131 +    }
   1.132 +    inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
   1.133 +    {
   1.134 +        int32_t t;
   1.135 +        asm("smull  %0, %2, %1, %3          \n"
   1.136 +            "mov    %0, %0, lsr #16         \n"
   1.137 +            "orr    %0, %0, %2, lsl #16     \n"
   1.138 +            : "=r"(x), "=&r"(y), "=r"(t)
   1.139 +            : "r"(x), "1"(y)
   1.140 +            :
   1.141 +            );
   1.142 +        return x;
   1.143 +    }
   1.144 +    #undef SkFixedMul
   1.145 +    #define SkFixedMul(x, y)        SkFixedMul_arm(x, y)
   1.146 +
   1.147 +    #undef SkFloatToFixed
   1.148 +    #define SkFloatToFixed(x)  SkFloatToFixed_arm(x)
   1.149 +#endif
   1.150 +
   1.151 +#ifndef SkFixedMul
   1.152 +    #define SkFixedMul(x, y)    SkFixedMul_portable(x, y)
   1.153 +#endif
   1.154 +
   1.155 +///////////////////////////////////////////////////////////////////////////////
   1.156 +
   1.157 +typedef int64_t SkFixed48;
   1.158 +
   1.159 +#define SkIntToFixed48(x)       ((SkFixed48)(x) << 48)
   1.160 +#define SkFixed48ToInt(x)       ((int)((x) >> 48))
   1.161 +#define SkFixedToFixed48(x)     ((SkFixed48)(x) << 32)
   1.162 +#define SkFixed48ToFixed(x)     ((SkFixed)((x) >> 32))
   1.163 +#define SkFloatToFixed48(x)     ((SkFixed48)((x) * (65536.0f * 65536.0f * 65536.0f)))
   1.164 +
   1.165 +#define SkScalarToFixed48(x)    SkFloatToFixed48(x)
   1.166 +
   1.167 +#endif

mercurial