55 operator float()
const {
return toFloat(); }
57 explicit operator int()
const {
59 if (v >= 2147483647.0f)
return INT32_MAX;
60 if (v <= -2147483648.0f)
return INT32_MIN;
70 int16_t
toInt16()
const {
return (int16_t)clampRound(toFloat(), -32768.0, 32767.0); }
72 int24_t toInt24()
const {
return clampRound(toFloat(), -8388608.0, 8388607.0); }
74 int32_t
toInt32()
const {
return clampRound(toFloat(), -2147483648.0, 2147483647.0); }
90 int16_t
scale(int16_t sample)
const {
91 return (int16_t)clampRound(toFloat() * (
double)sample, -32768.0, 32767.0);
95 return clampRound(toFloat() * (
double)(int32_t)sample, -8388608.0, 8388607.0);
98 int32_t
scale(int32_t sample)
const {
99 return clampRound(toFloat() * (
double)sample, -2147483648.0, 2147483647.0);
103 return addAligned(m_, e_, o.m_, o.e_);
111 int32_t prod = ((int32_t)m_ * (int32_t)o.m_) >> 15;
112 return normalize(prod, (int32_t)e_ + o.e_ + 15);
119 return soft_float_t((int16_t)(m_ < 0 ? -32767 : 32767), (int16_t)30);
121 int32_t numerator = (int32_t)m_ << 15;
122 int32_t quotient = numerator / o.m_;
123 return normalize(quotient, (int32_t)e_ - o.e_ - 15);
200 static int32_t clampRound(
double v,
double lo,
double hi) {
201 if (v >= hi)
return (int32_t)hi;
202 if (v <= lo)
return (int32_t)lo;
203 v += (v >= 0.0 ? 0.5 : -0.5);
207 void fromFloat(
float f) {
214 double mag = neg ? -(double)f : (double)f;
216 while (mag >= 32768.0) {
220 while (mag < 16384.0) {
224 int16_t mag16 = (int16_t)mag;
225 m_ = neg ? (int16_t)(-(int32_t)mag16) : mag16;
229 float toFloat()
const {
230 return (
float)((double)m_ *
pow(2.0, (
double)e_));
236 if (val == 0)
return soft_float_t((int16_t)0, (int16_t)0);
238 int64_t mag = neg ? -(int64_t)val : (int64_t)val;
239 while (mag > 32767) {
243 while (mag < 16384) {
247 int16_t mi = (int16_t)(neg ? -mag : mag);
253 static soft_float_t addAligned(int16_t ma, int16_t ea, int16_t mb,
257 int32_t a = ma, b = mb;
261 b = (shift >= 31) ? 0 : (b >> shift);
265 a = (shift >= 31) ? 0 : (a >> shift);
268 return normalize(a + b, e);
uint8_t pow(uint8_t x, intmax_t power)
Definition gf.hpp:111