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

#include <immintrin.h>

#include "xnnpack/ibilinear.h"


void xnn_f32_ibilinear_chw_ukernel__sse_p${PIXEL_TILE}(
    size_t output_pixels,
    size_t channels,
    const float** restrict input,
    size_t input_offset,
    const float* restrict weights,
    float* restrict output,
    size_t input_increment) XNN_OOB_READS
{
  assert(output_pixels != 0);
  assert(channels != 0);
  assert(input_increment % sizeof(float) == 0);

  do {
    const float** i = input;
    const float* w = weights;
    size_t p = output_pixels;
    $if PIXEL_TILE > 4:
      for (; p >= ${PIXEL_TILE}; p -= ${PIXEL_TILE}) {
        $for P in range(PIXEL_TILE):
          const float* itl${ABC[P]} = (const float*) ((uintptr_t) i[${2 * P}] + input_offset);
          const float* ibl${ABC[P]} = (const float*) ((uintptr_t) i[${2 * P + 1}] + input_offset);
        i += 2 * ${PIXEL_TILE};

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vw${ABC[P:P+4]}p0 = _mm_loadu_ps(w + ${2 * P});
          const __m128 vw${ABC[P:P+4]}p1 = _mm_loadu_ps(w + ${2 * P + 4});
        w += 2 * ${PIXEL_TILE};

        $for P in range(0, PIXEL_TILE, 2):
          const __m128 vtltr${ABC[P]} = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) itl${ABC[P]});
          const __m128 vblbr${ABC[P]} = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) ibl${ABC[P]});

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 valphah${ABC[P:P+4]} = _mm_shuffle_ps(vw${ABC[P:P+4]}p0, vw${ABC[P:P+4]}p1, _MM_SHUFFLE(2, 0, 2, 0));
          const __m128 valphav${ABC[P:P+4]} = _mm_shuffle_ps(vw${ABC[P:P+4]}p0, vw${ABC[P:P+4]}p1, _MM_SHUFFLE(3, 1, 3, 1));

        $for P in range(0, PIXEL_TILE, 2):
          const __m128 vtltr${ABC[P:P+2]} = _mm_loadh_pi(vtltr${ABC[P]}, (const __m64*) itl${ABC[P+1]});
          const __m128 vblbr${ABC[P:P+2]} = _mm_loadh_pi(vblbr${ABC[P]}, (const __m64*) ibl${ABC[P+1]});

        $for P in range(0, PIXEL_TILE, 2):
          const __m128 vldrd${ABC[P:P+2]} = _mm_sub_ps(vblbr${ABC[P:P+2]}, vtltr${ABC[P:P+2]});

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vld${ABC[P:P+4]} = _mm_shuffle_ps(vldrd${ABC[P:P+2]}, vldrd${ABC[P+2:P+4]}, _MM_SHUFFLE(2, 0, 2, 0));
          const __m128 vrd${ABC[P:P+4]} = _mm_shuffle_ps(vldrd${ABC[P:P+2]}, vldrd${ABC[P+2:P+4]}, _MM_SHUFFLE(3, 1, 3, 1));

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vtl${ABC[P:P+4]} = _mm_shuffle_ps(vtltr${ABC[P:P+2]}, vtltr${ABC[P+2:P+4]}, _MM_SHUFFLE(2, 0, 2, 0));
          const __m128 vtr${ABC[P:P+4]} = _mm_shuffle_ps(vtltr${ABC[P:P+2]}, vtltr${ABC[P+2:P+4]}, _MM_SHUFFLE(3, 1, 3, 1));

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vl${ABC[P:P+4]} = _mm_add_ps(vtl${ABC[P:P+4]}, _mm_mul_ps(vld${ABC[P:P+4]}, valphav${ABC[P:P+4]}));
          const __m128 vr${ABC[P:P+4]} = _mm_add_ps(vtr${ABC[P:P+4]}, _mm_mul_ps(vrd${ABC[P:P+4]}, valphav${ABC[P:P+4]}));

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vd${ABC[P:P+4]} = _mm_sub_ps(vr${ABC[P:P+4]}, vl${ABC[P:P+4]});

        $for P in range(0, PIXEL_TILE, 4):
          const __m128 vo${ABC[P:P+4]} = _mm_add_ps(vl${ABC[P:P+4]}, _mm_mul_ps(vd${ABC[P:P+4]}, valphah${ABC[P:P+4]}));

        $for P in range(0, PIXEL_TILE, 4):
          _mm_storeu_ps(output + ${P}, vo${ABC[P:P+4]});
        output += ${PIXEL_TILE};
      }

    for (; p >= 4; p -= 4) {
      $for P in range(4):
        const float* itl${P} = (const float*) ((uintptr_t) i[${2 * P}] + input_offset);
        const float* ibl${P} = (const float*) ((uintptr_t) i[${2 * P + 1}] + input_offset);
      i += 8;

      const __m128 vw0 = _mm_loadu_ps(w);
      const __m128 vw1 = _mm_loadu_ps(w + 4);
      w += 8;

      $for P in range(0, 4, 2):
        const __m128 vtltr${ABC[P]} = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) itl${P});
        const __m128 vblbr${ABC[P]} = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) ibl${P});

      const __m128 valphah = _mm_shuffle_ps(vw0, vw1, _MM_SHUFFLE(2, 0, 2, 0));
      const __m128 valphav = _mm_shuffle_ps(vw0, vw1, _MM_SHUFFLE(3, 1, 3, 1));

      $for P in range(0, 4, 2):
        const __m128 vtltr${ABC[P:P+2]} = _mm_loadh_pi(vtltr${ABC[P]}, (const __m64*) itl${P+1});
        const __m128 vblbr${ABC[P:P+2]} = _mm_loadh_pi(vblbr${ABC[P]}, (const __m64*) ibl${P+1});

      $for P in range(0, 4, 2):
        const __m128 vldrd${ABC[P:P+2]} = _mm_sub_ps(vblbr${ABC[P:P+2]}, vtltr${ABC[P:P+2]});

      const __m128 vld = _mm_shuffle_ps(vldrd01, vldrd23, _MM_SHUFFLE(2, 0, 2, 0));
      const __m128 vrd = _mm_shuffle_ps(vldrd01, vldrd23, _MM_SHUFFLE(3, 1, 3, 1));

      const __m128 vtl = _mm_shuffle_ps(vtltr01, vtltr23, _MM_SHUFFLE(2, 0, 2, 0));
      const __m128 vtr = _mm_shuffle_ps(vtltr01, vtltr23, _MM_SHUFFLE(3, 1, 3, 1));

      const __m128 vl = _mm_add_ps(vtl, _mm_mul_ps(vld, valphav));
      const __m128 vr = _mm_add_ps(vtr, _mm_mul_ps(vrd, valphav));

      const __m128 vd = _mm_sub_ps(vr, vl);
      const __m128 vo = _mm_add_ps(vl, _mm_mul_ps(vd, valphah));

      _mm_storeu_ps(output, vo);
      output += 4;
    }

    if XNN_UNLIKELY(p != 0) {
      if (p & 2) {
        const __m128 vw = _mm_loadu_ps(w);
        w += 4;

        const __m128 valphah = _mm_shuffle_ps(vw, vw, _MM_SHUFFLE(2, 0, 2, 0));
        const __m128 valphav = _mm_shuffle_ps(vw, vw, _MM_SHUFFLE(3, 1, 3, 1));

        $for P in range(2):
          const float* itl${P} = (const float*) ((uintptr_t) i[${2 * P}] + input_offset);
          const float* ibl${P} = (const float*) ((uintptr_t) i[${2 * P + 1}] + input_offset);
        i += 4;

        const __m128 vtltr = _mm_loadh_pi(_mm_loadl_pi(_mm_undefined_ps(), (const __m64*) itl0), (const __m64*) itl1);
        const __m128 vblbr = _mm_loadh_pi(_mm_loadl_pi(_mm_undefined_ps(), (const __m64*) ibl0), (const __m64*) ibl1);

        const __m128 vldrd = _mm_sub_ps(vblbr, vtltr);
        const __m128 vld = _mm_shuffle_ps(vldrd, vldrd, _MM_SHUFFLE(2, 0, 2, 0));
        const __m128 vrd = _mm_shuffle_ps(vldrd, vldrd, _MM_SHUFFLE(3, 1, 3, 1));

        const __m128 vtl = _mm_shuffle_ps(vtltr, vtltr, _MM_SHUFFLE(2, 0, 2, 0));
        const __m128 vtr = _mm_shuffle_ps(vtltr, vtltr, _MM_SHUFFLE(3, 1, 3, 1));

        const __m128 vl = _mm_add_ps(vtl, _mm_mul_ps(vld, valphav));
        const __m128 vr = _mm_add_ps(vtr, _mm_mul_ps(vrd, valphav));

        const __m128 vd = _mm_sub_ps(vr, vl);
        const __m128 vo = _mm_add_ps(vl, _mm_mul_ps(vd, valphah));

        _mm_storel_pi((__m64*) output, vo);
        output += 2;
      }

      if (p & 1) {
        // We are computing the following formula:
        //   result = (1 - alpha_h) * (1 - alpha_v) * top_left +
        //                 alpha_h  * (1 - alpha_v) * top_right +
        //            (1 - alpha_h) *      alpha_v  * bottom_left +
        //                 alpha_h  *      alpha_v  * bottom_right.
        //
        // Rearranging gives
        //   result =    left + alpha_h * (right        - left),
        // where
        //   left =  top_left + alpha_v * (bottom_left  - top_left),
        //  right = top_right + alpha_v * (bottom_right - top_right).

        const float alphah = *w;
        const __m128 valphav = _mm_load_ps1(w + 1);
        w += 2;

        const float* itl = (const float*) ((uintptr_t) i[0] + input_offset);
        const float* ibl = (const float*) ((uintptr_t) i[1] + input_offset);
        i += 2;

        const __m128 vtltr = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) itl);
        const __m128 vblbr = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) ibl);

        // Compute at once
        //    left_diff = bottom_left  - top_left
        //   right_diff = bottom_right - top_right
        const __m128 vldrd = _mm_sub_ps(vblbr, vtltr);
        const __m128 vlr = _mm_add_ps(vtltr, _mm_mul_ps(vldrd, valphav));

        // Extract them and compute the result.
        const float l = _mm_cvtss_f32(vlr);
        const float r = _mm_cvtss_f32(_mm_shuffle_ps(vlr, vlr, 1));

        *output++ = l + alphah * (r - l);
      }
    }

    input_offset += input_increment;
  } while (--channels != 0);
}
