Home | History | Annotate | Download | only in core
      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.52587890625e-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.52587890625e-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 #define SkFixedDiv(numer, denom)    SkDivBits(numer, denom, 16)
     82 
     83 //////////////////////////////////////////////////////////////////////////////////////////////////////
     84 // Now look for ASM overrides for our portable versions (should consider putting this in its own file)
     85 
     86 inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) {
     87     return (SkFixed)((int64_t)a * b >> 16);
     88 }
     89 #define SkFixedMul(a,b)     SkFixedMul_longlong(a,b)
     90 
     91 
     92 #if defined(SK_CPU_ARM32)
     93     /* This guy does not handle NaN or other obscurities, but is faster than
     94        than (int)(x*65536).  When built on Android with -Os, needs forcing
     95        to inline or we lose the speed benefit.
     96     */
     97     SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
     98     {
     99         int32_t y, z;
    100         asm("movs    %1, %3, lsl #1         \n"
    101             "mov     %2, #0x8E              \n"
    102             "sub     %1, %2, %1, lsr #24    \n"
    103             "mov     %2, %3, lsl #8         \n"
    104             "orr     %2, %2, #0x80000000    \n"
    105             "mov     %1, %2, lsr %1         \n"
    106             "it cs                          \n"
    107             "rsbcs   %1, %1, #0             \n"
    108             : "=r"(x), "=&r"(y), "=&r"(z)
    109             : "r"(x)
    110             : "cc"
    111             );
    112         return y;
    113     }
    114     inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
    115     {
    116         int32_t t;
    117         asm("smull  %0, %2, %1, %3          \n"
    118             "mov    %0, %0, lsr #16         \n"
    119             "orr    %0, %0, %2, lsl #16     \n"
    120             : "=r"(x), "=&r"(y), "=r"(t)
    121             : "r"(x), "1"(y)
    122             :
    123             );
    124         return x;
    125     }
    126     #undef SkFixedMul
    127     #define SkFixedMul(x, y)        SkFixedMul_arm(x, y)
    128 
    129     #undef SkFloatToFixed
    130     #define SkFloatToFixed(x)  SkFloatToFixed_arm(x)
    131 #endif
    132 
    133 ///////////////////////////////////////////////////////////////////////////////
    134 
    135 typedef int64_t SkFixed3232;   // 32.32
    136 
    137 #define SkIntToFixed3232(x)       ((SkFixed3232)(x) << 32)
    138 #define SkFixed3232ToInt(x)       ((int)((x) >> 32))
    139 #define SkFixedToFixed3232(x)     ((SkFixed3232)(x) << 16)
    140 #define SkFixed3232ToFixed(x)     ((SkFixed)((x) >> 16))
    141 #define SkFloatToFixed3232(x)     ((SkFixed3232)((x) * (65536.0f * 65536.0f)))
    142 
    143 #define SkScalarToFixed3232(x)    SkFloatToFixed3232(x)
    144 
    145 ///////////////////////////////////////////////////////////////////////////////
    146 
    147 // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so
    148 // we don't overflow along the way
    149 typedef int64_t Sk48Dot16;
    150 
    151 #define Sk48Dot16FloorToInt(x)    static_cast<int>((x) >> 16)
    152 
    153 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) {
    154     return static_cast<float>(x * 1.5258789e-5);  // x * (1.0f / (1 << 16))
    155 }
    156 #define SkFloatTo48Dot16(x)       (static_cast<Sk48Dot16>((x) * (1 << 16)))
    157 
    158 #define SkScalarTo48Dot16(x)      SkFloatTo48Dot16(x)
    159 
    160 #endif
    161