cubiomes icon indicating copy to clipboard operation
cubiomes copied to clipboard

Fix undefined wrapping behavior in rng.h (Zig compatibility)

Open DuncanRuns opened this issue 8 months ago • 1 comments

Takes advantage of the defined wrapping addition/subtraction behavior of unsigned integers as opposed to leaving it undefined with regular ints. I don't think the casts should cost any performance if the compiler does its job but I could be wrong since I'm a bit of a low level noob.

This is important for automatically translating the header to other languages, notably Zig where behavior is strongly defined. Most functions are translated to definitions without implementation, and linking the library provides the implementation (which can be compiled without sanitization, so cubiomes compiles and works as intended for these functions and lets things wrap if they need). But since the implementation of nextInt is included in the header, Zig translates it to a form which has the regular addition and subtraction operators, which has overflow checks leading to a panic in rare cases where overflow happens. The changes in this PR successfully "trick" Zig into using the wrapping versions of the addition and subtraction operators since that is the defined behavior of unsigned integers in C.

DuncanRuns avatar Jun 12 '24 23:06 DuncanRuns