SkFixed.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef SkFixed_DEFINED
  8. #define SkFixed_DEFINED
  9. #include "SkSafe_math.h"
  10. #include "SkScalar.h"
  11. #include "SkTo.h"
  12. #include "SkTypes.h"
  13. /** \file SkFixed.h
  14. Types and macros for 16.16 fixed point
  15. */
  16. /** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
  17. */
  18. typedef int32_t SkFixed;
  19. #define SK_Fixed1 (1 << 16)
  20. #define SK_FixedHalf (1 << 15)
  21. #define SK_FixedQuarter (1 << 14)
  22. #define SK_FixedMax (0x7FFFFFFF)
  23. #define SK_FixedMin (-SK_FixedMax)
  24. #define SK_FixedPI (0x3243F)
  25. #define SK_FixedSqrt2 (92682)
  26. #define SK_FixedTanPIOver8 (0x6A0A)
  27. #define SK_FixedRoot2Over2 (0xB505)
  28. // NOTE: SkFixedToFloat is exact. SkFloatToFixed seems to lack a rounding step. For all fixed-point
  29. // values, this version is as accurate as possible for (fixed -> float -> fixed). Rounding reduces
  30. // accuracy if the intermediate floats are in the range that only holds integers (adding 0.5f to an
  31. // odd integer then snaps to nearest even). Using double for the rounding math gives maximum
  32. // accuracy for (float -> fixed -> float), but that's usually overkill.
  33. #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
  34. #define SkFloatToFixed(x) sk_float_saturate2int((x) * SK_Fixed1)
  35. #ifdef SK_DEBUG
  36. static inline SkFixed SkFloatToFixed_Check(float x) {
  37. int64_t n64 = (int64_t)(x * SK_Fixed1);
  38. SkFixed n32 = (SkFixed)n64;
  39. SkASSERT(n64 == n32);
  40. return n32;
  41. }
  42. #else
  43. #define SkFloatToFixed_Check(x) SkFloatToFixed(x)
  44. #endif
  45. #define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
  46. #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
  47. /** Converts an integer to a SkFixed, asserting that the result does not overflow
  48. a 32 bit signed integer
  49. */
  50. #ifdef SK_DEBUG
  51. inline SkFixed SkIntToFixed(int n)
  52. {
  53. SkASSERT(n >= -32768 && n <= 32767);
  54. // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
  55. // shifting.
  56. return (unsigned)n << 16;
  57. }
  58. #else
  59. // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
  60. // shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
  61. // debug version).
  62. #define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
  63. #endif
  64. #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
  65. #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
  66. #define SkFixedFloorToInt(x) ((x) >> 16)
  67. static inline SkFixed SkFixedRoundToFixed(SkFixed x) {
  68. return (x + SK_FixedHalf) & 0xFFFF0000;
  69. }
  70. static inline SkFixed SkFixedCeilToFixed(SkFixed x) {
  71. return (x + SK_Fixed1 - 1) & 0xFFFF0000;
  72. }
  73. static inline SkFixed SkFixedFloorToFixed(SkFixed x) {
  74. return x & 0xFFFF0000;
  75. }
  76. #define SkFixedAbs(x) SkAbs32(x)
  77. #define SkFixedAve(a, b) (((a) + (b)) >> 1)
  78. // The divide may exceed 32 bits. Clamp to a signed 32 bit result.
  79. #define SkFixedDiv(numer, denom) \
  80. SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)(numer), 16) / (denom)), SK_MinS32, SK_MaxS32))
  81. static inline SkFixed SkFixedMul(SkFixed a, SkFixed b) {
  82. return (SkFixed)((int64_t)a * b >> 16);
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // Platform-specific alternatives to our portable versions.
  86. // The VCVT float-to-fixed instruction is part of the VFPv3 instruction set.
  87. #if defined(__ARM_VFPV3__)
  88. /* This guy does not handle NaN or other obscurities, but is faster than
  89. than (int)(x*65536). When built on Android with -Os, needs forcing
  90. to inline or we lose the speed benefit.
  91. */
  92. SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
  93. {
  94. int32_t y;
  95. asm("vcvt.s32.f32 %0, %0, #16": "+w"(x));
  96. memcpy(&y, &x, sizeof(y));
  97. return y;
  98. }
  99. #undef SkFloatToFixed
  100. #define SkFloatToFixed(x) SkFloatToFixed_arm(x)
  101. #endif
  102. ///////////////////////////////////////////////////////////////////////////////
  103. #define SkFixedToScalar(x) SkFixedToFloat(x)
  104. #define SkScalarToFixed(x) SkFloatToFixed(x)
  105. ///////////////////////////////////////////////////////////////////////////////
  106. typedef int64_t SkFixed3232; // 32.32
  107. #define SkFixed3232Max SK_MaxS64
  108. #define SkFixed3232Min (-SkFixed3232Max)
  109. #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32))
  110. #define SkFixed3232ToInt(x) ((int)((x) >> 32))
  111. #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16))
  112. #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16))
  113. #define SkFloatToFixed3232(x) sk_float_saturate2int64((x) * (65536.0f * 65536.0f))
  114. #define SkFixed3232ToFloat(x) (x * (1 / (65536.0f * 65536.0f)))
  115. #define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
  116. #endif