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) { | 10 static bool arguments_denormalized(float a, float b, int epsilon) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 bool AlmostPequalUlps(float a, float b) { | 95 bool AlmostPequalUlps(float a, float b) { |
96 const int UlpsEpsilon = 8; | 96 const int UlpsEpsilon = 8; |
97 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); | 97 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); |
98 } | 98 } |
99 | 99 |
100 bool AlmostDequalUlps(float a, float b) { | 100 bool AlmostDequalUlps(float a, float b) { |
101 const int UlpsEpsilon = 16; | 101 const int UlpsEpsilon = 16; |
102 return d_equal_ulps(a, b, UlpsEpsilon); | 102 return d_equal_ulps(a, b, UlpsEpsilon); |
103 } | 103 } |
104 | 104 |
| 105 bool AlmostDequalUlps(double a, double b) { |
| 106 if (SkScalarIsFinite(a) || SkScalarIsFinite(b)) { |
| 107 return AlmostDequalUlps(SkDoubleToScalar(a), SkDoubleToScalar(b)); |
| 108 } |
| 109 return fabs(a - b) / SkTMax(fabs(a), fabs(b)) < FLT_EPSILON * 16; |
| 110 } |
| 111 |
105 bool AlmostEqualUlps(float a, float b) { | 112 bool AlmostEqualUlps(float a, float b) { |
106 const int UlpsEpsilon = 16; | 113 const int UlpsEpsilon = 16; |
107 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); | 114 return equal_ulps(a, b, UlpsEpsilon, UlpsEpsilon); |
108 } | 115 } |
109 | 116 |
110 bool NotAlmostEqualUlps(float a, float b) { | 117 bool NotAlmostEqualUlps(float a, float b) { |
111 const int UlpsEpsilon = 16; | 118 const int UlpsEpsilon = 16; |
112 return not_equal_ulps(a, b, UlpsEpsilon); | 119 return not_equal_ulps(a, b, UlpsEpsilon); |
113 } | 120 } |
114 | 121 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 double SkDCubeRoot(double x) { | 191 double SkDCubeRoot(double x) { |
185 if (approximately_zero_cubed(x)) { | 192 if (approximately_zero_cubed(x)) { |
186 return 0; | 193 return 0; |
187 } | 194 } |
188 double result = halley_cbrt3d(fabs(x)); | 195 double result = halley_cbrt3d(fabs(x)); |
189 if (x < 0) { | 196 if (x < 0) { |
190 result = -result; | 197 result = -result; |
191 } | 198 } |
192 return result; | 199 return result; |
193 } | 200 } |
| 201 |
| 202 union SkDoubleLong { |
| 203 double fDouble; |
| 204 int64_t fLong; |
| 205 }; |
| 206 |
| 207 static inline int64_t SkSignBitTo2sCompliment(int64_t x) { |
| 208 if (x < 0) { |
| 209 x &= 0x7FFFFFFFFFFFFFFFL; |
| 210 x = -x; |
| 211 } |
| 212 return x; |
| 213 } |
| 214 |
| 215 |
| 216 static inline int64_t Sk2sComplimentToSignBit(int64_t x) { |
| 217 int64_t sign = x >> 63; |
| 218 // make x positive |
| 219 x = (x ^ sign) - sign; |
| 220 // set the sign bit as needed |
| 221 x |= sign << 63; |
| 222 return x; |
| 223 } |
| 224 |
| 225 static SkFloat128 SkFloat128AddMid(const SkFloat128& a, int64_t b) { |
| 226 uint64_t low = (uint64_t) b << 32; |
| 227 SkFloat128 result = a; |
| 228 result.fLow += low; |
| 229 if (a.fLow > result.fLow) { |
| 230 ++result.fHigh; |
| 231 } |
| 232 int64_t high = b >> 32; |
| 233 result.fHigh += high; |
| 234 // implementation incomplete |
| 235 return result; |
| 236 } |
| 237 |
| 238 double SkFloat128Sub(const SkFloat128& a, const SkFloat128& b) { |
| 239 SkFloat128 result = a; |
| 240 result.fLow -= b.fLow; |
| 241 if (a.fLow < result.fLow) { |
| 242 --result.fHigh; |
| 243 } |
| 244 result.fHigh -= b.fHigh; |
| 245 SkASSERT(result.fHigh == 0 || result.fHigh == -1L); |
| 246 SkASSERT(result.fHigh * result.fLow >= 0); // same sign |
| 247 if (result.fLow == 0) { |
| 248 return 0.; |
| 249 } |
| 250 int64_t uVal = abs((int64_t) result.fLow); |
| 251 while (uVal > 0) { |
| 252 uVal <<= 1; |
| 253 --result.fExp; |
| 254 } |
| 255 int64_t signBit = Sk2sComplimentToSignBit((int64_t) result.fLow); |
| 256 // implementation incomplete |
| 257 return (double) signBit; |
| 258 } |
| 259 |
| 260 // Helper to see a float as its bit pattern (w/o aliasing warnings) |
| 261 SkFloat128 SkFloat128Mul(double a, double b) { |
| 262 SkFloat128 a128, b128; |
| 263 SkDoubleLong aMantissa, bMantissa; |
| 264 aMantissa.fDouble = frexp(a, &a128.fExp); |
| 265 a128.fLow = SkSignBitTo2sCompliment(aMantissa.fLong); |
| 266 a128.fHigh = a128.fLow < 0 ? (uint64_t) -1L : 0; |
| 267 bMantissa.fDouble = frexp(b, &b128.fExp); |
| 268 b128.fLow = SkSignBitTo2sCompliment(bMantissa.fLong); |
| 269 a128.fHigh = b128.fLow < 0 ? (uint64_t) -1L : 0; |
| 270 int expDiff = a128.fExp - b128.fExp; |
| 271 SkASSERT(abs(expDiff) < 12); |
| 272 SkFloat128 result; |
| 273 if (expDiff > 0) { |
| 274 b128.fLow <<= expDiff; |
| 275 result.fExp = a128.fExp; |
| 276 } else { |
| 277 a128.fLow <<= expDiff; |
| 278 result.fExp = b128.fExp; |
| 279 } |
| 280 uint64_t aLo = a128.fLow & 0xFFFFFFFF; |
| 281 int64_t aHi = a128.fLow >> 32; |
| 282 uint64_t bLo = b128.fLow & 0xFFFFFFFF; |
| 283 int64_t bHi = b128.fLow >> 32; |
| 284 result.fLow = aLo * bLo; |
| 285 int64_t mid = aLo * bHi; |
| 286 int64_t mid2 = aHi * bLo; |
| 287 result.fHigh = aHi * bHi; |
| 288 result = SkFloat128AddMid(result, mid); |
| 289 result = SkFloat128AddMid(result, mid2); |
| 290 return result; |
| 291 } |
| 292 |
| 293 SkFloat128 SkFloat128Mul(SkFloat128& a, double b) { |
| 294 |
| 295 int bExp; |
| 296 SkDoubleLong bMantissa; |
| 297 bMantissa.fDouble = frexp(b, &bExp); |
| 298 bMantissa.fLong = SkSignBitTo2sCompliment(bMantissa.fLong); |
| 299 // implementation incomplete |
| 300 return a; |
| 301 } |
OLD | NEW |