/src/skia/src/core/SkFDot6.h
Line | Count | Source (jump to first uncovered line) |
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 | | typedef int32_t SkFDot6; |
17 | | |
18 | | /* This uses the magic number approach suggested here: |
19 | | * http://stereopsis.com/sree/fpu2006.html and used in |
20 | | * _cairo_fixed_from_double. It does banker's rounding |
21 | | * (i.e. round to nearest even) |
22 | | */ |
23 | | inline SkFDot6 SkScalarRoundToFDot6(SkScalar x, int shift = 0) |
24 | 0 | { |
25 | 0 | union { |
26 | 0 | double fDouble; |
27 | 0 | int32_t fBits[2]; |
28 | 0 | } tmp; |
29 | 0 | int fractionalBits = 6 + shift; |
30 | 0 | double magic = (1LL << (52 - (fractionalBits))) * 1.5; |
31 | 0 |
|
32 | 0 | tmp.fDouble = SkScalarToDouble(x) + magic; |
33 | 0 | #ifdef SK_CPU_BENDIAN |
34 | 0 | return tmp.fBits[1]; |
35 | 0 | #else |
36 | 0 | return tmp.fBits[0]; |
37 | 0 | #endif |
38 | 0 | } |
39 | | |
40 | 182k | #define SK_FDot6One (64) |
41 | | #define SK_FDot6Half (32) |
42 | | |
43 | | #ifdef SK_DEBUG |
44 | 0 | inline SkFDot6 SkIntToFDot6(int x) { |
45 | 0 | SkASSERT(SkToS16(x) == x); |
46 | 0 | return x << 6; |
47 | 0 | } |
48 | | #else |
49 | 2.24M | #define SkIntToFDot6(x) ((x) << 6) |
50 | | #endif |
51 | | |
52 | 1.44M | #define SkFDot6Floor(x) ((x) >> 6) |
53 | 1.44M | #define SkFDot6Ceil(x) (((x) + 63) >> 6) |
54 | 373M | #define SkFDot6Round(x) (((x) + 32) >> 6) |
55 | | |
56 | 39.4M | #define SkFixedToFDot6(x) ((x) >> 10) |
57 | | |
58 | 126M | inline SkFixed SkFDot6ToFixed(SkFDot6 x) { |
59 | 126M | SkASSERT((SkLeftShift(x, 10) >> 10) == x); |
60 | | |
61 | 126M | return SkLeftShift(x, 10); |
62 | 126M | } |
63 | | |
64 | 35.3M | #define SkScalarToFDot6(x) (SkFDot6)((x) * 64) |
65 | 2.47M | #define SkFDot6ToScalar(x) ((SkScalar)(x) * 0.015625f) |
66 | 34.8k | #define SkFDot6ToFloat SkFDot6ToScalar |
67 | | |
68 | 83.6M | inline SkFixed SkFDot6Div(SkFDot6 a, SkFDot6 b) { |
69 | 83.6M | SkASSERT(b != 0); |
70 | | |
71 | 83.6M | if (SkTFitsIn<int16_t>(a)) { |
72 | 82.5M | return SkLeftShift(a, 16) / b; |
73 | 82.5M | } else { |
74 | 1.09M | return SkFixedDiv(a, b); |
75 | 1.09M | } |
76 | 83.6M | } |
77 | | |
78 | | #endif |