blob: 16dfbbb92994fa8f5066bd62ae9641b911bc6b31 [file] [log] [blame]
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
$assert MR % 4 == 0
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>
#include <arm_neon.h>
#include <xnnpack/spmm.h>
void xnn_f32_spmm_minmax_ukernel_${MR}x${NR}__${"neonfma" if FMA else "neon"}${"_x" + str(UNROLL) if UNROLL > 1 else ""}(
uint32_t batch_size,
uint32_t output_channels,
const float*restrict input,
const float*restrict weights,
const int32_t*restrict widx_dmap,
const uint32_t*restrict nidx_nnzmap,
float*restrict output,
const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
assert(batch_size != 0);
const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
size_t n = batch_size;
while XNN_LIKELY(n >= ${MR}) {
const float*restrict w = weights;
const int32_t* dmap = widx_dmap;
const uint32_t* nnzmap = nidx_nnzmap;
size_t c = output_channels;
do {
uint32_t nnz = *nnzmap++;
$if UNROLL > 1:
float32x4_t vacc0123x0 = vld1q_dup_f32(w); w += 1;
$for K in range(1, UNROLL):
float32x4_t vacc0123x${K} = vmovq_n_f32(0.0f);
$for M in range(4, MR, 4):
float32x4_t vacc${ABC[M:M+4]}x0 = vacc0123x0;
$for K in range(1, UNROLL):
float32x4_t vacc${ABC[M:M+4]}x${K} = vmovq_n_f32(0.0f);
for (; nnz >= ${UNROLL}; nnz -= ${UNROLL}) {
$for K in range(UNROLL):
const intptr_t diff${K} = dmap[${K}];
dmap += ${UNROLL};
$for K in range(UNROLL):
const float32x4_t vi0123x${K} = vld1q_f32(input);
$for M in range(4, MR, 4):
const float32x4_t vi${ABC[M:M+4]}x${K} = vld1q_f32(input + ${M});
input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff${K});
$for M in range(0, MR, 16):
__builtin_prefetch(input + ${M+16});
const float32x4_t vw${K} = vld1q_dup_f32(w); w += 1;
__builtin_prefetch(w + 32);
$for M in range(0, MR, 4):
vacc${ABC[M:M+4]}x${K} = vfmaq_f32(vacc${ABC[M:M+4]}x${K}, vi${ABC[M:M+4]}x${K}, vw${K});
}
$for M in range(0, MR, 4):
float32x4_t vacc${ABC[M:M+4]} = vacc${ABC[M:M+4]}x0;
$for K in range(1, UNROLL):
$for M in range(0, MR, 4):
vacc${ABC[M:M+4]} = vaddq_f32(vacc${ABC[M:M+4]}, vacc${ABC[M:M+4]}x${K});
$else:
float32x4_t vacc0123 = vld1q_dup_f32(w); w += 1;
$for M in range(4, MR, 4):
float32x4_t vacc${ABC[M:M+4]} = vacc0123;
if XNN_LIKELY(nnz != 0) {
do {
const intptr_t diff = *dmap++;
const float32x4_t vi0123 = vld1q_f32(input);
$for M in range(4, MR, 4):
const float32x4_t vi${ABC[M:M+4]} = vld1q_f32(input + ${M});
input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
$for M in range(0, MR, 16):
__builtin_prefetch(input + ${M+16});
const float32x4_t vw = vld1q_dup_f32(w); w += 1;
__builtin_prefetch(w + 32);
$for M in range(0, MR, 4):
vacc${ABC[M:M+4]} = vfmaq_f32(vacc${ABC[M:M+4]}, vi${ABC[M:M+4]}, vw);
} while (--nnz != 0);
}
$for M in range(0, MR, 4):
float32x4_t vout${ABC[M:M+4]} = vminq_f32(vacc${ABC[M:M+4]}, vmax);
$for M in range(0, MR, 4):
vout${ABC[M:M+4]} = vmaxq_f32(vout${ABC[M:M+4]}, vmin);
vst1q_f32(output, vout0123);
$for M in range(4, MR, 4):
vst1q_f32(output + ${M}, vout${ABC[M:M+4]});
output += batch_size;
} while (--c != 0);
output -= batch_size * output_channels;
output += ${MR};
input += ${MR};
n -= ${MR};
}
if XNN_UNLIKELY(n != 0) {
$for LOG2M in reversed(range((MR - 1).bit_length())):
$SUBMR = 1 << LOG2M
if (n & ${SUBMR}) {
const float*restrict w = weights;
const int32_t* dmap = widx_dmap;
const uint32_t* nnzmap = nidx_nnzmap;
size_t c = output_channels;
do {
uint32_t nnz = *nnzmap++;
$if SUBMR <= 2:
float32x2_t vacc${ABC[0:SUBMR]} = vld1_dup_f32(w); w += 1;
$else:
float32x4_t vacc0123 = vld1q_dup_f32(w); w += 1;
$for M in range(4, SUBMR, 4):
float32x4_t vacc${ABC[M:M+4]} = vacc0123;
if XNN_LIKELY(nnz != 0) {
do {
const intptr_t diff = *dmap++;
$if SUBMR == 1:
const float32x2_t vi0 = vld1_dup_f32(input);
$elif SUBMR == 2:
const float32x2_t vi01 = vld1_f32(input);
$else:
const float32x4_t vi0123 = vld1q_f32(input);
$for M in range(4, SUBMR, 4):
const float32x4_t vi${ABC[M:M+4]} = vld1q_f32(input + ${M});
input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
$if SUBMR <= 2:
const float32x2_t vw = vld1_dup_f32(w); w += 1;
$else:
const float32x4_t vw = vld1q_dup_f32(w); w += 1;
$if SUBMR <= 2:
vacc${ABC[0:SUBMR]} = vfma_f32(vacc${ABC[0:SUBMR]}, vi${ABC[0:SUBMR]}, vw);
$else:
$for M in range(0, SUBMR, 4):
vacc${ABC[M:M+4]} = vfmaq_f32(vacc${ABC[M:M+4]}, vi${ABC[M:M+4]}, vw);
} while (--nnz != 0);
}
$if SUBMR <= 2:
float32x2_t vout${ABC[0:SUBMR]} = vmin_f32(vacc${ABC[0:SUBMR]}, vget_low_f32(vmax));
vout${ABC[0:SUBMR]} = vmax_f32(vout${ABC[0:SUBMR]}, vget_low_f32(vmin));
$if SUBMR == 1:
vst1_lane_f32(output, vout${ABC[0]}, 0);
$else:
vst1_f32(output, vout${ABC[0:SUBMR]});
$else:
$for M in range(0, SUBMR, 4):
float32x4_t vout${ABC[M:M+4]} = vminq_f32(vacc${ABC[M:M+4]}, vmax);
$for M in range(0, SUBMR, 4):
vout${ABC[M:M+4]} = vmaxq_f32(vout${ABC[M:M+4]}, vmin);
vst1q_f32(output, vout0123);
$for M in range(4, SUBMR, 4):
vst1q_f32(output + ${M}, vout${ABC[M:M+4]});
output += batch_size;
} while (--c != 0);
output -= batch_size * output_channels;
output += ${SUBMR};
input += ${SUBMR};
}
}
}