blob: 5ec3dc2f2bcbb4c3ba08464177153b79b4215714 [file] [log] [blame]
mtkleinc9adb052015-03-30 10:50:27 -07001/*
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_neon_DEFINED
9#define SkNx_neon_DEFINED
10
mtkleine18fa442016-06-09 13:40:56 -070011#include <arm_neon.h>
12
Mike Klein1e764642016-10-14 17:09:03 -040013namespace {
14
mtklein126626e2016-02-09 15:41:36 -080015// ARMv8 has vrndmq_f32 to floor 4 floats. Here we emulate it:
mtkleine5fe9a42016-02-10 07:55:56 -080016// - roundtrip through integers via truncation
17// - subtract 1 if that's too big (possible for negative values).
18// This restricts the domain of our inputs to a maximum somehwere around 2^31. Seems plenty big.
Mike Klein7c78f3a2016-10-19 09:21:11 -040019AI static float32x4_t armv7_vrndmq_f32(float32x4_t v) {
mtklein7c0db752016-07-30 14:18:49 -070020 auto roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
21 auto too_big = vcgtq_f32(roundtrip, v);
22 return vsubq_f32(roundtrip, (float32x4_t)vandq_u32(too_big, (uint32x4_t)vdupq_n_f32(1)));
mtklein126626e2016-02-09 15:41:36 -080023}
24
mtkleinc9adb052015-03-30 10:50:27 -070025template <>
mtklein6c221b42015-11-20 13:53:19 -080026class SkNx<2, float> {
mtkleinc9adb052015-03-30 10:50:27 -070027public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040028 AI SkNx(float32x2_t vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070029
Mike Klein7c78f3a2016-10-19 09:21:11 -040030 AI SkNx() {}
31 AI SkNx(float val) : fVec(vdup_n_f32(val)) {}
32 AI SkNx(float a, float b) { fVec = (float32x2_t) { a, b }; }
mtklein7c0db752016-07-30 14:18:49 -070033
Mike Klein7c78f3a2016-10-19 09:21:11 -040034 AI static SkNx Load(const void* ptr) { return vld1_f32((const float*)ptr); }
35 AI void store(void* ptr) const { vst1_f32((float*)ptr, fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070036
Mike Klein7c78f3a2016-10-19 09:21:11 -040037 AI SkNx invert() const {
mtklein7c0db752016-07-30 14:18:49 -070038 float32x2_t est0 = vrecpe_f32(fVec),
39 est1 = vmul_f32(vrecps_f32(est0, fVec), est0);
40 return est1;
41 }
mtkleinc9adb052015-03-30 10:50:27 -070042
Mike Klein7c78f3a2016-10-19 09:21:11 -040043 AI SkNx operator + (const SkNx& o) const { return vadd_f32(fVec, o.fVec); }
44 AI SkNx operator - (const SkNx& o) const { return vsub_f32(fVec, o.fVec); }
45 AI SkNx operator * (const SkNx& o) const { return vmul_f32(fVec, o.fVec); }
46 AI SkNx operator / (const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -070047 #if defined(SK_CPU_ARM64)
48 return vdiv_f32(fVec, o.fVec);
49 #else
50 float32x2_t est0 = vrecpe_f32(o.fVec),
51 est1 = vmul_f32(vrecps_f32(est0, o.fVec), est0),
52 est2 = vmul_f32(vrecps_f32(est1, o.fVec), est1);
53 return vmul_f32(fVec, est2);
54 #endif
55 }
56
Mike Klein7c78f3a2016-10-19 09:21:11 -040057 AI SkNx operator==(const SkNx& o) const { return vreinterpret_f32_u32(vceq_f32(fVec, o.fVec)); }
58 AI SkNx operator <(const SkNx& o) const { return vreinterpret_f32_u32(vclt_f32(fVec, o.fVec)); }
59 AI SkNx operator >(const SkNx& o) const { return vreinterpret_f32_u32(vcgt_f32(fVec, o.fVec)); }
60 AI SkNx operator<=(const SkNx& o) const { return vreinterpret_f32_u32(vcle_f32(fVec, o.fVec)); }
61 AI SkNx operator>=(const SkNx& o) const { return vreinterpret_f32_u32(vcge_f32(fVec, o.fVec)); }
62 AI SkNx operator!=(const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -070063 return vreinterpret_f32_u32(vmvn_u32(vceq_f32(fVec, o.fVec)));
64 }
mtkleinc9adb052015-03-30 10:50:27 -070065
Mike Klein7c78f3a2016-10-19 09:21:11 -040066 AI static SkNx Min(const SkNx& l, const SkNx& r) { return vmin_f32(l.fVec, r.fVec); }
67 AI static SkNx Max(const SkNx& l, const SkNx& r) { return vmax_f32(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070068
Mike Klein7c78f3a2016-10-19 09:21:11 -040069 AI SkNx rsqrt() const {
mtkleinf8f90e42016-03-21 10:04:46 -070070 float32x2_t est0 = vrsqrte_f32(fVec);
mtkleind7c014f2015-04-27 14:22:32 -070071 return vmul_f32(vrsqrts_f32(fVec, vmul_f32(est0, est0)), est0);
72 }
mtkleinc9adb052015-03-30 10:50:27 -070073
Mike Klein7c78f3a2016-10-19 09:21:11 -040074 AI SkNx sqrt() const {
mtkleinc9adb052015-03-30 10:50:27 -070075 #if defined(SK_CPU_ARM64)
76 return vsqrt_f32(fVec);
77 #else
mtkleinf8f90e42016-03-21 10:04:46 -070078 float32x2_t est0 = vrsqrte_f32(fVec),
79 est1 = vmul_f32(vrsqrts_f32(fVec, vmul_f32(est0, est0)), est0),
80 est2 = vmul_f32(vrsqrts_f32(fVec, vmul_f32(est1, est1)), est1);
81 return vmul_f32(fVec, est2);
mtkleinc9adb052015-03-30 10:50:27 -070082 #endif
83 }
84
Mike Klein7c78f3a2016-10-19 09:21:11 -040085 AI float operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -070086 SkASSERT(0 <= k && k < 2);
87 union { float32x2_t v; float fs[2]; } pun = {fVec};
88 return pun.fs[k&1];
mtkleinc9adb052015-03-30 10:50:27 -070089 }
90
Mike Klein7c78f3a2016-10-19 09:21:11 -040091 AI bool allTrue() const {
mtkleinb5e86112015-06-24 15:18:39 -070092 auto v = vreinterpret_u32_f32(fVec);
93 return vget_lane_u32(v,0) && vget_lane_u32(v,1);
94 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040095 AI bool anyTrue() const {
mtkleinb5e86112015-06-24 15:18:39 -070096 auto v = vreinterpret_u32_f32(fVec);
97 return vget_lane_u32(v,0) || vget_lane_u32(v,1);
98 }
99
mtkleinc9adb052015-03-30 10:50:27 -0700100 float32x2_t fVec;
101};
102
mtkleinc9adb052015-03-30 10:50:27 -0700103template <>
mtklein6c221b42015-11-20 13:53:19 -0800104class SkNx<4, float> {
mtkleinc9adb052015-03-30 10:50:27 -0700105public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400106 AI SkNx(float32x4_t vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -0700107
Mike Klein7c78f3a2016-10-19 09:21:11 -0400108 AI SkNx() {}
109 AI SkNx(float val) : fVec(vdupq_n_f32(val)) {}
110 AI SkNx(float a, float b, float c, float d) { fVec = (float32x4_t) { a, b, c, d }; }
mtklein7c0db752016-07-30 14:18:49 -0700111
Mike Klein7c78f3a2016-10-19 09:21:11 -0400112 AI static SkNx Load(const void* ptr) { return vld1q_f32((const float*)ptr); }
113 AI void store(void* ptr) const { vst1q_f32((float*)ptr, fVec); }
Mike Klein33cbfd72016-10-06 11:09:27 -0400114
Mike Klein7c78f3a2016-10-19 09:21:11 -0400115 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400116 float32x4x4_t rgba = vld4q_f32((const float*) ptr);
117 *r = rgba.val[0];
118 *g = rgba.val[1];
119 *b = rgba.val[2];
120 *a = rgba.val[3];
121 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400122 AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400123 float32x4x4_t rgba = {{
124 r.fVec,
125 g.fVec,
126 b.fVec,
127 a.fVec,
128 }};
129 vst4q_f32((float*) dst, rgba);
130 }
131
Mike Klein7c78f3a2016-10-19 09:21:11 -0400132 AI SkNx invert() const {
mtklein7c0db752016-07-30 14:18:49 -0700133 float32x4_t est0 = vrecpeq_f32(fVec),
134 est1 = vmulq_f32(vrecpsq_f32(est0, fVec), est0);
135 return est1;
136 }
mtkleinc9adb052015-03-30 10:50:27 -0700137
Mike Klein7c78f3a2016-10-19 09:21:11 -0400138 AI SkNx operator + (const SkNx& o) const { return vaddq_f32(fVec, o.fVec); }
139 AI SkNx operator - (const SkNx& o) const { return vsubq_f32(fVec, o.fVec); }
140 AI SkNx operator * (const SkNx& o) const { return vmulq_f32(fVec, o.fVec); }
141 AI SkNx operator / (const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -0700142 #if defined(SK_CPU_ARM64)
143 return vdivq_f32(fVec, o.fVec);
144 #else
145 float32x4_t est0 = vrecpeq_f32(o.fVec),
146 est1 = vmulq_f32(vrecpsq_f32(est0, o.fVec), est0),
147 est2 = vmulq_f32(vrecpsq_f32(est1, o.fVec), est1);
148 return vmulq_f32(fVec, est2);
149 #endif
150 }
mtkleinc9adb052015-03-30 10:50:27 -0700151
Mike Klein7c78f3a2016-10-19 09:21:11 -0400152 AI SkNx operator==(const SkNx& o) const {return vreinterpretq_f32_u32(vceqq_f32(fVec, o.fVec));}
153 AI SkNx operator <(const SkNx& o) const {return vreinterpretq_f32_u32(vcltq_f32(fVec, o.fVec));}
154 AI SkNx operator >(const SkNx& o) const {return vreinterpretq_f32_u32(vcgtq_f32(fVec, o.fVec));}
155 AI SkNx operator<=(const SkNx& o) const {return vreinterpretq_f32_u32(vcleq_f32(fVec, o.fVec));}
156 AI SkNx operator>=(const SkNx& o) const {return vreinterpretq_f32_u32(vcgeq_f32(fVec, o.fVec));}
157 AI SkNx operator!=(const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -0700158 return vreinterpretq_f32_u32(vmvnq_u32(vceqq_f32(fVec, o.fVec)));
159 }
mtkleinc9adb052015-03-30 10:50:27 -0700160
Mike Klein7c78f3a2016-10-19 09:21:11 -0400161 AI static SkNx Min(const SkNx& l, const SkNx& r) { return vminq_f32(l.fVec, r.fVec); }
162 AI static SkNx Max(const SkNx& l, const SkNx& r) { return vmaxq_f32(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700163
Mike Klein7c78f3a2016-10-19 09:21:11 -0400164 AI SkNx abs() const { return vabsq_f32(fVec); }
165 AI SkNx floor() const {
mtklein126626e2016-02-09 15:41:36 -0800166 #if defined(SK_CPU_ARM64)
167 return vrndmq_f32(fVec);
168 #else
169 return armv7_vrndmq_f32(fVec);
170 #endif
171 }
172
mtklein7c0db752016-07-30 14:18:49 -0700173
Mike Klein7c78f3a2016-10-19 09:21:11 -0400174 AI SkNx rsqrt() const {
mtkleinf8f90e42016-03-21 10:04:46 -0700175 float32x4_t est0 = vrsqrteq_f32(fVec);
mtkleind7c014f2015-04-27 14:22:32 -0700176 return vmulq_f32(vrsqrtsq_f32(fVec, vmulq_f32(est0, est0)), est0);
177 }
mtkleinc9adb052015-03-30 10:50:27 -0700178
Mike Klein7c78f3a2016-10-19 09:21:11 -0400179 AI SkNx sqrt() const {
mtkleinc9adb052015-03-30 10:50:27 -0700180 #if defined(SK_CPU_ARM64)
181 return vsqrtq_f32(fVec);
182 #else
mtkleinf8f90e42016-03-21 10:04:46 -0700183 float32x4_t est0 = vrsqrteq_f32(fVec),
184 est1 = vmulq_f32(vrsqrtsq_f32(fVec, vmulq_f32(est0, est0)), est0),
185 est2 = vmulq_f32(vrsqrtsq_f32(fVec, vmulq_f32(est1, est1)), est1);
186 return vmulq_f32(fVec, est2);
mtkleinc9adb052015-03-30 10:50:27 -0700187 #endif
188 }
189
Mike Klein7c78f3a2016-10-19 09:21:11 -0400190 AI float operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700191 SkASSERT(0 <= k && k < 4);
192 union { float32x4_t v; float fs[4]; } pun = {fVec};
193 return pun.fs[k&3];
mtkleinc9adb052015-03-30 10:50:27 -0700194 }
195
Mike Klein7c78f3a2016-10-19 09:21:11 -0400196 AI bool allTrue() const {
mtkleinb5e86112015-06-24 15:18:39 -0700197 auto v = vreinterpretq_u32_f32(fVec);
198 return vgetq_lane_u32(v,0) && vgetq_lane_u32(v,1)
199 && vgetq_lane_u32(v,2) && vgetq_lane_u32(v,3);
200 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400201 AI bool anyTrue() const {
mtkleinb5e86112015-06-24 15:18:39 -0700202 auto v = vreinterpretq_u32_f32(fVec);
203 return vgetq_lane_u32(v,0) || vgetq_lane_u32(v,1)
204 || vgetq_lane_u32(v,2) || vgetq_lane_u32(v,3);
205 }
206
Mike Klein7c78f3a2016-10-19 09:21:11 -0400207 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleincf4e5672015-07-27 06:12:05 -0700208 return vbslq_f32(vreinterpretq_u32_f32(fVec), t.fVec, e.fVec);
mtklein2aab22a2015-06-26 10:46:31 -0700209 }
210
mtkleinc9adb052015-03-30 10:50:27 -0700211 float32x4_t fVec;
212};
213
Mike Kleinf0348c22016-11-03 14:43:48 -0400214#if defined(SK_CPU_ARM64)
215 AI static Sk4f SkNx_fma(const Sk4f& f, const Sk4f& m, const Sk4f& a) {
216 return vfmaq_f32(a.fVec, f.fVec, m.fVec);
217 }
218#endif
219
mtklein550e9b02016-01-20 11:55:51 -0800220// It's possible that for our current use cases, representing this as
221// half a uint16x8_t might be better than representing it as a uint16x4_t.
222// It'd make conversion to Sk4b one step simpler.
223template <>
224class SkNx<4, uint16_t> {
225public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400226 AI SkNx(const uint16x4_t& vec) : fVec(vec) {}
mtklein550e9b02016-01-20 11:55:51 -0800227
Mike Klein7c78f3a2016-10-19 09:21:11 -0400228 AI SkNx() {}
229 AI SkNx(uint16_t val) : fVec(vdup_n_u16(val)) {}
230 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d) {
mtklein7c0db752016-07-30 14:18:49 -0700231 fVec = (uint16x4_t) { a,b,c,d };
232 }
233
Mike Klein7c78f3a2016-10-19 09:21:11 -0400234 AI static SkNx Load(const void* ptr) { return vld1_u16((const uint16_t*)ptr); }
235 AI void store(void* ptr) const { vst1_u16((uint16_t*)ptr, fVec); }
mtklein550e9b02016-01-20 11:55:51 -0800236
Mike Klein7c78f3a2016-10-19 09:21:11 -0400237 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400238 uint16x4x4_t rgba = vld4_u16((const uint16_t*)ptr);
239 *r = rgba.val[0];
240 *g = rgba.val[1];
241 *b = rgba.val[2];
242 *a = rgba.val[3];
243 }
Matt Sarett5bee0b62017-01-19 12:04:32 -0500244 AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) {
245 uint16x4x3_t rgba = vld3_u16((const uint16_t*)ptr);
246 *r = rgba.val[0];
247 *g = rgba.val[1];
248 *b = rgba.val[2];
249 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400250 AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400251 uint16x4x4_t rgba = {{
252 r.fVec,
253 g.fVec,
254 b.fVec,
255 a.fVec,
256 }};
257 vst4_u16((uint16_t*) dst, rgba);
258 }
259
Mike Klein7c78f3a2016-10-19 09:21:11 -0400260 AI SkNx operator + (const SkNx& o) const { return vadd_u16(fVec, o.fVec); }
261 AI SkNx operator - (const SkNx& o) const { return vsub_u16(fVec, o.fVec); }
262 AI SkNx operator * (const SkNx& o) const { return vmul_u16(fVec, o.fVec); }
Matt Sarett379938e2017-01-12 18:34:29 -0500263 AI SkNx operator & (const SkNx& o) const { return vand_u16(fVec, o.fVec); }
264 AI SkNx operator | (const SkNx& o) const { return vorr_u16(fVec, o.fVec); }
mtklein550e9b02016-01-20 11:55:51 -0800265
Mike Klein7c78f3a2016-10-19 09:21:11 -0400266 AI SkNx operator << (int bits) const { return fVec << SkNx(bits).fVec; }
267 AI SkNx operator >> (int bits) const { return fVec >> SkNx(bits).fVec; }
mtklein550e9b02016-01-20 11:55:51 -0800268
Mike Klein7c78f3a2016-10-19 09:21:11 -0400269 AI static SkNx Min(const SkNx& a, const SkNx& b) { return vmin_u16(a.fVec, b.fVec); }
mtklein550e9b02016-01-20 11:55:51 -0800270
Mike Klein7c78f3a2016-10-19 09:21:11 -0400271 AI uint16_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700272 SkASSERT(0 <= k && k < 4);
273 union { uint16x4_t v; uint16_t us[4]; } pun = {fVec};
274 return pun.us[k&3];
275 }
mtklein550e9b02016-01-20 11:55:51 -0800276
Mike Klein7c78f3a2016-10-19 09:21:11 -0400277 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein550e9b02016-01-20 11:55:51 -0800278 return vbsl_u16(fVec, t.fVec, e.fVec);
279 }
280
281 uint16x4_t fVec;
282};
283
mtkleind2ffd362015-05-12 06:11:21 -0700284template <>
mtklein6c221b42015-11-20 13:53:19 -0800285class SkNx<8, uint16_t> {
mtkleind2ffd362015-05-12 06:11:21 -0700286public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400287 AI SkNx(const uint16x8_t& vec) : fVec(vec) {}
mtkleind2ffd362015-05-12 06:11:21 -0700288
Mike Klein7c78f3a2016-10-19 09:21:11 -0400289 AI SkNx() {}
290 AI SkNx(uint16_t val) : fVec(vdupq_n_u16(val)) {}
291 AI static SkNx Load(const void* ptr) { return vld1q_u16((const uint16_t*)ptr); }
mtklein7c0db752016-07-30 14:18:49 -0700292
Mike Klein7c78f3a2016-10-19 09:21:11 -0400293 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
294 uint16_t e, uint16_t f, uint16_t g, uint16_t h) {
mtklein7c0db752016-07-30 14:18:49 -0700295 fVec = (uint16x8_t) { a,b,c,d, e,f,g,h };
296 }
297
Mike Klein7c78f3a2016-10-19 09:21:11 -0400298 AI void store(void* ptr) const { vst1q_u16((uint16_t*)ptr, fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700299
Mike Klein7c78f3a2016-10-19 09:21:11 -0400300 AI SkNx operator + (const SkNx& o) const { return vaddq_u16(fVec, o.fVec); }
301 AI SkNx operator - (const SkNx& o) const { return vsubq_u16(fVec, o.fVec); }
302 AI SkNx operator * (const SkNx& o) const { return vmulq_u16(fVec, o.fVec); }
Matt Sarett379938e2017-01-12 18:34:29 -0500303 AI SkNx operator & (const SkNx& o) const { return vandq_u16(fVec, o.fVec); }
304 AI SkNx operator | (const SkNx& o) const { return vorrq_u16(fVec, o.fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700305
Mike Klein7c78f3a2016-10-19 09:21:11 -0400306 AI SkNx operator << (int bits) const { return fVec << SkNx(bits).fVec; }
307 AI SkNx operator >> (int bits) const { return fVec >> SkNx(bits).fVec; }
mtkleind2ffd362015-05-12 06:11:21 -0700308
Mike Klein7c78f3a2016-10-19 09:21:11 -0400309 AI static SkNx Min(const SkNx& a, const SkNx& b) { return vminq_u16(a.fVec, b.fVec); }
mtklein27e517a2015-05-14 17:53:04 -0700310
Mike Klein7c78f3a2016-10-19 09:21:11 -0400311 AI uint16_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700312 SkASSERT(0 <= k && k < 8);
313 union { uint16x8_t v; uint16_t us[8]; } pun = {fVec};
314 return pun.us[k&7];
315 }
mtkleind2ffd362015-05-12 06:11:21 -0700316
Mike Klein7c78f3a2016-10-19 09:21:11 -0400317 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleincf4e5672015-07-27 06:12:05 -0700318 return vbslq_u16(fVec, t.fVec, e.fVec);
mtklein4be181e2015-07-14 10:54:19 -0700319 }
320
mtkleind2ffd362015-05-12 06:11:21 -0700321 uint16x8_t fVec;
322};
323
324template <>
mtklein6f37b4a2015-12-14 11:25:18 -0800325class SkNx<4, uint8_t> {
326public:
mtkleina5e1e332016-07-26 10:07:34 -0700327 typedef uint32_t __attribute__((aligned(1))) unaligned_uint32_t;
328
Mike Klein7c78f3a2016-10-19 09:21:11 -0400329 AI SkNx(const uint8x8_t& vec) : fVec(vec) {}
mtklein6f37b4a2015-12-14 11:25:18 -0800330
Mike Klein7c78f3a2016-10-19 09:21:11 -0400331 AI SkNx() {}
332 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
mtklein7c0db752016-07-30 14:18:49 -0700333 fVec = (uint8x8_t){a,b,c,d, 0,0,0,0};
334 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400335 AI static SkNx Load(const void* ptr) {
mtkleina5e1e332016-07-26 10:07:34 -0700336 return (uint8x8_t)vld1_dup_u32((const unaligned_uint32_t*)ptr);
mtklein6f37b4a2015-12-14 11:25:18 -0800337 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400338 AI void store(void* ptr) const {
mtkleina5e1e332016-07-26 10:07:34 -0700339 return vst1_lane_u32((unaligned_uint32_t*)ptr, (uint32x2_t)fVec, 0);
mtklein6f37b4a2015-12-14 11:25:18 -0800340 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400341 AI uint8_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700342 SkASSERT(0 <= k && k < 4);
343 union { uint8x8_t v; uint8_t us[8]; } pun = {fVec};
344 return pun.us[k&3];
345 }
mtklein6f37b4a2015-12-14 11:25:18 -0800346
mtklein7c0db752016-07-30 14:18:49 -0700347 // TODO as needed
mtklein6f37b4a2015-12-14 11:25:18 -0800348
349 uint8x8_t fVec;
350};
351
352template <>
mtklein6c221b42015-11-20 13:53:19 -0800353class SkNx<16, uint8_t> {
mtkleind2ffd362015-05-12 06:11:21 -0700354public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400355 AI SkNx(const uint8x16_t& vec) : fVec(vec) {}
mtkleind2ffd362015-05-12 06:11:21 -0700356
Mike Klein7c78f3a2016-10-19 09:21:11 -0400357 AI SkNx() {}
358 AI SkNx(uint8_t val) : fVec(vdupq_n_u8(val)) {}
359 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
360 uint8_t e, uint8_t f, uint8_t g, uint8_t h,
361 uint8_t i, uint8_t j, uint8_t k, uint8_t l,
362 uint8_t m, uint8_t n, uint8_t o, uint8_t p) {
mtklein7c0db752016-07-30 14:18:49 -0700363 fVec = (uint8x16_t) { a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p };
364 }
mtkleind2ffd362015-05-12 06:11:21 -0700365
Mike Klein7c78f3a2016-10-19 09:21:11 -0400366 AI static SkNx Load(const void* ptr) { return vld1q_u8((const uint8_t*)ptr); }
367 AI void store(void* ptr) const { vst1q_u8((uint8_t*)ptr, fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700368
Mike Klein7c78f3a2016-10-19 09:21:11 -0400369 AI SkNx saturatedAdd(const SkNx& o) const { return vqaddq_u8(fVec, o.fVec); }
mtklein04d24a32015-05-13 08:02:14 -0700370
Mike Klein7c78f3a2016-10-19 09:21:11 -0400371 AI SkNx operator + (const SkNx& o) const { return vaddq_u8(fVec, o.fVec); }
372 AI SkNx operator - (const SkNx& o) const { return vsubq_u8(fVec, o.fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700373
Mike Klein7c78f3a2016-10-19 09:21:11 -0400374 AI static SkNx Min(const SkNx& a, const SkNx& b) { return vminq_u8(a.fVec, b.fVec); }
375 AI SkNx operator < (const SkNx& o) const { return vcltq_u8(fVec, o.fVec); }
mtklein27e517a2015-05-14 17:53:04 -0700376
Mike Klein7c78f3a2016-10-19 09:21:11 -0400377 AI uint8_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700378 SkASSERT(0 <= k && k < 16);
379 union { uint8x16_t v; uint8_t us[16]; } pun = {fVec};
380 return pun.us[k&15];
381 }
mtkleind2ffd362015-05-12 06:11:21 -0700382
Mike Klein7c78f3a2016-10-19 09:21:11 -0400383 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleincf4e5672015-07-27 06:12:05 -0700384 return vbslq_u8(fVec, t.fVec, e.fVec);
mtkleinb5e86112015-06-24 15:18:39 -0700385 }
386
mtkleind2ffd362015-05-12 06:11:21 -0700387 uint8x16_t fVec;
388};
389
mtkleinf8f90e42016-03-21 10:04:46 -0700390template <>
mtkleind05a8752016-07-29 10:10:15 -0700391class SkNx<4, int32_t> {
mtkleinf8f90e42016-03-21 10:04:46 -0700392public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400393 AI SkNx(const int32x4_t& vec) : fVec(vec) {}
mtkleinf8f90e42016-03-21 10:04:46 -0700394
Mike Klein7c78f3a2016-10-19 09:21:11 -0400395 AI SkNx() {}
396 AI SkNx(int32_t v) {
mtklein7c0db752016-07-30 14:18:49 -0700397 fVec = vdupq_n_s32(v);
398 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400399 AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d) {
mtklein7c0db752016-07-30 14:18:49 -0700400 fVec = (int32x4_t){a,b,c,d};
401 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400402 AI static SkNx Load(const void* ptr) {
mtklein7c0db752016-07-30 14:18:49 -0700403 return vld1q_s32((const int32_t*)ptr);
404 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400405 AI void store(void* ptr) const {
mtklein7c0db752016-07-30 14:18:49 -0700406 return vst1q_s32((int32_t*)ptr, fVec);
407 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400408 AI int32_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700409 SkASSERT(0 <= k && k < 4);
410 union { int32x4_t v; int32_t is[4]; } pun = {fVec};
411 return pun.is[k&3];
412 }
mtkleinf8f90e42016-03-21 10:04:46 -0700413
Mike Klein7c78f3a2016-10-19 09:21:11 -0400414 AI SkNx operator + (const SkNx& o) const { return vaddq_s32(fVec, o.fVec); }
415 AI SkNx operator - (const SkNx& o) const { return vsubq_s32(fVec, o.fVec); }
416 AI SkNx operator * (const SkNx& o) const { return vmulq_s32(fVec, o.fVec); }
mtkleinf8f90e42016-03-21 10:04:46 -0700417
Mike Klein7c78f3a2016-10-19 09:21:11 -0400418 AI SkNx operator & (const SkNx& o) const { return vandq_s32(fVec, o.fVec); }
419 AI SkNx operator | (const SkNx& o) const { return vorrq_s32(fVec, o.fVec); }
420 AI SkNx operator ^ (const SkNx& o) const { return veorq_s32(fVec, o.fVec); }
mtklein64f061a2016-06-17 12:09:16 -0700421
Mike Klein7c78f3a2016-10-19 09:21:11 -0400422 AI SkNx operator << (int bits) const { return fVec << SkNx(bits).fVec; }
423 AI SkNx operator >> (int bits) const { return fVec >> SkNx(bits).fVec; }
mtkleinf8f90e42016-03-21 10:04:46 -0700424
Mike Klein7c78f3a2016-10-19 09:21:11 -0400425 AI SkNx operator == (const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -0700426 return vreinterpretq_s32_u32(vceqq_s32(fVec, o.fVec));
427 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400428 AI SkNx operator < (const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -0700429 return vreinterpretq_s32_u32(vcltq_s32(fVec, o.fVec));
430 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400431 AI SkNx operator > (const SkNx& o) const {
mtklein7c0db752016-07-30 14:18:49 -0700432 return vreinterpretq_s32_u32(vcgtq_s32(fVec, o.fVec));
433 }
mtklein58e389b2016-07-15 07:00:11 -0700434
Mike Klein7c78f3a2016-10-19 09:21:11 -0400435 AI static SkNx Min(const SkNx& a, const SkNx& b) { return vminq_s32(a.fVec, b.fVec); }
Yuqian Li7da6ba22017-07-12 13:36:05 -0400436 AI static SkNx Max(const SkNx& a, const SkNx& b) { return vmaxq_s32(a.fVec, b.fVec); }
mtklein7c0db752016-07-30 14:18:49 -0700437 // TODO as needed
mtkleinf8f90e42016-03-21 10:04:46 -0700438
Mike Klein7c78f3a2016-10-19 09:21:11 -0400439 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein58e389b2016-07-15 07:00:11 -0700440 return vbslq_s32(vreinterpretq_u32_s32(fVec), t.fVec, e.fVec);
441 }
442
Yuqian Li7da6ba22017-07-12 13:36:05 -0400443 AI SkNx abs() const { return vabsq_s32(fVec); }
444
mtkleinf8f90e42016-03-21 10:04:46 -0700445 int32x4_t fVec;
446};
447
mtkleind05a8752016-07-29 10:10:15 -0700448template <>
449class SkNx<4, uint32_t> {
450public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400451 AI SkNx(const uint32x4_t& vec) : fVec(vec) {}
mtkleind05a8752016-07-29 10:10:15 -0700452
Mike Klein7c78f3a2016-10-19 09:21:11 -0400453 AI SkNx() {}
454 AI SkNx(uint32_t v) {
mtklein7c0db752016-07-30 14:18:49 -0700455 fVec = vdupq_n_u32(v);
456 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400457 AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
mtklein7c0db752016-07-30 14:18:49 -0700458 fVec = (uint32x4_t){a,b,c,d};
459 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400460 AI static SkNx Load(const void* ptr) {
mtklein7c0db752016-07-30 14:18:49 -0700461 return vld1q_u32((const uint32_t*)ptr);
462 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400463 AI void store(void* ptr) const {
mtklein7c0db752016-07-30 14:18:49 -0700464 return vst1q_u32((uint32_t*)ptr, fVec);
465 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400466 AI uint32_t operator[](int k) const {
mtklein7c0db752016-07-30 14:18:49 -0700467 SkASSERT(0 <= k && k < 4);
468 union { uint32x4_t v; uint32_t us[4]; } pun = {fVec};
469 return pun.us[k&3];
470 }
mtkleind05a8752016-07-29 10:10:15 -0700471
Mike Klein7c78f3a2016-10-19 09:21:11 -0400472 AI SkNx operator + (const SkNx& o) const { return vaddq_u32(fVec, o.fVec); }
473 AI SkNx operator - (const SkNx& o) const { return vsubq_u32(fVec, o.fVec); }
474 AI SkNx operator * (const SkNx& o) const { return vmulq_u32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700475
Mike Klein7c78f3a2016-10-19 09:21:11 -0400476 AI SkNx operator & (const SkNx& o) const { return vandq_u32(fVec, o.fVec); }
477 AI SkNx operator | (const SkNx& o) const { return vorrq_u32(fVec, o.fVec); }
478 AI SkNx operator ^ (const SkNx& o) const { return veorq_u32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700479
Mike Klein7c78f3a2016-10-19 09:21:11 -0400480 AI SkNx operator << (int bits) const { return fVec << SkNx(bits).fVec; }
481 AI SkNx operator >> (int bits) const { return fVec >> SkNx(bits).fVec; }
mtkleind05a8752016-07-29 10:10:15 -0700482
Mike Klein7c78f3a2016-10-19 09:21:11 -0400483 AI SkNx operator == (const SkNx& o) const { return vceqq_u32(fVec, o.fVec); }
484 AI SkNx operator < (const SkNx& o) const { return vcltq_u32(fVec, o.fVec); }
485 AI SkNx operator > (const SkNx& o) const { return vcgtq_u32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700486
Mike Klein7c78f3a2016-10-19 09:21:11 -0400487 AI static SkNx Min(const SkNx& a, const SkNx& b) { return vminq_u32(a.fVec, b.fVec); }
mtklein7c0db752016-07-30 14:18:49 -0700488 // TODO as needed
mtkleind05a8752016-07-29 10:10:15 -0700489
Mike Klein7c78f3a2016-10-19 09:21:11 -0400490 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleind05a8752016-07-29 10:10:15 -0700491 return vbslq_u32(fVec, t.fVec, e.fVec);
492 }
493
494 uint32x4_t fVec;
495};
496
Mike Klein7c78f3a2016-10-19 09:21:11 -0400497template<> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700498 return vcvtq_s32_f32(src.fVec);
499
500}
Mike Klein7c78f3a2016-10-19 09:21:11 -0400501template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700502 return vcvtq_f32_s32(src.fVec);
503}
Mike Klein7c78f3a2016-10-19 09:21:11 -0400504template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) {
mtkleind05a8752016-07-29 10:10:15 -0700505 return SkNx_cast<float>(Sk4i::Load(&src));
506}
mtkleinf8f90e42016-03-21 10:04:46 -0700507
Mike Klein7c78f3a2016-10-19 09:21:11 -0400508template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) {
mtkleinbe8c19e2016-02-19 09:40:24 -0800509 return vqmovn_u32(vcvtq_u32_f32(src.fVec));
510}
511
Mike Klein7c78f3a2016-10-19 09:21:11 -0400512template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) {
mtkleinbe8c19e2016-02-19 09:40:24 -0800513 return vcvtq_f32_u32(vmovl_u16(src.fVec));
514}
515
Mike Klein7c78f3a2016-10-19 09:21:11 -0400516template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) {
mtklein6f37b4a2015-12-14 11:25:18 -0800517 uint32x4_t _32 = vcvtq_u32_f32(src.fVec);
518 uint16x4_t _16 = vqmovn_u32(_32);
519 return vqmovn_u16(vcombine_u16(_16, _16));
520}
521
Mike Klein06a65e22016-11-17 12:39:09 -0500522template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint8_t>(const Sk4b& src) {
523 uint16x8_t _16 = vmovl_u8(src.fVec);
Mike Kleinb6ab4ae2016-11-17 14:33:11 -0500524 return vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(_16)));
Mike Klein06a65e22016-11-17 12:39:09 -0500525}
526
Mike Klein7c78f3a2016-10-19 09:21:11 -0400527template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) {
Mike Kleinb6ab4ae2016-11-17 14:33:11 -0500528 return vcvtq_f32_s32(SkNx_cast<int32_t>(src).fVec);
mtklein6f37b4a2015-12-14 11:25:18 -0800529}
530
Mike Klein7c78f3a2016-10-19 09:21:11 -0400531template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700532 Sk8f ab, cd;
533 SkNx_split(src, &ab, &cd);
534
535 Sk4f a,b,c,d;
536 SkNx_split(ab, &a, &b);
537 SkNx_split(cd, &c, &d);
538 return vuzpq_u8(vuzpq_u8((uint8x16_t)vcvtq_u32_f32(a.fVec),
539 (uint8x16_t)vcvtq_u32_f32(b.fVec)).val[0],
540 vuzpq_u8((uint8x16_t)vcvtq_u32_f32(c.fVec),
541 (uint8x16_t)vcvtq_u32_f32(d.fVec)).val[0]).val[0];
mtklein6f37b4a2015-12-14 11:25:18 -0800542}
543
Mike Klein7c78f3a2016-10-19 09:21:11 -0400544template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) {
mtklein550e9b02016-01-20 11:55:51 -0800545 return vget_low_u16(vmovl_u8(src.fVec));
546}
547
Mike Klein7c78f3a2016-10-19 09:21:11 -0400548template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) {
mtklein550e9b02016-01-20 11:55:51 -0800549 return vmovn_u16(vcombine_u16(src.fVec, src.fVec));
550}
551
Mike Klein7c78f3a2016-10-19 09:21:11 -0400552template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) {
msarett7d3ff712016-07-12 14:55:45 -0700553 uint16x4_t _16 = vqmovun_s32(src.fVec);
554 return vqmovn_u16(vcombine_u16(_16, _16));
555}
556
Mike Klein7c78f3a2016-10-19 09:21:11 -0400557template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) {
mtklein58e389b2016-07-15 07:00:11 -0700558 return vreinterpretq_s32_u32(vmovl_u16(src.fVec));
559}
560
Mike Klein7c78f3a2016-10-19 09:21:11 -0400561template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) {
mtklein58e389b2016-07-15 07:00:11 -0700562 return vmovn_u32(vreinterpretq_u32_s32(src.fVec));
563}
564
Mike Klein7c78f3a2016-10-19 09:21:11 -0400565template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) {
msarett15ee3de2016-08-02 11:30:30 -0700566 return vreinterpretq_s32_u32(src.fVec);
567}
568
Mike Klein7c78f3a2016-10-19 09:21:11 -0400569AI static Sk4i Sk4f_round(const Sk4f& x) {
msarett7d3ff712016-07-12 14:55:45 -0700570 return vcvtq_s32_f32((x + 0.5f).fVec);
571}
572
Mike Klein1e764642016-10-14 17:09:03 -0400573} // namespace
574
mtkleinc9adb052015-03-30 10:50:27 -0700575#endif//SkNx_neon_DEFINED