mirror of
https://github.com/gbdev/rgbds.git
synced 2026-09-02 03:48:41 +00:00
Guard against undefined behavior in double2fix
I haven't found input which triggers any, but this is more technically correct just in case.
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
#include <numbers>
|
#include <numbers>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "helpers.hpp" // assume
|
||||||
|
|
||||||
static constexpr double tau = std::numbers::pi * 2;
|
static constexpr double tau = std::numbers::pi * 2;
|
||||||
|
|
||||||
static double fix2double(int32_t i, int32_t q) {
|
static double fix2double(int32_t i, int32_t q) {
|
||||||
@@ -21,7 +23,8 @@ static int32_t double2fix(double d, int32_t q) {
|
|||||||
if (isinf(d)) {
|
if (isinf(d)) {
|
||||||
return d < 0 ? INT32_MIN : INT32_MAX;
|
return d < 0 ? INT32_MIN : INT32_MAX;
|
||||||
}
|
}
|
||||||
return static_cast<int32_t>(round(d * pow(2.0, q)));
|
double v = round(d * pow(2.0, q));
|
||||||
|
return v < INT32_MIN ? INT32_MIN : v > INT32_MAX ? INT32_MAX : static_cast<int32_t>(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static double turn2rad(double t) {
|
static double turn2rad(double t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user