Avoid overflowing dividend products before division (by Codex)

Use 64-bit products in actual monthly fund/property settlement, monthly and annual previews, and weekly dividend offers. Retain per-unit rounding, recipient policy and final native fields. Test representable high-value settlements against their forecasts. Full 32-bit money storage overflow remains unresolved.

Refs: #46
This commit is contained in:
Codex committed 2026-11-01 18:20:05 +01:00
1 parent 5caf23c452
commit feb6ca2717
8 files changed
+90 -7

No files matched your search

+3
View File
@@ -1186,3 +1186,6 @@ add_test(NAME long_career_tests COMMAND long_career_tests)
add_executable(bank_robbery_month_tests tests/bank_robbery_month_tests.cpp)
target_link_libraries(bank_robbery_month_tests PRIVATE bmhcore)
add_test(NAME bank_robbery_month_tests COMMAND bank_robbery_month_tests)
add_executable(monthly_dividend_tests tests/monthly_dividend_tests.cpp)
target_link_libraries(monthly_dividend_tests PRIVATE bmhui)
add_test(NAME monthly_dividend_tests COMMAND monthly_dividend_tests)
+9
View File
@@ -145,3 +145,12 @@ receives a grace period without a loss or random draw; subsequent saves retain
that guard. The next calendar month can trigger the ordinary loss. Import cannot
infer whether an event already occurred before this feature. Fresh histories
receive the same initial grace month.
OpenBMH deviates from the original: monthly fund/property dividends multiply
unit price by the dividend factor in 64 bits before the existing integer division.
The monthly and annual previews and weekly offers use the same widened product.
Per-unit rounding and the selected legacy/typed recipient policy are unchanged.
This corrects intermediate overflow for representable dividends; account,
deposit, property book value and monthly totals still use their native 32-bit
storage and can overflow. It is not a complete large-balance finance fix.
+1 -1
View File
@@ -68,7 +68,7 @@ int32_t shares_and_loans(const GameState& g, int m, int flags, int months) {
int32_t sum = 0;
if (flags & 1) {
for (int k = 0; k < 6; k++) {
const uint32_t unit = g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareUnits[k]) / 10000;
const uint32_t unit = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareUnits[k])) / 10000);
sum = wrap(int64_t(sum) + int64_t(unit * g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c)));
}
if (flags & 4) sum = wrap(int64_t(sum) * months);
+11 -4
View File
@@ -6,6 +6,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <cstdio>
#include <cstdlib>
#include <optional>
@@ -654,7 +655,7 @@ void board_month(GameState& g, int m, WatcomRandom& rand, MinuteReport& report,
for (int s = 0; s < 24; s++) g.tactics.at(board(s) + 0xb) &= 0x7F;
}
// MonthChange2: share prices grow by 2.5 %, dividends, loans between the managers.
// MonthChange2: share prices grow by 0.25 %, dividends, loans between the managers.
void shares_and_loans(GameState& g) {
StaticMemory& mem = g.memory;
std::vector<uint8_t>& r = g.manager_records;
@@ -665,12 +666,16 @@ void shares_and_loans(GameState& g) {
uint32_t price = mem.u32(at);
price += ((price / 10000) * 0x19 / 10) * 10;
mem.set32(at, price);
// OpenBMH deviates from the original: keep the price product in 64 bits
// until division, retaining per-unit truncation and recipient policy.
const uint64_t unit_dividend = (uint64_t(price) * uint32_t(kShareFactor[k])) / 10000;
// (a bug of the original: only the last manager's dividend is kept and it is booked
// for the manager after the last one)
int32_t dividend = 0;
for (int m = 0; m < managers; m++)
dividend = int32_t(mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c) *
((price * uint32_t(kShareFactor[k])) / 10000));
dividend = std::bit_cast<int32_t>(uint32_t(
mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c) *
unit_dividend));
book_money(g, managers, -dividend, true);
add32(r, size_t(managers) * kRecord + 0x870, dividend);
}
@@ -694,6 +699,8 @@ void shares_and_loans(GameState& g) {
} // namespace
void monthly_shares_and_loans(GameState& g) { shares_and_loans(g); }
bool list_transfer_player(GameState& g, int slot, WatcomRandom& rand, MinuteReport& report) {
return list_player(g, slot, rand, report);
}
@@ -989,7 +996,7 @@ void month_change(GameState& g, WatcomRandom& rand, MinuteReport& report, GameRa
if (report.unsupported) return;
// 5. shares and loans
turn_step = "month_change: shares";
shares_and_loans(g);
monthly_shares_and_loans(g);
}
} // namespace bmh
+3
View File
@@ -28,6 +28,9 @@ namespace bmh {
// `game` is needed when a player leaves at the end of his contract (MonthChange).
void month_change(GameState& g, WatcomRandom& rand, MinuteReport& report, GameRandom* game = nullptr);
// The investment/loan phase, retaining the original dividend recipient policy.
void monthly_shares_and_loans(GameState& g);
// CareerFirstSteps: new players on the transfer list (part of the month change; also at the
// start of a season).
void new_transfer_list_entries(GameState& g, WatcomRandom& rand, MinuteReport& report);
+1 -1
View File
@@ -216,7 +216,7 @@ void manager_offers(GameState& g, int manager, WatcomRandom& rand, MinuteReport&
if (kind == 14) {
for (int k = 0; k < 6; k++) {
const uint32_t held = g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(manager) * 2 + uint32_t(k) * 0x1c);
const uint32_t value = uint32_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareFactor[k])) / 10000;
const uint32_t value = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareFactor[k])) / 10000);
sum += int(uint32_t(rand.range(10, 20)) * (held * value));
}
if (sum == 0) continue;
+1 -1
View File
@@ -88,7 +88,7 @@ int32_t original_share_adjustment(const GameState& game, int manager, bool incom
if (income) {
for (int k = 0; k < 6; ++k) {
const uint32_t shares = game.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c);
const uint32_t unit = uint32_t(shares) * uint32_t(kShareUnits[size_t(k)]) / 10000;
const uint32_t unit = uint32_t((uint64_t(shares) * uint32_t(kShareUnits[size_t(k)])) / 10000);
const uint32_t owned = game.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(manager) * 2 + uint32_t(k) * 0x1c);
amount = wrap_add(amount, signed32(unit * owned));
}
+61
View File
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/executable_layout.hpp"
#include "game/month.hpp"
#include "game/balance_plan.hpp"
#include "ui/advertising.hpp"
#include <stdexcept>
using namespace bmh;
namespace {
uint32_t word(const GameState& g,int manager,size_t offset) {
return le32(g.manager_records,size_t(manager)*0xb0c+offset);
}
GameState fixture(uint32_t price) {
GameState g;g.level=1;g.manager_teams={0,1};g.manager_records.resize(5*0xb0c);
g.memory.add_block(layout::FundBlock,6*0x1c);
g.memory.add_block(layout::Loans,4*0x78);
g.memory.add_block(layout::Spending,4*0x24);
g.memory.add_block(layout::ManagerCount,1);g.memory.set8(layout::ManagerCount,2);
g.memory.add_block(layout::ActiveManager,1);g.memory.set8(layout::ActiveManager,1);
g.memory.add_block(layout::GameMode,1);g.memory.set8(layout::GameMode,3);
g.memory.add_block(layout::CareerDuration,1);g.memory.set8(layout::CareerDuration,5);
g.tactics.resize(4*0x840);g.league_rounds.resize(9);
for(size_t m=0;m<4;++m)for(size_t row=0;row<24;++row)g.tactics[m*0x840+row*0x58+0x3d]=0xff;
g.memory.set32(layout::FundBlock+5*0x1c,price);
g.memory.set16(layout::Advertising_OriginalShareAdjustment_RuntimeField+5*0x1c,3);
g.memory.set16(layout::Advertising_OriginalShareAdjustment_RuntimeField+5*0x1c+2,10);
return g;
}
void verify(uint32_t initial,uint32_t grown,uint32_t per_unit) {
auto g=fixture(initial);monthly_shares_and_loans(g);
if(g.memory.u32(layout::FundBlock+5*0x1c)!=grown)
throw std::runtime_error("monthly dividend changed price-growth rounding");
// The default legacy policy retains only the last holder and pays record2.
if(word(g,0,0x314)!=0||word(g,1,0x314)!=0||
word(g,2,0x314)!=10*per_unit||word(g,2,0x870)!=10*per_unit)
throw std::runtime_error("representable monthly dividend or legacy recipient is incorrect");
const auto settled_records=g.manager_records;
const auto preview=bmh::ui::original_finance_preview(g,1);
if(preview.income!=int32_t(word(g,2,0x314)))
throw std::runtime_error("monthly property preview differs from the real settled dividend");
auto forecast=g;forecast.manager_records[0xb0c+0x310]=2; // Twelve-month view.
const auto forecast_records=forecast.manager_records;
const auto plan=open_balance_plan(forecast,1);
if(plan.rows[5]!=int32_t(120*per_unit))
throw std::runtime_error("annual property preview differs from settled monthly dividends");
if(g.manager_records!=settled_records||forecast.manager_records!=forecast_records)
throw std::runtime_error("dividend previews mutated the financial records");
#ifdef BMH_TEST_PER_HOLDER_DIVIDENDS
auto typed=fixture(initial);monthly_shares_and_loans(typed,true);
if(word(typed,0,0x314)!=3*per_unit||word(typed,1,0x314)!=10*per_unit||
word(typed,2,0x314)!=0||word(typed,0,0x870)!=3*per_unit||word(typed,1,0x870)!=10*per_unit)
throw std::runtime_error("typed policy did not pay the actual holders");
#endif
}
}
int main() {
verify(1'000'000,1'002'500,3'007);
verify(999,999,2); // Per-unit truncation must precede multiplying holdings.
verify(200'000'000,200'500'000,601'500); // Product exceeds UINT32_MAX, payout fits.
verify(2'000'000'000,2'005'000'000,6'015'000);
}