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 CHANNEL_TILE % 4 == 0 7$assert CHANNEL_TILE >= 4 8$assert ROW_TILE >= 1 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <arm_neon.h> 13 14#include <xnnpack/math.h> 15#include <xnnpack/prelu.h> 16 17 18void xnn_f32_prelu_ukernel__neon_${ROW_TILE}x${CHANNEL_TILE}( 19 size_t rows, 20 size_t channels, 21 const float*restrict input, 22 size_t input_stride, 23 const float*restrict weights, 24 float*restrict output, 25 size_t output_stride) XNN_DISABLE_TSAN 26{ 27 assert(rows != 0); 28 assert(channels != 0); 29 assert(channels % sizeof(float) == 0); 30 31 const float* i0 = input; 32 float* o0 = output; 33 $for M in range(1, ROW_TILE): 34 const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride); 35 float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride); 36 $if M % 2 == 0: 37 if XNN_UNPREDICTABLE(rows <= ${M}) { 38 i${M} = i${M-1}; 39 o${M} = o${M-1}; 40 } 41 $else: 42 if XNN_UNPREDICTABLE(rows < ${M+1}) { 43 i${M} = i${M-1}; 44 o${M} = o${M-1}; 45 } 46 47 const size_t input_increment = input_stride * ${ROW_TILE} - channels; 48 const size_t output_increment = output_stride * ${ROW_TILE} - channels; 49 50 do { 51 const float* w = weights; 52 size_t c = channels; 53 for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) { 54 $for C in range(0, CHANNEL_TILE, 4): 55 const float32x4_t vw${ABC[C:C+4]} = vld1q_f32(w); w += 4; 56 57 $for M in range(ROW_TILE): 58 $for C in range(0, CHANNEL_TILE, 4): 59 const float32x4_t vi${M}x${ABC[C:C+4]} = vld1q_f32(i${M}); i${M} += 4; 60 61 $for M in range(ROW_TILE): 62 $for C in range(0, CHANNEL_TILE, 4): 63 float32x4_t vacc${M}x${ABC[C:C+4]} = vmulq_f32(vi${M}x${ABC[C:C+4]}, vw${ABC[C:C+4]}); 64 const uint32x4_t vm${M}x${ABC[C:C+4]} = vcltq_s32(vreinterpretq_s32_f32(vi${M}x${ABC[C:C+4]}), vmovq_n_s32(0)); 65 66 $for M in range(ROW_TILE): 67 $for C in range(0, CHANNEL_TILE, 4): 68 vacc${M}x${ABC[C:C+4]} = vbslq_f32(vm${M}x${ABC[C:C+4]}, vacc${M}x${ABC[C:C+4]}, vi${M}x${ABC[C:C+4]}); 69 70 $for M in range(ROW_TILE): 71 $for C in range(0, CHANNEL_TILE, 4): 72 vst1q_f32(o${M}, vacc${M}x${ABC[C:C+4]}); o${M} += 4; 73 } 74 $if CHANNEL_TILE != 4: 75 for (; c >= 4 * sizeof(float); c -= 4 * sizeof(float)) { 76 const float32x4_t vw0123 = vld1q_f32(w); w += 4; 77 78 $for M in range(ROW_TILE): 79 const float32x4_t vi${M}x0123 = vld1q_f32(i${M}); 80 i${M} += 4; 81 82 $for M in range(ROW_TILE): 83 float32x4_t vacc${M}x0123 = vmulq_f32(vi${M}x0123, vw0123); 84 const uint32x4_t vm${M}x0123 = vcltq_s32(vreinterpretq_s32_f32(vi${M}x0123), vmovq_n_s32(0)); 85 86 $for M in range(ROW_TILE): 87 vacc${M}x0123 = vbslq_f32(vm${M}x0123, vacc${M}x0123, vi${M}x0123); 88 89 $for M in range(ROW_TILE): 90 vst1q_f32(o${M}, vacc${M}x0123); o${M} += 4; 91 } 92 if XNN_UNLIKELY(c != 0) { 93 const float32x4_t vw0123 = vld1q_f32(w); w += 4; 94 95 $for M in range(ROW_TILE): 96 const float32x4_t vi${M}x0123 = vld1q_f32(i${M}); 97 i${M} = (const float*) ((uintptr_t) i${M} + c); 98 99 $for M in range(ROW_TILE): 100 float32x4_t vacc${M}x0123 = vmulq_f32(vi${M}x0123, vw0123); 101 const uint32x4_t vm${M}x0123 = vcltq_s32(vreinterpretq_s32_f32(vi${M}x0123), vmovq_n_s32(0)); 102 103 $for M in range(ROW_TILE): 104 vacc${M}x0123 = vbslq_f32(vm${M}x0123, vacc${M}x0123, vi${M}x0123); 105 106 $for M in range(ROW_TILE): 107 float32x2_t vacc${M}x01 = vget_low_f32(vacc${M}x0123); 108 if (c & (2 * sizeof(float))) { 109 $for M in range(ROW_TILE): 110 vst1_f32(o${M}, vacc${M}x01); o${M} += 2; 111 112 $for M in range(ROW_TILE): 113 vacc${M}x01 = vget_high_f32(vacc${M}x0123); 114 } 115 if (c & (1 * sizeof(float))) { 116 $for M in range(ROW_TILE): 117 vst1_lane_f32(o${M}, vacc${M}x01, 0); o${M} += 1; 118 } 119 } 120 $for M in range(ROW_TILE): 121 i${M} = (const float*) ((uintptr_t) i${M} + input_increment); 122 o${M} = (float*) ((uintptr_t) o${M} + output_increment); 123 $if M % 2 == 1: 124 if XNN_UNPREDICTABLE(rows < ${ROW_TILE+M+1}) { 125 i${M} = i${M-1}; 126 o${M} = o${M-1}; 127 } 128 $elif M != 0: 129 if XNN_UNPREDICTABLE(rows <= ${ROW_TILE+M}) { 130 i${M} = i${M-1}; 131 o${M} = o${M-1}; 132 } 133 rows = doz(rows, ${ROW_TILE}); 134 } while (rows != 0); 135} 136