Skip to content

Keep an integer stored into a real a real - #1484

Merged
Frotty merged 6 commits into
masterfrom
fix-int-stored-into-real
Oct 17, 2026
Merged

Frotty merged 6 commits into
masterfrom
fix-int-stored-into-real

Conversation

@Frotty

@Frotty Frotty commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Jass converts an integer stored into a real variable, so after set r = n the read r / m is a real division. Three optimiser rewrites put the integer source in place of the read and emitted n / m, which the game evaluates as an integer division:

  • copy propagation (ConstantAndCopyPropagation), also for an int constant from Wurst (real r = 14 div 2 then r / m);
  • the consecutive-set rewrite (SimpleRewrites.optimizeConsecutiveSet), set r = n then set r = r / m;
  • the globals inliner (GlobalsInliner), for a real global written once with an integer literal: real g = 14 / 2 in a Jass globals block, or constant real g = 14 div 2 in Wurst, then g / m gave 7 / m.

An integer literal is now propagated as the real literal it becomes (7. / m), an integer copy not at all. realFormatting_consistent_fromIntOps now sees print(1.) instead of print(1), the same value.

Both interpreters kept the integer as it was, so the unoptimised run already computed the wrong result: the IM interpreter on an assignment (r * r wrapped at 32 bits), the Jass interpreter on an assignment, a parameter, a return value and a global initialiser (r / m divided as integers). They now convert where the engine does.

Tests: BugTests.jassIntStoredIntoReal* (Jass, interpreter and Lua runs) and OptimizerTests.intCopiedIntoRealIsNotPropagatedIntoRealDivision, intConstantStoredIntoRealIsPropagatedAsReal, intStoredIntoRealIsNotSplicedIntoTheNextSet, intConstantStoredIntoRealGlobalIsInlinedAsReal, jassIntExpressionInRealGlobalIsInlinedAsReal, each red before its fix. Locally green: BugTests, OptimizerTests, ExpressionTests, InterpreterTests (Windows). The Lua backend is unchanged; its / is always a real division.

Measured on the 3.0.0 client, with a map built by master before and after this change (-inline -localOptimizations, Jass, every operand read from an array at run time):

before after
real r = 14 div 2, then r / 4 1 1.75
7 stored into a real local by its initialiser, then / 2 3 3.5
set r = n then set r = r / m, n = 7, m = 2 3 3.5
7 passed to a real parameter, then / 2 3 3.5
a real global initialised with 7, then / 2 3.5 3.5
65536 stored into a real global, then squared and / 65536. 65536 65536
a real global initialised with 14 / 2 (Jass), then / 2 3 3.5

For the last row, "before" is this PR without the globals inliner change (measured on the 3.0.0 client too); the two scripts differ only in 7 / m against 7. / m. The Wurst form constant real g = 14 div 2 gave 1.75 in both builds of that map, whose startup order keeps the inliner from replacing that global; the unit test covers it.

Two cases still differ from the game's own conversion after this change; neither is in its scope:

  • A Jass function declared returns real which returns an integer, once inlined: f(7) / 2 gives 3 in both builds (set a = n / m in the output).
  • On Lua, 65536 stored into a real global and squared gives 0 in the game in both builds, where Jass gives 2^32: the global keeps the integer and the product wraps.

Jass converts an integer stored into a real variable, so `set r = n`
followed by `r / m` divides as reals. Copy propagation replaced the read
of r by n, and the consecutive-set rewrite spliced n into `r / m`, which
emitted `n / m`: an integer division. An int constant from Wurst, such
as `real r = 14 div 2`, went the same way. A literal is now propagated
as the real it becomes and an integer copy not at all.

Both interpreters stored the integer as it was: the IM interpreter on
an assignment, the Jass interpreter on an assignment, a parameter and
a return value, so `r * r` wrapped at 32 bits and `r / m` divided as
integers in the no-optimisation run.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T19:34:34.974661Z 24ff304 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92dc1019a2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The globals inliner put the integer literal of a single-write real global in
place of its reads, so `real g = 14 / 2` in a Jass globals block, or
`constant real g = 14 div 2` in Wurst, then `g / m` emitted `7 / m`, an
integer division in Jass. It now puts the real literal the global holds
(`7. / m`), as copy propagation does for a local.
…-real

# Conflicts:
#	de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/intermediatelang/optimizer/SimpleRewrites.java
@Frotty

Frotty commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24ff304def

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +71 to +72
// Jass converts an integer stored into a real, as it does for a parameter or a return value
ILconst right = ILInterpreter.adjustTypeOfConstant(s.getRight().evaluate(globalState, localState), var.getType());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Coerce integer-to-real boundaries in emitted Lua

When targeting Lua, this conversion only runs in the IM interpreter; translation/lua/translation/StmtTranslation.java:237-238 still emits g = n unchanged for a real g. On the game's runtime, the added jassIntStoredIntoRealGlobalMultipliesAsReal case therefore leaves g as an integer and 65536 * 65536 wraps to 0 instead of producing the real value around 4.29e9; the stock-Lua test masks this because its numeric behavior differs from the game. Apply the coercion in Lua lowering at stores and the analogous real parameter/return boundaries.

AGENTS.md reference: AGENTS.md:L316-L322

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real, and out of scope here: this PR covers the interpreters and the Jass optimiser rewrites. The Lua side is #1492, which makes the conversion explicit in the IM for Lua (stores, real parameters and returns); measured on the 3.0.0 client, the squared global goes from 0 to 65536 with it.

@Frotty
Frotty merged commit 85b99cc into master Oct 17, 2026
2 checks passed
@Frotty
Frotty deleted the fix-int-stored-into-real branch October 17, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant