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 "SkOpCoincidence.h" | 8 #include "SkOpCoincidence.h" |
9 #include "SkPathOpsTypes.h" | 9 #include "SkPathOpsTypes.h" |
10 | 10 |
11 static bool arguments_denormalized(float a, float b, int epsilon) { | 11 static bool arguments_denormalized(float a, float b, int epsilon) { |
12 float denormalizedCheck = FLT_EPSILON * epsilon / 2; | 12 float denormalizedCheck = FLT_EPSILON * epsilon / 2; |
13 return fabsf(a) <= denormalizedCheck && fabsf(b) <= denormalizedCheck; | 13 return fabsf(a) <= denormalizedCheck && fabsf(b) <= denormalizedCheck; |
14 } | 14 } |
15 | 15 |
16 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num bers-2012-edition/ | 16 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num bers-2012-edition/ |
17 // FIXME: move to SkFloatBits.h | 17 // FIXME: move to SkFloatBits.h |
18 static bool equal_ulps(float a, float b, int epsilon, int depsilon) { | 18 static bool equal_ulps(float a, float b, int epsilon, int depsilon) { |
19 if (arguments_denormalized(a, b, depsilon)) { | |
20 return true; | |
21 } | |
22 int aBits = SkFloatAs2sCompliment(a); | |
23 int bBits = SkFloatAs2sCompliment(b); | |
24 // Find the difference in ULPs. | |
25 return aBits < bBits + epsilon && bBits < aBits + epsilon; | |
26 } | |
27 | |
28 static bool equal_ulps_pin(float a, float b, int epsilon, int depsilon) { | |
reed1
2015/12/15 14:17:21
Just an idea, may not be any clearer...
To avoid
| |
19 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 29 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
20 return false; | 30 return false; |
21 } | 31 } |
22 if (arguments_denormalized(a, b, depsilon)) { | 32 if (arguments_denormalized(a, b, depsilon)) { |
23 return true; | 33 return true; |
24 } | 34 } |
25 int aBits = SkFloatAs2sCompliment(a); | 35 int aBits = SkFloatAs2sCompliment(a); |
26 int bBits = SkFloatAs2sCompliment(b); | 36 int bBits = SkFloatAs2sCompliment(b); |
27 // Find the difference in ULPs. | 37 // Find the difference in ULPs. |
28 return aBits < bBits + epsilon && bBits < aBits + epsilon; | 38 return aBits < bBits + epsilon && bBits < aBits + epsilon; |
29 } | 39 } |
30 | 40 |
31 static bool d_equal_ulps(float a, float b, int epsilon) { | 41 static bool d_equal_ulps(float a, float b, int epsilon) { |
32 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | |
33 return false; | |
34 } | |
35 int aBits = SkFloatAs2sCompliment(a); | 42 int aBits = SkFloatAs2sCompliment(a); |
36 int bBits = SkFloatAs2sCompliment(b); | 43 int bBits = SkFloatAs2sCompliment(b); |
37 // Find the difference in ULPs. | 44 // Find the difference in ULPs. |
38 return aBits < bBits + epsilon && bBits < aBits + epsilon; | 45 return aBits < bBits + epsilon && bBits < aBits + epsilon; |
39 } | 46 } |
40 | 47 |
41 static bool not_equal_ulps(float a, float b, int epsilon) { | 48 static bool not_equal_ulps(float a, float b, int epsilon) { |
49 if (arguments_denormalized(a, b, epsilon)) { | |
50 return false; | |
51 } | |
52 int aBits = SkFloatAs2sCompliment(a); | |
53 int bBits = SkFloatAs2sCompliment(b); | |
54 // Find the difference in ULPs. | |
55 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; | |
56 } | |
57 | |
58 static bool not_equal_ulps_pin(float a, float b, int epsilon) { | |
42 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | 59 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { |
43 return false; | 60 return false; |
44 } | 61 } |
45 if (arguments_denormalized(a, b, epsilon)) { | 62 if (arguments_denormalized(a, b, epsilon)) { |
46 return false; | 63 return false; |
47 } | 64 } |
48 int aBits = SkFloatAs2sCompliment(a); | 65 int aBits = SkFloatAs2sCompliment(a); |
49 int bBits = SkFloatAs2sCompliment(b); | 66 int bBits = SkFloatAs2sCompliment(b); |
50 // Find the difference in ULPs. | 67 // Find the difference in ULPs. |
51 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; | 68 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; |
52 } | 69 } |
53 | 70 |
54 static bool d_not_equal_ulps(float a, float b, int epsilon) { | 71 static bool d_not_equal_ulps(float a, float b, int epsilon) { |
55 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | |
56 return false; | |
57 } | |
58 int aBits = SkFloatAs2sCompliment(a); | 72 int aBits = SkFloatAs2sCompliment(a); |
59 int bBits = SkFloatAs2sCompliment(b); | 73 int bBits = SkFloatAs2sCompliment(b); |
60 // Find the difference in ULPs. | 74 // Find the difference in ULPs. |
61 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; | 75 return aBits >= bBits + epsilon || bBits >= aBits + epsilon; |
62 } | 76 } |
63 | 77 |
64 static bool less_ulps(float a, float b, int epsilon) { | 78 static bool less_ulps(float a, float b, int epsilon) { |
65 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | |
66 return false; | |
67 } | |
68 if (arguments_denormalized(a, b, epsilon)) { | 79 if (arguments_denormalized(a, b, epsilon)) { |
69 return a <= b - FLT_EPSILON * epsilon; | 80 return a <= b - FLT_EPSILON * epsilon; |
70 } | 81 } |
71 int aBits = SkFloatAs2sCompliment(a); | 82 int aBits = SkFloatAs2sCompliment(a); |
72 int bBits = SkFloatAs2sCompliment(b); | 83 int bBits = SkFloatAs2sCompliment(b); |
73 // Find the difference in ULPs. | 84 // Find the difference in ULPs. |
74 return aBits <= bBits - epsilon; | 85 return aBits <= bBits - epsilon; |
75 } | 86 } |
76 | 87 |
77 static bool less_or_equal_ulps(float a, float b, int epsilon) { | 88 static bool less_or_equal_ulps(float a, float b, int epsilon) { |
78 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | |
79 return false; | |
80 } | |
81 if (arguments_denormalized(a, b, epsilon)) { | 89 if (arguments_denormalized(a, b, epsilon)) { |
82 return a < b + FLT_EPSILON * epsilon; | 90 return a < b + FLT_EPSILON * epsilon; |
83 } | 91 } |
84 int aBits = SkFloatAs2sCompliment(a); | 92 int aBits = SkFloatAs2sCompliment(a); |
85 int bBits = SkFloatAs2sCompliment(b); | 93 int bBits = SkFloatAs2sCompliment(b); |
86 // Find the difference in ULPs. | 94 // Find the difference in ULPs. |
87 return aBits < bBits + epsilon; | 95 return aBits < bBits + epsilon; |
88 } | 96 } |
89 | 97 |
90 // equality using the same error term as between | 98 // equality using the same error term as between |
91 bool AlmostBequalUlps(float a, float b) { | 99 bool AlmostBequalUlps(float a, float b) { |
92 const int UlpsEpsilon = 2; | 100 const int UlpsEpsilon = 2; |
93 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); | 101 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); |
94 } | 102 } |
95 | 103 |
96 bool AlmostPequalUlps(float a, float b) { | 104 bool AlmostPequalUlps(float a, float b) { |
97 const int UlpsEpsilon = 8; | 105 const int UlpsEpsilon = 8; |
98 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); | 106 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); |
99 } | 107 } |
100 | 108 |
101 bool AlmostDequalUlps(float a, float b) { | 109 bool AlmostDequalUlps(float a, float b) { |
102 const int UlpsEpsilon = 16; | 110 const int UlpsEpsilon = 16; |
103 return d_equal_ulps(a, b, UlpsEpsilon); | 111 return d_equal_ulps(a, b, UlpsEpsilon); |
104 } | 112 } |
105 | 113 |
106 bool AlmostDequalUlps(double a, double b) { | 114 bool AlmostDequalUlps(double a, double b) { |
107 if (SkScalarIsFinite(a) || SkScalarIsFinite(b)) { | 115 return AlmostDequalUlps(SkDoubleToScalar(a), SkDoubleToScalar(b)); |
108 return AlmostDequalUlps(SkDoubleToScalar(a), SkDoubleToScalar(b)); | |
109 } | |
110 return fabs(a - b) / SkTMax(fabs(a), fabs(b)) < FLT_EPSILON * 16; | |
111 } | 116 } |
112 | 117 |
113 bool AlmostEqualUlps(float a, float b) { | 118 bool AlmostEqualUlps(float a, float b) { |
114 const int UlpsEpsilon = 16; | 119 const int UlpsEpsilon = 16; |
115 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); | 120 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); |
116 } | 121 } |
117 | 122 |
123 bool AlmostEqualUlps_Pin(float a, float b) { | |
124 const int UlpsEpsilon = 16; | |
125 return equal_ulps_pin(a, b, UlpsEpsilon, UlpsEpsilon); | |
126 } | |
127 | |
118 bool NotAlmostEqualUlps(float a, float b) { | 128 bool NotAlmostEqualUlps(float a, float b) { |
119 const int UlpsEpsilon = 16; | 129 const int UlpsEpsilon = 16; |
120 return not_equal_ulps(a, b, UlpsEpsilon); | 130 return not_equal_ulps(a, b, UlpsEpsilon); |
121 } | 131 } |
122 | 132 |
133 bool NotAlmostEqualUlps_Pin(float a, float b) { | |
134 const int UlpsEpsilon = 16; | |
135 return not_equal_ulps_pin(a, b, UlpsEpsilon); | |
136 } | |
137 | |
123 bool NotAlmostDequalUlps(float a, float b) { | 138 bool NotAlmostDequalUlps(float a, float b) { |
124 const int UlpsEpsilon = 16; | 139 const int UlpsEpsilon = 16; |
125 return d_not_equal_ulps(a, b, UlpsEpsilon); | 140 return d_not_equal_ulps(a, b, UlpsEpsilon); |
126 } | 141 } |
127 | 142 |
128 bool RoughlyEqualUlps(float a, float b) { | 143 bool RoughlyEqualUlps(float a, float b) { |
129 const int UlpsEpsilon = 256; | 144 const int UlpsEpsilon = 256; |
130 const int DUlpsEpsilon = 1024; | 145 const int DUlpsEpsilon = 1024; |
131 return equal_ulps(a, b, UlpsEpsilon, DUlpsEpsilon); | 146 return equal_ulps(a, b, UlpsEpsilon, DUlpsEpsilon); |
132 } | 147 } |
133 | 148 |
134 bool AlmostBetweenUlps(float a, float b, float c) { | 149 bool AlmostBetweenUlps(float a, float b, float c) { |
135 const int UlpsEpsilon = 2; | 150 const int UlpsEpsilon = 2; |
136 return a <= c ? less_or_equal_ulps(a, b, UlpsEpsilon) && less_or_equal_ulps( b, c, UlpsEpsilon) | 151 return a <= c ? less_or_equal_ulps(a, b, UlpsEpsilon) && less_or_equal_ulps( b, c, UlpsEpsilon) |
137 : less_or_equal_ulps(b, a, UlpsEpsilon) && less_or_equal_ulps(c, b, Ulps Epsilon); | 152 : less_or_equal_ulps(b, a, UlpsEpsilon) && less_or_equal_ulps(c, b, Ulps Epsilon); |
138 } | 153 } |
139 | 154 |
140 bool AlmostLessUlps(float a, float b) { | 155 bool AlmostLessUlps(float a, float b) { |
141 const int UlpsEpsilon = 16; | 156 const int UlpsEpsilon = 16; |
142 return less_ulps(a, b, UlpsEpsilon); | 157 return less_ulps(a, b, UlpsEpsilon); |
143 } | 158 } |
144 | 159 |
145 bool AlmostLessOrEqualUlps(float a, float b) { | 160 bool AlmostLessOrEqualUlps(float a, float b) { |
146 const int UlpsEpsilon = 16; | 161 const int UlpsEpsilon = 16; |
147 return less_or_equal_ulps(a, b, UlpsEpsilon); | 162 return less_or_equal_ulps(a, b, UlpsEpsilon); |
148 } | 163 } |
149 | 164 |
150 int UlpsDistance(float a, float b) { | 165 int UlpsDistance(float a, float b) { |
151 if (!SkScalarIsFinite(a) || !SkScalarIsFinite(b)) { | |
152 return SK_MaxS32; | |
153 } | |
154 SkFloatIntUnion floatIntA, floatIntB; | 166 SkFloatIntUnion floatIntA, floatIntB; |
155 floatIntA.fFloat = a; | 167 floatIntA.fFloat = a; |
156 floatIntB.fFloat = b; | 168 floatIntB.fFloat = b; |
157 // Different signs means they do not match. | 169 // Different signs means they do not match. |
158 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { | 170 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { |
159 // Check for equality to make sure +0 == -0 | 171 // Check for equality to make sure +0 == -0 |
160 return a == b ? 0 : SK_MaxS32; | 172 return a == b ? 0 : SK_MaxS32; |
161 } | 173 } |
162 // Find the difference in ULPs. | 174 // Find the difference in ULPs. |
163 return SkTAbs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); | 175 return SkTAbs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 SkDEBUGPARAMS(fSegmentID(0)) | 228 SkDEBUGPARAMS(fSegmentID(0)) |
217 SkDEBUGPARAMS(fSpanID(0)) { | 229 SkDEBUGPARAMS(fSpanID(0)) { |
218 if (coincidence) { | 230 if (coincidence) { |
219 coincidence->debugSetGlobalState(this); | 231 coincidence->debugSetGlobalState(this); |
220 } | 232 } |
221 #if DEBUG_T_SECT_LOOP_COUNT | 233 #if DEBUG_T_SECT_LOOP_COUNT |
222 debugResetLoopCounts(); | 234 debugResetLoopCounts(); |
223 #endif | 235 #endif |
224 } | 236 } |
225 | 237 |
OLD | NEW |