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>
This commit is contained in:
parent
70295ea220
commit
d280d425bd
@ -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",
|
||||
|
69
src/core/SkFixed15.h
Normal file
69
src/core/SkFixed15.h
Normal file
@ -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
|
29
tests/SkFixed15Test.cpp
Normal file
29
tests/SkFixed15Test.cpp
Normal file
@ -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)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user