File tree Expand file tree Collapse file tree 1 file changed +10
-4
lines changed
mlir/include/mlir/ExecutionEngine/SparseTensor Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -131,16 +131,22 @@ template <typename To, typename From>
131
131
return static_cast <To>(x);
132
132
}
133
133
134
- // TODO: would be better to use various architectures' intrinsics to
135
- // detect the overflow directly, instead of doing the assertion beforehand
136
- // (which requires an expensive division).
137
- //
138
134
// / A version of `operator*` on `uint64_t` which guards against overflows
139
135
// / (when assertions are enabled).
140
136
inline uint64_t checkedMul (uint64_t lhs, uint64_t rhs) {
137
+ // If assertions are enabled and we have the intrinsic, then use it to
138
+ // avoid the expensive division. If assertions are disabled, then don't
139
+ // bother with intrinsics (to avoid any possible slowdown vs `operator*`).
140
+ #if !defined(NDEBUG) && __has_builtin(__builtin_mul_overflow)
141
+ uint64_t result;
142
+ bool overflowed = __builtin_mul_overflow (lhs, rhs, &result);
143
+ assert (!overflowed && " Integer overflow" );
144
+ return result;
145
+ #else
141
146
assert ((lhs == 0 || rhs <= std::numeric_limits<uint64_t >::max () / lhs) &&
142
147
" Integer overflow" );
143
148
return lhs * rhs;
149
+ #endif
144
150
}
145
151
146
152
} // namespace detail
You can’t perform that action at this time.
0 commit comments