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

[Lang] Support abs(i64) #6018

Merged
merged 2 commits into from
Sep 8, 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
3 changes: 3 additions & 0 deletions taichi/codegen/cc/runtime/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ static inline Ti_f32 Ti_rsqrtf(Ti_f32 x) {
static inline Ti_f64 Ti_rsqrt(Ti_f64 x) {
return 1 / sqrt(x);
}
static inline Ti_i64 Ti_llabs(Ti_i64 x) {
return x >= 0 ? x : -x;
}

) "\n" STR(

Expand Down
2 changes: 2 additions & 0 deletions taichi/codegen/cuda/codegen_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class TaskCodeGenCUDA : public TaskCodeGenLLVM {
llvm_val[stmt] = create_call("__nv_fabs", input);
} else if (input_taichi_type->is_primitive(PrimitiveTypeID::i32)) {
llvm_val[stmt] = create_call("__nv_abs", input);
} else if (input_taichi_type->is_primitive(PrimitiveTypeID::i64)) {
llvm_val[stmt] = create_call("__nv_llabs", input);
} else {
TI_NOT_IMPLEMENTED
}
Expand Down
2 changes: 2 additions & 0 deletions taichi/codegen/llvm/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ void TaskCodeGenLLVM::emit_extra_unary(UnaryOpStmt *stmt) {
llvm_val[stmt] = create_call(#x "_f64", input); \
} else if (input_taichi_type->is_primitive(PrimitiveTypeID::i32)) { \
llvm_val[stmt] = create_call(#x "_i32", input); \
} else if (input_taichi_type->is_primitive(PrimitiveTypeID::i64)) { \
llvm_val[stmt] = create_call(#x "_i64", input); \
} else { \
TI_NOT_IMPLEMENTED \
} \
Expand Down
12 changes: 6 additions & 6 deletions taichi/runtime/llvm/runtime_module/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ DEFINE_UNARY_REAL_FUNC(sin)
DEFINE_FAST_POW(i32)
DEFINE_FAST_POW(i64)

int abs_i32(int a) {
if (a > 0) {
return a;
} else {
return -a;
}
i32 abs_i32(i32 a) {
return a >= 0 ? a : -a;
}

i64 abs_i64(i64 a) {
return a >= 0 ? a : -a;
}

i32 floordiv_i32(i32 a, i32 b) {
Expand Down
10 changes: 10 additions & 0 deletions tests/python/test_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ def sgn(x):
for i in range(N):
assert x[i] == abs(y[i])
assert x.dual[i] == sgn(y[i])


@test_utils.test(require=ti.extension.data64)
def test_abs_i64():
@ti.kernel
def foo(x: ti.i64) -> ti.i64:
return abs(x)

for x in [-2**40, 0, 2**40]:
assert foo(x) == abs(x)