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 | 10 |
11 | 11 |
12 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ | 12 // from http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-num
bers-2012-edition/ |
13 // FIXME: move to SkFloatBits.h | 13 // FIXME: move to SkFloatBits.h |
14 static bool equal_ulps(float A, float B, int epsilon) { | 14 static bool equal_ulps(float a, float b, int epsilon) { |
15 SkFloatIntUnion floatIntA, floatIntB; | 15 SkFloatIntUnion floatIntA, floatIntB; |
16 floatIntA.fFloat = A; | 16 floatIntA.fFloat = a; |
17 floatIntB.fFloat = B; | 17 floatIntB.fFloat = b; |
18 // Different signs means they do not match. | 18 // Different signs means they do not match. |
19 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { | 19 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { |
20 // Check for equality to make sure +0 == -0 | 20 // Check for equality to make sure +0 == -0 |
21 return A == B; | 21 return a == b; |
22 } | 22 } |
23 // Find the difference in ULPs. | 23 // Find the difference in ULPs. |
24 int ulpsDiff = abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); | 24 int ulpsDiff = abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); |
25 return ulpsDiff <= epsilon; | 25 return ulpsDiff <= epsilon; |
26 } | 26 } |
27 | 27 |
28 static bool less_ulps(float A, float B, int epsilon) { | 28 static bool less_ulps(float a, float b, int epsilon) { |
29 SkFloatIntUnion floatIntA, floatIntB; | 29 SkFloatIntUnion floatIntA, floatIntB; |
30 floatIntA.fFloat = A; | 30 floatIntA.fFloat = a; |
31 floatIntB.fFloat = B; | 31 floatIntB.fFloat = b; |
32 // Check different signs with float epsilon since we only care if they're bo
th close to 0. | 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)) { | 33 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { |
34 return A <= B + FLT_EPSILON * epsilon; | 34 return a <= b + FLT_EPSILON * epsilon; |
35 } | 35 } |
36 // Find the difference in ULPs. | 36 // Find the difference in ULPs. |
37 return floatIntA.fSignBitInt <= floatIntB.fSignBitInt + epsilon; | 37 return floatIntA.fSignBitInt <= floatIntB.fSignBitInt + epsilon; |
38 } | 38 } |
39 | 39 |
40 bool AlmostEqualUlps(float A, float B) { | 40 bool AlmostEqualUlps(float a, float b) { |
41 const int UlpsEpsilon = 16; | 41 const int UlpsEpsilon = 16; |
42 return equal_ulps(A, B, UlpsEpsilon); | 42 return equal_ulps(a, b, UlpsEpsilon); |
43 } | 43 } |
44 | 44 |
45 bool RoughlyEqualUlps(float A, float B) { | 45 bool RoughlyEqualUlps(float a, float b) { |
46 const int UlpsEpsilon = 256; | 46 const int UlpsEpsilon = 256; |
47 return equal_ulps(A, B, UlpsEpsilon); | 47 return equal_ulps(a, b, UlpsEpsilon); |
48 } | 48 } |
49 | 49 |
50 bool AlmostBetweenUlps(float a, float b, float c) { | 50 bool AlmostBetweenUlps(float a, float b, float c) { |
51 const int UlpsEpsilon = 1; | 51 const int UlpsEpsilon = 1; |
52 return a <= c ? less_ulps(a, b, UlpsEpsilon) && less_ulps(b, c, UlpsEpsilon) | 52 return a <= c ? less_ulps(a, b, UlpsEpsilon) && less_ulps(b, c, UlpsEpsilon) |
53 : less_ulps(b, a, UlpsEpsilon) && less_ulps(c, b, UlpsEpsilon); | 53 : less_ulps(b, a, UlpsEpsilon) && less_ulps(c, b, UlpsEpsilon); |
54 } | 54 } |
55 | 55 |
| 56 int UlpsDistance(float a, float b) { |
| 57 SkFloatIntUnion floatIntA, floatIntB; |
| 58 floatIntA.fFloat = a; |
| 59 floatIntB.fFloat = b; |
| 60 // Different signs means they do not match. |
| 61 if ((floatIntA.fSignBitInt < 0) != (floatIntB.fSignBitInt < 0)) { |
| 62 // Check for equality to make sure +0 == -0 |
| 63 return a == b ? 0 : SK_MaxS32; |
| 64 } |
| 65 // Find the difference in ULPs. |
| 66 return abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); |
| 67 } |
| 68 |
56 // cube root approximation using bit hack for 64-bit float | 69 // cube root approximation using bit hack for 64-bit float |
57 // adapted from Kahan's cbrt | 70 // adapted from Kahan's cbrt |
58 static double cbrt_5d(double d) { | 71 static double cbrt_5d(double d) { |
59 const unsigned int B1 = 715094163; | 72 const unsigned int B1 = 715094163; |
60 double t = 0.0; | 73 double t = 0.0; |
61 unsigned int* pt = (unsigned int*) &t; | 74 unsigned int* pt = (unsigned int*) &t; |
62 unsigned int* px = (unsigned int*) &d; | 75 unsigned int* px = (unsigned int*) &d; |
63 pt[1] = px[1] / 3 + B1; | 76 pt[1] = px[1] / 3 + B1; |
64 return t; | 77 return t; |
65 } | 78 } |
(...skipping 16 matching lines...) Expand all Loading... |
82 double SkDCubeRoot(double x) { | 95 double SkDCubeRoot(double x) { |
83 if (approximately_zero_cubed(x)) { | 96 if (approximately_zero_cubed(x)) { |
84 return 0; | 97 return 0; |
85 } | 98 } |
86 double result = halley_cbrt3d(fabs(x)); | 99 double result = halley_cbrt3d(fabs(x)); |
87 if (x < 0) { | 100 if (x < 0) { |
88 result = -result; | 101 result = -result; |
89 } | 102 } |
90 return result; | 103 return result; |
91 } | 104 } |
OLD | NEW |