| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <assert.h> |
| 12 |
|
| 13 |
#include <arm_neon.h> |
| 14 |
|
| 15 |
#include "xnnpack/igemm.h" |
| 16 |
#include "xnnpack/intrinsics-polyfill.h" |
| 17 |
|
| 18 |
|
| 19 |
void xnn_f16_igemm_minmax_ukernel_4x16__neonfp16arith_ld64( |
| 20 |
size_t mr, |
| 21 |
size_t nc, |
| 22 |
size_t kc, |
| 23 |
size_t ks, |
| 24 |
const xnn_float16** restrict a, |
| 25 |
const xnn_float16* restrict w, |
| 26 |
xnn_float16* restrict c, |
| 27 |
size_t cm_stride, |
| 28 |
size_t cn_stride, |
| 29 |
size_t a_offset, |
| 30 |
const xnn_float16* zero, |
| 31 |
const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) |
| 32 |
{ |
| 33 |
assert(mr != 0); |
| 34 |
assert(mr <= 4); |
| 35 |
assert(nc != 0); |
| 36 |
assert(kc != 0); |
| 37 |
assert(kc % sizeof(uint16_t) == 0); |
| 38 |
assert(ks != 0); |
| 39 |
assert(ks % (4 * sizeof(void*)) == 0); |
| 40 |
assert(a_offset % sizeof(uint16_t) == 0); |
| 41 |
assert(a != NULL); |
| 42 |
assert(w != NULL); |
| 43 |
assert(c != NULL); |
| 44 |
|
| 45 |
uint16_t* c0 = (uint16_t*) c; |
| 46 |
uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride); |
| 47 |
if XNN_UNPREDICTABLE(mr < 2) { |
| 48 |
c1 = c0; |
| 49 |
} |
| 50 |
uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride); |
| 51 |
if XNN_UNPREDICTABLE(mr <= 2) { |
| 52 |
c2 = c1; |
| 53 |
} |
| 54 |
uint16_t* c3 = (uint16_t*) ((uintptr_t) c2 + cm_stride); |
| 55 |
if XNN_UNPREDICTABLE(mr != 4) { |
| 56 |
c3 = c2; |
| 57 |
} |
| 58 |
|
| 59 |
do { |
| 60 |
float16x8_t vacc0x0 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 61 |
float16x8_t vacc0x1 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 62 |
float16x8_t vacc1x0 = vacc0x0; |
| 63 |
float16x8_t vacc1x1 = vacc0x1; |
| 64 |
float16x8_t vacc2x0 = vacc0x0; |
| 65 |
float16x8_t vacc2x1 = vacc0x1; |
| 66 |
float16x8_t vacc3x0 = vacc0x0; |
| 67 |
float16x8_t vacc3x1 = vacc0x1; |
| 68 |
|
| 69 |
size_t p = ks; |
| 70 |
do { |
| 71 |
const uint16_t* restrict a0 = (const uint16_t*) a[0]; |
| 72 |
assert(a0 != NULL); |
| 73 |
if XNN_UNPREDICTABLE(a0 != (const uint16_t*) zero) { |
| 74 |
a0 = (const uint16_t*) ((uintptr_t) a0 + a_offset); |
| 75 |
} |
| 76 |
const uint16_t* restrict a1 = (const uint16_t*) a[1]; |
| 77 |
assert(a1 != NULL); |
| 78 |
if XNN_UNPREDICTABLE(a1 != (const uint16_t*) zero) { |
| 79 |
a1 = (const uint16_t*) ((uintptr_t) a1 + a_offset); |
| 80 |
} |
| 81 |
const uint16_t* restrict a2 = (const uint16_t*) a[2]; |
| 82 |
assert(a2 != NULL); |
| 83 |
if XNN_UNPREDICTABLE(a2 != (const uint16_t*) zero) { |
| 84 |
a2 = (const uint16_t*) ((uintptr_t) a2 + a_offset); |
| 85 |
} |
| 86 |
const uint16_t* restrict a3 = (const uint16_t*) a[3]; |
| 87 |
assert(a3 != NULL); |
| 88 |
if XNN_UNPREDICTABLE(a3 != (const uint16_t*) zero) { |
| 89 |
a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset); |
| 90 |
} |
| 91 |
a += 4; |
| 92 |
|
| 93 |
size_t k = kc; |
| 94 |
for (; k >= 4 * sizeof(uint16_t); k -= 4 * sizeof(uint16_t)) { |
| 95 |
const float16x4_t va0 = vreinterpret_f16_u16(vld1_u16(a0)); a0 += 4; |
| 96 |
const float16x4_t va1 = vreinterpret_f16_u16(vld1_u16(a1)); a1 += 4; |
| 97 |
const float16x4_t va2 = vreinterpret_f16_u16(vld1_u16(a2)); a2 += 4; |
| 98 |
const float16x4_t va3 = vreinterpret_f16_u16(vld1_u16(a3)); a3 += 4; |
| 99 |
|
| 100 |
const float16x8_t vb0c0 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 101 |
const float16x8_t vb1c0 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 102 |
|
| 103 |
#if XNN_ARCH_ARM64 |
| 104 |
vacc0x0 = vfmaq_lane_f16(vacc0x0, vb0c0, va0, 0); |
| 105 |
vacc1x0 = vfmaq_lane_f16(vacc1x0, vb0c0, va1, 0); |
| 106 |
vacc2x0 = vfmaq_lane_f16(vacc2x0, vb0c0, va2, 0); |
| 107 |
vacc3x0 = vfmaq_lane_f16(vacc3x0, vb0c0, va3, 0); |
| 108 |
vacc0x1 = vfmaq_lane_f16(vacc0x1, vb1c0, va0, 0); |
| 109 |
vacc1x1 = vfmaq_lane_f16(vacc1x1, vb1c0, va1, 0); |
| 110 |
vacc2x1 = vfmaq_lane_f16(vacc2x1, vb1c0, va2, 0); |
| 111 |
vacc3x1 = vfmaq_lane_f16(vacc3x1, vb1c0, va3, 0); |
| 112 |
#else |
| 113 |
vacc0x0 = vmlaq_lane_f16(vacc0x0, vb0c0, va0, 0); |
| 114 |
vacc1x0 = vmlaq_lane_f16(vacc1x0, vb0c0, va1, 0); |
| 115 |
vacc2x0 = vmlaq_lane_f16(vacc2x0, vb0c0, va2, 0); |
| 116 |
vacc3x0 = vmlaq_lane_f16(vacc3x0, vb0c0, va3, 0); |
| 117 |
vacc0x1 = vmlaq_lane_f16(vacc0x1, vb1c0, va0, 0); |
| 118 |
vacc1x1 = vmlaq_lane_f16(vacc1x1, vb1c0, va1, 0); |
| 119 |
vacc2x1 = vmlaq_lane_f16(vacc2x1, vb1c0, va2, 0); |
| 120 |
vacc3x1 = vmlaq_lane_f16(vacc3x1, vb1c0, va3, 0); |
| 121 |
#endif |
| 122 |
const float16x8_t vb0c1 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 123 |
const float16x8_t vb1c1 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 124 |
|
| 125 |
#if XNN_ARCH_ARM64 |
| 126 |
vacc0x0 = vfmaq_lane_f16(vacc0x0, vb0c1, va0, 1); |
| 127 |
vacc1x0 = vfmaq_lane_f16(vacc1x0, vb0c1, va1, 1); |
| 128 |
vacc2x0 = vfmaq_lane_f16(vacc2x0, vb0c1, va2, 1); |
| 129 |
vacc3x0 = vfmaq_lane_f16(vacc3x0, vb0c1, va3, 1); |
| 130 |
vacc0x1 = vfmaq_lane_f16(vacc0x1, vb1c1, va0, 1); |
| 131 |
vacc1x1 = vfmaq_lane_f16(vacc1x1, vb1c1, va1, 1); |
| 132 |
vacc2x1 = vfmaq_lane_f16(vacc2x1, vb1c1, va2, 1); |
| 133 |
vacc3x1 = vfmaq_lane_f16(vacc3x1, vb1c1, va3, 1); |
| 134 |
#else |
| 135 |
vacc0x0 = vmlaq_lane_f16(vacc0x0, vb0c1, va0, 1); |
| 136 |
vacc1x0 = vmlaq_lane_f16(vacc1x0, vb0c1, va1, 1); |
| 137 |
vacc2x0 = vmlaq_lane_f16(vacc2x0, vb0c1, va2, 1); |
| 138 |
vacc3x0 = vmlaq_lane_f16(vacc3x0, vb0c1, va3, 1); |
| 139 |
vacc0x1 = vmlaq_lane_f16(vacc0x1, vb1c1, va0, 1); |
| 140 |
vacc1x1 = vmlaq_lane_f16(vacc1x1, vb1c1, va1, 1); |
| 141 |
vacc2x1 = vmlaq_lane_f16(vacc2x1, vb1c1, va2, 1); |
| 142 |
vacc3x1 = vmlaq_lane_f16(vacc3x1, vb1c1, va3, 1); |
| 143 |
#endif |
| 144 |
const float16x8_t vb0c2 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 145 |
const float16x8_t vb1c2 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 146 |
|
| 147 |
#if XNN_ARCH_ARM64 |
| 148 |
vacc0x0 = vfmaq_lane_f16(vacc0x0, vb0c2, va0, 2); |
| 149 |
vacc1x0 = vfmaq_lane_f16(vacc1x0, vb0c2, va1, 2); |
| 150 |
vacc2x0 = vfmaq_lane_f16(vacc2x0, vb0c2, va2, 2); |
| 151 |
vacc3x0 = vfmaq_lane_f16(vacc3x0, vb0c2, va3, 2); |
| 152 |
vacc0x1 = vfmaq_lane_f16(vacc0x1, vb1c2, va0, 2); |
| 153 |
vacc1x1 = vfmaq_lane_f16(vacc1x1, vb1c2, va1, 2); |
| 154 |
vacc2x1 = vfmaq_lane_f16(vacc2x1, vb1c2, va2, 2); |
| 155 |
vacc3x1 = vfmaq_lane_f16(vacc3x1, vb1c2, va3, 2); |
| 156 |
#else |
| 157 |
vacc0x0 = vmlaq_lane_f16(vacc0x0, vb0c2, va0, 2); |
| 158 |
vacc1x0 = vmlaq_lane_f16(vacc1x0, vb0c2, va1, 2); |
| 159 |
vacc2x0 = vmlaq_lane_f16(vacc2x0, vb0c2, va2, 2); |
| 160 |
vacc3x0 = vmlaq_lane_f16(vacc3x0, vb0c2, va3, 2); |
| 161 |
vacc0x1 = vmlaq_lane_f16(vacc0x1, vb1c2, va0, 2); |
| 162 |
vacc1x1 = vmlaq_lane_f16(vacc1x1, vb1c2, va1, 2); |
| 163 |
vacc2x1 = vmlaq_lane_f16(vacc2x1, vb1c2, va2, 2); |
| 164 |
vacc3x1 = vmlaq_lane_f16(vacc3x1, vb1c2, va3, 2); |
| 165 |
#endif |
| 166 |
const float16x8_t vb0c3 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 167 |
const float16x8_t vb1c3 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 168 |
|
| 169 |
#if XNN_ARCH_ARM64 |
| 170 |
vacc0x0 = vfmaq_lane_f16(vacc0x0, vb0c3, va0, 3); |
| 171 |
vacc1x0 = vfmaq_lane_f16(vacc1x0, vb0c3, va1, 3); |
| 172 |
vacc2x0 = vfmaq_lane_f16(vacc2x0, vb0c3, va2, 3); |
| 173 |
vacc3x0 = vfmaq_lane_f16(vacc3x0, vb0c3, va3, 3); |
| 174 |
vacc0x1 = vfmaq_lane_f16(vacc0x1, vb1c3, va0, 3); |
| 175 |
vacc1x1 = vfmaq_lane_f16(vacc1x1, vb1c3, va1, 3); |
| 176 |
vacc2x1 = vfmaq_lane_f16(vacc2x1, vb1c3, va2, 3); |
| 177 |
vacc3x1 = vfmaq_lane_f16(vacc3x1, vb1c3, va3, 3); |
| 178 |
#else |
| 179 |
vacc0x0 = vmlaq_lane_f16(vacc0x0, vb0c3, va0, 3); |
| 180 |
vacc1x0 = vmlaq_lane_f16(vacc1x0, vb0c3, va1, 3); |
| 181 |
vacc2x0 = vmlaq_lane_f16(vacc2x0, vb0c3, va2, 3); |
| 182 |
vacc3x0 = vmlaq_lane_f16(vacc3x0, vb0c3, va3, 3); |
| 183 |
vacc0x1 = vmlaq_lane_f16(vacc0x1, vb1c3, va0, 3); |
| 184 |
vacc1x1 = vmlaq_lane_f16(vacc1x1, vb1c3, va1, 3); |
| 185 |
vacc2x1 = vmlaq_lane_f16(vacc2x1, vb1c3, va2, 3); |
| 186 |
vacc3x1 = vmlaq_lane_f16(vacc3x1, vb1c3, va3, 3); |
| 187 |
#endif |
| 188 |
} |
| 189 |
if XNN_UNLIKELY(k != 0) { |
| 190 |
do { |
| 191 |
const float16x8_t va0 = vreinterpretq_f16_u16(vld1q_dup_u16(a0)); a0 += 1; |
| 192 |
const float16x8_t va1 = vreinterpretq_f16_u16(vld1q_dup_u16(a1)); a1 += 1; |
| 193 |
const float16x8_t va2 = vreinterpretq_f16_u16(vld1q_dup_u16(a2)); a2 += 1; |
| 194 |
const float16x8_t va3 = vreinterpretq_f16_u16(vld1q_dup_u16(a3)); a3 += 1; |
| 195 |
|
| 196 |
const float16x8_t vb0 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 197 |
const float16x8_t vb1 = vreinterpretq_f16_u16(vld1q_u16((const uint16_t*) w)); w = (const xnn_float16*) w + 8; |
| 198 |
|
| 199 |
vacc0x0 = vfmaq_f16(vacc0x0, va0, vb0); |
| 200 |
vacc1x0 = vfmaq_f16(vacc1x0, va1, vb0); |
| 201 |
vacc2x0 = vfmaq_f16(vacc2x0, va2, vb0); |
| 202 |
vacc3x0 = vfmaq_f16(vacc3x0, va3, vb0); |
| 203 |
vacc0x1 = vfmaq_f16(vacc0x1, va0, vb1); |
| 204 |
vacc1x1 = vfmaq_f16(vacc1x1, va1, vb1); |
| 205 |
vacc2x1 = vfmaq_f16(vacc2x1, va2, vb1); |
| 206 |
vacc3x1 = vfmaq_f16(vacc3x1, va3, vb1); |
| 207 |
|
| 208 |
k -= sizeof(uint16_t); |
| 209 |
} while (k != 0); |
| 210 |
} |
| 211 |
p -= 4 * sizeof(void*); |
| 212 |
} while (p != 0); |
| 213 |
|
| 214 |
|
| 215 |
const float16x8_t vmin = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) ¶ms->scalar.min)); |
| 216 |
vacc0x0 = vmaxq_f16(vacc0x0, vmin); |
| 217 |
vacc1x0 = vmaxq_f16(vacc1x0, vmin); |
| 218 |
vacc2x0 = vmaxq_f16(vacc2x0, vmin); |
| 219 |
vacc3x0 = vmaxq_f16(vacc3x0, vmin); |
| 220 |
vacc0x1 = vmaxq_f16(vacc0x1, vmin); |
| 221 |
vacc1x1 = vmaxq_f16(vacc1x1, vmin); |
| 222 |
vacc2x1 = vmaxq_f16(vacc2x1, vmin); |
| 223 |
vacc3x1 = vmaxq_f16(vacc3x1, vmin); |
| 224 |
|
| 225 |
const float16x8_t vmax = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) ¶ms->scalar.max)); |
| 226 |
vacc0x0 = vminq_f16(vacc0x0, vmax); |
| 227 |
vacc1x0 = vminq_f16(vacc1x0, vmax); |
| 228 |
vacc2x0 = vminq_f16(vacc2x0, vmax); |
| 229 |
vacc3x0 = vminq_f16(vacc3x0, vmax); |
| 230 |
vacc0x1 = vminq_f16(vacc0x1, vmax); |
| 231 |
vacc1x1 = vminq_f16(vacc1x1, vmax); |
| 232 |
vacc2x1 = vminq_f16(vacc2x1, vmax); |
| 233 |
vacc3x1 = vminq_f16(vacc3x1, vmax); |
| 234 |
|
| 235 |
if XNN_LIKELY(nc >= 16) { |
| 236 |
vst1q_u16(c3, vreinterpretq_u16_f16(vacc3x0)); |
| 237 |
vst1q_u16(c3 + 8, vreinterpretq_u16_f16(vacc3x1)); |
| 238 |
c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride); |
| 239 |
vst1q_u16(c2, vreinterpretq_u16_f16(vacc2x0)); |
| 240 |
vst1q_u16(c2 + 8, vreinterpretq_u16_f16(vacc2x1)); |
| 241 |
c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride); |
| 242 |
vst1q_u16(c1, vreinterpretq_u16_f16(vacc1x0)); |
| 243 |
vst1q_u16(c1 + 8, vreinterpretq_u16_f16(vacc1x1)); |
| 244 |
c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride); |
| 245 |
vst1q_u16(c0, vreinterpretq_u16_f16(vacc0x0)); |
| 246 |
vst1q_u16(c0 + 8, vreinterpretq_u16_f16(vacc0x1)); |
| 247 |
c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride); |
| 248 |
|
| 249 |
a = (const xnn_float16**restrict) ((uintptr_t) a - ks); |
| 250 |
nc -= 16; |
| 251 |
} else { |
| 252 |
if (nc & 8) { |
| 253 |
vst1q_u16(c3, vreinterpretq_u16_f16(vacc3x0)); c3 += 8; |
| 254 |
vst1q_u16(c2, vreinterpretq_u16_f16(vacc2x0)); c2 += 8; |
| 255 |
vst1q_u16(c1, vreinterpretq_u16_f16(vacc1x0)); c1 += 8; |
| 256 |
vst1q_u16(c0, vreinterpretq_u16_f16(vacc0x0)); c0 += 8; |
| 257 |
|
| 258 |
vacc3x0 = vacc3x1; |
| 259 |
vacc2x0 = vacc2x1; |
| 260 |
vacc1x0 = vacc1x1; |
| 261 |
vacc0x0 = vacc0x1; |
| 262 |
} |
| 263 |
float16x4_t vacc3 = vget_low_f16(vacc3x0); |
| 264 |
float16x4_t vacc2 = vget_low_f16(vacc2x0); |
| 265 |
float16x4_t vacc1 = vget_low_f16(vacc1x0); |
| 266 |
float16x4_t vacc0 = vget_low_f16(vacc0x0); |
| 267 |
if (nc & 4) { |
| 268 |
vst1_u16(c3, vreinterpret_u16_f16(vacc3)); c3 += 4; |
| 269 |
vst1_u16(c2, vreinterpret_u16_f16(vacc2)); c2 += 4; |
| 270 |
vst1_u16(c1, vreinterpret_u16_f16(vacc1)); c1 += 4; |
| 271 |
vst1_u16(c0, vreinterpret_u16_f16(vacc0)); c0 += 4; |
| 272 |
|
| 273 |
vacc3 = vget_high_f16(vacc3x0); |
| 274 |
vacc2 = vget_high_f16(vacc2x0); |
| 275 |
vacc1 = vget_high_f16(vacc1x0); |
| 276 |
vacc0 = vget_high_f16(vacc0x0); |
| 277 |
} |
| 278 |
if (nc & 2) { |
| 279 |
vst1_lane_u32((void*) c3, vreinterpret_u32_f16(vacc3), 0); c3 += 2; |
| 280 |
vst1_lane_u32((void*) c2, vreinterpret_u32_f16(vacc2), 0); c2 += 2; |
| 281 |
vst1_lane_u32((void*) c1, vreinterpret_u32_f16(vacc1), 0); c1 += 2; |
| 282 |
vst1_lane_u32((void*) c0, vreinterpret_u32_f16(vacc0), 0); c0 += 2; |
| 283 |
|
| 284 |
vacc3 = vext_f16(vacc3, vacc3, 2); |
| 285 |
vacc2 = vext_f16(vacc2, vacc2, 2); |
| 286 |
vacc1 = vext_f16(vacc1, vacc1, 2); |
| 287 |
vacc0 = vext_f16(vacc0, vacc0, 2); |
| 288 |
} |
| 289 |
if (nc & 1) { |
| 290 |
vst1_lane_u16(c3, vreinterpret_u16_f16(vacc3), 0); |
| 291 |
vst1_lane_u16(c2, vreinterpret_u16_f16(vacc2), 0); |
| 292 |
vst1_lane_u16(c1, vreinterpret_u16_f16(vacc1), 0); |
| 293 |
vst1_lane_u16(c0, vreinterpret_u16_f16(vacc0), 0); |
| 294 |
} |
| 295 |
|
| 296 |
nc = 0; |
| 297 |
} |
| 298 |
} while (nc != 0); |
| 299 |
} |