/*!
 * \file volk_gnsssdr_16ic_x2_multiply_16ic.h
 * \brief VOLK_GNSSSDR kernel: multiplies two 16 bits vectors and accumulates them.
 * \authors <ul>
 *          <li> Javier Arribas, 2015. jarribas(at)cttc.es
 *          </ul>
 *
 * VOLK_GNSSSDR kernel that multiplies two 16 bits vectors (8 bits the real part
 * and 8 bits the imaginary part) and accumulates them
 *
 * -----------------------------------------------------------------------------
 *
 * GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
 * This file is part of GNSS-SDR.
 *
 * Copyright (C) 2010-2020  (see AUTHORS file for a list of contributors)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * -----------------------------------------------------------------------------
 */

/*!
 * \page volk_gnsssdr_16ic_x2_multiply_16ic
 *
 * \b Overview
 *
 * Multiplies two input complex vectors, point-by-point, storing the result in the third vector.
 *
 * <b>Dispatcher Prototype</b>
 * \code
 * void volk_gnsssdr_16ic_x2_multiply_16ic(lv_16sc_t* result, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points);
 * \endcode
 *
 * \b Inputs
 * \li in_a: One of the vectors to be multiplied.
 * \li in_b: The other vector to be multiplied.
 * \li num_points: The number of complex data points.
 *
 * \b Outputs
 * \li result: The vector where the result will be stored.
 *
 */

#ifndef INCLUDED_volk_gnsssdr_16ic_x2_multiply_16ic_H
#define INCLUDED_volk_gnsssdr_16ic_x2_multiply_16ic_H

#include <volk_gnsssdr/volk_gnsssdr_common.h>
#include <volk_gnsssdr/volk_gnsssdr_complex.h>

#ifdef LV_HAVE_GENERIC

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_generic(lv_16sc_t* result, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    unsigned int n;
    for (n = 0; n < num_points; n++)
        {
            // r*a.r - i*a.i, i*a.r + r*a.i
            result[n] = in_a[n] * in_b[n];
        }
}

#endif /* LV_HAVE_GENERIC */


#ifdef LV_HAVE_SSE2
#include <emmintrin.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_a_sse2(lv_16sc_t* out, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    const unsigned int sse_iters = num_points / 4;
    unsigned int number;
    __m128i a, b, c, c_sr, mask_imag, mask_real, real, imag, imag1, imag2, b_sl, a_sl, result;

    mask_imag = _mm_set_epi8(0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0);
    mask_real = _mm_set_epi8(0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF);

    const lv_16sc_t* _in_a = in_a;
    const lv_16sc_t* _in_b = in_b;
    lv_16sc_t* _out = out;
    for (number = 0; number < sse_iters; number++)
        {
            // std::complex<T> memory structure: real part -> reinterpret_cast<cv T*>(a)[2*i]
            // imaginary part -> reinterpret_cast<cv T*>(a)[2*i + 1]
            // a[127:0]=[a3.i,a3.r,a2.i,a2.r,a1.i,a1.r,a0.i,a0.r]
            a = _mm_load_si128((__m128i*)_in_a);  // load (2 byte imag, 2 byte real) x 4 into 128 bits reg
            b = _mm_load_si128((__m128i*)_in_b);
            c = _mm_mullo_epi16(a, b);  // a3.i*b3.i, a3.r*b3.r, ....

            c_sr = _mm_srli_si128(c, 2);  // Shift a right by imm8 bytes while shifting in zeros, and store the results in dst.
            real = _mm_subs_epi16(c, c_sr);
            real = _mm_and_si128(real, mask_real);  // a3.r*b3.r-a3.i*b3.i , 0,  a3.r*b3.r- a3.i*b3.i

            b_sl = _mm_slli_si128(b, 2);  // b3.r, b2.i ....
            a_sl = _mm_slli_si128(a, 2);  // a3.r, a2.i ....

            imag1 = _mm_mullo_epi16(a, b_sl);  // a3.i*b3.r, ....
            imag2 = _mm_mullo_epi16(b, a_sl);  // b3.i*a3.r, ....

            imag = _mm_adds_epi16(imag1, imag2);
            imag = _mm_and_si128(imag, mask_imag);  // a3.i*b3.r+b3.i*a3.r, 0, ...

            result = _mm_or_si128(real, imag);

            _mm_store_si128((__m128i*)_out, result);

            _in_a += 4;
            _in_b += 4;
            _out += 4;
        }

    for (number = sse_iters * 4; number < num_points; ++number)
        {
            *_out++ = (*_in_a++) * (*_in_b++);
        }
}
#endif /* LV_HAVE_SSE2 */


#ifdef LV_HAVE_SSE2
#include <emmintrin.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_u_sse2(lv_16sc_t* out, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    const unsigned int sse_iters = num_points / 4;
    unsigned int number;
    __m128i a, b, c, c_sr, mask_imag, mask_real, real, imag, imag1, imag2, b_sl, a_sl, result;

    mask_imag = _mm_set_epi8(0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0);
    mask_real = _mm_set_epi8(0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF);

    const lv_16sc_t* _in_a = in_a;
    const lv_16sc_t* _in_b = in_b;
    lv_16sc_t* _out = out;
    for (number = 0; number < sse_iters; number++)
        {
            // std::complex<T> memory structure: real part -> reinterpret_cast<cv T*>(a)[2*i]
            // imaginary part -> reinterpret_cast<cv T*>(a)[2*i + 1]
            // a[127:0]=[a3.i,a3.r,a2.i,a2.r,a1.i,a1.r,a0.i,a0.r]
            a = _mm_loadu_si128((__m128i*)_in_a);  // load (2 byte imag, 2 byte real) x 4 into 128 bits reg
            b = _mm_loadu_si128((__m128i*)_in_b);
            c = _mm_mullo_epi16(a, b);  // a3.i*b3.i, a3.r*b3.r, ....

            c_sr = _mm_srli_si128(c, 2);  // Shift a right by imm8 bytes while shifting in zeros, and store the results in dst.
            real = _mm_subs_epi16(c, c_sr);
            real = _mm_and_si128(real, mask_real);  // a3.r*b3.r-a3.i*b3.i , 0,  a3.r*b3.r- a3.i*b3.i

            b_sl = _mm_slli_si128(b, 2);  // b3.r, b2.i ....
            a_sl = _mm_slli_si128(a, 2);  // a3.r, a2.i ....

            imag1 = _mm_mullo_epi16(a, b_sl);  // a3.i*b3.r, ....
            imag2 = _mm_mullo_epi16(b, a_sl);  // b3.i*a3.r, ....

            imag = _mm_adds_epi16(imag1, imag2);
            imag = _mm_and_si128(imag, mask_imag);  // a3.i*b3.r+b3.i*a3.r, 0, ...

            result = _mm_or_si128(real, imag);

            _mm_storeu_si128((__m128i*)_out, result);

            _in_a += 4;
            _in_b += 4;
            _out += 4;
        }

    for (number = sse_iters * 4; number < num_points; ++number)
        {
            *_out++ = (*_in_a++) * (*_in_b++);
        }
}
#endif /* LV_HAVE_SSE2 */


#ifdef LV_HAVE_AVX2
#include <immintrin.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_u_avx2(lv_16sc_t* out, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    unsigned int number = 0;
    const unsigned int avx2_points = num_points / 8;

    const lv_16sc_t* _in_a = in_a;
    const lv_16sc_t* _in_b = in_b;
    lv_16sc_t* _out = out;

    __m256i a, b, c, c_sr, real, imag, imag1, imag2, b_sl, a_sl, result;

    const __m256i mask_imag = _mm256_set_epi8(0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0);
    const __m256i mask_real = _mm256_set_epi8(0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF);

    for (; number < avx2_points; number++)
        {
            a = _mm256_loadu_si256((__m256i*)_in_a);  // Load the ar + ai, br + bi as ar,ai,br,bi
            b = _mm256_loadu_si256((__m256i*)_in_b);  // Load the cr + ci, dr + di as cr,ci,dr,di
            c = _mm256_mullo_epi16(a, b);

            c_sr = _mm256_srli_si256(c, 2);  // Shift a right by imm8 bytes while shifting in zeros, and store the results in dst.
            real = _mm256_subs_epi16(c, c_sr);
            real = _mm256_and_si256(real, mask_real);  // a3.r*b3.r-a3.i*b3.i , 0,  a3.r*b3.r- a3.i*b3.i

            b_sl = _mm256_slli_si256(b, 2);  // b3.r, b2.i ....
            a_sl = _mm256_slli_si256(a, 2);  // a3.r, a2.i ....

            imag1 = _mm256_mullo_epi16(a, b_sl);  // a3.i*b3.r, ....
            imag2 = _mm256_mullo_epi16(b, a_sl);  // b3.i*a3.r, ....

            imag = _mm256_adds_epi16(imag1, imag2);
            imag = _mm256_and_si256(imag, mask_imag);  // a3.i*b3.r+b3.i*a3.r, 0, ...

            result = _mm256_or_si256(real, imag);

            _mm256_storeu_si256((__m256i*)_out, result);

            _in_a += 8;
            _in_b += 8;
            _out += 8;
        }

    number = avx2_points * 8;
    for (; number < num_points; number++)
        {
            *_out++ = (*_in_a++) * (*_in_b++);
        }
}
#endif /* LV_HAVE_AVX2  */


#ifdef LV_HAVE_AVX2
#include <immintrin.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_a_avx2(lv_16sc_t* out, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    unsigned int number = 0;
    const unsigned int avx2_points = num_points / 8;

    const lv_16sc_t* _in_a = in_a;
    const lv_16sc_t* _in_b = in_b;
    lv_16sc_t* _out = out;

    __m256i a, b, c, c_sr, real, imag, imag1, imag2, b_sl, a_sl, result;

    const __m256i mask_imag = _mm256_set_epi8(0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0);
    const __m256i mask_real = _mm256_set_epi8(0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF);

    for (; number < avx2_points; number++)
        {
            a = _mm256_load_si256((__m256i*)_in_a);  // Load the ar + ai, br + bi as ar,ai,br,bi
            b = _mm256_load_si256((__m256i*)_in_b);  // Load the cr + ci, dr + di as cr,ci,dr,di
            c = _mm256_mullo_epi16(a, b);

            c_sr = _mm256_srli_si256(c, 2);  // Shift a right by imm8 bytes while shifting in zeros, and store the results in dst.
            real = _mm256_subs_epi16(c, c_sr);
            real = _mm256_and_si256(real, mask_real);  // a3.r*b3.r-a3.i*b3.i , 0,  a3.r*b3.r- a3.i*b3.i

            b_sl = _mm256_slli_si256(b, 2);  // b3.r, b2.i ....
            a_sl = _mm256_slli_si256(a, 2);  // a3.r, a2.i ....

            imag1 = _mm256_mullo_epi16(a, b_sl);  // a3.i*b3.r, ....
            imag2 = _mm256_mullo_epi16(b, a_sl);  // b3.i*a3.r, ....

            imag = _mm256_adds_epi16(imag1, imag2);
            imag = _mm256_and_si256(imag, mask_imag);  // a3.i*b3.r+b3.i*a3.r, 0, ...

            result = _mm256_or_si256(real, imag);

            _mm256_store_si256((__m256i*)_out, result);

            _in_a += 8;
            _in_b += 8;
            _out += 8;
        }

    number = avx2_points * 8;
    for (; number < num_points; number++)
        {
            *_out++ = (*_in_a++) * (*_in_b++);
        }
}
#endif /* LV_HAVE_AVX2  */


#ifdef LV_HAVE_NEON
#include <arm_neon.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_neon(lv_16sc_t* out, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    lv_16sc_t* a_ptr = (lv_16sc_t*)in_a;
    lv_16sc_t* b_ptr = (lv_16sc_t*)in_b;
    unsigned int quarter_points = num_points / 4;
    int16x4x2_t a_val, b_val, c_val;
    int16x4x2_t tmp_real, tmp_imag;
    unsigned int number = 0;

    for (number = 0; number < quarter_points; ++number)
        {
            a_val = vld2_s16((int16_t*)a_ptr);  // a0r|a1r|a2r|a3r || a0i|a1i|a2i|a3i
            b_val = vld2_s16((int16_t*)b_ptr);  // b0r|b1r|b2r|b3r || b0i|b1i|b2i|b3i
            __VOLK_GNSSSDR_PREFETCH(a_ptr + 4);
            __VOLK_GNSSSDR_PREFETCH(b_ptr + 4);

            // multiply the real*real and imag*imag to get real result
            // a0r*b0r|a1r*b1r|a2r*b2r|a3r*b3r
            tmp_real.val[0] = vmul_s16(a_val.val[0], b_val.val[0]);
            // a0i*b0i|a1i*b1i|a2i*b2i|a3i*b3i
            tmp_real.val[1] = vmul_s16(a_val.val[1], b_val.val[1]);

            // Multiply cross terms to get the imaginary result
            // a0r*b0i|a1r*b1i|a2r*b2i|a3r*b3i
            tmp_imag.val[0] = vmul_s16(a_val.val[0], b_val.val[1]);
            // a0i*b0r|a1i*b1r|a2i*b2r|a3i*b3r
            tmp_imag.val[1] = vmul_s16(a_val.val[1], b_val.val[0]);

            // store the results
            c_val.val[0] = vsub_s16(tmp_real.val[0], tmp_real.val[1]);
            c_val.val[1] = vadd_s16(tmp_imag.val[0], tmp_imag.val[1]);
            vst2_s16((int16_t*)out, c_val);

            a_ptr += 4;
            b_ptr += 4;
            out += 4;
        }

    for (number = quarter_points * 4; number < num_points; number++)
        {
            *out++ = (*a_ptr++) * (*b_ptr++);
        }
}
#endif /* LV_HAVE_NEON*/


#ifdef LV_HAVE_RVV
#include <riscv_vector.h>

static inline void volk_gnsssdr_16ic_x2_multiply_16ic_rvv(lv_16sc_t* result, const lv_16sc_t* in_a, const lv_16sc_t* in_b, unsigned int num_points)
{
    size_t n = num_points;

    // Initialize pointers to keep track as stripmine
    short* resPtr = (short*)result;
    const short* aPtr = (const short*)in_a;
    const short* bPtr = (const short*)in_b;

    for (size_t vl; n > 0; n -= vl, resPtr += vl * 2, aPtr += vl * 2, bPtr += vl * 2)
        {
            // Record how many elements that will actually be processed
            vl = __riscv_vsetvl_e16m4(n);

            // Load aReal[0..vl), aImag[0..vl)
            vint16m4x2_t aVal = __riscv_vlseg2e16_v_i16m4x2(aPtr, vl);
            vint16m4_t aRealVal = __riscv_vget_v_i16m4x2_i16m4(aVal, 0);
            vint16m4_t aImagVal = __riscv_vget_v_i16m4x2_i16m4(aVal, 1);

            // Load bReal[0..vl), bImag[0..vl)
            vint16m4x2_t bVal = __riscv_vlseg2e16_v_i16m4x2(bPtr, vl);
            vint16m4_t bRealVal = __riscv_vget_v_i16m4x2_i16m4(bVal, 0);
            vint16m4_t bImagVal = __riscv_vget_v_i16m4x2_i16m4(bVal, 1);

            // resReal[i] = aReal[i] * bReal[i]
            vint16m4_t resRealVal = __riscv_vmul_vv_i16m4(aRealVal, bRealVal, vl);

            // resReal[i] = -(aImag[i] * bImag[i]) + resReal[i]
            resRealVal = __riscv_vnmsac_vv_i16m4(resRealVal, aImagVal, bImagVal, vl);

            // resImag[i] = aReal[i] * bImag[i]
            vint16m4_t resImagVal = __riscv_vmul_vv_i16m4(aRealVal, bImagVal, vl);

            // resImag[i] = (aImag[i] * bReal[i]) + resImag[i]
            resImagVal = __riscv_vmacc_vv_i16m4(resImagVal, aImagVal, bRealVal, vl);

            // Store resReal[0..vl), resImag[0..vl)
            vint16m4x2_t resVal = __riscv_vcreate_v_i16m4x2(resRealVal, resImagVal);
            __riscv_vsseg2e16_v_i16m4x2(resPtr, resVal, vl);

            // In looping, decrement the number of
            // elements left and increment the pointers
            // by the number of elements processed,
            // taking into account how the `vl` complex
            // numbers are each stored as 2 `short`s
        }
}

#endif /* LV_HAVE_RVV */

#endif /* INCLUDED_volk_gnsssdr_16ic_x2_multiply_16ic_H */
