22#ifndef STARTREK_FIXEDINT_H
23#define STARTREK_FIXEDINT_H
25#include "common/serializer.h"
27#include "startrek/common.h"
34template<
typename T, u
int totalBits, u
int decimalBits>
36 static const int max = (1 << (totalBits - decimalBits - 1)) - 1;
37 static const int min = -max - 1;
50 assert(d >= min && d <= max);
51 val = (T)(d * (1 << decimalBits));
57 template<
typename T2, u
int otherTB, u
int otherDB>
59 int diff = otherDB - decimalBits;
60 if (otherDB >= decimalBits)
61 val = fi.raw() >> diff;
63 val = fi.raw() << (-diff);
71 return val >> decimalBits;
74 double toDouble()
const {
75 return ((
double)val) / (1 << decimalBits);
78 TFixedInt operator-()
const {
87 return ((int32)(val * i)) >> decimalBits;
94 return fromRaw(val * i);
100 return fromRaw(((int32)(val * f.val)) >> decimalBits);
106 return fromRaw(val / i);
109 return fromRaw(val + f.val);
111 TFixedInt operator-(
const TFixedInt &f)
const {
112 return fromRaw(val - f.val);
115 void operator+=(
const TFixedInt &f) {
118 void operator-=(
const TFixedInt &f) {
122 bool operator==(
double d)
const {
123 return toDouble() == d;
125 bool operator!=(
double d)
const {
126 return toDouble() != d;
128 bool operator<(
double d)
const {
129 return toDouble() < d;
131 bool operator<=(
double d)
const {
132 return toDouble() <= d;
134 bool operator>(
double d)
const {
135 return toDouble() > d;
137 bool operator>=(
double d)
const {
138 return toDouble() >= d;
141 bool operator==(
const TFixedInt &f)
const {
144 bool operator!=(
const TFixedInt &f)
const {
147 bool operator<(
const TFixedInt &f)
const {
150 bool operator<=(
const TFixedInt &f)
const {
153 bool operator>(
const TFixedInt &f)
const {
156 bool operator>=(
const TFixedInt &f)
const {
162 ser.syncAsSint16LE(val);
163 else if (totalBits == 32)
164 ser.syncAsSint32LE(val);
166 error(
"Unsupported bit size for TFixedInt");
170template<
typename T, u
int totalBits, u
int decimalBits>
171int32 operator*(
const int16 lhs,
const TFixedInt<T, totalBits, decimalBits> &rhs) {
177typedef TFixedInt<int16, 16, 14> Fixed14;
180typedef TFixedInt<int16, 16, 8> Fixed8;
183typedef TFixedInt<int32, 32, 16> Fixed16;
Definition: serializer.h:352
Definition: serializer.h:80
Definition: fixedint.h:35
TFixedInt operator*(const TFixedInt &f) const
Definition: fixedint.h:99
TFixedInt(const TFixedInt< T2, otherTB, otherDB > &fi)
Definition: fixedint.h:58
TFixedInt operator/(int32 i) const
Definition: fixedint.h:105
TFixedInt operator*(int32 i) const
Definition: fixedint.h:93
int16 multToInt(int32 i)
Definition: fixedint.h:86
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1