1 /*
2  * Copyright 2015 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 #include "SkUtils.h"
9 
10 static_assert(sizeof(Sk4px) == 16, "This file uses memcpy / sk_memset32, so exact size matters.");
11 
Sk4px(SkPMColor px)12 inline Sk4px::Sk4px(SkPMColor px) {
13     sk_memset32((uint32_t*)this, px, 4);
14 }
15 
Load4(const SkPMColor px[4])16 inline Sk4px Sk4px::Load4(const SkPMColor px[4]) {
17     Sk4px px4 = Sk16b();
18     memcpy(&px4, px, 16);
19     return px4;
20 }
21 
Load2(const SkPMColor px[2])22 inline Sk4px Sk4px::Load2(const SkPMColor px[2]) {
23     Sk4px px2 = Sk16b();
24     memcpy(&px2, px, 8);
25     return px2;
26 }
27 
Load1(const SkPMColor px[1])28 inline Sk4px Sk4px::Load1(const SkPMColor px[1]) {
29     Sk4px px1 = Sk16b();
30     memcpy(&px1, px, 4);
31     return px1;
32 }
33 
store4(SkPMColor px[4])34 inline void Sk4px::store4(SkPMColor px[4]) const { memcpy(px, this, 16); }
store2(SkPMColor px[2])35 inline void Sk4px::store2(SkPMColor px[2]) const { memcpy(px, this,  8); }
store1(SkPMColor px[1])36 inline void Sk4px::store1(SkPMColor px[1]) const { memcpy(px, this,  4); }
37 
widenLo()38 inline Sk4px::Wide Sk4px::widenLo() const {
39     return Sk16h(this->kth< 0>(), this->kth< 1>(), this->kth< 2>(), this->kth< 3>(),
40                  this->kth< 4>(), this->kth< 5>(), this->kth< 6>(), this->kth< 7>(),
41                  this->kth< 8>(), this->kth< 9>(), this->kth<10>(), this->kth<11>(),
42                  this->kth<12>(), this->kth<13>(), this->kth<14>(), this->kth<15>());
43 }
44 
widenHi()45 inline Sk4px::Wide Sk4px::widenHi() const { return this->widenLo() << 8; }
46 
mulWiden(const Sk16b & other)47 inline Sk4px::Wide Sk4px::mulWiden(const Sk16b& other) const {
48     return this->widenLo() * Sk4px(other).widenLo();
49 }
50 
addNarrowHi(const Sk16h & other)51 inline Sk4px Sk4px::Wide::addNarrowHi(const Sk16h& other) const {
52     Sk4px::Wide r = (*this + other) >> 8;
53     return Sk16b(r.kth< 0>(), r.kth< 1>(), r.kth< 2>(), r.kth< 3>(),
54                  r.kth< 4>(), r.kth< 5>(), r.kth< 6>(), r.kth< 7>(),
55                  r.kth< 8>(), r.kth< 9>(), r.kth<10>(), r.kth<11>(),
56                  r.kth<12>(), r.kth<13>(), r.kth<14>(), r.kth<15>());
57 }
58 
alphas()59 inline Sk4px Sk4px::alphas() const {
60     static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
61     return Sk16b(this->kth< 3>(), this->kth< 3>(), this->kth< 3>(), this->kth< 3>(),
62                  this->kth< 7>(), this->kth< 7>(), this->kth< 7>(), this->kth< 7>(),
63                  this->kth<11>(), this->kth<11>(), this->kth<11>(), this->kth<11>(),
64                  this->kth<15>(), this->kth<15>(), this->kth<15>(), this->kth<15>());
65 }
66 
Load4Alphas(const SkAlpha a[4])67 inline Sk4px Sk4px::Load4Alphas(const SkAlpha a[4]) {
68     return Sk16b(a[0], a[0], a[0], a[0],
69                  a[1], a[1], a[1], a[1],
70                  a[2], a[2], a[2], a[2],
71                  a[3], a[3], a[3], a[3]);
72 }
73 
Load2Alphas(const SkAlpha a[2])74 inline Sk4px Sk4px::Load2Alphas(const SkAlpha a[2]) {
75     return Sk16b(a[0], a[0], a[0], a[0],
76                  a[1], a[1], a[1], a[1],
77                  0,0,0,0,
78                  0,0,0,0);
79 }
80 
zeroAlphas()81 inline Sk4px Sk4px::zeroAlphas() const {
82     static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
83     return Sk16b(this->kth< 0>(), this->kth< 1>(), this->kth< 2>(), 0,
84                  this->kth< 4>(), this->kth< 5>(), this->kth< 6>(), 0,
85                  this->kth< 8>(), this->kth< 9>(), this->kth<10>(), 0,
86                  this->kth<12>(), this->kth<13>(), this->kth<14>(), 0);
87 }
88 
zeroColors()89 inline Sk4px Sk4px::zeroColors() const {
90     static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
91     return Sk16b(0,0,0, this->kth< 3>(),
92                  0,0,0, this->kth< 7>(),
93                  0,0,0, this->kth<11>(),
94                  0,0,0, this->kth<15>());
95 }
96 
97