// Copyright 2024 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 % 8 == 0
$SIMD_SIZE = 8
$SIMD_TILE = NR // 8

#include <assert.h>
#include <stddef.h>
#include <stdint.h>

#include <immintrin.h>

#include "xnnpack/intrinsics-polyfill.h"
#include "xnnpack/packw.h"
$if PREFETCH:
  #include "xnnpack/prefetch.h"


void xnn_x32_packw_gemm_gio_ukernel_x${NR}__avx_u${KBLOCK}${"_prfm" if PREFETCH else ""}(
  size_t g,
  size_t nc,
  size_t kc,
  size_t nr,
  size_t kr,
  size_t sr,
  size_t k_stride,
  const uint32_t* weights,
  const uint32_t* bias,
  const void* scale,
  uint32_t* packed_weights,
  size_t extra_bytes,
  const void* params)
{
  assert(g != 0);
  assert(nc != 0);
  assert(kc != 0);
  assert(nr == ${NR});   // This kernel is for NR=${NR}
  assert(kr == 1);
  assert(sr == 1);
  assert(k_stride != 0);
  assert(weights != NULL);
  assert(packed_weights != NULL);
  static const int32_t mask_table[${NR*2}] = {
    $for N in range(SIMD_TILE):
      -1, -1, -1, -1, -1, -1, -1, -1,
    $for N in range(SIMD_TILE):
      0, 0, 0, 0, 0, 0, 0, 0,
  };

  const __m256 vzero = _mm256_setzero_ps();
  const float* b = (const float*) bias;
  float* packed_w = (float*) packed_weights;
  do {
    // NC main loop multiple of ${NR}
    const float* w = (const float*) weights;
    size_t n = nc;

    for (; n >= ${NR}; n -= ${NR}) {
      if XNN_LIKELY(b != NULL) {
        $for N in range(SIMD_TILE):
          const __m256 vb${N} = _mm256_loadu_ps(b + ${N*SIMD_SIZE});
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, vb${N});
        b += ${NR};
      } else {
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, vzero);
      }
      packed_w += ${NR};

      size_t k = kc;
      $if KBLOCK > 1:
        // KC main loop ${KBLOCK}x${NR}
        for (; k >= ${KBLOCK}; k -= ${KBLOCK}) {
          $for K in range(KBLOCK):
            $for N in range(SIMD_TILE):
              const __m256 v${N}_${K} = _mm256_loadu_ps(w + ${N*SIMD_SIZE} + ${K} * k_stride);
          $if PREFETCH:
            $for K in range(KBLOCK):
              $for N in range(0,SIMD_TILE*SIMD_SIZE,64):
                xnn_prefetch_to_l1((const int8_t*) w + ${N+960} + ${K} * k_stride);
          $for K in range(KBLOCK):
            $for N in range(SIMD_TILE):
              _mm256_store_ps(packed_w + ${N*SIMD_SIZE+K*NR}, v${N}_${K});
          w += k_stride * ${KBLOCK};
          packed_w += ${NR*KBLOCK};
        }

      // KC remainder loop
      for (; k > 0; --k) {
        $for N in range(SIMD_TILE):
          const __m256 v${N} = _mm256_loadu_ps(w + ${N*SIMD_SIZE});
        $if PREFETCH:
          $for N in range(0,SIMD_TILE*SIMD_SIZE,64):
            xnn_prefetch_to_l1((const int8_t*) w + ${N+960});
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, v${N});
        w += k_stride;
        packed_w += ${NR};
      }
      w = w - kc * k_stride + ${NR};  // Advance to next column of ${NR} floats
    }

    // NC remainder (1..${NR-1})
    if XNN_UNLIKELY(n != 0) {
      assert(n >= 1);
      assert(n <= ${NR-1});
      $for N in range(SIMD_TILE):
        const __m256i vmask${N} = _mm256_loadu_si256((const __m256i*) &mask_table[${NR+N*SIMD_SIZE} - n]);

      if XNN_LIKELY(b != NULL) {
        $for N in range(SIMD_TILE):
          const __m256 vb${N} = _mm256_maskload_ps(b + ${N*SIMD_SIZE}, vmask${N});
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, vb${N});
        b += n;
      } else {
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, vzero);
      }
      packed_w += ${NR};

      // KC main loop
      for (size_t k = kc; k > 0; --k) {
        $for N in range(SIMD_TILE):
          const __m256 v${N} = _mm256_maskload_ps(w + ${N*SIMD_SIZE}, vmask${N});
        $for N in range(SIMD_TILE):
          _mm256_store_ps(packed_w + ${N*SIMD_SIZE}, v${N});
        w += k_stride;
        packed_w += ${NR};
      }
    }
    weights += nc * kc;
  } while (--g != 0);
}
