/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef CATLASS_EPILOGUE_TILE_TILE_COMPLEX_MUL_HPP
#define CATLASS_EPILOGUE_TILE_TILE_COMPLEX_MUL_HPP

#include "catlass/catlass.hpp"

namespace Catlass::Epilogue::Tile {

template <
    /// Tag indicating architecture
    class ArchTag_,
    /// Compute data type
    class ComputeType_,
    /// Length of the compute buffer
    uint32_t COMPUTE_LENGTH_
>
struct TileComplexMul {
    using ArchTag = ArchTag_;
    using ElementCompute = typename ComputeType_::Element;

    static constexpr uint32_t COMPUTE_LENGTH = COMPUTE_LENGTH_;

    CATLASS_DEVICE
    TileComplexMul() {}

    CATLASS_DEVICE
    void operator()(
        AscendC::LocalTensor<ElementCompute> const &ubOutReal,
        AscendC::LocalTensor<ElementCompute> const &ubOutImag,
        AscendC::LocalTensor<ElementCompute> const &ubSrc1Real,
        AscendC::LocalTensor<ElementCompute> const &ubSrc1Imag,
        AscendC::LocalTensor<ElementCompute> const &ubSrc2Real,
        AscendC::LocalTensor<ElementCompute> const &ubSrc2Imag,
        AscendC::LocalTensor<ElementCompute> const &ubtempBuf)
    {
        // R * R
        AscendC::Mul(ubOutReal, ubSrc1Real, ubSrc2Real, COMPUTE_LENGTH);
        // I * I
        AscendC::Mul(ubOutImag, ubSrc1Imag, ubSrc2Imag, COMPUTE_LENGTH);

        AscendC::PipeBarrier<PIPE_V>();
        // R * R - I * I
        AscendC::Sub(ubOutReal, ubOutReal, ubOutImag, COMPUTE_LENGTH);

        // R * I
        AscendC::Mul(ubOutImag, ubSrc1Real, ubSrc2Imag, COMPUTE_LENGTH);

        // I * R
        AscendC::Mul(ubtempBuf, ubSrc1Imag, ubSrc2Real, COMPUTE_LENGTH);

        AscendC::PipeBarrier<PIPE_V>();
        // R * I + I * R
        AscendC::Add(ubOutImag, ubOutImag, ubtempBuf, COMPUTE_LENGTH);
        AscendC::PipeBarrier<PIPE_V>();
    }
};

} // namespace Catlass::Epilogue::Tile

#endif