From d280d425bd541ebbb3a5e9045913eabb3f85f04b Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Mon, 31 Oct 2016 09:20:54 -0400 Subject: 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 Commit-Queue: Mike Klein --- src/core/SkFixed15.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/core/SkFixed15.h (limited to 'src') 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 -- cgit v1.2.3