Marat Dukhan | 0f349c4 | 2019-11-27 11:58:54 -0800 | [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 % 16 == 0 |
| 7 | $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include <immintrin.h> |
| 11 | |
| 12 | #include <xnnpack/gemm.h> |
| 13 | |
| 14 | |
| 15 | void xnn_f32_gemm${"inc" if INC else ""}_ukernel_${MR}x${NR}__avx512f_broadcast( |
| 16 | size_t mr, |
| 17 | size_t nc, |
| 18 | size_t kc, |
| 19 | const float*restrict a, |
| 20 | size_t a_stride, |
| 21 | const float*restrict w, |
| 22 | float*restrict c, |
| 23 | size_t cm_stride, |
| 24 | size_t cn_stride, |
| 25 | $if INC: |
| 26 | const float*restrict acc, |
| 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(a != NULL); |
| 35 | assert(w != NULL); |
| 36 | assert(c != NULL); |
| 37 | $if INC: |
| 38 | assert(acc != NULL); |
| 39 | |
| 40 | const float* a0 = a; |
| 41 | float* c0 = c; |
| 42 | $for M in range(1, MR): |
| 43 | const float* a${M} = (const float*) ((uintptr_t) a${M-1} + a_stride); |
| 44 | float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride); |
| 45 | $if M % 2 == 0: |
| 46 | if XNN_UNPREDICTABLE(mr <= ${M}) { |
| 47 | a${M} = a${M-1}; |
| 48 | c${M} = c${M-1}; |
| 49 | } |
| 50 | $elif M + 1 == MR: |
| 51 | if XNN_UNPREDICTABLE(mr != ${M+1}) { |
| 52 | a${M} = a${M-1}; |
| 53 | c${M} = c${M-1}; |
| 54 | } |
| 55 | $else: |
| 56 | if XNN_UNPREDICTABLE(mr < ${M+1}) { |
| 57 | a${M} = a${M-1}; |
| 58 | c${M} = c${M-1}; |
| 59 | } |
| 60 | |
| 61 | do { |
| 62 | $if INC: |
| 63 | $for M in range(MR): |
| 64 | $for N in range(0, NR, 16): |
| 65 | __m512 vacc${M}x${ABC[N:N+16]} = _mm512_load_ps(acc + ${M*NR+N}); |
| 66 | acc += ${MR*NR}; |
| 67 | $else: |
| 68 | __m512 vacc0x${ABC[0:16]} = _mm512_load_ps(w); |
| 69 | $for N in range(16, NR, 16): |
| 70 | __m512 vacc0x${ABC[N:N+16]} = _mm512_load_ps(w + ${N}); |
| 71 | $for M in range(1, MR): |
| 72 | $for N in range(0, NR, 16): |
| 73 | __m512 vacc${M}x${ABC[N:N+16]} = vacc0x${ABC[N:N+16]}; |
| 74 | w += ${NR}; |
| 75 | |
| 76 | size_t k = kc; |
| 77 | do { |
| 78 | const __m512 vb${ABC[0:16]} = _mm512_load_ps(w); |
| 79 | $for N in range(16, NR, 16): |
| 80 | const __m512 vb${ABC[N:N+16]} = _mm512_load_ps(w + ${N}); |
| 81 | w += ${NR}; |
| 82 | |
| 83 | $for N in range(0, NR, 16): |
| 84 | $for M in range(MR): |
| 85 | vacc${M}x${ABC[N:N+16]} = _mm512_fmadd_ps(_mm512_set1_ps(*a${M}), vb${ABC[N:N+16]}, vacc${M}x${ABC[N:N+16]}); |
| 86 | |
| 87 | $for M in range(MR): |
| 88 | a${M} += 1; |
| 89 | |
| 90 | k -= sizeof(float); |
| 91 | } while (k != 0); |
| 92 | |
| 93 | const __m512 vmax = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.max)); |
| 94 | $for N in range(0, NR, 16): |
| 95 | $for M in range(MR): |
| 96 | vacc${M}x${ABC[N:N+16]} = _mm512_min_ps(vacc${M}x${ABC[N:N+16]}, vmax); |
| 97 | |
| 98 | const __m512 vmin = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.min)); |
| 99 | $for N in range(0, NR, 16): |
| 100 | $for M in range(MR): |
| 101 | vacc${M}x${ABC[N:N+16]} = _mm512_max_ps(vacc${M}x${ABC[N:N+16]}, vmin); |
| 102 | |
| 103 | if XNN_LIKELY(nc >= ${NR}) { |
| 104 | $for M in reversed(range(MR)): |
| 105 | _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]}); |
| 106 | $for N in range(16, NR, 16): |
| 107 | _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]}); |
| 108 | c${M} = (float*) ((uintptr_t) c${M} + cn_stride); |
| 109 | |
| 110 | $for M in reversed(range(MR)): |
| 111 | a${M} = (const float*) ((uintptr_t) a${M} - kc); |
| 112 | |
| 113 | nc -= ${NR}; |
| 114 | } else { |
| 115 | $for LOG2N in reversed(range(4, NR.bit_length())): |
| 116 | $if NR != 1 << LOG2N: |
| 117 | if (nc & ${1 << LOG2N}) { |
| 118 | $if LOG2N >= 4: |
| 119 | $for M in reversed(range(MR)): |
| 120 | _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]}); |
| 121 | $for N in range(16, 1 << LOG2N, 16): |
| 122 | _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]}); |
| 123 | |
| 124 | $for M in reversed(range(MR)): |
| 125 | $for N in range(0, 1 << (LOG2N - 1), 16): |
| 126 | vacc${M}x${ABC[N:N+16]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+16]}; |
| 127 | |
| 128 | $for M in reversed(range(MR)): |
| 129 | c${M} += ${1 << LOG2N}; |
| 130 | } |
| 131 | $if LOG2N == 4: |
| 132 | if (nc & 15) { |
| 133 | // Prepare mask for valid 32-bit elements (depends on nc). |
| 134 | const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << nc) - UINT32_C(1))); |
| 135 | |
| 136 | $for M in reversed(range(MR)): |
| 137 | _mm512_mask_storeu_ps(c${M}, vmask, vacc${M}x${ABC[0:16]}); |
| 138 | } |
| 139 | |
| 140 | nc = 0; |
| 141 | } |
| 142 | } while (nc != 0); |
| 143 | } |