def SetFieldValue(regValue, lsb, fsize, fieldValue):
    """
    An internal utility function to assign a field value to a register value.
    Perform range check on fieldValue using fsize.

    :type  regValue: ``int`` or ``long``
    :param regValue: The value of the register to parse
    :type  lsb: ``int``
    :param lsb: The least significant bit of the field
    :type  fsize: ``int``
    :param fsize: The size of the field in bits
    :type  fieldValue: ``int``
    :param fieldValue: The new field value
    :rtype: ``int`` or ``long``
    :return: The new register value
    """
    if (-1 << fsize) & fieldValue:
        raise ValueError("field value '{}' exceeds range of {} bits".format(fieldValue,
                                                                            fsize))
    msb = lsb + fsize
    mask = ~((pow(2, msb) - 1) & ~(pow(2, lsb) - 1))
    return (regValue & mask) | (fieldValue << lsb)