Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Fixed numerical error for Atomic-Sub between unsigned values with different number of bits #5011

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions taichi/ir/frontend_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ void AtomicOpExpression::type_check(CompileConfig *) {
void AtomicOpExpression::flatten(FlattenContext *ctx) {
// replace atomic sub with negative atomic add
if (op_type == AtomicOpType::sub) {
if (val->ret_type != ret_type) {
val.set(Expr::make<UnaryOpExpression>(UnaryOpType::cast_value, val,
ret_type));
}

val.set(Expr::make<UnaryOpExpression>(UnaryOpType::neg, val));
op_type = AtomicOpType::add;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/python/test_atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,47 @@ def test():
assert ret[None] == 1


@test_utils.test(arch=[ti.cpu, ti.cuda])
def test_atomic_sub_with_type_promotion():
# Test Case 1
@ti.kernel
def test_u16_sub_u8() -> ti.uint16:
x: ti.uint16 = 1000
y: ti.uint8 = 255

ti.atomic_sub(x, y)
return x

res = test_u16_sub_u8()
assert res == 745

# Test Case 2
@ti.kernel
def test_u8_sub_u16() -> ti.uint8:
x: ti.uint8 = 255
y: ti.uint16 = 100

ti.atomic_sub(x, y)
return x

res = test_u8_sub_u16()
assert res == 155

# Test Case 3
A = ti.field(ti.uint8, shape=())
B = ti.field(ti.uint16, shape=())

@ti.kernel
def test_with_field():
v: ti.uint16 = 1000
v -= A[None]
B[None] = v

A[None] = 255
test_with_field()
assert B[None] == 745


@test_utils.test()
def test_atomic_sub_expr_evaled():
c = ti.field(ti.i32)
Expand Down