Ejemplo n.º 1
0
def make_constant_expr(val):
    _taichi_skip_traceback = 1
    if isinstance(val, (int, np.integer)):
        if pytaichi.default_ip in {ti.i32, ti.u32}:
            # It is not always correct to do such clamp without the type info on
            # the LHS, but at least this makes assigning constant to unsigned
            # int work. See https://github.com/taichi-dev/taichi/issues/2060
            return Expr(
                _ti_core.make_const_expr_i32(
                    _clamp_unsigned_to_range(np.int32, val)))
        elif pytaichi.default_ip in {ti.i64, ti.u64}:
            return Expr(
                _ti_core.make_const_expr_i64(
                    _clamp_unsigned_to_range(np.int64, val)))
        else:
            assert False
    elif isinstance(val, (float, np.floating, np.ndarray)):
        if pytaichi.default_fp == ti.f32:
            return Expr(_ti_core.make_const_expr_f32(val))
        elif pytaichi.default_fp == ti.f64:
            return Expr(_ti_core.make_const_expr_f64(val))
        else:
            assert False
    else:
        raise ValueError(f'Invalid constant scalar expression: {type(val)}')
Ejemplo n.º 2
0
def make_constant_expr_i32(val):
    _taichi_skip_traceback = 1
    assert isinstance(val, (int, np.integer))
    return Expr(
        _ti_core.make_const_expr_i32(_clamp_unsigned_to_range(np.int32, val)))