Skip to content

Commit

Permalink
fixup! Optimize division by a power of two
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Jul 30, 2023
1 parent 64a45b3 commit 6eb95d5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions erts/emulator/beam/jit/arm/instr_arith.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,14 @@ void BeamModuleAssembler::emit_div_rem_literal(Sint divisor,
if (need_div) {
comment("optimized div by replacing with right shift");
}
add(TMP1, dividend, (divisor - 1) << _TAG_IMMED1_SIZE);
a.cmp(dividend, imm(0));
a.csel(TMP3, TMP1, dividend, imm(arm::CondCode::kLT));
if (divisor == 2) {
ERTS_CT_ASSERT(_TAG_IMMED1_SMALL == _TAG_IMMED1_MASK);
a.add(TMP3, dividend, dividend, arm::lsr(63));
} else {
add(TMP1, dividend, (divisor - 1) << _TAG_IMMED1_SIZE);
a.cmp(dividend, imm(0));
a.csel(TMP3, TMP1, dividend, imm(arm::CondCode::kLT));
}
if (need_div && need_rem && quotient == dividend) {
a.mov(TMP4, dividend);
dividend_reg = TMP4;
Expand All @@ -891,7 +896,7 @@ void BeamModuleAssembler::emit_div_rem_literal(Sint divisor,
}
if (need_rem) {
Uint mask = (Uint)-1 << (shift + _TAG_IMMED1_SIZE);
comment("optimized rem by replacing with masking");
comment("optimized rem by replacing with subtraction");
a.and_(TMP1, TMP3, imm(mask));
a.sub(remainder, dividend_reg, TMP1);
}
Expand Down

0 comments on commit 6eb95d5

Please sign in to comment.