def cent(q: int, halfmod: int, logmod: int, val: int) -> int:
    """
    Constant-time remainder.

    :param q: Input modulus
    :type q: int
    :param val: Input value
    :type val: int
    :param halfmod: q//2
    :type halfmod: int
    :param logmod: ceil(log2(q))
    :type logmod: int

    :return: Value in the set [-(q//2), ..., q//2] equivalent to x mod q.
    :rtype: int
    """
    y: int = val % q
    intermediate_value: int = y - halfmod - 1
    return y - (1 + (intermediate_value >> logmod)) * q