Skip to content

Commit

Permalink
avoid loops with non-power-of-2 indices
Browse files Browse the repository at this point in the history
per
ziglang/zig#17768 (comment)
this removes truncation on the loop index and allows LLVM to better
simplify the code. This claws back half of the 10% regression,
though there is still a 5% gap.
  • Loading branch information
scheibo committed Nov 25, 2023
1 parent 12e7292 commit e3a8883
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/lib/gen1/mechanics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ fn doMove(
var ohko = false;
var immune = false;
var mist = false;
var hits: u4 = 1;
var hits: u8 = 1;
var effectiveness = Effectiveness.neutral;

// Due to control flow shenanigans we need to clear last_damage for Pokémon Showdown
Expand Down Expand Up @@ -972,7 +972,7 @@ fn doMove(
const sub = showdown and foe.active.volatiles.Substitute;

var nullified = false;
var hit: u4 = 0;
var hit: u8 = 0;
while (hit < hits) {
if (hit == 0) {
if (crit) try log.crit(.{battle.active(player.foe())});
Expand Down Expand Up @@ -3022,11 +3022,11 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
},
.Switch => {
const side = battle.side(player);
var slot: u4 = 2;
var slot: u8 = 2;
while (slot <= 6) : (slot += 1) {
const id = side.order[slot - 1];
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
out[n] = .{ .type = .Switch, .data = slot };
out[n] = .{ .type = .Switch, .data = @intCast(slot) };
n += 1;
}
if (n == 0) {
Expand Down Expand Up @@ -3057,11 +3057,11 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
return n;
}

var slot: u4 = 2;
var slot: u8 = 2;
while (slot <= 6) : (slot += 1) {
const id = side.order[slot - 1];
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
out[n] = .{ .type = .Switch, .data = slot };
out[n] = .{ .type = .Switch, .data = @intCast(slot) };
n += 1;
}

Expand Down Expand Up @@ -3094,7 +3094,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
const struggle =
m.id == .Bide and (m.pp == 0 or active.volatiles.disable_move == slot);
const s = if (struggle) 0 else slot;
out[n] = .{ .type = .Move, .data = s };
out[n] = .{ .type = .Move, .data = @intCast(s) };
n += 1;
return n;
}
Expand All @@ -3108,7 +3108,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
if (m.id == .None) break;
if (m.pp == 0) continue;
if (active.volatiles.disable_move == slot) continue;
out[n] = .{ .type = .Move, .data = slot };
out[n] = .{ .type = .Move, .data = @intCast(slot) };
n += 1;
}
// Struggle (Pokémon Showdown would use 'move 1' here)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/gen2/mechanics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3452,7 +3452,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
},
.Switch => {
const side = battle.side(player);
var slot: u4 = 2;
var slot: u8 = 2;
while (slot <= 6) : (slot += 1) {
const id = side.order[slot - 1];
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
Expand Down Expand Up @@ -3486,7 +3486,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
return n;
}

var slot: u4 = 2;
var slot: u8 = 2;
while (!active.volatiles.Trapped and slot <= 6) : (slot += 1) {
const id = side.order[slot - 1];
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
Expand Down

0 comments on commit e3a8883

Please sign in to comment.