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
78 changes: 63 additions & 15 deletions mlx/backend/cpu/simd/base_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Simd<T, 1> recip(Simd<T, 1> in) {
return op(in.value); \
}

DEFAULT_UNARY(operator-, std::negate{})
DEFAULT_UNARY(operator!, std::logical_not{})
DEFAULT_UNARY(acos, std::acos)
DEFAULT_UNARY(acosh, std::acosh)
Expand All @@ -103,15 +102,6 @@ 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 Expand Up @@ -178,9 +168,41 @@ Simd<bool, 1> isnan(Simd<T, 1> in) {
return a.value OP b; \
}

DEFAULT_BINARY(+)
DEFAULT_BINARY(-)
DEFAULT_BINARY(*)
// Signed integer overflow is undefined behavior in C++, while mlx wraps integer
// add, subtract and multiply around. When the result type is a signed integer,
// apply op to the operands converted to the unsigned type of the same size and
// convert the result back (modular since C++20). The result type is the one to
// look at: operands of 8 and 16 bits are promoted to int first, and the product
// of two uint16 values can overflow int.
template <typename Op, typename T1, typename T2>
auto wrapping(Op op, T1 a, T2 b) {
using R = decltype(op(a, b));
if constexpr (std::is_integral_v<R> && std::is_signed_v<R>) {
using U = std::make_unsigned_t<R>;
return static_cast<R>(op(static_cast<U>(a), static_cast<U>(b)));
} else {
return op(a, b);
}
}

#define WRAPPING_BINARY(OP) \
template <typename T1, typename T2> \
auto operator OP(Simd<T1, 1> a, Simd<T2, 1> b) \
->Simd<decltype(a.value OP b.value), 1> { \
return wrapping([](auto x, auto y) { return x OP y; }, a.value, b.value); \
} \
template <typename T1, typename T2> \
auto operator OP(T1 a, Simd<T2, 1> b)->Simd<decltype(a OP b.value), 1> { \
return wrapping([](auto x, auto y) { return x OP y; }, a, b.value); \
} \
template <typename T1, typename T2> \
auto operator OP(Simd<T1, 1> a, T2 b)->Simd<decltype(a.value OP b), 1> { \
return wrapping([](auto x, auto y) { return x OP y; }, a.value, b); \
}

WRAPPING_BINARY(+)
WRAPPING_BINARY(-)
WRAPPING_BINARY(*)
DEFAULT_BINARY(/)
DEFAULT_BINARY(<<)
DEFAULT_BINARY(>>)
Expand All @@ -190,6 +212,29 @@ DEFAULT_BINARY(&)
DEFAULT_BINARY(&&)
DEFAULT_BINARY(||)

// Negation and abs wrap around too: -MIN and abs(MIN) are MIN, as in two's
// complement. A signed result is computed as 0 - x in the unsigned type, like
// the arithmetic above.
template <typename T>
Simd<T, 1> operator-(Simd<T, 1> in) {
if constexpr (std::is_integral_v<T>) {
return wrapping([](auto x, auto y) { return x - y; }, T(0), in.value);
} else {
return std::negate{}(in.value);
}
}

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

template <typename T>
Simd<T, 1> clz(Simd<T, 1> x_) {
#ifdef _MSC_VER
Expand Down Expand Up @@ -276,12 +321,15 @@ Simd<T, 1> pow(Simd<T, 1> a, Simd<T, 1> b) {
return 0;
}
}
// The power is the exact one modulo 2^N: the products wrap around, and the
// last squaring of the base, which the result does not need, may too.
auto mul = [](auto x, auto y) { return x * y; };
while (exp) {
if (exp & 1) {
res *= base;
res = wrapping(mul, res, base);
}
exp >>= 1;
base *= base;
base = wrapping(mul, base, base);
}
return res;
}
Expand Down
Loading