From e3a88830ab3c5eceffeb7b48a2e0e5769d5582ca Mon Sep 17 00:00:00 2001 From: Kirk Scheibelhut Date: Sat, 25 Nov 2023 11:50:18 -0800 Subject: [PATCH] avoid loops with non-power-of-2 indices per https://github.com/ziglang/zig/issues/17768#issuecomment-1784195024 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. --- src/lib/gen1/mechanics.zig | 16 ++++++++-------- src/lib/gen2/mechanics.zig | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib/gen1/mechanics.zig b/src/lib/gen1/mechanics.zig index 92336c40..ecdf9440 100644 --- a/src/lib/gen1/mechanics.zig +++ b/src/lib/gen1/mechanics.zig @@ -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 @@ -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())}); @@ -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) { @@ -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; } @@ -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; } @@ -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) diff --git a/src/lib/gen2/mechanics.zig b/src/lib/gen2/mechanics.zig index f7fca1f0..b4d23c7e 100644 --- a/src/lib/gen2/mechanics.zig +++ b/src/lib/gen2/mechanics.zig @@ -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; @@ -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;