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 static bool arguments_denormalized(float a, float b, int epsilon) { |
| 11 float denomalizedCheck = FLT_EPSILON * epsilon / 2; |
| 12 return fabsf(a) <= denomalizedCheck && fabsf(b) <= denomalizedCheck; |
| 13 } |
| 14 |
10 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ | 15 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ |
11 // FIXME: move to SkFloatBits.h | 16 // FIXME: move to SkFloatBits.h |
12 static bool equal_ulps(float a, float b, int epsilon) { | 17 static bool equal_ulps(float a, float b, int epsilon) { |
13 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 18 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
14 return false; | 19 return false; |
15 } | 20 } |
| 21 if (arguments_denormalized(a, b, epsilon)) { |
| 22 return true; |
| 23 } |
16 int aBits = SkFloatAs2sCompliment(a); | 24 int aBits = SkFloatAs2sCompliment(a); |
17 int bBits = SkFloatAs2sCompliment(b); | 25 int bBits = SkFloatAs2sCompliment(b); |
18 // Find the difference in ULPs. | 26 // Find the difference in ULPs. |
| 27 return aBits < bBits + epsilon && bBits < aBits + epsilon; |
| 28 } |
| 29 |
| 30 static bool d_equal_ulps(float a, float b, int epsilon) { |
| 31 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 32 return false; |
| 33 } |
| 34 int aBits = SkFloatAs2sCompliment(a); |
| 35 int bBits = SkFloatAs2sCompliment(b); |
| 36 // Find the difference in ULPs. |
19 return aBits < bBits + epsilon && bBits < aBits + epsilon; | 37 return aBits < bBits + epsilon && bBits < aBits + epsilon; |
20 } | 38 } |
21 | 39 |
22 static bool not_equal_ulps(float a, float b, int epsilon) { | 40 static bool not_equal_ulps(float a, float b, int epsilon) { |
23 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 41 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
24 return false; | 42 return false; |
25 } | 43 } |
| 44 if (arguments_denormalized(a, b, epsilon)) { |
| 45 return false; |
| 46 } |
| 47 int aBits = SkFloatAs2sCompliment(a); |
| 48 int bBits = SkFloatAs2sCompliment(b); |
| 49 // Find the difference in ULPs. |
| 50 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; |
| 51 } |
| 52 |
| 53 static bool d_not_equal_ulps(float a, float b, int epsilon) { |
| 54 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
| 55 return false; |
| 56 } |
26 int aBits = SkFloatAs2sCompliment(a); | 57 int aBits = SkFloatAs2sCompliment(a); |
27 int bBits = SkFloatAs2sCompliment(b); | 58 int bBits = SkFloatAs2sCompliment(b); |
28 // Find the difference in ULPs. | 59 // Find the difference in ULPs. |
29 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; | 60 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; |
30 } | 61 } |
31 | 62 |
32 static bool less_ulps(float a, float b, int epsilon) { | 63 static bool less_ulps(float a, float b, int epsilon) { |
33 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 64 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
34 return false; | 65 return false; |
35 } | 66 } |
| 67 if (arguments_denormalized(a, b, epsilon)) { |
| 68 return a <= b - FLT_EPSILON * epsilon; |
| 69 } |
36 int aBits = SkFloatAs2sCompliment(a); | 70 int aBits = SkFloatAs2sCompliment(a); |
37 int bBits = SkFloatAs2sCompliment(b); | 71 int bBits = SkFloatAs2sCompliment(b); |
38 // Find the difference in ULPs. | 72 // Find the difference in ULPs. |
39 return aBits <= bBits - epsilon; | 73 return aBits <= bBits - epsilon; |
40 } | 74 } |
41 | 75 |
42 static bool less_or_equal_ulps(float a, float b, int epsilon) { | 76 static bool less_or_equal_ulps(float a, float b, int epsilon) { |
43 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 77 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
44 return false; | 78 return false; |
45 } | 79 } |
| 80 if (arguments_denormalized(a, b, epsilon)) { |
| 81 return a < b + FLT_EPSILON * epsilon; |
| 82 } |
46 int aBits = SkFloatAs2sCompliment(a); | 83 int aBits = SkFloatAs2sCompliment(a); |
47 int bBits = SkFloatAs2sCompliment(b); | 84 int bBits = SkFloatAs2sCompliment(b); |
48 // Find the difference in ULPs. | 85 // Find the difference in ULPs. |
49 return aBits < bBits + epsilon; | 86 return aBits < bBits + epsilon; |
50 } | 87 } |
51 | 88 |
52 // equality using the same error term as between | 89 // equality using the same error term as between |
53 bool AlmostBequalUlps(float a, float b) { | 90 bool AlmostBequalUlps(float a, float b) { |
54 const int UlpsEpsilon = 2; | 91 const int UlpsEpsilon = 2; |
55 return equal_ulps(a, b, UlpsEpsilon); | 92 return equal_ulps(a, b, UlpsEpsilon); |
56 } | 93 } |
57 | 94 |
| 95 bool AlmostDequalUlps(float a, float b) { |
| 96 const int UlpsEpsilon = 16; |
| 97 return d_equal_ulps(a, b, UlpsEpsilon); |
| 98 } |
| 99 |
58 bool AlmostEqualUlps(float a, float b) { | 100 bool AlmostEqualUlps(float a, float b) { |
59 const int UlpsEpsilon = 16; | 101 const int UlpsEpsilon = 16; |
60 return equal_ulps(a, b, UlpsEpsilon); | 102 return equal_ulps(a, b, UlpsEpsilon); |
61 } | 103 } |
62 | 104 |
63 bool NotAlmostEqualUlps(float a, float b) { | 105 bool NotAlmostEqualUlps(float a, float b) { |
64 const int UlpsEpsilon = 16; | 106 const int UlpsEpsilon = 16; |
65 return not_equal_ulps(a, b, UlpsEpsilon); | 107 return not_equal_ulps(a, b, UlpsEpsilon); |
66 } | 108 } |
67 | 109 |
| 110 bool NotAlmostDequalUlps(float a, float b) { |
| 111 const int UlpsEpsilon = 16; |
| 112 return d_not_equal_ulps(a, b, UlpsEpsilon); |
| 113 } |
| 114 |
68 bool RoughlyEqualUlps(float a, float b) { | 115 bool RoughlyEqualUlps(float a, float b) { |
69 const int UlpsEpsilon = 256; | 116 const int UlpsEpsilon = 256; |
70 return equal_ulps(a, b, UlpsEpsilon); | 117 return equal_ulps(a, b, UlpsEpsilon); |
71 } | 118 } |
72 | 119 |
73 bool AlmostBetweenUlps(float a, float b, float c) { | 120 bool AlmostBetweenUlps(float a, float b, float c) { |
74 const int UlpsEpsilon = 2; | 121 const int UlpsEpsilon = 2; |
75 return a <= c ? less_or_equal_ulps(a, b, UlpsEpsilon) && less_or_equal_ulps(
b, c, UlpsEpsilon) | 122 return a <= c ? less_or_equal_ulps(a, b, UlpsEpsilon) && less_or_equal_ulps(
b, c, UlpsEpsilon) |
76 : less_or_equal_ulps(b, a, UlpsEpsilon) && less_or_equal_ulps(c, b, Ulps
Epsilon); | 123 : less_or_equal_ulps(b, a, UlpsEpsilon) && less_or_equal_ulps(c, b, Ulps
Epsilon); |
77 } | 124 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 double SkDCubeRoot(double x) { | 178 double SkDCubeRoot(double x) { |
132 if (approximately_zero_cubed(x)) { | 179 if (approximately_zero_cubed(x)) { |
133 return 0; | 180 return 0; |
134 } | 181 } |
135 double result = halley_cbrt3d(fabs(x)); | 182 double result = halley_cbrt3d(fabs(x)); |
136 if (x < 0) { | 183 if (x < 0) { |
137 result = -result; | 184 result = -result; |
138 } | 185 } |
139 return result; | 186 return result; |
140 } | 187 } |
OLD | NEW |