diff --git a/src/devices/video/poly.h b/src/devices/video/poly.h index 1c1998a92f422..8fa634283219d 100644 --- a/src/devices/video/poly.h +++ b/src/devices/video/poly.h @@ -37,8 +37,9 @@ #pragma once -#include #include +#include +#include #define KEEP_POLY_STATISTICS 0 @@ -365,12 +366,19 @@ class poly_manager using unit_array = poly_array; // round in a cross-platform consistent manner - inline int32_t round_coordinate(BaseType value) + static int32_t round_coordinate(BaseType value) { - int32_t result = int32_t(std::floor(value)); - if (value > 0 && result < 0) - return INT_MAX - 1; - return result + (value - BaseType(result) > BaseType(0.5)); + // saturate (avoid overflow/underflow) + if (value >= BaseType(std::numeric_limits::max())) + return std::numeric_limits::max(); + const BaseType ipart = std::floor(value); + if (ipart < BaseType(std::numeric_limits::min())) + return std::numeric_limits::min(); + + // round + // TODO: this rounds the midpoint towards negative infinity - should it use more standard behaviour? + const BaseType fpart = value - ipart; + return int32_t(ipart) + ((fpart > BaseType(0.5)) ? 1 : 0); } // internal helpers