#include "fixed_point.h" #include "fatal_error.h" #include fp_t fp_t::operator+(const fp_t& other) const { return {static_cast(static_cast(raw) + static_cast(other.raw))}; } fp_t fp_t::operator-(const fp_t& other) const { return {static_cast(static_cast(raw) - static_cast(other.raw))}; } fp_t fp_t::operator*(const fp_t& other) const { std::int32_t result; std::int64_t temp; temp = static_cast(raw) * static_cast(other.raw); temp += fp_t::K; result = static_cast(temp >> fp_t::Q); return {result}; } fp_t fp_t::operator/(const fp_t& other) const { if (other.raw == 0) { fatal_error("fp_t: division by zero"); } std::int64_t temp = static_cast(raw) << fp_t::Q; if ((temp >= 0 && other.raw >= 0) || (temp < 0 && other.raw < 0)) { temp += (other.raw / 2); } else { temp -= (other.raw / 2); } temp = temp / other.raw; if (temp > std::numeric_limits::max() || temp < std::numeric_limits::min()) { fatal_error("fp_t: division overflow (raw={} / raw={})", raw, other.raw); } return {static_cast(temp)}; } bool fp_t::operator==(const fp_t& other) const { return raw == other.raw; }