# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import triton
import triton.language as tl
import triton.language.extra.cann.libdevice as libdevice
import numpy as np
import torch
import pytest
import test_common
import os

PERF_TEST_ENABLE = os.getenv('PERF_TEST_ENABLE', 'False').lower() == 'true'

def torch_byte_perm(x, y, s):
    result = torch.zeros_like(x, dtype=torch.int32)
    for i in range(4):
        sel = (s >> (i * 4 + 0)) & 0x7
        byte = ((sel < 4) * ((x >> (sel * 8)) & 0xFF) +
                (sel >= 4) * ((y >> ((sel - 4) * 8)) & 0xFF))
        result = result | (byte << (i * 8))
    return result

@triton.jit
def triton_byte_perm(in_ptr0, in_ptr1, in_ptr2, out_ptr0, XBLOCK: tl.constexpr, XBLOCK_SUB: tl.constexpr):
    offset = tl.program_id(0) * XBLOCK
    base1 = tl.arange(0, XBLOCK_SUB)
    loops1: tl.constexpr = (XBLOCK + XBLOCK_SUB - 1) // XBLOCK_SUB
    for loop1 in range(loops1):
        x0 = offset + (loop1 * XBLOCK_SUB) + base1
        tmp0 = tl.load(in_ptr0 + (x0), None)
        tmp1 = tl.load(in_ptr1 + (x0), None)
        tmp2 = tl.load(in_ptr2 + (x0), None)
        tmp3 = libdevice.byte_perm(tmp0, tmp1, tmp2)
        tl.store(out_ptr0 + (x0), tmp3, None)


default_param_list = test_common.make_default_param_list(['int32'])

full_param_list = test_common.make_full_param_list(['int32'])

@pytest.mark.parametrize(
    'param_list',
    default_param_list if not PERF_TEST_ENABLE else full_param_list,
)
def test_common_case(param_list):
    dtype, shape, ncore, xblock, xblock_sub = param_list
    x0 = test_common.generate_tensor(shape, dtype).npu()
    x1 = test_common.generate_tensor(shape, dtype).npu()
    x2 = test_common.generate_tensor(shape, dtype).npu()
    y_ref = torch_byte_perm(x0, x1, x2)
    y_cal = torch.zeros(shape, dtype = eval('torch.int32')).npu()
    if PERF_TEST_ENABLE:
        test_common.run_with_profiler(
            lambda: triton_byte_perm[ncore, 1, 1](x0, x1, x2, y_cal, xblock, xblock_sub, force_simt_only=True),
            shape,
            'byte_perm'
        )
    else:
        triton_byte_perm[ncore, 1, 1](x0, x1, x2, y_cal, xblock, xblock_sub, force_simt_only=True)
    test_common.validate_cmp(dtype, y_cal, y_ref)

@pytest.mark.parametrize('param_list',
                        [
                            ['int32', (1, 16), 1, 16, 16],
                        ]
                        )
def test_special_case(param_list):
    dtype, shape, ncore, xblock, xblock_sub = param_list
    x0 = test_common.generate_tensor(shape, dtype).npu()
    x1 = test_common.generate_tensor(shape, dtype).npu()
    x2 = test_common.generate_tensor(shape, dtype).npu()
    x0[0, 0] = 0
    x0[0, 1] = torch.iinfo(eval('torch.' + dtype)).max
    x0[0, 2] = torch.iinfo(eval('torch.' + dtype)).min
    y_ref = torch_byte_perm(x0, x1, x2)
    y_cal = torch.zeros(shape, dtype = eval('torch.int32')).npu()
    triton_byte_perm[ncore, 1, 1](x0, x1, x2, y_cal, xblock, xblock_sub, force_simt_only=True)
    test_common.validate_cmp(dtype, y_cal, y_ref)