aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mike Klein <mtklein@chromium.org>2016-10-31 09:20:54 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2016-10-31 13:41:39 +0000
commitd280d425bd541ebbb3a5e9045913eabb3f85f04b (patch)
treebe55c6adcc9e2a1362e01ee503750ff2c26dfd14
parent70295ea220e25ede0f967ad0168c51a8e3fd444d (diff)
SkFixed15
BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3981 Change-Id: I811b22004e8e2d5b7135f574caea296ffdff7011 Reviewed-on: https://skia-review.googlesource.com/3981 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Klein <mtklein@chromium.org>
-rw-r--r--gn/tests.gni1
-rw-r--r--src/core/SkFixed15.h69
-rw-r--r--tests/SkFixed15Test.cpp29
3 files changed, 99 insertions, 0 deletions
diff --git a/gn/tests.gni b/gn/tests.gni
index 790237baf6..6de39b0c11 100644
--- a/gn/tests.gni
+++ b/gn/tests.gni
@@ -188,6 +188,7 @@ tests_sources = [
"$_tests/skbug5221.cpp",
"$_tests/SkColor4fTest.cpp",
"$_tests/SkDOMTest.cpp",
+ "$_tests/SkFixed15Test.cpp",
"$_tests/SkImageTest.cpp",
"$_tests/SkLinearBitmapPipelineTest.cpp",
"$_tests/SkLiteDLTest.cpp",
diff --git a/src/core/SkFixed15.h b/src/core/SkFixed15.h
new file mode 100644
index 0000000000..69502434bf
--- /dev/null
+++ b/src/core/SkFixed15.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkFixed15_DEFINED
+#define SkFixed15_DEFINED
+
+#include "SkTypes.h"
+
+// SkFixed15 is a fixed point value that represents values in [0,1] as [0x0000, 0x8000].
+// This mapping allows us to implement most operations in tightly packed 16-bit SIMD,
+// most notably multiplying using Q15 multiplication instructions (and a little fixup).
+
+class SkFixed15 {
+public:
+ SkFixed15() = default;
+
+ SkFixed15(float val) : fVal(val * 32768) { SkASSERT(0.0f <= val && val <= 1.0f); }
+ explicit operator float() const { return fVal * (1/32768.0f); }
+
+ static SkFixed15 Load(uint16_t val) {
+ SkASSERT(val <= 32768);
+ return val;
+ }
+ uint16_t store() const { return fVal; }
+
+ static SkFixed15 FromU8(uint8_t val) {
+ return val*128 + (val>>1) // 32768/255 == 128.50196..., which is very close to 128 + 0.5.
+ + ((val+1)>>8); // All val but 255 are correct. +1 if val == 255 to get 32768.
+ }
+
+ SkFixed15 operator +(SkFixed15 o) const { return fVal + o.fVal; }
+ SkFixed15 operator -(SkFixed15 o) const { return fVal - o.fVal; }
+ SkFixed15 operator *(SkFixed15 o) const { return (fVal * o.fVal + (1<<14)) >> 15; }
+ SkFixed15 operator<<(int bits) const { return fVal << bits; }
+ SkFixed15 operator>>(int bits) const { return fVal >> bits; }
+
+ SkFixed15& operator +=(SkFixed15 o) { return (*this = *this + o); }
+ SkFixed15& operator -=(SkFixed15 o) { return (*this = *this - o); }
+ SkFixed15& operator *=(SkFixed15 o) { return (*this = *this * o); }
+ SkFixed15& operator<<=(int bits) { return (*this = *this << bits); }
+ SkFixed15& operator>>=(int bits) { return (*this = *this >> bits); }
+
+ bool operator==(SkFixed15 o) const { return fVal == o.fVal; }
+ bool operator!=(SkFixed15 o) const { return fVal != o.fVal; }
+ bool operator<=(SkFixed15 o) const { return fVal <= o.fVal; }
+ bool operator>=(SkFixed15 o) const { return fVal >= o.fVal; }
+ bool operator< (SkFixed15 o) const { return fVal < o.fVal; }
+ bool operator> (SkFixed15 o) const { return fVal > o.fVal; }
+
+private:
+ SkFixed15(int val) : fVal(val) {}
+
+ uint16_t fVal;
+};
+
+// Notes
+// - SSSE3+ multiply is _mm_abs_epi16(_mm_mulhrs_epi16(x, y));
+// - NEON multipy is vsraq_n_u16(vabsq_s16(vqrdmulhq_s16(x,y)),
+// vandq_s16(x,y), 15);
+// - Conversion to and from float can be done manually with bit masks and float add/subtract,
+// rather than the naive version here involving int<->float conversion and float multiply.
+// - On ARM, we can alternatively use the vcvtq_n_f32_u32(vmovl_u16(x), 15) to convert 4 at a
+// time to float, and vcvtq_n_u32_f32(..., 15) for the other way around.
+
+#endif//SkFixed15_DEFINED
diff --git a/tests/SkFixed15Test.cpp b/tests/SkFixed15Test.cpp
new file mode 100644
index 0000000000..e2108d5604
--- /dev/null
+++ b/tests/SkFixed15Test.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Test.h"
+#include "SkFixed15.h"
+
+DEF_TEST(SkFixed15, r) {
+ // For all v, v*0 == 0, v*1 == v.
+ for (uint16_t bits = 0; bits <= 32768; bits++) {
+ auto v = SkFixed15::Load(bits);
+ REPORTER_ASSERT(r, v * 0.0f == 0.0f);
+ REPORTER_ASSERT(r, v * 1.0f == v);
+ }
+
+ // Division and multiplication by powers of 2 is exact.
+ SkFixed15 v = 1.0f;
+ REPORTER_ASSERT(r, (v>>1) == 0.50f);
+ REPORTER_ASSERT(r, (v>>2) == 0.25f);
+ REPORTER_ASSERT(r, (v>>2<<1) == 0.50f);
+
+ // FromU8() should be just as good as going through float.
+ for (int x = 0; x < 256; x++) {
+ REPORTER_ASSERT(r, SkFixed15::FromU8(x) == SkFixed15(x * (1/255.0f)));
+ }
+}