// Copyright 2022 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 NR % 4 == 0
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

#include <assert.h>

#include <arm_neon.h>

#include <xnnpack/gemm.h>


void xnn_bf16_gemm_minmax_ukernel_${MR}x${NR}c2__neonbf16_bfdot_lane_ld128(
    size_t mr,
    size_t nc,
    size_t kc,
    const void* restrict a,
    size_t a_stride,
    const void* restrict w_ptr,
    void* restrict c,
    size_t cm_stride,
    size_t cn_stride,
    const union xnn_bf16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(mr != 0);
  assert(mr <= ${MR});
  assert(nc != 0);
  assert(kc != 0);
  assert(kc % sizeof(bfloat16_t) == 0);
  assert(a != NULL);
  assert(w_ptr != NULL);
  assert(c != NULL);

  const bfloat16_t* a0 = (const bfloat16_t*) a;
  bfloat16_t* c0 = (bfloat16_t*) c;
  $for M in range(1, MR):
    const bfloat16_t* a${M} = (const bfloat16_t*) ((uintptr_t) a${M-1} + a_stride);
    bfloat16_t* c${M} = (bfloat16_t*) ((uintptr_t) c${M-1} + cm_stride);
    $if M % 2 == 0:
      if XNN_UNPREDICTABLE(mr <= ${M}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }
    $elif M + 1 == MR:
      if XNN_UNPREDICTABLE(mr != ${M+1}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }
    $else:
      if XNN_UNPREDICTABLE(mr < ${M+1}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }

  const bfloat16_t* w = (const bfloat16_t*) w_ptr;
  do {
    $for N in range(0, NR, 4):
      float32x4_t vacc0x${ABC[N:N+4]} = vcvt_f32_bf16(vld1_bf16(w)); w += 4;
    $for M in range(1, MR):
      $for N in range(0, NR, 4):
        float32x4_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};

    size_t k = kc;
    for (; k >= 8 * sizeof(bfloat16_t); k -= 8 * sizeof(bfloat16_t)) {
      $for M in range(MR):
        const bfloat16x8_t va${M} = vld1q_bf16(a${M}); a${M} += 8;

      $for K in range(4):
        $for N in range(0, NR, 4):
          const bfloat16x8_t vb${ABC[N:N+4]}c${ABC[2*K:2*K+2]} = vld1q_bf16(w); w += 8;

        $for N in range(0, NR, 4):
          $for M in range(MR):
            vacc${M}x${ABC[N:N+4]} = vbfdotq_laneq_f32(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${ABC[2*K:2*K+2]}, va${M}, ${K});
    }
    if XNN_UNLIKELY(k != 0) {
      $for M in range(MR):
        const bfloat16x8_t va${M} = vld1q_bf16(a${M}); a${M} = (const bfloat16_t*) ((uintptr_t) a${M} + k);

      $for N in range(0, NR, 4):
        const bfloat16x8_t vb${ABC[N:N+4]}c${ABC[0:2]} = vld1q_bf16(w); w += 8;

      $for M in range(MR):
        const uint32x4_t va${M}c${ABC[0:2]} = vdupq_lane_u32(vreinterpret_u32_bf16(vget_low_bf16(va${M})), 0);

      $for N in range(0, NR, 4):
        const uint32x4_t vm${ABC[N:N+4]}c${ABC[0:2]} = vreinterpretq_u32_u16(vceqq_u16(vreinterpretq_u16_bf16(vb${ABC[N:N+4]}c${ABC[0:2]}), vmovq_n_u16(0)));

      $for N in range(0, NR, 4):
        $for M in range(MR):
          const uint32x4_t va${M}x${ABC[N:N+4]}c${ABC[0:2]} = vbicq_u32(va${M}c${ABC[0:2]}, vm${ABC[N:N+4]}c${ABC[0:2]});
          vacc${M}x${ABC[N:N+4]} = vbfdotq_f32(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${ABC[0:2]}, vreinterpretq_bf16_u32(va${M}x${ABC[N:N+4]}c${ABC[0:2]}));

      if (k > 2 * sizeof(bfloat16_t)) {
        $for N in range(0, NR, 4):
          const bfloat16x8_t vb${ABC[N:N+4]}c${ABC[2:4]} = vld1q_bf16(w); w += 8;

        $for M in range(MR):
          const uint32x4_t va${M}c${ABC[2:4]} = vdupq_lane_u32(vreinterpret_u32_bf16(vget_low_bf16(va${M})), 1);

        $for N in range(0, NR, 4):
          const uint32x4_t vm${ABC[N:N+4]}c${ABC[2:4]} = vreinterpretq_u32_u16(vceqq_u16(vreinterpretq_u16_bf16(vb${ABC[N:N+4]}c${ABC[2:4]}), vmovq_n_u16(0)));

        $for N in range(0, NR, 4):
          $for M in range(MR):
            const uint32x4_t va${M}x${ABC[N:N+4]}c${ABC[2:4]} = vbicq_u32(va${M}c${ABC[2:4]}, vm${ABC[N:N+4]}c${ABC[2:4]});
            vacc${M}x${ABC[N:N+4]} = vbfdotq_f32(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${ABC[2:4]}, vreinterpretq_bf16_u32(va${M}x${ABC[N:N+4]}c${ABC[2:4]}));

        if (k > 4 * sizeof(bfloat16_t)) {
          $for N in range(0, NR, 4):
            const bfloat16x8_t vb${ABC[N:N+4]}c${ABC[4:6]} = vld1q_bf16(w); w += 8;

          $for M in range(MR):
            const uint32x4_t va${M}c${ABC[4:6]} = vdupq_lane_u32(vreinterpret_u32_bf16(vget_high_bf16(va${M})), 0);

          $for N in range(0, NR, 4):
            const uint32x4_t vm${ABC[N:N+4]}c${ABC[4:6]} = vreinterpretq_u32_u16(vceqq_u16(vreinterpretq_u16_bf16(vb${ABC[N:N+4]}c${ABC[4:6]}), vmovq_n_u16(0)));

          $for N in range(0, NR, 4):
            $for M in range(MR):
              const uint32x4_t va${M}x${ABC[N:N+4]}c${ABC[4:6]} = vbicq_u32(va${M}c${ABC[4:6]}, vm${ABC[N:N+4]}c${ABC[4:6]});
              vacc${M}x${ABC[N:N+4]} = vbfdotq_f32(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${ABC[4:6]}, vreinterpretq_bf16_u32(va${M}x${ABC[N:N+4]}c${ABC[4:6]}));

          if (k > 6 * sizeof(bfloat16_t)) {
            $for N in range(0, NR, 4):
              const bfloat16x8_t vb${ABC[N:N+4]}c${ABC[6:8]} = vld1q_bf16(w); w += 8;

            $for M in range(MR):
              const uint32x4_t va${M}c${ABC[6:8]} = vdupq_lane_u32(vreinterpret_u32_bf16(vget_high_bf16(va${M})), 1);

            $for N in range(0, NR, 4):
              const uint32x4_t vm${ABC[N:N+4]}c${ABC[6:8]} = vreinterpretq_u32_u16(vceqq_u16(vreinterpretq_u16_bf16(vb${ABC[N:N+4]}c${ABC[6:8]}), vmovq_n_u16(0)));

            $for N in range(0, NR, 4):
              $for M in range(MR):
                const uint32x4_t va${M}x${ABC[N:N+4]}c${ABC[6:8]} = vbicq_u32(va${M}c${ABC[6:8]}, vm${ABC[N:N+4]}c${ABC[6:8]});
                vacc${M}x${ABC[N:N+4]} = vbfdotq_f32(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${ABC[6:8]}, vreinterpretq_bf16_u32(va${M}x${ABC[N:N+4]}c${ABC[6:8]}));
          }
        }
      }
    }

    const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
    $for N in range(0, NR, 4):
      $for M in range(MR):
        vacc${M}x${ABC[N:N+4]} = vminq_f32(vacc${M}x${ABC[N:N+4]}, vmax);

    const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
    $for N in range(0, NR, 4):
      $for M in range(MR):
        vacc${M}x${ABC[N:N+4]} = vmaxq_f32(vacc${M}x${ABC[N:N+4]}, vmin);

    $for N in range(0, NR, 4):
      $for M in range(MR):
        bfloat16x4_t vout${M}x${ABC[N:N+4]} = vcvt_bf16_f32(vacc${M}x${ABC[N:N+4]});

    if XNN_LIKELY(nc >= ${NR}) {
      $for M in range(MR):
        vst1_bf16(c${M}, vout${M}x${ABC[0:4]});
        $for N in range(4, NR, 4):
          vst1_bf16(c${M} + ${N}, vout${M}x${ABC[N:N+4]});
        c${M} = (bfloat16_t*) ((uintptr_t) c${M} + cn_stride);

      $for M in range(MR):
        a${M} = (const bfloat16_t*) ((uintptr_t) a${M} - kc);

      nc -= ${NR};
    } else {
      $for LOG2N in reversed(range(NR.bit_length())):
        $if NR != 1 << LOG2N:
          if (nc & ${1 << LOG2N}) {
            $if LOG2N >= 2:
              $for N in range(0, 1 << LOG2N, 4):
                $for M in range(MR):
                  vst1_bf16(c${M}, vout${M}x${ABC[N:N+4]}); c${M} += 4;

              $for M in range(MR):
                $for N in range(0, 1 << (LOG2N - 1), 4):
                  vout${M}x${ABC[N:N+4]} = vout${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
            $elif LOG2N == 1:
              $for M in range(MR):
                vst1_lane_u32((void*) c${M}, vreinterpret_u32_bf16(vout${M}x${ABC[0:4]}), 0); c${M} += 2;

              $for M in range(MR):
                vout${M}x${ABC[0:4]} = vreinterpret_bf16_u16(vext_u16(vreinterpret_u16_bf16(vout${M}x${ABC[0:4]}), vreinterpret_u16_bf16(vout${M}x${ABC[0:4]}), 2));
            $elif LOG2N == 0:
              $for M in range(MR):
                vst1_lane_bf16(c${M}, vout${M}x${ABC[0:4]}, 0);
          }

      nc = 0;
    }
  } while (nc != 0);
}
