Skip to content

Give a Dynamic Float taken as an Int what Std.int gives - #517

Open
dimensionscape wants to merge 1 commit into
feature/cppia-perf-b621b5from
fix/dynamic-float-to-int
Open

dimensionscape wants to merge 1 commit into
feature/cppia-perf-b621b5from
fix/dynamic-float-to-int

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

Stacked on #500. This is based on feature/cppia-perf-b621b5 rather than main because its JIT half changes code that only #500 has (the in-line read of a Float box in convert), and because it puts back the Float cases #500's CppiaJitDynamicNumbers had to leave out. When #500 merges, this retargets to main and the diff becomes this commit alone.

A Float in a Dynamic, taken as an Int, went through a plain (int) cast in DoubleData::__ToInt and in cpp::Variant, whose result past the Int range is undefined, and under the JIT through the hardware's conversion. x86 gives -2147483648 there and for NaN, while arm64 saturates and gives 0 for NaN. So 1e10 gave -2147483648 on Windows and Linux and 2147483647 on macOS. This showed up on #500's macOS job, in CppiaJitDynamicNumbers.

Std.int already had a rule, in the runtime's __int__: truncation in the range, the low 32 bits of the 64-bit truncation past it, and 0 for NaN, the infinities and anything past 64 bits. Every way of taking a Float as an Int follows it now:

  • hx::DoubleToInt in hx/StdLibs.h casts in the range and calls __int__ past it. DoubleData::__ToInt, Variant::asInt, the interpreter's Float to Int fallback and its float builtins use it.
  • The JIT keeps the hardware's conversion, which already gives the rule's answer in the range, and sends to the runtime only the results that may stand for a Float past it: -2147483648, and on platforms that saturate, 2147483647 as well. That covers Std.int in a script too, which cppia writes as a cast to Int and which took the hardware's conversion under the JIT. A Float box read in line goes to objToInt with the box still in hand; any other Float to Int goes to the new floatToInt.
  • __int__ applies its rule under Emscripten too, which it left out when asm.js had no 64-bit integers.

Results in the Int range are unchanged everywhere.

Bench

Against c65f8be (#500's head), the two bench hosts alternating on one pinned core at high priority, 9 processes x 10 samples each:

Under the JIT before after
closure_expr 33.98M 33.89M 0.997x
float_array 80.67M 81.18M 1.006x
bunny_update 20.52M 20.54M 1.001x

float_array takes Std.int of a Float in its loop. Checking both results on x86 as well cost it 2.4%, so x86, which never saturates, checks only -2147483648.

Tests

New DynamicFloatToInt takes Floats as Ints eight ways (a local, an argument, a field, an array element, a return, a Float field of a Dynamic object, a Dynamic sum and | 0), in compiled code and as a script with the JIT off and on, and checks each against Std.int. Its cases cover the range, past an Int, past 64 bits, NaN, the infinities and negative zero.

The code before this fails it in all three sections. Each of these fails it too: a plain cast in the Float box, a plain cast in Variant, the JIT without the rule in its conversion, and the JIT without the rule in its box read.

CppiaJitDynamicNumbers converts its Floats past an Int again, with the rule's values. It still leaves out the Strings past an Int, which #514 defines; that exclusion can go once both are in.

On Windows: the whole regression suite (401), test/haxe (2,880) and test/std (696). Other platforms are left to CI.

A Float in a Dynamic, taken as an Int (a local, an argument, a field, an
array element, a return, a Dynamic sum or a bit operation), went
through a plain (int) cast in DoubleData::__ToInt and in cpp::Variant,
whose result past the Int range is undefined, and under the JIT through
the hardware's conversion. x86 gives -2147483648 there and for NaN,
while arm64 saturates and gives 0 for NaN, so 1e10 gave -2147483648 on
Windows and Linux and 2147483647 on macOS. Std.int already had a rule,
in the runtime's __int__: truncation in the range, the low 32 bits of
the 64-bit truncation past it, and 0 for NaN, the infinities and
anything past 64 bits. Under the JIT, Std.int itself, which a script
writes as a cast to Int, took the hardware's conversion too.

hx::DoubleToInt in hx/StdLibs.h gives that rule, casting in the range
and calling __int__ past it. DoubleData::__ToInt, Variant::asInt, the
interpreter's Float to Int fallback and its float builtins use it. The
JIT keeps the hardware's conversion, which is the rule's in the range,
and sends only the results that may stand for a Float past it to the
runtime: -2147483648, and on platforms that saturate 2147483647 as
well. A Float box read in line goes to objToInt with the box still in
hand, and any other Float converted to an Int to the new floatToInt. A
target found through a scratch register, which a call would lose,
keeps the hardware's answer as before. __int__ applies its rule under
Emscripten too, which it left out when asm.js had no 64-bit integers.

Against c65f8be, the two bench hosts alternating on one pinned core at
high priority, 9 processes x 10 samples each:
  cppia_jit_closure_expr   33.98M to 33.89M per second (0.997x)
  cppia_jit_float_array    80.67M to 81.18M per second (1.006x)
  cppia_jit_bunny_update   20.52M to 20.54M per second (1.001x)
float_array takes Std.int of a Float in its loop. Checking both results
on x86 as well cost it 2.4%, so x86, which never saturates, checks only
-2147483648.

DynamicFloatToInt takes Floats as Ints eight ways, the five above, a
Float field of a Dynamic object, a Dynamic sum and | 0, in compiled code
and as a script with the JIT off and on, and checks each against
Std.int: in the range, past an Int, past 64 bits, NaN, the infinities
and negative zero. The code before this fails it in all three
sections, and so does a plain cast in the Float box; a plain cast in
Variant, or the JIT without the rule in its conversion or its box read,
fails it too. CppiaJitDynamicNumbers converts its Floats past an Int
again, with the rule's values, and still leaves out the Strings past an
Int that #514 defines.

Tests: DynamicFloatToInt, CppiaJitDynamicNumbers
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