Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkFixed15_DEFINED
      9 #define SkFixed15_DEFINED
     10 
     11 #include "SkTypes.h"
     12 
     13 // SkFixed15 is a fixed point value that represents values in [0,1] as [0x0000, 0x8000].
     14 // This mapping allows us to implement most operations in tightly packed 16-bit SIMD,
     15 // most notably multiplying using Q15 multiplication instructions (and a little fixup).
     16 
     17 class SkFixed15 {
     18 public:
     19     SkFixed15() = default;
     20 
     21     SkFixed15(float val) : fVal(val * 32768) { SkASSERT(0.0f <= val && val <= 1.0f); }
     22     explicit operator float() const { return fVal * (1/32768.0f); }
     23 
     24     static SkFixed15 Load(uint16_t val) {
     25         SkASSERT(val <= 32768);
     26         return val;
     27     }
     28     uint16_t store() const { return fVal; }
     29 
     30     static SkFixed15 FromU8(uint8_t val) {
     31         return val*128 + (val>>1)  // 32768/255 == 128.50196..., which is very close to 128 + 0.5.
     32              + ((val+1)>>8);       // All val but 255 are correct.  +1 if val == 255 to get 32768.
     33     }
     34 
     35     uint8_t to_u8() const {
     36         // FromU8() and to_u8() roundtrip all bytes.
     37         // There is still much room to tweak this towards the ideal, a rounding scale by 255/32768.
     38         return (fVal - (fVal>>8))>>7;
     39     }
     40 
     41     SkFixed15 operator +(SkFixed15 o) const { return fVal + o.fVal; }
     42     SkFixed15 operator -(SkFixed15 o) const { return fVal - o.fVal; }
     43     SkFixed15 operator *(SkFixed15 o) const { return (fVal * o.fVal + (1<<14)) >> 15; }
     44     SkFixed15 operator<<(int bits) const { return fVal << bits; }
     45     SkFixed15 operator>>(int bits) const { return fVal >> bits; }
     46 
     47     SkFixed15& operator +=(SkFixed15 o) { return (*this = *this + o); }
     48     SkFixed15& operator -=(SkFixed15 o) { return (*this = *this - o); }
     49     SkFixed15& operator *=(SkFixed15 o) { return (*this = *this * o); }
     50     SkFixed15& operator<<=(int bits) { return (*this = *this << bits); }
     51     SkFixed15& operator>>=(int bits) { return (*this = *this >> bits); }
     52 
     53     bool operator==(SkFixed15 o) const { return fVal == o.fVal; }
     54     bool operator!=(SkFixed15 o) const { return fVal != o.fVal; }
     55     bool operator<=(SkFixed15 o) const { return fVal <= o.fVal; }
     56     bool operator>=(SkFixed15 o) const { return fVal >= o.fVal; }
     57     bool operator< (SkFixed15 o) const { return fVal <  o.fVal; }
     58     bool operator> (SkFixed15 o) const { return fVal >  o.fVal; }
     59 
     60 private:
     61     SkFixed15(int val) : fVal(val) {}
     62 
     63     uint16_t fVal;
     64 };
     65 
     66 // Notes
     67 //  - SSSE3+ multiply is _mm_abs_epi16(_mm_mulhrs_epi16(x, y));
     68 //  - NEON multipy is vsraq_n_u16(vabsq_s16(vqrdmulhq_s16(x,y)),
     69 //                                vandq_s16(x,y), 15);
     70 //  - Conversion to and from float can be done manually with bit masks and float add/subtract,
     71 //    rather than the naive version here involving int<->float conversion and float multiply.
     72 //  - On x86, conversion to float is _mm_sub_ps(_mm_unpacklo_epi16(x, _mm_set1_epi16(0x4380)),
     73 //                                              _mm_set1_ps(256.0f)).  // 0x43800000
     74 //  - On ARM, we can use the vcvtq_n_f32_u32(vmovl_u16(x), 15) to convert to float,
     75 //    and vcvtq_n_u32_f32(..., 15) for the other way around.
     76 
     77 #endif//SkFixed15_DEFINED
     78