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

Unified 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, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkEdge.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkFDot6.h
diff --git a/src/core/SkFDot6.h b/src/core/SkFDot6.h
index 5a0ec57f5954d7a0c3c5abfee64f39418e849c75..3da753da413180de77c162092c4fc98f64d02239 100644
--- a/src/core/SkFDot6.h
+++ b/src/core/SkFDot6.h
@@ -15,6 +15,28 @@
typedef int32_t SkFDot6;
+/* This uses the magic number approach suggested here:
+ * http://stereopsis.com/sree/fpu2006.html and used in
+ * _cairo_fixed_from_double. It does banker's rounding
+ * (i.e. round to nearest even)
+ */
+inline SkFDot6 SkScalarRoundToFDot6(SkScalar x, int shift = 0)
+{
+ union {
+ double fDouble;
+ int32_t fBits[2];
+ } tmp;
+ int fractionalBits = 6 + shift;
+ double magic = (1LL << (52 - (fractionalBits))) * 1.5;
+
+ tmp.fDouble = SkScalarToDouble(x) + magic;
+#ifdef SK_CPU_BENDIAN
+ return tmp.fBits[1];
+#else
+ return tmp.fBits[0];
+#endif
+}
+
#define SK_FDot6One (64)
#define SK_FDot6Half (32)
« 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