XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame^] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | $assert NR == 2 |
| 7 | $assert MR % 2 == 0 |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include <xmmintrin.h> |
| 11 | |
| 12 | #include <xnnpack/igemm.h> |
| 13 | |
| 14 | |
| 15 | void xnn_f32_igemm_ukernel_${MR}x${NR}c4__sse( |
| 16 | size_t mr, |
| 17 | size_t nc, |
| 18 | size_t kc, |
| 19 | size_t ks, |
| 20 | const float**restrict a, |
| 21 | const float*restrict w, |
| 22 | float*restrict c, |
| 23 | size_t cm_stride, |
| 24 | size_t cn_stride, |
| 25 | size_t a_offset, |
| 26 | const float* zero, |
| 27 | const union xnn_f32_output_params params[restrict static 1]) |
| 28 | { |
| 29 | assert(mr != 0); |
| 30 | assert(mr <= ${MR}); |
| 31 | assert(nc != 0); |
| 32 | assert(kc != 0); |
| 33 | assert(kc % sizeof(float) == 0); |
| 34 | assert(ks != 0); |
| 35 | assert(ks % (${MR} * sizeof(void*)) == 0); |
| 36 | assert(a_offset % sizeof(float) == 0); |
| 37 | |
| 38 | float* c0 = c; |
| 39 | $for M in range(1, MR): |
| 40 | float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride); |
| 41 | $if M % 2 == 0: |
| 42 | if XNN_UNPREDICTABLE(mr <= ${M}) { |
| 43 | c${M} = c${M-1}; |
| 44 | } |
| 45 | $elif M + 1 == MR: |
| 46 | if XNN_UNPREDICTABLE(mr != ${M+1}) { |
| 47 | c${M} = c${M-1}; |
| 48 | } |
| 49 | $else: |
| 50 | if XNN_UNPREDICTABLE(mr < ${M+1}) { |
| 51 | c${M} = c${M-1}; |
| 52 | } |
| 53 | |
| 54 | do { |
| 55 | __m128 vacc0x0c4 = _mm_load_ss(w); |
| 56 | $for N in range(1, NR): |
| 57 | __m128 vacc0x${N}c4 = _mm_load_ss(w + ${N}); |
| 58 | $for M in range(1, MR): |
| 59 | $for N in range(NR): |
| 60 | __m128 vacc${M}x${N}c4 = vacc0x${N}c4; |
| 61 | w += ${NR}; |
| 62 | |
| 63 | size_t p = ks; |
| 64 | do { |
| 65 | $for M in range(MR): |
| 66 | const float* restrict a${M} = a[${M}]; |
| 67 | if XNN_UNPREDICTABLE(a${M} != zero) { |
| 68 | a${M} = (const float*) ((uintptr_t) a${M} + a_offset); |
| 69 | } |
| 70 | a += ${MR}; |
| 71 | |
| 72 | size_t k = kc; |
| 73 | for (; k >= 4 * sizeof(float); k -= 4 * sizeof(float)) { |
| 74 | $for M in range(MR): |
| 75 | const __m128 va${M} = _mm_loadu_ps(a${M}); |
| 76 | a${M} += 4; |
| 77 | |
| 78 | const __m128 vb0 = _mm_loadu_ps(w); |
| 79 | $for N in range(1, NR): |
| 80 | const __m128 vb${N} = _mm_loadu_ps(w + ${N * 4}); |
| 81 | w += ${NR * 4}; |
| 82 | |
| 83 | $for M in range(MR): |
| 84 | $for N in range(NR): |
| 85 | vacc${M}x${N}c4 = _mm_add_ps(vacc${M}x${N}c4, _mm_mul_ps(va${M}, vb${N})); |
| 86 | } |
| 87 | if XNN_UNLIKELY(k != 0) { |
| 88 | $for M in range(MR): |
| 89 | const __m128 va${M} = _mm_loadu_ps(a${M}); |
| 90 | |
| 91 | const __m128 vb0 = _mm_loadu_ps(w); |
| 92 | $for N in range(1, NR): |
| 93 | const __m128 vb${N} = _mm_loadu_ps(w + ${N * 4}); |
| 94 | w += ${NR * 4}; |
| 95 | |
| 96 | $for N in range(NR): |
| 97 | const __m128 vmask${N} = _mm_cmpeq_ps(_mm_setzero_ps(), vb${N}); |
| 98 | |
| 99 | $for M in range(MR): |
| 100 | $for N in range(NR): |
| 101 | vacc${M}x${N}c4 = _mm_add_ps(vacc${M}x${N}c4, _mm_mul_ps(_mm_andnot_ps(vmask${N}, va${M}), vb${N})); |
| 102 | } |
| 103 | p -= ${MR} * sizeof(void*); |
| 104 | } while (p != 0); |
| 105 | |
| 106 | $for M in range(MR): |
| 107 | const __m128 vacc${M}x01c2 = _mm_add_ps(_mm_unpacklo_ps(vacc${M}x0c4, vacc${M}x1c4), _mm_unpackhi_ps(vacc${M}x0c4, vacc${M}x1c4)); |
| 108 | |
| 109 | $for M in range(0, MR, 2): |
| 110 | __m128 vacc${M}${M+1}x01 = _mm_add_ps(_mm_movelh_ps(vacc${M}x01c2, vacc${M+1}x01c2), _mm_movehl_ps(vacc${M+1}x01c2, vacc${M}x01c2)); |
| 111 | |
| 112 | const __m128 vmax = _mm_load_ps(params->sse.max); |
| 113 | $for M in range(0, MR, 2): |
| 114 | vacc${M}${M+1}x01 = _mm_min_ps(vacc${M}${M+1}x01, vmax); |
| 115 | |
| 116 | const __m128 vmin = _mm_load_ps(params->sse.min); |
| 117 | $for M in range(0, MR, 2): |
| 118 | vacc${M}${M+1}x01 = _mm_max_ps(vacc${M}${M+1}x01, vmin); |
| 119 | |
| 120 | if XNN_LIKELY(nc >= ${NR}) { |
| 121 | $for M in reversed(range(0, MR, 2)): |
| 122 | _mm_storeh_pi((__m64*) c${M+1}, vacc${M}${M+1}x01); |
| 123 | c${M+1} = (float*) ((uintptr_t) c${M+1} + cn_stride); |
| 124 | _mm_storel_pi((__m64*) c${M}, vacc${M}${M+1}x01); |
| 125 | c${M} = (float*) ((uintptr_t) c${M} + cn_stride); |
| 126 | |
| 127 | a = (const float**restrict) ((uintptr_t) a - ks); |
| 128 | nc -= ${NR}; |
| 129 | } else { |
| 130 | assert(nc == 1); |
| 131 | $for M in reversed(range(0, MR, 2)): |
| 132 | _mm_store_ss(c${M+1}, _mm_movehl_ps(vacc${M}${M+1}x01, vacc${M}${M+1}x01)); |
| 133 | _mm_store_ss(c${M}, vacc${M}${M+1}x01); |
| 134 | |
| 135 | nc = 0; |
| 136 | } |
| 137 | } while (nc != 0); |
| 138 | } |