| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #include "SkFloatBits.h" | 7 #include "SkFloatBits.h" |
| 8 #include "SkPathOpsTypes.h" | 8 #include "SkPathOpsTypes.h" |
| 9 | 9 |
| 10 | |
| 11 | |
| 12 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ | 10 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ |
| 13 // FIXME: move to SkFloatBits.h | 11 // FIXME: move to SkFloatBits.h |
| 14 static bool equal_ulps(float a, float b, int epsilon) { | 12 static bool equal_ulps(float a, float b, int epsilon) { |
| 15 SkFloatIntUnion floatIntA, floatIntB; | 13 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 16 floatIntA.fFloat = a; | 14 return false; |
| 17 floatIntB.fFloat = b; | |
| 18 // Different signs means they do not match. | |
| 19 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { | |
| 20 // Check for equality to make sure +0 == -0 | |
| 21 return a == b; | |
| 22 } | 15 } |
| 16 int aBits = SkFloatAs2sCompliment(a); |
| 17 int bBits = SkFloatAs2sCompliment(b); |
| 23 // Find the difference in ULPs. | 18 // Find the difference in ULPs. |
| 24 int ulpsDiff = abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); | 19 return aBits < bBits + epsilon && bBits < aBits + epsilon; |
| 25 return ulpsDiff <= epsilon; | 20 } |
| 21 |
| 22 static bool not_equal_ulps(float a, float b, int epsilon) { |
| 23 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 24 return false; |
| 25 } |
| 26 int aBits = SkFloatAs2sCompliment(a); |
| 27 int bBits = SkFloatAs2sCompliment(b); |
| 28 // Find the difference in ULPs. |
| 29 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; |
| 26 } | 30 } |
| 27 | 31 |
| 28 static bool less_ulps(float a, float b, int epsilon) { | 32 static bool less_ulps(float a, float b, int epsilon) { |
| 29 SkFloatIntUnion floatIntA, floatIntB; | 33 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 30 floatIntA.fFloat = a; | 34 return false; |
| 31 floatIntB.fFloat = b; | |
| 32 // Check different signs with float epsilon since we only care if they're bo
th close to 0. | |
| 33 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { | |
| 34 return a <= b + FLT_EPSILON * epsilon; | |
| 35 } | 35 } |
| 36 int aBits = SkFloatAs2sCompliment(a); |
| 37 int bBits = SkFloatAs2sCompliment(b); |
| 36 // Find the difference in ULPs. | 38 // Find the difference in ULPs. |
| 37 return floatIntA.fSignBitInt <= floatIntB.fSignBitInt + epsilon; | 39 return aBits <= bBits - epsilon; |
| 40 } |
| 41 |
| 42 static bool less_or_equal_ulps(float a, float b, int epsilon) { |
| 43 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 44 return false; |
| 45 } |
| 46 int aBits = SkFloatAs2sCompliment(a); |
| 47 int bBits = SkFloatAs2sCompliment(b); |
| 48 // Find the difference in ULPs. |
| 49 return aBits < bBits + epsilon; |
| 50 } |
| 51 |
| 52 // equality using the same error term as between |
| 53 bool AlmostBequalUlps(float a, float b) { |
| 54 const int UlpsEpsilon = 2; |
| 55 return equal_ulps(a, b, UlpsEpsilon); |
| 38 } | 56 } |
| 39 | 57 |
| 40 bool AlmostEqualUlps(float a, float b) { | 58 bool AlmostEqualUlps(float a, float b) { |
| 41 const int UlpsEpsilon = 16; | 59 const int UlpsEpsilon = 16; |
| 42 return equal_ulps(a, b, UlpsEpsilon); | 60 return equal_ulps(a, b, UlpsEpsilon); |
| 43 } | 61 } |
| 44 | 62 |
| 63 bool NotAlmostEqualUlps(float a, float b) { |
| 64 const int UlpsEpsilon = 16; |
| 65 return not_equal_ulps(a, b, UlpsEpsilon); |
| 66 } |
| 67 |
| 45 bool RoughlyEqualUlps(float a, float b) { | 68 bool RoughlyEqualUlps(float a, float b) { |
| 46 const int UlpsEpsilon = 256; | 69 const int UlpsEpsilon = 256; |
| 47 return equal_ulps(a, b, UlpsEpsilon); | 70 return equal_ulps(a, b, UlpsEpsilon); |
| 48 } | 71 } |
| 49 | 72 |
| 50 bool AlmostBetweenUlps(float a, float b, float c) { | 73 bool AlmostBetweenUlps(float a, float b, float c) { |
| 51 const int UlpsEpsilon = 1; | 74 const int UlpsEpsilon = 2; |
| 52 return a <= c ? less_ulps(a, b, UlpsEpsilon) && less_ulps(b, c, UlpsEpsilon) | 75 return a <= c ? less_or_equal_ulps(a, b, UlpsEpsilon) && less_or_equal_ulps(
b, c, UlpsEpsilon) |
| 53 : less_ulps(b, a, UlpsEpsilon) && less_ulps(c, b, UlpsEpsilon); | 76 : less_or_equal_ulps(b, a, UlpsEpsilon) && less_or_equal_ulps(c, b, Ulps
Epsilon); |
| 77 } |
| 78 |
| 79 bool AlmostLessUlps(float a, float b) { |
| 80 const int UlpsEpsilon = 16; |
| 81 return less_ulps(a, b, UlpsEpsilon); |
| 82 } |
| 83 |
| 84 bool AlmostLessOrEqualUlps(float a, float b) { |
| 85 const int UlpsEpsilon = 16; |
| 86 return less_or_equal_ulps(a, b, UlpsEpsilon); |
| 54 } | 87 } |
| 55 | 88 |
| 56 int UlpsDistance(float a, float b) { | 89 int UlpsDistance(float a, float b) { |
| 90 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 91 return SK_MaxS32; |
| 92 } |
| 57 SkFloatIntUnion floatIntA, floatIntB; | 93 SkFloatIntUnion floatIntA, floatIntB; |
| 58 floatIntA.fFloat = a; | 94 floatIntA.fFloat = a; |
| 59 floatIntB.fFloat = b; | 95 floatIntB.fFloat = b; |
| 60 // Different signs means they do not match. | 96 // Different signs means they do not match. |
| 61 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { | 97 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { |
| 62 // Check for equality to make sure +0 == -0 | 98 // Check for equality to make sure +0 == -0 |
| 63 return a == b ? 0 : SK_MaxS32; | 99 return a == b ? 0 : SK_MaxS32; |
| 64 } | 100 } |
| 65 // Find the difference in ULPs. | 101 // Find the difference in ULPs. |
| 66 return abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); | 102 return abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 95 double SkDCubeRoot(double x) { | 131 double SkDCubeRoot(double x) { |
| 96 if (approximately_zero_cubed(x)) { | 132 if (approximately_zero_cubed(x)) { |
| 97 return 0; | 133 return 0; |
| 98 } | 134 } |
| 99 double result = halley_cbrt3d(fabs(x)); | 135 double result = halley_cbrt3d(fabs(x)); |
| 100 if (x < 0) { | 136 if (x < 0) { |
| 101 result = -result; | 137 result = -result; |
| 102 } | 138 } |
| 103 return result; | 139 return result; |
| 104 } | 140 } |
| OLD | NEW |