Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: src/core/SkFDot6.h

Issue 270263005: Use even rounding for better results when converting from scalar to fdot6 (Closed) Base URL: https://skia.googlesource.com/skia.git@neon
Patch Set: Protect behind an ifdef Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkEdge.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkFDot6_DEFINED 10 #ifndef SkFDot6_DEFINED
11 #define SkFDot6_DEFINED 11 #define SkFDot6_DEFINED
12 12
13 #include "SkScalar.h" 13 #include "SkScalar.h"
14 #include "SkMath.h" 14 #include "SkMath.h"
15 15
16 typedef int32_t SkFDot6; 16 typedef int32_t SkFDot6;
17 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 {
25 union {
26 double fDouble;
27 int32_t fBits[2];
28 } tmp;
29 int fractionalBits = 6 + shift;
30 double magic = (1LL << (52 - (fractionalBits))) * 1.5;
31
32 tmp.fDouble = SkScalarToDouble(x) + magic;
33 #ifdef SK_CPU_BENDIAN
34 return tmp.fBits[1];
35 #else
36 return tmp.fBits[0];
37 #endif
38 }
39
18 #define SK_FDot6One (64) 40 #define SK_FDot6One (64)
19 #define SK_FDot6Half (32) 41 #define SK_FDot6Half (32)
20 42
21 #ifdef SK_DEBUG 43 #ifdef SK_DEBUG
22 inline SkFDot6 SkIntToFDot6(S16CPU x) { 44 inline SkFDot6 SkIntToFDot6(S16CPU x) {
23 SkASSERT(SkToS16(x) == x); 45 SkASSERT(SkToS16(x) == x);
24 return x << 6; 46 return x << 6;
25 } 47 }
26 #else 48 #else
27 #define SkIntToFDot6(x) ((x) << 6) 49 #define SkIntToFDot6(x) ((x) << 6)
(...skipping 18 matching lines...) Expand all
46 SkASSERT(b != 0); 68 SkASSERT(b != 0);
47 69
48 if (a == (int16_t)a) { 70 if (a == (int16_t)a) {
49 return (a << 16) / b; 71 return (a << 16) / b;
50 } else { 72 } else {
51 return SkFixedDiv(a, b); 73 return SkFixedDiv(a, b);
52 } 74 }
53 } 75 }
54 76
55 #endif 77 #endif
OLDNEW
« no previous file with comments | « src/core/SkEdge.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698