Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public static ImExpr translate(ExprInstanceOf e, ImTranslator t, ImFunction f) {
}

private static ImExpr wrapTranslation(Expr e, ImTranslator t, ImExpr translated) {
if (t.isLuaTarget() && isIntStoredAsReal(e)) {
return ImOperatorCall(WurstOperator.PLUS, ImExprs(translated, ImRealVal("0.")));
}
WurstType actualType = e.attrTypRaw();
WurstType expectedTypRaw = t.isLuaTarget()
&& actualType instanceof WurstTypeBoundTypeParam
Expand All @@ -108,6 +111,25 @@ private static ImExpr wrapTranslation(Expr e, ImTranslator t, ImExpr translated)
return wrapTranslation(e, t, translated, actualType, expectedTypRaw);
}

/**
* Whether e is an integer stored where a real is declared: assigned, initialising a variable, passed as an
* argument, or returned (which pjass rejects, so only a legacy target gets that far). Only Jass code may do that
* (an int is a real only there), and Jass converts it at the store.
* Lua keeps the integer, which the game's Lua computes in 32 bits, so a product of the stored value would wrap
* where Jass multiplies reals: measured on the 3.0.0 client, 65536 stored into a real global, squared and divided
* by 65536. gave 0 on Lua and 65536 on Jass. So on Lua the store adds 0., the conversion Lua's own arithmetic
* does; a literal is already translated as the real it stands for. An operand of an operator converts in the
* operation itself and gets nothing.
*/
private static boolean isIntStoredAsReal(Expr e) {
if (!(e.attrTypRaw() instanceof WurstTypeInt) || !(e.attrExpectedTypRaw() instanceof WurstTypeReal)) {
return false;
}
Element parent = e.getParent();
return parent instanceof StmtSet || parent instanceof VarDef || parent instanceof StmtReturn
|| parent instanceof Arguments;
}

static ImExpr wrapLua(Element trace, ImTranslator t, ImExpr translated, WurstType actualType) {
// Erased generic values are the one kind of Wurst value which can lose
// its primitive default when represented in Lua. Keep the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7966,4 +7966,81 @@ public void modKeepsTheSignOfANegativeZeroOnLua() throws IOException {
"init_Test");
assertTrue(compiled, compiled.contains(" / 360.)") && !compiled.contains(" % ") && !compiled.contains("math.fmod("));
}

/**
* Jass converts an integer stored where a real is declared: into a variable or as an argument for a real
* parameter. Lua keeps the integer unless the script converts it, and the game's Lua computes integers in 32 bits,
* so a product of the stored value wraps where Jass multiplies reals (measured on the 3.0.0 client: 65536 stored
* into a real global, squared and divided by 65536. gives 0 on Lua and 65536 on Jass). The test Lua's integers
* have 64 bits, so the fourth power, 2^64, wraps there too. An integer returned from a function which returns real
* is left out: pjass rejects it.
*/
@Test
public void jassIntStoredIntoRealIsARealOnLua() throws IOException {
test().testLua(true).executeProg().compilationUnits(jassIntStoredIntoReal());
String compiled = compiledLua("jassIntStoredIntoRealIsARealOnLua");
assertTrue(compiled, luaFunctionBody(compiled, "jassGlobal").contains("g = (a + 0.)"));
assertTrue(compiled, luaFunctionBody(compiled, "jassGlobal").contains("[1] = (a + 0.)"));
assertTrue(compiled, luaFunctionBody(compiled, "jassLocal").contains("rb = (b + 0.)"));
assertTrue(compiled, luaFunctionBody(compiled, "jassParameter").contains("jassFourth((c + 0.))"));
}

/** The same through the optimiser, whose copy propagation and inlining made each of them {@code n * n * n * n}. */
@Test
public void jassIntStoredIntoRealStaysARealWhenInlinedOnLua() {
test().testLua(true).inline().localOptimizations().executeProg().compilationUnits(jassIntStoredIntoReal());
}

private CU[] jassIntStoredIntoReal() {
return new CU[] {
compilationUnit("input.j",
"globals",
"\treal g",
"\treal array ga",
"endglobals",
"function jassGlobal takes integer a returns real",
"\tset g = a",
"\tset ga[1] = a",
"\treturn g * g * ga[1] * ga[1]",
"endfunction",
"function jassLocal takes integer b returns real",
"\tlocal real rb = b",
"\treturn rb * rb * rb * rb",
"endfunction",
"function jassFourth takes real r returns real",
"\treturn r * r * r * r",
"endfunction",
"function jassParameter takes integer c returns real",
"\treturn jassFourth(c)",
"endfunction"),
compilationUnit("test.wurst",
"package test",
"native testSuccess()",
"int array ints",
"init",
" ints[0] = 65536",
" let n = ints[0]",
" let twoTo64 = 18446744073709551616.",
" if jassGlobal(n) == twoTo64 and jassLocal(n) == twoTo64",
" and jassParameter(n) == twoTo64",
" testSuccess()")
};
}

/** Wurst stores no integer where a real is declared, so its real code gets no conversion. */
@Test
public void wurstRealCodeGetsNoIntConversionOnLua() {
String compiled = compileOptimizedLua("wurstRealCodeGetsNoIntConversionOnLua",
"package Test",
"native consume(real value)",
"real g = 7",
"@noinline function scale(real r, int n) returns real",
" real s = r * 2",
" g = r * n + 3",
" consume(n * r)",
" return s * r * n",
"init",
" consume(scale(2, 3))");
assertFalse(compiled, compiled.contains("+ 0.)"));
}
}
1 change: 1 addition & 0 deletions docs/WC3_RUNTIME.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Every client simulates the same game from the same inputs; anything that makes t
* **measured, 3.0.0** Jass literals and `S2R` are not correctly rounded and the two disagree: `0.1` reads one float high, `1.1` one float low, `S2R("100.0")` one above 100. Short exact binary fractions (`0.5`, `0.75`, `100.0`, `123456.78125`) read exactly; integer parts past 2^24 are truncated. This is why `SimpleRewrites.foldRealExactly` folds only exact literals and exact results.
* **measured, 3.0.0** Lua reals have a 24-bit mantissa, not 53: `(2^24 + 1) - 2^24` is 0, the literal `16777217.` reads as 16777216, `0.9999999999999999` reads as exactly 1, and `0.7 + 0.1 + 0.1 + 0.1` computes to 1 - 2^-23. Literals are rounded to nearest and each arithmetic result is truncated, which neither double nor 32-bit float arithmetic reproduces. One `R2I` result is not evidence of a precision: `R2I(0.7 + 0.1 + 0.1 + 0.1)` is 0 for that reason, not because of doubles.
* **measured, 3.0.0** `R2I` truncates, and saturates outside the 32-bit range, in Jass and in the game's Lua alike: a computed 5000000320 is 2147483647, its negative -2147483648, a computed 1e20 2147483647. Inside the range the arithmetic `x // 1 | 0` agrees with it; outside, the Lua lowering answers the bound itself (`LuaNativeLowering.lowerRealToInt`, `ExprTranslation.realToInt`). Integer arithmetic wraps at 32 bits on both (`2147483647 + 1` is -2147483648).
* **measured, 3.0.0** Jass converts an integer stored where a real is declared (`set r = n`, `local real r = n`, an argument for a real parameter); the game's Lua keeps an integer an integer. 65536 stored into a real global, squared and divided by `65536.` is 65536 on Jass and was 0 on Lua, the product wrapping at 32 bits. On Lua the translation converts at each such store with `n + 0.`, as it already wrote an integer literal there as a real (`ExprTranslation.isIntStoredAsReal`); only Jass code stores an integer there.
* **measured, 3.0.0** `R2S` and `R2SW` print the whole integer part of a computed value (3e9 is `3000000000.000`, -3e9 `-3000000000.000`); a computed 5e9 prints as `5000000320.000`, which the interpreter does not model. Jass reads a real literal whose integer part passes 2^31 wrapped to 32 bits, signed: the literal `3000000000.` is negative, `10000000000.` reads as 1410065408 and `1e20` written out as 1661992960, which is 10^20 modulo 2^32. Lua reads such a literal correctly. So the Jass backend prints the float a literal means wherever Jass would read it otherwise (`imtojass.ExprTranslation.realLiteral`): past 2^24 the float's own digits (`16777219.` becomes `16777220.`), and from 2^31 on a product of exact factors, its odd part and powers of two up to 2^30 (`3000000000.` becomes `5859375. * 512.`), which reads as the literal's float in the game. A literal past the largest float, such as the stdlib's `REAL_MAX` (2^128), is printed as the largest float: Jass has no product which gives infinity (2^150 compares below 3e38 and prints as `0.000`), and the largest float compares above 3e38 (its `R2S` is `inf`). A compile-time real is written as a plain decimal, since Jass parses no exponent.
* **measured, 3.0.0** A real modulo is Blizzard.j's `ModuloReal` on Jass, `a - I2R(R2I(a / b)) * b` plus `b` when negative. The same formula in the game's Lua gives Jass's result for every dividend both read alike (54 of 54 probed); Lua's float `%` and `math.fmod` do not: `360. % 7.` is 3.0000153 with either and 3 in Jass, `7.5 % 360.` is 7.4999995 and 7.5, and `-7.5 % 2.5` is 2.4999993 with fmod and its correction and 0 in Jass. The integer `%` agrees with Jass's `ModuloInteger` for a positive divisor on every value probed, the 32-bit edges included. So a real modulo is that formula with `R2I` as Lua arithmetic, and only an integer one by a positive literal is `%` (`LuaNativeLowering`).
* **measured, 3.0.0** A real division by zero stops the thread in Jass: `1. / 0.`, `-1. / 0.`, `0. / 0.`, `5. % 0.` and `ModuloReal(5., 0.)` all do. The game's Lua goes on: `1. / 0.` is 1.7014e38, which is 2^127: neither the largest real (about 3.4e38, which `R2S` prints as `inf`) nor an infinity, `-1. / 0.` its negative, `0. / 0.` is 1, and the Lua real modulo by 0 answers a number. The interpreters stop as Jass does (`WurstOperator.divideReal`); the Lua backend adds no check, which would cost every real division.
Expand Down
Loading