// 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 NR % 4 == 0
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <xmmintrin.h>

#include <xnnpack/igemm.h>


void xnn_f32_igemm_minmax_ukernel_${MR}x${NR}__sse_load1(
    size_t mr,
    size_t nc,
    size_t kc,
    size_t ks,
    const float**restrict a,
    const float*restrict w,
    float*restrict c,
    size_t cm_stride,
    size_t cn_stride,
    size_t a_offset,
    const float* zero,
    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(mr != 0);
  assert(mr <= ${MR});
  assert(nc != 0);
  assert(kc != 0);
  assert(kc % sizeof(float) == 0);
  assert(ks != 0);
  assert(ks % (${MR} * sizeof(void*)) == 0);
  assert(a_offset % sizeof(float) == 0);
  assert(a != NULL);
  assert(w != NULL);
  assert(c != NULL);

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

  do {
    __m128 vacc0x${ABC[0:4]} = _mm_load_ps(w);
    $for N in range(4, NR, 4):
      __m128 vacc0x${ABC[N:N+4]} = _mm_load_ps(w + ${N});
    $for M in range(1, MR):
      $for N in range(0, NR, 4):
        __m128 vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
    w += ${NR};

    size_t p = ks;
    do {
      $for M in range(MR):
        const float* restrict a${M} = a[${M}];
        assert(a${M} != NULL);
        if XNN_UNPREDICTABLE(a${M} != zero) {
          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
        }
      a += ${MR};

      size_t k = kc;
      do {
        const __m128 vb${ABC[0:4]} = _mm_load_ps(w);
        $for N in range(4, NR, 4):
          const __m128 vb${ABC[N:N+4]} = _mm_load_ps(w + ${N});
        w += ${NR};

        $for M in range(MR):
          const __m128 va${M} = _mm_load1_ps(a${M});
          a${M} += 1;

        $for M in range(MR):
          $for N in range(0, NR, 4):
            vacc${M}x${ABC[N:N+4]} = _mm_add_ps(vacc${M}x${ABC[N:N+4]}, _mm_mul_ps(va${M}, vb${ABC[N:N+4]}));
        k -= sizeof(float);
      } while (k != 0);
      p -= ${MR} * sizeof(void*);
    } while (p != 0);

    const __m128 vmax = _mm_load_ps(params->sse.max);
    $for N in range(0, NR, 4):
      $for M in range(MR):
        vacc${M}x${ABC[N:N+4]} = _mm_min_ps(vacc${M}x${ABC[N:N+4]}, vmax);

    const __m128 vmin = _mm_load_ps(params->sse.min);
    $for N in range(0, NR, 4):
      $for M in range(MR):
        vacc${M}x${ABC[N:N+4]} = _mm_max_ps(vacc${M}x${ABC[N:N+4]}, vmin);

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

      a = (const float**restrict) ((uintptr_t) a - ks);
      nc -= ${NR};
    } else {
      $for LOG2N in reversed(range(NR.bit_length())):
        $if NR != 1 << LOG2N:
          if (nc & ${1 << LOG2N}) {
            $if LOG2N >= 2:
              $for M in reversed(range(MR)):
                _mm_storeu_ps(c${M}, vacc${M}x${ABC[0:4]});
                $for N in range(4, 1 << LOG2N, 4):
                  _mm_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});

              $for M in reversed(range(MR)):
                $for N in range(0, 1 << (LOG2N - 1), 4):
                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};

              $for M in reversed(range(MR)):
                c${M} += ${1 << LOG2N};
            $elif LOG2N == 1:
              $for M in reversed(range(MR)):
                _mm_storel_pi((__m64*) c${M}, vacc${M}x${ABC[0:4]});

              $for M in reversed(range(MR)):
                vacc${M}x${ABC[0:4]} = _mm_movehl_ps(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]});

              $for M in reversed(range(MR)):
                c${M} += 2;
            $elif LOG2N == 0:
              $for M in reversed(range(MR)):
                _mm_store_ss(c${M}, vacc${M}x${ABC[0:4]});
          }

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