Skip to content
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
10 changes: 9 additions & 1 deletion mlx/backend/cpu/simd/base_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ Simd<T, 1> recip(Simd<T, 1> in) {

DEFAULT_UNARY(operator-, std::negate{})
DEFAULT_UNARY(operator!, std::logical_not{})
DEFAULT_UNARY(abs, std::abs)
DEFAULT_UNARY(acos, std::acos)
DEFAULT_UNARY(acosh, std::acosh)
DEFAULT_UNARY(asin, std::asin)
Expand All @@ -103,6 +102,15 @@ DEFAULT_UNARY(sqrt, std::sqrt)
DEFAULT_UNARY(tan, std::tan)
DEFAULT_UNARY(tanh, std::tanh)

template <typename T>
Simd<T, 1> abs(Simd<T, 1> in) {
if constexpr (std::is_unsigned_v<T>) {
return in;
} else {
return std::abs(in.value);
}
}

template <typename T>
Simd<T, 1> log1p(Simd<T, 1> in) {
if constexpr (is_complex<T>) {
Expand Down
7 changes: 7 additions & 0 deletions python/tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,13 @@ def p(x):
expected = w[::-1, :, ::-1, :] + 1.0
self.assertTrue(mx.array_equal(p(w[::-1, :, ::-1, :]), expected))

def test_compile_abs_unsigned(self):
# abs has to compile for the wider unsigned types too
fun = lambda x: mx.abs(x) + 1
for dtype in [mx.uint8, mx.uint16, mx.uint32, mx.uint64]:
x = mx.array([1, 2, 3], dtype)
self.assertTrue(mx.array_equal(mx.compile(fun)(x), fun(x)))


if __name__ == "__main__":
mlx_tests.MLXTestRunner()