Added AddOverflow, SubOverflow and MulOverflow to compute truncated results and return a flag indicating whether overflow occured.
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Can you confirm that the tests pass with UBSan enabled?
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
862 | I think you want to have T1 X, T2 Y and enable_if::<is_same<T1, T2>::value since you're trying to avoid promotions entirely. I'd also rather return a pair or a struct with named values instead of having a reference return value, and make the result nodiscard. | |
870 | We have bit_cast in ADT/bit.h if that's what you meant to use. I think you meant static_cast thought :) | |
llvm/unittests/Support/MathExtrasTest.cpp | ||
474 | You probably want signed char instead of char. |
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
783–786 | Do you want to also refactor the functions that already used that checking at the same time? |
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
783–786 | Unfortunately, I would like to avoid any refactoring in this diff. | |
862 | I would like to keep these functions simple so at some point we might make them straightforward wrappers over __builtin_add_overflow etc, reason why I would like to keep this signature. Promotions can be handled on the callee's side. For my use case, I always want to explicitly specify the template parameter type. Regarding struct returns, that sounds like a use case for a class such as CheckedInt/SafeInt etc, which doesn't seem to be required yet. A future implementation might use these methods though. | |
870 | If the sizes match, doesn't static cast simply move the number bit by bit to the result? I have adjusted the comment. |
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
862 | OK seeing this as the base helper SGTM. You probably still want nodiscard however. |
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
862 | This method is the only way to compute the result of overflow - if we know overflow happens, we might not want to look at the return flag at all. |
llvm/include/llvm/Support/MathExtras.h | ||
---|---|---|
862 | That sounds good! |
I think you want to have T1 X, T2 Y and enable_if::<is_same<T1, T2>::value since you're trying to avoid promotions entirely.
I'd also rather return a pair or a struct with named values instead of having a reference return value, and make the result nodiscard.