Skip to content

Commit

Permalink
Merge branch 'john/compiler/fix-bnot-mixed-bounds/GH-7251/OTP-18581' …
Browse files Browse the repository at this point in the history
…into maint

* john/compiler/fix-bnot-mixed-bounds/GH-7251/OTP-18581:
  beam_bounds: Fix 'bnot' bounds on mixed finite/infinite ranges
  • Loading branch information
jhogberg committed May 22, 2023
2 parents 4175866 + fc9fd32 commit 6942dac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/compiler/src/beam_bounds.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

bounds('bnot', R0) ->
case R0 of
{Exact, Exact} when is_integer(Exact) ->
N = -Exact - 1,
{N, N};
{A, B} when is_integer(A), is_integer(B), A =/= B ->
%% While it's easy to get an exact range, doing so can make certain
%% chains of operations slow to converge, e.g.
Expand All @@ -68,7 +71,10 @@ bounds('bnot', R0) ->
any
end;
{A, B} ->
R = {inf_add(inf_neg(B), -1), inf_add(inf_neg(A), -1)},
%% Widen the range as above to ensure that we get the same result
%% on the finite component(s).
R = {inf_add(inf_neg(inf_add(B, B)), -1),
inf_add(inf_neg(inf_add(A, A)), -1)},
normalize(R);
_ ->
any
Expand Down
9 changes: 5 additions & 4 deletions lib/compiler/test/beam_bounds_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ bnot_bounds(_Config) ->
A <- Seq,
B <- lists:nthtail(A-Min, Seq)],

{-43,'+inf'} = beam_bounds:bounds('bnot', {'-inf',42}),
{99,'+inf'} = beam_bounds:bounds('bnot', {'-inf',-100}),
{'-inf',-8} = beam_bounds:bounds('bnot', {7,'+inf'}),
{'-inf',9} = beam_bounds:bounds('bnot', {-10,'+inf'}),
{-85,'+inf'} = beam_bounds:bounds('bnot', {'-inf',42}),
{199,'+inf'} = beam_bounds:bounds('bnot', {'-inf',-100}),
{'-inf',-15} = beam_bounds:bounds('bnot', {7,'+inf'}),
{'-inf',19} = beam_bounds:bounds('bnot', {-10,'+inf'}),
{-2228221,'+inf'} = beam_bounds:bounds('bnot', {'-inf', 1114110}),

-1 = bnot_bounds_2(0),

Expand Down

0 comments on commit 6942dac

Please sign in to comment.