Skip to content

Commit

Permalink
UB in negate in mulh/mulhsu
Browse files Browse the repository at this point in the history
Ensuring No negation on -2^63(int64_t) is performed during mulh/mulhsu

Signed-off-by: Nicolas Brunie <[email protected]>
  • Loading branch information
nibrunieAtSi5 authored Dec 21, 2023
1 parent b98de6f commit d9dc86b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions riscv/arith.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ inline uint64_t mulhu(uint64_t a, uint64_t b)
inline int64_t mulh(int64_t a, int64_t b)
{
int negate = (a < 0) != (b < 0);
uint64_t res = mulhu(a < 0 ? -a : a, b < 0 ? -b : b);
uint64_t res = mulhu(a < 0 ? -(uint64_t)a : a, b < 0 ? -(uint64_t)b : b);
return negate ? ~res + ((uint64_t)a * (uint64_t)b == 0) : res;
}

inline int64_t mulhsu(int64_t a, uint64_t b)
{
int negate = a < 0;
uint64_t res = mulhu(a < 0 ? -a : a, b);
uint64_t res = mulhu(a < 0 ? -(uint64_t)a : a, b);
return negate ? ~res + (a * b == 0) : res;
}

Expand Down

0 comments on commit d9dc86b

Please sign in to comment.