24#include "pq_globals.h"
25#include "pq_constrain.h"
32typedef uint32_t q0_32u_t;
33typedef uint16_t q0_16u_t;
34typedef uint8_t q0_8u_t;
37constexpr q0_32u_t FIXED_32_MAX =
static_cast<q0_32u_t
>(0xFFFFFFFF);
38constexpr q0_32u_t HALF_FIXED_32_MAX =
static_cast<q0_32u_t
>(0x80000000);
39constexpr float INV_FIXED_32_MAX = 1.0f / FIXED_32_MAX;
41constexpr q0_16u_t FIXED_16_MAX =
static_cast<q0_16u_t
>(0xFFFF);
42constexpr q0_16u_t HALF_FIXED_16_MAX =
static_cast<q0_16u_t
>(0x8000);
43constexpr float INV_FIXED_16_MAX = 1.0f / FIXED_16_MAX;
45constexpr q0_8u_t FIXED_8_MAX =
static_cast<q0_8u_t
>(0xFF);
46constexpr q0_8u_t HALF_FIXED_8_MAX =
static_cast<q0_8u_t
>(0x80);
47constexpr float INV_FIXED_8_MAX = 1.0f / FIXED_8_MAX;
56inline I floatToFixed(
float value, I high) {
57#if defined(PQ_IEEE_754_SUPPORTED)
59 memcpy(&ui, &value,
sizeof ui);
62 if (ui & 0x80000000u)
return static_cast<I
>(0);
65 if (ui >= 0x3F800000u)
return high;
67 return static_cast<I
>(value + value * high);
69 return (value <= 0.0f) ? 0 : (value >= 1.0f) ? high : static_cast<I>(value + value * high);
80inline float fixedToFloat(I value, I high) {
81 return constrain01(value /
static_cast<float>(high));
91inline float fixedToFloatInv(I value,
float invHigh) {
92 return constrain01(value * invHigh);
96inline float fixed32ToFloat(q0_32u_t x) {
return fixedToFloatInv(x, INV_FIXED_32_MAX); }
99inline q0_32u_t floatToFixed32(
float x) {
return floatToFixed(x, FIXED_32_MAX); }
102inline float fixed16ToFloat(q0_16u_t x) {
return fixedToFloatInv(x, INV_FIXED_16_MAX); }
105inline q0_16u_t floatToFixed16(
float x) {
return floatToFixed(x, FIXED_16_MAX); }
108inline float fixed8ToFloat(q0_8u_t x) {
return fixedToFloatInv(x, INV_FIXED_8_MAX); }
111inline q0_8u_t floatToFixed8(
float x) {
return floatToFixed(x, FIXED_8_MAX); }