mtklein | 3e490b7 | 2015-03-20 06:33:02 -0700 | [diff] [blame] | 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 | #ifndef SkNx_DEFINED |
| 9 | #define SkNx_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkScalar.h" |
| 12 | #include "include/core/SkTypes.h" |
| 13 | #include "include/private/SkSafe_math.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 14 | |
| 15 | #include <algorithm> |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 16 | #include <limits> |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 17 | #include <type_traits> |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 18 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 19 | // Every single SkNx method wants to be fully inlined. (We know better than MSVC). |
| 20 | #define AI SK_ALWAYS_INLINE |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 21 | |
Mike Klein | c33d614 | 2018-12-12 08:47:54 -0500 | [diff] [blame] | 22 | namespace { // NOLINT(google-build-namespaces) |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 23 | |
| 24 | // The default SkNx<N,T> just proxies down to a pair of SkNx<N/2, T>. |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 25 | template <int N, typename T> |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 26 | struct SkNx { |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 27 | typedef SkNx<N/2, T> Half; |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 28 | |
| 29 | Half fLo, fHi; |
| 30 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 31 | AI SkNx() = default; |
| 32 | AI SkNx(const Half& lo, const Half& hi) : fLo(lo), fHi(hi) {} |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 33 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 34 | AI SkNx(T v) : fLo(v), fHi(v) {} |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 35 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 36 | AI SkNx(T a, T b) : fLo(a) , fHi(b) { static_assert(N==2, ""); } |
| 37 | AI SkNx(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) { static_assert(N==4, ""); } |
| 38 | AI SkNx(T a, T b, T c, T d, T e, T f, T g, T h) : fLo(a,b,c,d), fHi(e,f,g,h) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 39 | static_assert(N==8, ""); |
| 40 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 41 | AI SkNx(T a, T b, T c, T d, T e, T f, T g, T h, |
| 42 | T i, T j, T k, T l, T m, T n, T o, T p) |
| 43 | : fLo(a,b,c,d, e,f,g,h), fHi(i,j,k,l, m,n,o,p) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 44 | static_assert(N==16, ""); |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 47 | AI T operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 48 | SkASSERT(0 <= k && k < N); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 49 | return k < N/2 ? fLo[k] : fHi[k-N/2]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 52 | AI static SkNx Load(const void* vptr) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 53 | auto ptr = (const char*)vptr; |
| 54 | return { Half::Load(ptr), Half::Load(ptr + N/2*sizeof(T)) }; |
| 55 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 56 | AI void store(void* vptr) const { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 57 | auto ptr = (char*)vptr; |
| 58 | fLo.store(ptr); |
| 59 | fHi.store(ptr + N/2*sizeof(T)); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 62 | AI static void Load4(const void* vptr, SkNx* a, SkNx* b, SkNx* c, SkNx* d) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 63 | auto ptr = (const char*)vptr; |
| 64 | Half al, bl, cl, dl, |
| 65 | ah, bh, ch, dh; |
| 66 | Half::Load4(ptr , &al, &bl, &cl, &dl); |
| 67 | Half::Load4(ptr + 4*N/2*sizeof(T), &ah, &bh, &ch, &dh); |
| 68 | *a = SkNx{al, ah}; |
| 69 | *b = SkNx{bl, bh}; |
| 70 | *c = SkNx{cl, ch}; |
| 71 | *d = SkNx{dl, dh}; |
| 72 | } |
Matt Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 73 | AI static void Load3(const void* vptr, SkNx* a, SkNx* b, SkNx* c) { |
| 74 | auto ptr = (const char*)vptr; |
| 75 | Half al, bl, cl, |
| 76 | ah, bh, ch; |
| 77 | Half::Load3(ptr , &al, &bl, &cl); |
| 78 | Half::Load3(ptr + 3*N/2*sizeof(T), &ah, &bh, &ch); |
| 79 | *a = SkNx{al, ah}; |
| 80 | *b = SkNx{bl, bh}; |
| 81 | *c = SkNx{cl, ch}; |
| 82 | } |
Mike Klein | 213d821 | 2017-11-30 12:07:20 -0500 | [diff] [blame] | 83 | AI static void Load2(const void* vptr, SkNx* a, SkNx* b) { |
| 84 | auto ptr = (const char*)vptr; |
| 85 | Half al, bl, |
| 86 | ah, bh; |
| 87 | Half::Load2(ptr , &al, &bl); |
| 88 | Half::Load2(ptr + 2*N/2*sizeof(T), &ah, &bh); |
| 89 | *a = SkNx{al, ah}; |
| 90 | *b = SkNx{bl, bh}; |
| 91 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 92 | AI static void Store4(void* vptr, const SkNx& a, const SkNx& b, const SkNx& c, const SkNx& d) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 93 | auto ptr = (char*)vptr; |
| 94 | Half::Store4(ptr, a.fLo, b.fLo, c.fLo, d.fLo); |
| 95 | Half::Store4(ptr + 4*N/2*sizeof(T), a.fHi, b.fHi, c.fHi, d.fHi); |
| 96 | } |
Chris Dalton | 0cb7587 | 2017-12-01 13:23:05 -0700 | [diff] [blame] | 97 | AI static void Store3(void* vptr, const SkNx& a, const SkNx& b, const SkNx& c) { |
| 98 | auto ptr = (char*)vptr; |
| 99 | Half::Store3(ptr, a.fLo, b.fLo, c.fLo); |
| 100 | Half::Store3(ptr + 3*N/2*sizeof(T), a.fHi, b.fHi, c.fHi); |
| 101 | } |
Chris Dalton | 42f02aa | 2018-04-08 23:58:43 -0600 | [diff] [blame] | 102 | AI static void Store2(void* vptr, const SkNx& a, const SkNx& b) { |
| 103 | auto ptr = (char*)vptr; |
| 104 | Half::Store2(ptr, a.fLo, b.fLo); |
| 105 | Half::Store2(ptr + 2*N/2*sizeof(T), a.fHi, b.fHi); |
| 106 | } |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 107 | |
Mike Klein | bf45c70 | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 108 | AI T min() const { return SkTMin(fLo.min(), fHi.min()); } |
| 109 | AI T max() const { return SkTMax(fLo.max(), fHi.max()); } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 110 | AI bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
| 111 | AI bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 112 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 113 | AI SkNx abs() const { return { fLo. abs(), fHi. abs() }; } |
| 114 | AI SkNx sqrt() const { return { fLo. sqrt(), fHi. sqrt() }; } |
| 115 | AI SkNx rsqrt() const { return { fLo. rsqrt(), fHi. rsqrt() }; } |
| 116 | AI SkNx floor() const { return { fLo. floor(), fHi. floor() }; } |
| 117 | AI SkNx invert() const { return { fLo.invert(), fHi.invert() }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 118 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 119 | AI SkNx operator!() const { return { !fLo, !fHi }; } |
| 120 | AI SkNx operator-() const { return { -fLo, -fHi }; } |
| 121 | AI SkNx operator~() const { return { ~fLo, ~fHi }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 122 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 123 | AI SkNx operator<<(int bits) const { return { fLo << bits, fHi << bits }; } |
| 124 | AI SkNx operator>>(int bits) const { return { fLo >> bits, fHi >> bits }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 125 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 126 | AI SkNx operator+(const SkNx& y) const { return { fLo + y.fLo, fHi + y.fHi }; } |
| 127 | AI SkNx operator-(const SkNx& y) const { return { fLo - y.fLo, fHi - y.fHi }; } |
| 128 | AI SkNx operator*(const SkNx& y) const { return { fLo * y.fLo, fHi * y.fHi }; } |
| 129 | AI SkNx operator/(const SkNx& y) const { return { fLo / y.fLo, fHi / y.fHi }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 130 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 131 | AI SkNx operator&(const SkNx& y) const { return { fLo & y.fLo, fHi & y.fHi }; } |
| 132 | AI SkNx operator|(const SkNx& y) const { return { fLo | y.fLo, fHi | y.fHi }; } |
| 133 | AI SkNx operator^(const SkNx& y) const { return { fLo ^ y.fLo, fHi ^ y.fHi }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 134 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 135 | AI SkNx operator==(const SkNx& y) const { return { fLo == y.fLo, fHi == y.fHi }; } |
| 136 | AI SkNx operator!=(const SkNx& y) const { return { fLo != y.fLo, fHi != y.fHi }; } |
| 137 | AI SkNx operator<=(const SkNx& y) const { return { fLo <= y.fLo, fHi <= y.fHi }; } |
| 138 | AI SkNx operator>=(const SkNx& y) const { return { fLo >= y.fLo, fHi >= y.fHi }; } |
| 139 | AI SkNx operator< (const SkNx& y) const { return { fLo < y.fLo, fHi < y.fHi }; } |
| 140 | AI SkNx operator> (const SkNx& y) const { return { fLo > y.fLo, fHi > y.fHi }; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 141 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 142 | AI SkNx saturatedAdd(const SkNx& y) const { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 143 | return { fLo.saturatedAdd(y.fLo), fHi.saturatedAdd(y.fHi) }; |
| 144 | } |
Herb Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 145 | |
| 146 | AI SkNx mulHi(const SkNx& m) const { |
| 147 | return { fLo.mulHi(m.fLo), fHi.mulHi(m.fHi) }; |
| 148 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 149 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 150 | return { fLo.thenElse(t.fLo, e.fLo), fHi.thenElse(t.fHi, e.fHi) }; |
| 151 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 152 | AI static SkNx Min(const SkNx& x, const SkNx& y) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 153 | return { Half::Min(x.fLo, y.fLo), Half::Min(x.fHi, y.fHi) }; |
| 154 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 155 | AI static SkNx Max(const SkNx& x, const SkNx& y) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 156 | return { Half::Max(x.fLo, y.fLo), Half::Max(x.fHi, y.fHi) }; |
| 157 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 160 | // The N -> N/2 recursion bottoms out at N == 1, a scalar value. |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 161 | template <typename T> |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 162 | struct SkNx<1,T> { |
| 163 | T fVal; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 164 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 165 | AI SkNx() = default; |
| 166 | AI SkNx(T v) : fVal(v) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 167 | |
reed | 1264472 | 2016-07-12 09:12:30 -0700 | [diff] [blame] | 168 | // Android complains against unused parameters, so we guard it |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 169 | AI T operator[](int SkDEBUGCODE(k)) const { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 170 | SkASSERT(k == 0); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 171 | return fVal; |
| 172 | } |
| 173 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 174 | AI static SkNx Load(const void* ptr) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 175 | SkNx v; |
| 176 | memcpy(&v, ptr, sizeof(T)); |
| 177 | return v; |
| 178 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 179 | AI void store(void* ptr) const { memcpy(ptr, &fVal, sizeof(T)); } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 180 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 181 | AI static void Load4(const void* vptr, SkNx* a, SkNx* b, SkNx* c, SkNx* d) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 182 | auto ptr = (const char*)vptr; |
| 183 | *a = Load(ptr + 0*sizeof(T)); |
| 184 | *b = Load(ptr + 1*sizeof(T)); |
| 185 | *c = Load(ptr + 2*sizeof(T)); |
| 186 | *d = Load(ptr + 3*sizeof(T)); |
| 187 | } |
Matt Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 188 | AI static void Load3(const void* vptr, SkNx* a, SkNx* b, SkNx* c) { |
| 189 | auto ptr = (const char*)vptr; |
| 190 | *a = Load(ptr + 0*sizeof(T)); |
| 191 | *b = Load(ptr + 1*sizeof(T)); |
| 192 | *c = Load(ptr + 2*sizeof(T)); |
| 193 | } |
Mike Klein | 213d821 | 2017-11-30 12:07:20 -0500 | [diff] [blame] | 194 | AI static void Load2(const void* vptr, SkNx* a, SkNx* b) { |
| 195 | auto ptr = (const char*)vptr; |
| 196 | *a = Load(ptr + 0*sizeof(T)); |
| 197 | *b = Load(ptr + 1*sizeof(T)); |
| 198 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 199 | AI static void Store4(void* vptr, const SkNx& a, const SkNx& b, const SkNx& c, const SkNx& d) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 200 | auto ptr = (char*)vptr; |
| 201 | a.store(ptr + 0*sizeof(T)); |
| 202 | b.store(ptr + 1*sizeof(T)); |
| 203 | c.store(ptr + 2*sizeof(T)); |
| 204 | d.store(ptr + 3*sizeof(T)); |
| 205 | } |
Chris Dalton | 0cb7587 | 2017-12-01 13:23:05 -0700 | [diff] [blame] | 206 | AI static void Store3(void* vptr, const SkNx& a, const SkNx& b, const SkNx& c) { |
| 207 | auto ptr = (char*)vptr; |
| 208 | a.store(ptr + 0*sizeof(T)); |
| 209 | b.store(ptr + 1*sizeof(T)); |
| 210 | c.store(ptr + 2*sizeof(T)); |
| 211 | } |
Chris Dalton | 42f02aa | 2018-04-08 23:58:43 -0600 | [diff] [blame] | 212 | AI static void Store2(void* vptr, const SkNx& a, const SkNx& b) { |
| 213 | auto ptr = (char*)vptr; |
| 214 | a.store(ptr + 0*sizeof(T)); |
| 215 | b.store(ptr + 1*sizeof(T)); |
| 216 | } |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 217 | |
Chris Dalton | e3fda93 | 2018-04-11 13:18:09 -0600 | [diff] [blame] | 218 | AI T min() const { return fVal; } |
| 219 | AI T max() const { return fVal; } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 220 | AI bool anyTrue() const { return fVal != 0; } |
| 221 | AI bool allTrue() const { return fVal != 0; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 222 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 223 | AI SkNx abs() const { return Abs(fVal); } |
| 224 | AI SkNx sqrt() const { return Sqrt(fVal); } |
| 225 | AI SkNx rsqrt() const { return T(1) / this->sqrt(); } |
| 226 | AI SkNx floor() const { return Floor(fVal); } |
| 227 | AI SkNx invert() const { return T(1) / *this; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 228 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 229 | AI SkNx operator!() const { return !fVal; } |
| 230 | AI SkNx operator-() const { return -fVal; } |
| 231 | AI SkNx operator~() const { return FromBits(~ToBits(fVal)); } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 232 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 233 | AI SkNx operator<<(int bits) const { return fVal << bits; } |
| 234 | AI SkNx operator>>(int bits) const { return fVal >> bits; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 235 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 236 | AI SkNx operator+(const SkNx& y) const { return fVal + y.fVal; } |
| 237 | AI SkNx operator-(const SkNx& y) const { return fVal - y.fVal; } |
| 238 | AI SkNx operator*(const SkNx& y) const { return fVal * y.fVal; } |
| 239 | AI SkNx operator/(const SkNx& y) const { return fVal / y.fVal; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 240 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 241 | AI SkNx operator&(const SkNx& y) const { return FromBits(ToBits(fVal) & ToBits(y.fVal)); } |
| 242 | AI SkNx operator|(const SkNx& y) const { return FromBits(ToBits(fVal) | ToBits(y.fVal)); } |
| 243 | AI SkNx operator^(const SkNx& y) const { return FromBits(ToBits(fVal) ^ ToBits(y.fVal)); } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 244 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 245 | AI SkNx operator==(const SkNx& y) const { return FromBits(fVal == y.fVal ? ~0 : 0); } |
| 246 | AI SkNx operator!=(const SkNx& y) const { return FromBits(fVal != y.fVal ? ~0 : 0); } |
| 247 | AI SkNx operator<=(const SkNx& y) const { return FromBits(fVal <= y.fVal ? ~0 : 0); } |
| 248 | AI SkNx operator>=(const SkNx& y) const { return FromBits(fVal >= y.fVal ? ~0 : 0); } |
| 249 | AI SkNx operator< (const SkNx& y) const { return FromBits(fVal < y.fVal ? ~0 : 0); } |
| 250 | AI SkNx operator> (const SkNx& y) const { return FromBits(fVal > y.fVal ? ~0 : 0); } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 251 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 252 | AI static SkNx Min(const SkNx& x, const SkNx& y) { return x.fVal < y.fVal ? x : y; } |
| 253 | AI static SkNx Max(const SkNx& x, const SkNx& y) { return x.fVal > y.fVal ? x : y; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 254 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 255 | AI SkNx saturatedAdd(const SkNx& y) const { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 256 | static_assert(std::is_unsigned<T>::value, ""); |
| 257 | T sum = fVal + y.fVal; |
| 258 | return sum < fVal ? std::numeric_limits<T>::max() : sum; |
| 259 | } |
| 260 | |
Herb Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 261 | AI SkNx mulHi(const SkNx& m) const { |
| 262 | static_assert(std::is_unsigned<T>::value, ""); |
| 263 | static_assert(sizeof(T) <= 4, ""); |
| 264 | return static_cast<T>((static_cast<uint64_t>(fVal) * m.fVal) >> (sizeof(T)*8)); |
| 265 | } |
| 266 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 267 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { return fVal != 0 ? t : e; } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 268 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 269 | private: |
| 270 | // Helper functions to choose the right float/double methods. (In <cmath> madness lies...) |
Yuqian Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 271 | AI static int Abs(int val) { return val < 0 ? -val : val; } |
| 272 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 273 | AI static float Abs(float val) { return ::fabsf(val); } |
| 274 | AI static float Sqrt(float val) { return ::sqrtf(val); } |
| 275 | AI static float Floor(float val) { return ::floorf(val); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 276 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 277 | AI static double Abs(double val) { return ::fabs(val); } |
| 278 | AI static double Sqrt(double val) { return ::sqrt(val); } |
| 279 | AI static double Floor(double val) { return ::floor(val); } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 280 | |
| 281 | // Helper functions for working with floats/doubles as bit patterns. |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 282 | template <typename U> |
| 283 | AI static U ToBits(U v) { return v; } |
| 284 | AI static int32_t ToBits(float v) { int32_t bits; memcpy(&bits, &v, sizeof(v)); return bits; } |
| 285 | AI static int64_t ToBits(double v) { int64_t bits; memcpy(&bits, &v, sizeof(v)); return bits; } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 286 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 287 | template <typename Bits> |
| 288 | AI static T FromBits(Bits bits) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 289 | static_assert(std::is_pod<T >::value && |
| 290 | std::is_pod<Bits>::value && |
| 291 | sizeof(T) <= sizeof(Bits), ""); |
| 292 | T val; |
| 293 | memcpy(&val, &bits, sizeof(T)); |
| 294 | return val; |
| 295 | } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 298 | // Allow scalars on the left or right of binary operators, and things like +=, &=, etc. |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 299 | #define V template <int N, typename T> AI static SkNx<N,T> |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 300 | V operator+ (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) + y; } |
| 301 | V operator- (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) - y; } |
| 302 | V operator* (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) * y; } |
| 303 | V operator/ (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) / y; } |
| 304 | V operator& (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) & y; } |
| 305 | V operator| (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) | y; } |
| 306 | V operator^ (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) ^ y; } |
| 307 | V operator==(T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) == y; } |
| 308 | V operator!=(T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) != y; } |
| 309 | V operator<=(T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) <= y; } |
| 310 | V operator>=(T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) >= y; } |
| 311 | V operator< (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) < y; } |
| 312 | V operator> (T x, const SkNx<N,T>& y) { return SkNx<N,T>(x) > y; } |
| 313 | |
| 314 | V operator+ (const SkNx<N,T>& x, T y) { return x + SkNx<N,T>(y); } |
| 315 | V operator- (const SkNx<N,T>& x, T y) { return x - SkNx<N,T>(y); } |
| 316 | V operator* (const SkNx<N,T>& x, T y) { return x * SkNx<N,T>(y); } |
| 317 | V operator/ (const SkNx<N,T>& x, T y) { return x / SkNx<N,T>(y); } |
| 318 | V operator& (const SkNx<N,T>& x, T y) { return x & SkNx<N,T>(y); } |
| 319 | V operator| (const SkNx<N,T>& x, T y) { return x | SkNx<N,T>(y); } |
| 320 | V operator^ (const SkNx<N,T>& x, T y) { return x ^ SkNx<N,T>(y); } |
| 321 | V operator==(const SkNx<N,T>& x, T y) { return x == SkNx<N,T>(y); } |
| 322 | V operator!=(const SkNx<N,T>& x, T y) { return x != SkNx<N,T>(y); } |
| 323 | V operator<=(const SkNx<N,T>& x, T y) { return x <= SkNx<N,T>(y); } |
| 324 | V operator>=(const SkNx<N,T>& x, T y) { return x >= SkNx<N,T>(y); } |
| 325 | V operator< (const SkNx<N,T>& x, T y) { return x < SkNx<N,T>(y); } |
| 326 | V operator> (const SkNx<N,T>& x, T y) { return x > SkNx<N,T>(y); } |
| 327 | |
| 328 | V& operator<<=(SkNx<N,T>& x, int bits) { return (x = x << bits); } |
| 329 | V& operator>>=(SkNx<N,T>& x, int bits) { return (x = x >> bits); } |
| 330 | |
| 331 | V& operator +=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x + y); } |
| 332 | V& operator -=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x - y); } |
| 333 | V& operator *=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x * y); } |
| 334 | V& operator /=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x / y); } |
| 335 | V& operator &=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x & y); } |
| 336 | V& operator |=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x | y); } |
| 337 | V& operator ^=(SkNx<N,T>& x, const SkNx<N,T>& y) { return (x = x ^ y); } |
| 338 | |
| 339 | V& operator +=(SkNx<N,T>& x, T y) { return (x = x + SkNx<N,T>(y)); } |
| 340 | V& operator -=(SkNx<N,T>& x, T y) { return (x = x - SkNx<N,T>(y)); } |
| 341 | V& operator *=(SkNx<N,T>& x, T y) { return (x = x * SkNx<N,T>(y)); } |
| 342 | V& operator /=(SkNx<N,T>& x, T y) { return (x = x / SkNx<N,T>(y)); } |
| 343 | V& operator &=(SkNx<N,T>& x, T y) { return (x = x & SkNx<N,T>(y)); } |
| 344 | V& operator |=(SkNx<N,T>& x, T y) { return (x = x | SkNx<N,T>(y)); } |
| 345 | V& operator ^=(SkNx<N,T>& x, T y) { return (x = x ^ SkNx<N,T>(y)); } |
| 346 | #undef V |
| 347 | |
| 348 | // SkNx<N,T> ~~> SkNx<N/2,T> + SkNx<N/2,T> |
| 349 | template <int N, typename T> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 350 | AI static void SkNx_split(const SkNx<N,T>& v, SkNx<N/2,T>* lo, SkNx<N/2,T>* hi) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 351 | *lo = v.fLo; |
| 352 | *hi = v.fHi; |
| 353 | } |
| 354 | |
| 355 | // SkNx<N/2,T> + SkNx<N/2,T> ~~> SkNx<N,T> |
| 356 | template <int N, typename T> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 357 | AI static SkNx<N*2,T> SkNx_join(const SkNx<N,T>& lo, const SkNx<N,T>& hi) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 358 | return { lo, hi }; |
| 359 | } |
| 360 | |
| 361 | // A very generic shuffle. Can reorder, duplicate, contract, expand... |
| 362 | // Sk4f v = { R,G,B,A }; |
| 363 | // SkNx_shuffle<2,1,0,3>(v) ~~> {B,G,R,A} |
| 364 | // SkNx_shuffle<2,1>(v) ~~> {B,G} |
| 365 | // SkNx_shuffle<2,1,2,1,2,1,2,1>(v) ~~> {B,G,B,G,B,G,B,G} |
| 366 | // SkNx_shuffle<3,3,3,3>(v) ~~> {A,A,A,A} |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 367 | template <int... Ix, int N, typename T> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 368 | AI static SkNx<sizeof...(Ix),T> SkNx_shuffle(const SkNx<N,T>& v) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 369 | return { v[Ix]... }; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 370 | } |
mtklein | a1c0ee4 | 2015-09-10 14:16:07 -0700 | [diff] [blame] | 371 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 372 | // Cast from SkNx<N, Src> to SkNx<N, Dst>, as if you called static_cast<Dst>(Src). |
| 373 | template <typename Dst, typename Src, int N> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 374 | AI static SkNx<N,Dst> SkNx_cast(const SkNx<N,Src>& v) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 375 | return { SkNx_cast<Dst>(v.fLo), SkNx_cast<Dst>(v.fHi) }; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 376 | } |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 377 | template <typename Dst, typename Src> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 378 | AI static SkNx<1,Dst> SkNx_cast(const SkNx<1,Src>& v) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 379 | return static_cast<Dst>(v.fVal); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 380 | } |
mtklein | fce612a | 2015-12-15 07:38:54 -0800 | [diff] [blame] | 381 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 382 | template <int N, typename T> |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 383 | AI static SkNx<N,T> SkNx_fma(const SkNx<N,T>& f, const SkNx<N,T>& m, const SkNx<N,T>& a) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 384 | return f*m+a; |
| 385 | } |
| 386 | |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 387 | } // namespace |
| 388 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 389 | typedef SkNx<2, float> Sk2f; |
| 390 | typedef SkNx<4, float> Sk4f; |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 391 | typedef SkNx<8, float> Sk8f; |
| 392 | typedef SkNx<16, float> Sk16f; |
| 393 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 394 | typedef SkNx<2, SkScalar> Sk2s; |
| 395 | typedef SkNx<4, SkScalar> Sk4s; |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 396 | typedef SkNx<8, SkScalar> Sk8s; |
| 397 | typedef SkNx<16, SkScalar> Sk16s; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 398 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 399 | typedef SkNx<4, uint8_t> Sk4b; |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 400 | typedef SkNx<8, uint8_t> Sk8b; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 401 | typedef SkNx<16, uint8_t> Sk16b; |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 402 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 403 | typedef SkNx<4, uint16_t> Sk4h; |
| 404 | typedef SkNx<8, uint16_t> Sk8h; |
| 405 | typedef SkNx<16, uint16_t> Sk16h; |
| 406 | |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 407 | typedef SkNx<4, int32_t> Sk4i; |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 408 | typedef SkNx<8, int32_t> Sk8i; |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 409 | typedef SkNx<4, uint32_t> Sk4u; |
mtklein | ff9febf | 2016-02-16 14:33:08 -0800 | [diff] [blame] | 410 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 411 | // Include platform specific specializations if available. |
mtklein | 81bb79b | 2016-02-08 15:50:22 -0800 | [diff] [blame] | 412 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 413 | #include "include/private/SkNx_sse.h" |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 414 | #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 415 | #include "include/private/SkNx_neon.h" |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 416 | #else |
| 417 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 418 | AI static Sk4i Sk4f_round(const Sk4f& x) { |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 419 | return { (int) lrintf (x[0]), |
| 420 | (int) lrintf (x[1]), |
| 421 | (int) lrintf (x[2]), |
| 422 | (int) lrintf (x[3]), }; |
| 423 | } |
| 424 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 425 | #endif |
| 426 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 427 | AI static void Sk4f_ToBytes(uint8_t p[16], |
| 428 | const Sk4f& a, const Sk4f& b, const Sk4f& c, const Sk4f& d) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 429 | SkNx_cast<uint8_t>(SkNx_join(SkNx_join(a,b), SkNx_join(c,d))).store(p); |
| 430 | } |
| 431 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 432 | #undef AI |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 433 | |
mtklein | 3e490b7 | 2015-03-20 06:33:02 -0700 | [diff] [blame] | 434 | #endif//SkNx_DEFINED |