SkFloatingPoint.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 SkFloatingPoint_DEFINED
  8. #define SkFloatingPoint_DEFINED
  9. #include "../private/SkFloatBits.h"
  10. #include "SkTypes.h"
  11. #include "SkSafe_math.h"
  12. #include <float.h>
  13. #include <math.h>
  14. #include <cstring>
  15. #include <limits>
  16. #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
  17. #include <xmmintrin.h>
  18. #elif defined(SK_ARM_HAS_NEON)
  19. #include <arm_neon.h>
  20. #endif
  21. // For _POSIX_VERSION
  22. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  23. #include <unistd.h>
  24. #endif
  25. // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
  26. // However, on Linux including cmath undefines isfinite.
  27. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
  28. static inline float sk_float_pow(float base, float exp) {
  29. return powf(base, exp);
  30. }
  31. #define sk_float_sqrt(x) sqrtf(x)
  32. #define sk_float_sin(x) sinf(x)
  33. #define sk_float_cos(x) cosf(x)
  34. #define sk_float_tan(x) tanf(x)
  35. #define sk_float_floor(x) floorf(x)
  36. #define sk_float_ceil(x) ceilf(x)
  37. #define sk_float_trunc(x) truncf(x)
  38. #ifdef SK_BUILD_FOR_MAC
  39. # define sk_float_acos(x) static_cast<float>(acos(x))
  40. # define sk_float_asin(x) static_cast<float>(asin(x))
  41. #else
  42. # define sk_float_acos(x) acosf(x)
  43. # define sk_float_asin(x) asinf(x)
  44. #endif
  45. #define sk_float_atan2(y,x) atan2f(y,x)
  46. #define sk_float_abs(x) fabsf(x)
  47. #define sk_float_copysign(x, y) copysignf(x, y)
  48. #define sk_float_mod(x,y) fmodf(x,y)
  49. #define sk_float_exp(x) expf(x)
  50. #define sk_float_log(x) logf(x)
  51. #define sk_float_round(x) sk_float_floor((x) + 0.5f)
  52. // can't find log2f on android, but maybe that just a tool bug?
  53. #ifdef SK_BUILD_FOR_ANDROID
  54. static inline float sk_float_log2(float x) {
  55. const double inv_ln_2 = 1.44269504088896;
  56. return (float)(log(x) * inv_ln_2);
  57. }
  58. #else
  59. #define sk_float_log2(x) log2f(x)
  60. #endif
  61. static inline bool sk_float_isfinite(float x) {
  62. return SkFloatBits_IsFinite(SkFloat2Bits(x));
  63. }
  64. static inline bool sk_float_isinf(float x) {
  65. return SkFloatBits_IsInf(SkFloat2Bits(x));
  66. }
  67. static inline bool sk_float_isnan(float x) {
  68. return !(x == x);
  69. }
  70. #define sk_double_isnan(a) sk_float_isnan(a)
  71. #define SK_MaxS32FitsInFloat 2147483520
  72. #define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat
  73. #define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000
  74. #define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat
  75. /**
  76. * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
  77. */
  78. static inline int sk_float_saturate2int(float x) {
  79. x = SkTMin<float>(x, SK_MaxS32FitsInFloat);
  80. x = SkTMax<float>(x, SK_MinS32FitsInFloat);
  81. return (int)x;
  82. }
  83. /**
  84. * Return the closest int for the given double. Returns SK_MaxS32 for NaN.
  85. */
  86. static inline int sk_double_saturate2int(double x) {
  87. x = SkTMin<double>(x, SK_MaxS32);
  88. x = SkTMax<double>(x, SK_MinS32);
  89. return (int)x;
  90. }
  91. /**
  92. * Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
  93. */
  94. static inline int64_t sk_float_saturate2int64(float x) {
  95. x = SkTMin<float>(x, SK_MaxS64FitsInFloat);
  96. x = SkTMax<float>(x, SK_MinS64FitsInFloat);
  97. return (int64_t)x;
  98. }
  99. #define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
  100. #define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
  101. #define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
  102. #define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x)
  103. #define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f)
  104. #define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x)
  105. #define sk_double_floor(x) floor(x)
  106. #define sk_double_round(x) floor((x) + 0.5)
  107. #define sk_double_ceil(x) ceil(x)
  108. #define sk_double_floor2int(x) (int)floor(x)
  109. #define sk_double_round2int(x) (int)floor((x) + 0.5)
  110. #define sk_double_ceil2int(x) (int)ceil(x)
  111. // Cast double to float, ignoring any warning about too-large finite values being cast to float.
  112. // Clang thinks this is undefined, but it's actually implementation defined to return either
  113. // the largest float or infinity (one of the two bracketing representable floats). Good enough!
  114. #if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
  115. __attribute__((no_sanitize("float-cast-overflow")))
  116. #endif
  117. static inline float sk_double_to_float(double x) {
  118. return static_cast<float>(x);
  119. }
  120. #define SK_FloatNaN std::numeric_limits<float>::quiet_NaN()
  121. #define SK_FloatInfinity (+std::numeric_limits<float>::infinity())
  122. #define SK_FloatNegativeInfinity (-std::numeric_limits<float>::infinity())
  123. static inline float sk_float_rsqrt_portable(float x) {
  124. // Get initial estimate.
  125. int i;
  126. memcpy(&i, &x, 4);
  127. i = 0x5F1FFFF9 - (i>>1);
  128. float estimate;
  129. memcpy(&estimate, &i, 4);
  130. // One step of Newton's method to refine.
  131. const float estimate_sq = estimate*estimate;
  132. estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
  133. return estimate;
  134. }
  135. // Fast, approximate inverse square root.
  136. // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON.
  137. static inline float sk_float_rsqrt(float x) {
  138. // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
  139. // it at compile time. This is going to be too fast to productively hide behind a function pointer.
  140. //
  141. // We do one step of Newton's method to refine the estimates in the NEON and portable paths. No
  142. // refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
  143. //
  144. // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
  145. #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
  146. return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
  147. #elif defined(SK_ARM_HAS_NEON)
  148. // Get initial estimate.
  149. const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
  150. float32x2_t estimate = vrsqrte_f32(xx);
  151. // One step of Newton's method to refine.
  152. const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
  153. estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
  154. return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
  155. #else
  156. return sk_float_rsqrt_portable(x);
  157. #endif
  158. }
  159. // This is the number of significant digits we can print in a string such that when we read that
  160. // string back we get the floating point number we expect. The minimum value C requires is 6, but
  161. // most compilers support 9
  162. #ifdef FLT_DECIMAL_DIG
  163. #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
  164. #else
  165. #define SK_FLT_DECIMAL_DIG 9
  166. #endif
  167. // IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not
  168. // so we have a helper that suppresses the possible undefined-behavior warnings.
  169. #ifdef __clang__
  170. __attribute__((no_sanitize("float-divide-by-zero")))
  171. #endif
  172. static inline float sk_ieee_float_divide(float numer, float denom) {
  173. return numer / denom;
  174. }
  175. #ifdef __clang__
  176. __attribute__((no_sanitize("float-divide-by-zero")))
  177. #endif
  178. static inline double sk_ieee_double_divide(double numer, double denom) {
  179. return numer / denom;
  180. }
  181. // While we clean up divide by zero, we'll replace places that do divide by zero with this TODO.
  182. static inline float sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n, float d) {
  183. return sk_ieee_float_divide(n,d);
  184. }
  185. static inline float sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(double n, double d) {
  186. return sk_ieee_double_divide(n,d);
  187. }
  188. #endif