import torch


def xcycwh2xyxy(x, inplace=False):
    """Convert Nx4 boxes from center, width and height to top-left and bottom-right coordinates.
    Args:
        x(Tensor[N, 4]): N boxes in the format [xc, yc, w, h]
        inplace(bool): whether to modify the input
    Returns:
        boxes(Tensor[N, 4]): N boxes in the format of [x1, y1, x2, y2]
    """
    # Boolean mask causes a new tensor unless assignment inplace
    y = torch.zeros_like(x)
    even = (x[:, 2] % 2) == 0
    odd =  (x[:, 2] % 2) == 1
    y[:, 0][even] = x[:, 0][even] - x[:, 2][even].div(2, rounding_mode='floor') + 1
    y[:, 0][odd] = x[:, 0][odd] - x[:, 2][odd].div(2, rounding_mode='floor')
    even = (x[:, 3] % 2) == 0
    odd =  (x[:, 3] % 2) == 1
    y[:, 1][even] = x[:, 1][even] - x[:, 3][even].div(2, rounding_mode='floor') + 1
    y[:, 1][odd] = x[:, 1][odd] - x[:, 3][odd].div(2, rounding_mode='floor')
    y[:, 2] = x[:, 0] + x[:, 2].div(2, rounding_mode='floor')
    y[:, 3] = x[:, 1] + x[:, 3].div(2, rounding_mode='floor')
    if inplace:
        x.copy_(y)
        return x
    return y