Skip to main content

Module crt

Module crt 

Source
Expand description

The bit-exact foundations, from their own crate (unbanished-core). The float functions of the Microsoft C runtime, as the game calls them: sinf, cosf, acosf and atan2f, and the compiler’s float-to-integer conversion, truncate.

These functions are not correctly rounded, so their results can differ from Rust’s f32::sin and its relatives in the last bit. That is enough to change the game’s results. So this module ports the C runtime’s own algorithms, operation by operation, and simulation code uses them. tools/emu/crt_math.py checks them against the original functions. powf, floorf and sqrtf are correctly rounded in both, so Rust’s own functions give the same results.

use unbanished_core::crt;

let s = crt::sinf(0.5);
let angle = crt::atan2f(1.0, 2.0);
assert!(s > 0.47 && angle > 0.46);

Original: FUN_140137160 (sinf, the Runtime’s copy at 0x18012a620 is the same), FUN_140136560 (cosf, Runtime 0x1801295a0), FUN_140136090 (acosf), Runtime 0x180128f10 (atan2f, behind Vector3::Heading).

Functions§

acosf
The arc cosine of x, in radians, as the C runtime’s acosf computes it.
atan2f
The angle of the point (x, y), in radians from -π to π, as the C runtime’s atan2f computes it.
cosf
The cosine of x (radians), as the C runtime’s cosf computes it.
sinf
The sine of x (radians), as the C runtime’s sinf computes it.
truncate
(int)v as the compiler writes it, with cvttss2si: it rounds towards zero, and a NaN or a value outside the i32 range gives i32::MIN. Rust’s v as i32 gives 0 for a NaN and the nearest limit for a value out of range, so ported code that converts a float the game could leave NaN uses this.