Esempio n. 1
0
def fixp_resolver(opexp, cast_to):
    cast_to = cast(opexp.dtype, cast_to)

    val_dtype = opexp.dtype

    if typeof(val_dtype, Integer):
        other_cls = Fixp if val_dtype.signed else Ufixp
        val_dtype = other_cls[val_dtype.width, val_dtype.width]

    if cast_to is Fixpnumber:
        return ir.CastExpr(opexp, val_dtype)

    val_fract = val_dtype.fract
    fract = cast_to.fract

    if val_dtype.signed:
        opexp = ir.CastExpr(opexp, Int[val_dtype.width])
    else:
        opexp = ir.CastExpr(opexp, Uint[val_dtype.width])

    if fract > val_fract:
        shift = ir.BinOpExpr(
            [opexp, ir.ResExpr(Uint(fract - val_fract))], ir.opc.LShift)
    else:
        shift = ir.BinOpExpr(
            [opexp, ir.ResExpr(Uint(val_fract - fract))], ir.opc.RShift)

    return ir.CastExpr(shift, cast_to)
Esempio n. 2
0
def fixp_resolver(opexp, cast_to):
    val_dtype = opexp.dtype

    if typeof(val_dtype, Integer):
        other_cls = Fixp if val_dtype.signed else Ufixp
        val_dtype = other_cls[val_dtype.width, val_dtype.width]

    val_fract = val_dtype.fract
    fract = cast_to.fract

    if fract > val_fract:
        shift = expr.BinOpExpr([opexp, expr.ResExpr(Uint(fract - val_fract))], expr.opc.LShift)
    else:
        shift = expr.BinOpExpr([opexp, expr.ResExpr(Uint(val_fract - fract))], expr.opc.RShift)

    return expr.CastExpr(shift, cast_to)
Esempio n. 3
0
async def group_other(din, size: Uint, *, init=1) -> Queue['din']:
    cnt = size.dtype(init)
    last: Bool

    async with size as c:
        assert c >= init, 'group: incorrect configuration'
        last = False
        while not last:
            async with din as data:
                last = (cnt == c)
                yield (data, last)
                cnt += 1
Esempio n. 4
0
def infer_dtype(val, dtype):
    if is_type(type(val)):
        return type(val)

    if not is_type(dtype) or typeof(dtype, Any):
        if isinstance(val, int):
            if val < 0:
                return type(Int(val))
            else:
                return type(Uint(val))

    if dtype.specified:
        return dtype

    return type(dtype.base(val))
Esempio n. 5
0
async def group(din: Queue, size: Uint, *,
                init=1) -> Queue['din.data', 'din.lvl + 1']:

    cnt = size.dtype(init)
    last: Bool
    out_eot: Uint[din.dtype.lvl+1]

    async with size as c:
        assert c >= init, 'group: incorrect configuration'
        last = False
        while not last:
            async for (data, eot) in din:
                last = (cnt == c)
                out_eot = last @ eot
                yield (data, out_eot)
                if not last and all(eot):
                    cnt += 1
Esempio n. 6
0
def test_autowidth():
    assert type(Uint(7)) == Uint[3]
    assert type(Uint(8)) == Uint[4]

    assert type(Int(-7)) == Int[4]
    assert type(Int(7)) == Int[4]