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 const int UlpsEpsilon = 16; | 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 bool AlmostEqualUlps(float A, float B) { | 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 { | 20 { |
21 // Check for equality to make sure +0 == -0 | 21 // Check for equality to make sure +0 == -0 |
22 return A == B; | 22 return A == B; |
23 } | 23 } |
24 // Find the difference in ULPs. | 24 // Find the difference in ULPs. |
25 int ulpsDiff = abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); | 25 int ulpsDiff = abs(floatIntA.fSignBitInt - floatIntB.fSignBitInt); |
26 return ulpsDiff <= UlpsEpsilon; | 26 return ulpsDiff <= epsilon; |
| 27 } |
| 28 |
| 29 bool AlmostEqualUlps(float A, float B) { |
| 30 const int UlpsEpsilon = 16; |
| 31 return equal_ulps(A, B, UlpsEpsilon); |
| 32 } |
| 33 |
| 34 bool RoughlyEqualUlps(float A, float B) { |
| 35 const int UlpsEpsilon = 256; |
| 36 return equal_ulps(A, B, UlpsEpsilon); |
27 } | 37 } |
28 | 38 |
29 // cube root approximation using bit hack for 64-bit float | 39 // cube root approximation using bit hack for 64-bit float |
30 // adapted from Kahan's cbrt | 40 // adapted from Kahan's cbrt |
31 static double cbrt_5d(double d) { | 41 static double cbrt_5d(double d) { |
32 const unsigned int B1 = 715094163; | 42 const unsigned int B1 = 715094163; |
33 double t = 0.0; | 43 double t = 0.0; |
34 unsigned int* pt = (unsigned int*) &t; | 44 unsigned int* pt = (unsigned int*) &t; |
35 unsigned int* px = (unsigned int*) &d; | 45 unsigned int* px = (unsigned int*) &d; |
36 pt[1] = px[1] / 3 + B1; | 46 pt[1] = px[1] / 3 + B1; |
(...skipping 18 matching lines...) Expand all Loading... |
55 double SkDCubeRoot(double x) { | 65 double SkDCubeRoot(double x) { |
56 if (approximately_zero_cubed(x)) { | 66 if (approximately_zero_cubed(x)) { |
57 return 0; | 67 return 0; |
58 } | 68 } |
59 double result = halley_cbrt3d(fabs(x)); | 69 double result = halley_cbrt3d(fabs(x)); |
60 if (x < 0) { | 70 if (x < 0) { |
61 result = -result; | 71 result = -result; |
62 } | 72 } |
63 return result; | 73 return result; |
64 } | 74 } |
OLD | NEW |