Skip to content

Commit

Permalink
Fix a bug for a corner case in Intp (#167)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #167

As title.
The inverse of `kMin` should still be `kMin`.
E.g. for 8bit signed integer, the range is [-128, 127]. The inverse of -128 is 128, which is mapped to -128 again.

When k is an unsigned n-bit integer, then -k is defined as 2^n - k.

Reviewed By: ashiquemfb

Differential Revision: D35262756

fbshipit-source-id: e23eb4290c9e490e149c64f331671c82eeaf3f2d
  • Loading branch information
RuiyuZhu authored and facebook-github-bot committed Mar 30, 2022
1 parent 520b4ad commit 75f1edc
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion fbpcf/mpc_std_lib/util/Intp_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ class Intp {
}

Intp<isSigned, width> operator-() const {
return Intp<isSigned, width>(round(kOffSet - v_));
if constexpr (isSigned) {
if (v_ == kMin) {
return v_;
} else {
return -v_;
}
} else {
return kOffSet - v_;
}
}

Intp<isSigned, width> operator-(const Intp<isSigned, width>& other) const {
Expand Down

0 comments on commit 75f1edc

Please sign in to comment.