2020-04-11 21:50:43 +00:00
|
|
|
#ifndef __M_FIXED__
|
|
|
|
#define __M_FIXED__
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "basics.h"
|
|
|
|
|
|
|
|
|
2020-11-23 14:54:06 +00:00
|
|
|
__forceinline constexpr int32_t MulScale(int32_t a, int32_t b, int32_t shift) { return (int32_t)(((int64_t)a * b) >> shift); }
|
2021-01-04 11:16:09 +00:00
|
|
|
__forceinline constexpr int32_t MulScaleF(double a, double b, int32_t shift) { return (a * b) * (1. / (uint32_t(1) << shift)); }
|
2020-11-23 14:54:06 +00:00
|
|
|
__forceinline constexpr int32_t DMulScale(int32_t a, int32_t b, int32_t c, int32_t d, int32_t shift) { return (int32_t)(((int64_t)a * b + (int64_t)c * d) >> shift); }
|
2021-01-04 10:18:07 +00:00
|
|
|
__forceinline constexpr int32_t TMulScale(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e, int32_t f, int32_t shift) { return (int32_t)(((int64_t)a * b + (int64_t)c * d + (int64_t)e * f) >> shift); }
|
2020-11-23 14:54:06 +00:00
|
|
|
__forceinline constexpr int32_t DivScale(int32_t a, int32_t b, int shift) { return (int32_t)(((int64_t)a << shift) / b); }
|
2020-04-11 21:50:43 +00:00
|
|
|
|
|
|
|
#include "xs_Float.h"
|
|
|
|
|
|
|
|
inline fixed_t FloatToFixed(double f)
|
|
|
|
{
|
|
|
|
return xs_Fix<16>::ToFix(f);
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:37:05 +00:00
|
|
|
inline constexpr fixed_t IntToFixed(int32_t f)
|
2020-09-01 13:00:35 +00:00
|
|
|
{
|
2020-09-01 17:47:01 +00:00
|
|
|
return f << FRACBITS;
|
2020-09-01 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 17:37:05 +00:00
|
|
|
inline constexpr double FixedToFloat(fixed_t f)
|
2020-04-11 21:50:43 +00:00
|
|
|
{
|
2020-09-01 17:37:05 +00:00
|
|
|
return f * (1/65536.);
|
2020-04-11 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 17:37:05 +00:00
|
|
|
inline constexpr int32_t FixedToInt(fixed_t f)
|
2020-09-01 13:00:35 +00:00
|
|
|
{
|
2020-09-01 17:47:01 +00:00
|
|
|
return (f + FRACUNIT/2) >> FRACBITS;
|
2020-09-01 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 17:48:21 +00:00
|
|
|
inline unsigned FloatToAngle(double f)
|
2020-04-11 21:50:43 +00:00
|
|
|
{
|
|
|
|
return xs_CRoundToInt((f)* (0x40000000 / 90.));
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:37:05 +00:00
|
|
|
inline constexpr double AngleToFloat(unsigned f)
|
2020-04-11 21:50:43 +00:00
|
|
|
{
|
|
|
|
return f * (90. / 0x40000000);
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:37:05 +00:00
|
|
|
inline constexpr double AngleToFloat(int f)
|
2020-04-11 21:50:43 +00:00
|
|
|
{
|
|
|
|
return f * (90. / 0x40000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FLOAT2FIXED(f) FloatToFixed(f)
|
|
|
|
#define FIXED2FLOAT(f) float(FixedToFloat(f))
|
|
|
|
#define FIXED2DBL(f) FixedToFloat(f)
|
|
|
|
|
|
|
|
#define ANGLE2DBL(f) AngleToFloat(f)
|
|
|
|
|
|
|
|
#endif
|