import logging

import triton

from ..utils.pointwise_dynamic import pointwise_dynamic

logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))


@pointwise_dynamic(is_tensor=[True, True, False], promotion_methods=[(0, 1, "DEFAULT")])
@triton.jit
def bitwise_and_func(x, y, inplace):
    return x & y


def bitwise_and_tensor(A, B):
    logger.debug("GEMS_CAMBRICON BITWISE AND")
    return bitwise_and_func(A, B, False)


def bitwise_and_tensor_(A, B):
    logger.debug("GEMS_CAMBRICON AND_")
    return bitwise_and_func(A, B, True, out0=A)


@pointwise_dynamic(
    is_tensor=[True, False, False], promotion_methods=[(0, 1, "DEFAULT")]
)
@triton.jit
def bitwise_and_func_scalar(x, y, inplace):
    return x & y


def bitwise_and_scalar(A, B):
    logger.debug("GEMS_CAMBRICON BITWISE AND SCALAR")
    return bitwise_and_func_scalar(A, B, False)


def bitwise_and_scalar_(A, B):
    logger.debug("GEMS_CAMBRICON BITWISE AND_ SCALAR")
    return bitwise_and_func_scalar(A, B, True, out0=A)


def bitwise_and_scalar_tensor(A, B):
    logger.debug("GEMS_CAMBRICON BITWISE AND SCALAR TENSOR")
    return bitwise_and_func_scalar(B, A, False)
