| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkFDot6_DEFINED |
| 9 |
#define SkFDot6_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkScalar.h" |
| 12 |
#include "include/private/base/SkFixed.h" |
| 13 |
#include "include/private/base/SkMath.h" |
| 14 |
#include "include/private/base/SkTo.h" |
| 15 |
|
| 16 |
#include <limits> |
| 17 |
|
| 18 |
typedef int32_t SkFDot6; |
| 19 |
|
| 20 |
/* This uses the magic number approach suggested here: |
| 21 |
* http://stereopsis.com/sree/fpu2006.html and used in |
| 22 |
* _cairo_fixed_from_double. It does banker's rounding |
| 23 |
* (i.e. round to nearest even) |
| 24 |
*/ |
| 25 |
inline SkFDot6 SkScalarRoundToFDot6(SkScalar x, int shift) |
| 26 |
{ |
| 27 |
union { |
| 28 |
double fDouble; |
| 29 |
int32_t fBits[2]; |
| 30 |
} tmp; |
| 31 |
int fractionalBits = 6 + shift; |
| 32 |
double magic = (1LL << (52 - (fractionalBits))) * 1.5; |
| 33 |
|
| 34 |
tmp.fDouble = SkScalarToDouble(x) + magic; |
| 35 |
#ifdef SK_CPU_BENDIAN |
| 36 |
return tmp.fBits[1]; |
| 37 |
#else |
| 38 |
return tmp.fBits[0]; |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
#define SK_FDot6One (64) |
| 43 |
#define SK_FDot6Half (32) |
| 44 |
|
| 45 |
#ifdef SK_DEBUG |
| 46 |
constexpr inline SkFDot6 SkIntToFDot6(int x) { |
| 47 |
SkASSERT( (std::numeric_limits<SkFDot6>::min() >> 6) <= x && |
| 48 |
x <= (std::numeric_limits<SkFDot6>::max() >> 6)); |
| 49 |
return x << 6; |
| 50 |
} |
| 51 |
#else |
| 52 |
#define SkIntToFDot6(x) ((x) << 6) |
| 53 |
#endif |
| 54 |
|
| 55 |
#define SkFDot6Floor(x) ((x) >> 6) |
| 56 |
#define SkFDot6Ceil(x) (((x) + 63) >> 6) |
| 57 |
#define SkFDot6Round(x) (((x) + SK_FDot6Half) >> 6) |
| 58 |
|
| 59 |
#define SkFixedToFDot6(x) ((x) >> 10) |
| 60 |
|
| 61 |
inline SkFixed SkFDot6ToFixed(SkFDot6 x) { |
| 62 |
SkASSERT((SkLeftShift(x, 10) >> 10) == x); |
| 63 |
|
| 64 |
return SkLeftShift(x, 10); |
| 65 |
} |
| 66 |
|
| 67 |
#define SkFloatToFDot6(x) (SkFDot6)((x) * SK_FDot6One) |
| 68 |
#define SkScalarToFDot6 SkFloatToFDot6 |
| 69 |
#define SkFDot6ToFloat(x) ((float)(x) * 0.015625f) |
| 70 |
#define SkFDot6ToScalar SkFDot6ToFloat |
| 71 |
|
| 72 |
inline SkFixed SkFDot6Div(SkFDot6 a, SkFDot6 b) { |
| 73 |
SkASSERT(b != 0); |
| 74 |
|
| 75 |
if (SkTFitsIn<int16_t>(a)) { |
| 76 |
return SkLeftShift(a, 16) / b; |
| 77 |
} else { |
| 78 |
return SkFixedDiv(a, b); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#endif |