Fixed.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #pragma once
  2. #include <cinttypes>
  3. #include <cmath>
  4. struct Fixed128
  5. {
  6. uint64_t upper;
  7. uint64_t lower;
  8. Fixed128(const Fixed128&) = default;
  9. ~Fixed128() = default;
  10. inline Fixed128(uint64_t upper, uint64_t lower) :
  11. upper{ upper }, lower{ lower }
  12. {
  13. }
  14. inline Fixed128(uint32_t a, uint32_t b, uint32_t c, uint32_t d) :
  15. upper{ (uint64_t(a) << 32) | b }, lower{ (uint64_t(c) << 32) | d }
  16. {
  17. }
  18. inline Fixed128(double x)
  19. {
  20. const double twoToThe32 = double(0x100000000ULL);
  21. upper = uint64_t(int64_t(x * twoToThe32));
  22. double remainder = x - double(upper) / twoToThe32;
  23. lower = uint64_t(int64_t(x * twoToThe32 * twoToThe32 * twoToThe32));
  24. /*int integerPart = ::floor(x);
  25. double fractionalPart = x - integerPart;
  26. upper = int64_t(integerPart) << 32;
  27. upper |= uint64_t(fractionalPart * (1ULL << 32)) & 0xFFFFFFFFULL;
  28. lower = 0;// uint64_t(fractionalPart * (1ULL << 32) * (1ULL << 63) * 2);*/
  29. }
  30. inline Fixed128 operator + (const Fixed128& other) const {
  31. uint64_t lowerAdded = lower + other.lower;
  32. uint64_t upperAdded = upper + other.upper + (lowerAdded < lower);
  33. return Fixed128{ upperAdded, lowerAdded };
  34. }
  35. inline Fixed128& operator +=(const Fixed128& other) {
  36. uint64_t lowerAdded = lower + other.lower;
  37. upper += other.upper + (lowerAdded < lower);
  38. lower = lowerAdded;
  39. return *this;
  40. }
  41. inline Fixed128 operator - (const Fixed128& other) const {
  42. uint64_t lowerSubbed = lower - other.lower;
  43. uint64_t upperSubbed = upper - other.upper - (lowerSubbed > lower);
  44. return Fixed128{ upperSubbed, lowerSubbed };
  45. }
  46. inline Fixed128 operator - (void) const {
  47. return this->operator~() + Fixed128{ 0, 0, 0, 1 };
  48. }
  49. //private:
  50. static inline std::pair<uint64_t, uint64_t> mul64(int64_t a, int64_t b) {
  51. int32_t aa[2] = { a >> 32, a & 0xFFFFFFFF };
  52. int32_t bb[2] = { b >> 32, b & 0xFFFFFFFF };
  53. int32_t res[4];
  54. int64_t temp = int64_t(aa[1]) * bb[1];
  55. res[3] = temp & 0xFFFFFFFF;
  56. int32_t carry = temp >> 32;
  57. temp = int64_t(aa[0]) * bb[1] + int64_t(aa[1]) * bb[0] + carry;
  58. res[2] = temp & 0xFFFFFFFF;
  59. carry = temp >> 32;
  60. temp = int64_t(aa[0]) * bb[0] + carry;
  61. res[1] = temp & 0xFFFFFFFF;
  62. res[0] = temp >> 32;
  63. return std::make_pair(uint64_t((int64_t(res[0]) << 32) | res[1]), uint64_t((int64_t(res[2]) << 32) | res[3]));
  64. }
  65. static inline std::pair<uint64_t, uint64_t> mulu64(uint64_t a, uint64_t b) {
  66. uint32_t aa[2] = { a >> 32, a & 0xFFFFFFFF };
  67. uint32_t bb[2] = { b >> 32, b & 0xFFFFFFFF };
  68. uint32_t res[4];
  69. uint64_t temp = uint64_t(aa[1]) * bb[1];
  70. res[3] = temp & 0xFFFFFFFF;
  71. uint32_t carry = temp >> 32;
  72. temp = uint64_t(aa[0]) * bb[1] + uint64_t(aa[1]) * bb[0] + carry;
  73. res[2] = temp & 0xFFFFFFFF;
  74. carry = temp >> 32;
  75. temp = uint64_t(aa[0]) * bb[0] + carry;
  76. res[1] = temp & 0xFFFFFFFF;
  77. res[0] = temp >> 32;
  78. return std::make_pair((uint64_t(res[0]) << 32) | res[1], (uint64_t(res[2]) << 32) | res[3] );
  79. }
  80. public:
  81. inline Fixed128 operator * (const Fixed128& other) const {
  82. if (isNegative()) {
  83. return -(other * this->operator-());
  84. }
  85. if (other.isNegative()) {
  86. return -(*this * (-other));
  87. }
  88. auto [uuc, uu] = mulu64(upper, other.upper);
  89. auto [ulc, ul] = mulu64(upper, other.lower);
  90. auto [luc, lu] = mulu64(lower, other.upper);
  91. auto [llc, ll] = mulu64(lower, other.lower);
  92. uint64_t res[4] = { 0, 0, 0, 0 };
  93. res[3] = ll;
  94. res[2] += lu;
  95. res[2] += ul;
  96. if (res[2] < ul)
  97. res[1]++;
  98. res[2] += llc;
  99. if (res[2] < llc)
  100. res[1]++;
  101. res[1] += uu;
  102. if (res[1] < uu)
  103. res[0]++;
  104. res[1] += ulc;
  105. if (res[1] < ulc)
  106. res[0]++;
  107. res[1] += luc;
  108. if (res[1] < luc)
  109. res[0]++;
  110. res[0] += uuc;
  111. return Fixed128{ uint32_t(res[0] & 0xFFFFFFFF), uint32_t(int64_t(res[1]) >> 32), uint32_t(res[1] & 0xFFFFFFFF), uint32_t(int64_t(res[2]) >> 32) };
  112. /*if (isNegative()) {
  113. return -(this->operator-() * other);
  114. }
  115. if (other.isNegative()) {
  116. return -(*this * (-other));
  117. }
  118. bool otherNegative = other.isNegative();
  119. uint32_t quarters[4] = {
  120. (upper >> 32) & 0xFFFFFFFF,
  121. upper & 0xFFFFFFFF,
  122. (lower >> 32) & 0xFFFFFFFF,
  123. lower & 0xFFFFFFFF
  124. };
  125. auto [a, ra] = other.mul(quarters[0]);
  126. auto [b, rb] = other.mul(quarters[1]);
  127. auto [c, rc] = other.mul(quarters[2]);
  128. auto [d, rd] = other.mul(quarters[3]);
  129. b.arshift(1);
  130. c.arshift(2);
  131. d.arshift(3);
  132. Fixed128 carries = { uint32_t(rb), uint32_t(rc), uint32_t(rd), 0 };
  133. Fixed128 result = a + b + c + d + carries;
  134. return result;*/
  135. }
  136. inline std::pair<Fixed128, uint32_t> mul(uint32_t factor) const {
  137. uint32_t quarters[4] = {
  138. (upper >> 32) & 0xFFFFFFFF,
  139. upper & 0xFFFFFFFF,
  140. (lower >> 32) & 0xFFFFFFFF,
  141. lower & 0xFFFFFFFF
  142. };
  143. uint32_t newQ[4];
  144. uint32_t carry = 0;
  145. for (int i = 3; i >= 0; i--) {
  146. int64_t prod = int64_t(quarters[i]) * factor + carry;
  147. newQ[i] = prod & 0xFFFFFFFF;
  148. carry = prod >> 32;
  149. }
  150. /* newQ[i] = quarters[i] * factor;
  151. uint64_t tempLower = newQ[3];
  152. uint64_t newLower = tempLower + (newQ[2] << 32);
  153. uint64_t newUpper = (newQ[2] >> 32) + newQ[1] + (newQ[0] << 32) + (newLower < tempLower ? 1 : 0);*/
  154. return std::make_pair(Fixed128{ newQ[0], newQ[1], newQ[2], newQ[3] }, carry);
  155. }
  156. /*
  157. inline void arshift(int fac32) {
  158. uint32_t temp = 0;
  159. switch (fac32) {
  160. case 0:
  161. return;
  162. case 1:
  163. temp = upper & 0xFFFFFFFF;
  164. upper = uint64_t(int64_t(upper) >> 32);
  165. lower >>= 32;
  166. lower |= uint64_t(temp) << 32;
  167. case 2:
  168. lower = upper;
  169. upper = uint64_t(int64_t(upper) >> 63);
  170. case 3:
  171. lower = uint64_t(int64_t(upper) >> 32);
  172. upper = uint64_t(int64_t(upper) >> 63);
  173. default:
  174. lower = uint64_t(int64_t(upper) >> 63);
  175. upper = uint64_t(int64_t(upper) >> 63);
  176. }
  177. }*/
  178. /*
  179. inline Fixed128 operator * (const Fixed128& other) const {
  180. int32_t quarters[4] = {
  181. (upper >> 32) & 0xFFFFFFFF,
  182. upper & 0xFFFFFFFF,
  183. (lower >> 32) & 0xFFFFFFFF,
  184. lower & 0xFFFFFFFF
  185. };
  186. int32_t otherQuarters[4] = {
  187. (other.upper >> 32) & 0xFFFFFFFF,
  188. other.upper & 0xFFFFFFFF,
  189. (other.lower >> 32) & 0xFFFFFFFF,
  190. other.lower & 0xFFFFFFFF
  191. };
  192. int64_t prods[4][4];
  193. for (int i = 0; i < 4; i++) {
  194. for (int j = 0; j < 4 && j + i < 5; j++) {
  195. if (i == 0 || j == 0)
  196. prods[i][j] = int64_t(quarters[i]) * int64_t(otherQuarters[j]);
  197. else
  198. prods[i][j] = uint64_t(uint32_t(quarters[i])) * uint64_t(uint32_t(otherQuarters[j]));
  199. }
  200. }
  201. Fixed128 ret = { 0, 0 };
  202. for (int i = 0; i < 4; i++) {
  203. for (int j = 0; j < 4 && j + i < 5; j++) {
  204. if (i == 0 || j == 0)
  205. ret.addSigned(prods[i][j], i + j);
  206. else
  207. ret.add(prods[i][j], i + j);
  208. }
  209. }
  210. return ret;
  211. /*
  212. int64_t x00 = int64_t(quarters[0]) * int64_t(otherQuarters[0]);
  213. int64_t x01 = int64_t(quarters[0]) * int64_t(otherQuarters[1]);
  214. int64_t x02 = int64_t(quarters[0]) * int64_t(otherQuarters[2]);
  215. int64_t x03 = int64_t(quarters[0]) * int64_t(otherQuarters[3]);
  216. int64_t x10 = int64_t(quarters[1]) * int64_t(otherQuarters[0]);
  217. int64_t x11 = int64_t(quarters[1]) * int64_t(otherQuarters[1]);
  218. int64_t x12 = int64_t(quarters[1]) * int64_t(otherQuarters[2]);
  219. int64_t x13 = int64_t(quarters[1]) * int64_t(otherQuarters[3]);
  220. int64_t x20 = int64_t(quarters[2]) * int64_t(otherQuarters[0]);
  221. int64_t x21 = int64_t(quarters[2]) * int64_t(otherQuarters[1]);
  222. int64_t x22 = int64_t(quarters[2]) * int64_t(otherQuarters[2]);
  223. int64_t x30 = int64_t(quarters[3]) * int64_t(otherQuarters[0]);
  224. int64_t x31 = int64_t(quarters[3]) * int64_t(otherQuarters[1]);
  225. Fixed128 ret = { 0, 0 };
  226. /*uint32_t newQuarters[4] = {
  227. x00,
  228. x01 + x10,
  229. x02 + x11 + x20,
  230. x03 + x12 + x21 + x30,
  231. };*//*
  232. ret.add(x00, 0);
  233. ret.add(x01 + x10, 1);
  234. ret.add(x02 + x11 + x20, 2);
  235. ret.add(x03 + x12 + x21 + x30, 3);
  236. ret.add(x13 + x22 + x31, 4);
  237. return ret;*/
  238. /*}*/
  239. private:
  240. inline void add(uint64_t val, int b32offset) {
  241. switch (b32offset) {
  242. case 0:
  243. upper += val << 32;
  244. return;
  245. case 1:
  246. upper += val;
  247. return;
  248. case 2:
  249. upper += val >> 32;
  250. lower += val << 32;
  251. return;
  252. case 3: {
  253. uint64_t newLower = lower + val;
  254. if (newLower < lower) upper++;
  255. lower = newLower;
  256. return;
  257. }
  258. case 4:
  259. uint64_t newLower = lower + (val >> 32);
  260. if (lower > newLower) upper++;
  261. lower += newLower;
  262. return;
  263. }
  264. }
  265. inline void addSigned(int64_t val, int b32offset) {
  266. switch (b32offset) {
  267. case 0:
  268. upper += val << 32;
  269. return;
  270. case 1:
  271. upper += val;
  272. return;
  273. case 2:
  274. upper += val >> 32;
  275. lower += val << 32;
  276. return;
  277. case 3:
  278. lower += val;
  279. if (val < 0) upper--;
  280. return;
  281. case 4: {
  282. uint64_t newLower = lower + (val >> 32);
  283. if (lower > newLower) upper++;
  284. lower = newLower;
  285. return;
  286. }
  287. default:
  288. if (val < 0) {
  289. if (lower == 0) upper--;
  290. lower--;
  291. }
  292. return;
  293. }
  294. }
  295. public:
  296. bool isNegative(void) const {
  297. return (upper & (uint64_t(1) << 63)) != 0;
  298. }
  299. operator double(void) const {
  300. const int64_t twoToThe32 = 0x100000000ULL;
  301. return double(int64_t(upper)) / twoToThe32 + int64_t(lower) / twoToThe32 / twoToThe32 / twoToThe32;
  302. }
  303. inline Fixed128 operator ~ (void) const {
  304. return Fixed128{ ~upper, ~lower };
  305. }
  306. inline bool operator == (const Fixed128& other) const {
  307. return upper == other.upper && lower == other.lower;
  308. }
  309. inline bool operator != (const Fixed128& other) const {
  310. return !operator==(other);
  311. }
  312. inline bool operator < (const Fixed128& other) const {
  313. return upper < other.upper || (upper == other.upper && lower < other.lower);
  314. }
  315. inline bool operator <= (const Fixed128& other) const {
  316. return operator<(other) || operator==(other);
  317. }
  318. inline bool operator > (const Fixed128& other) const {
  319. return upper > other.upper || (upper == other.upper && lower > other.lower);
  320. }
  321. inline bool operator >= (const Fixed128& other) const {
  322. return operator>(other) || operator==(other);
  323. }
  324. };
  325. struct Fixed64
  326. {
  327. bool sign;
  328. uint64_t bits;
  329. Fixed64(const Fixed64&) = default;
  330. ~Fixed64() = default;
  331. inline Fixed64(uint64_t bits, bool dummy) :
  332. bits{ bits }
  333. {
  334. }
  335. inline Fixed64(double x)
  336. {
  337. if (x < 0) {
  338. sign = true;
  339. x *= -1;
  340. }
  341. else {
  342. sign = false;
  343. }
  344. int integerPart = int(x);
  345. double fractionalPart = x - integerPart;
  346. bits = uint64_t(integerPart) << 32;
  347. bits |= uint64_t(fractionalPart * (1ULL << 32)) & 0xFFFFFFFF;
  348. }
  349. inline Fixed64 operator + (const Fixed64& other) {
  350. return Fixed64{ bits + other.bits, true };
  351. }
  352. inline Fixed64& operator +=(const Fixed64& other) {
  353. bits += other.bits;
  354. return *this;
  355. }
  356. inline Fixed64 operator - (const Fixed64& other) {
  357. return Fixed64{ bits - other.bits, true };
  358. }
  359. inline Fixed64 operator * (const Fixed64& other) {
  360. /*int32_t upper = bits >> 32;
  361. uint32_t lower = uint32_t(bits & 0xFFFFFFFF);
  362. int64_t upup = int64_t(upper) * int64_t(upper);
  363. int64_t loup = int64_t(upper) * int64_t(lower);
  364. int64_t lolo = int64_t(lower) * int64_t(lower);
  365. int32_t newUp = upup & 0xFFFFFFFF + (loup >> 32);
  366. int32_t newLo = loup & 0xFFFFFFFF + (lolo >> 32);*/
  367. double d = int32_t(bits >> 32) + double(uint32_t(bits)) / (1ULL << 32);
  368. double od = int32_t(other.bits >> 32) + double(uint32_t(other.bits)) / (1ULL << 32);
  369. return d * od * (other.sign != sign) ? -1 : 1;
  370. //return Fixed64{ (uint64_t(newUp) << 32) | newLo, true };
  371. }
  372. inline bool operator == (const Fixed64& other) {
  373. return bits == other.bits;
  374. }
  375. inline bool operator != (const Fixed64& other) {
  376. return !operator==(other);
  377. }
  378. inline bool operator < (const Fixed64& other) {
  379. return bits < other.bits;
  380. }
  381. inline bool operator <= (const Fixed64& other) {
  382. return operator<(other) || operator==(other);
  383. }
  384. inline bool operator > (const Fixed64& other) {
  385. return bits > other.bits;
  386. }
  387. inline bool operator >= (const Fixed64& other) {
  388. return operator>(other) || operator==(other);
  389. }
  390. };
  391. struct Fixed32
  392. {
  393. int32_t bits;
  394. Fixed32(const Fixed32&) = default;
  395. ~Fixed32() = default;
  396. inline Fixed32(int32_t bits, bool dummy) :
  397. bits{ bits }
  398. {
  399. }
  400. inline Fixed32(double x)
  401. {
  402. int integerPart = ::floor(x);
  403. double fractionalPart = x - integerPart;
  404. /*if (x < 0) {
  405. integerPart--;
  406. fractionalPart = 1.0 - fractionalPart;
  407. }*/
  408. bits = int32_t(integerPart) << 16;
  409. bits |= uint32_t(fractionalPart * (1ULL << 16)) & 0xFFFF;
  410. }
  411. inline Fixed32 operator + (const Fixed32& other) {
  412. return Fixed32{ bits + other.bits, true };
  413. }
  414. inline Fixed32& operator +=(const Fixed32& other) {
  415. bits += other.bits;
  416. return *this;
  417. }
  418. inline Fixed32 operator - (const Fixed32& other) {
  419. return Fixed32{ bits - other.bits, true };
  420. }
  421. inline Fixed32 operator * (const Fixed32& other) {
  422. int64_t prod = int64_t(bits) * int64_t(other.bits);
  423. return Fixed32{ int32_t(prod >> 16), true };
  424. //return Fixed32{ (uint64_t(newUp) << 32) | newLo, true };
  425. }
  426. inline bool operator == (const Fixed32& other) {
  427. return bits == other.bits;
  428. }
  429. inline bool operator != (const Fixed32& other) {
  430. return !operator==(other);
  431. }
  432. inline bool operator < (const Fixed32& other) {
  433. return bits < other.bits;
  434. }
  435. inline bool operator <= (const Fixed32& other) {
  436. return operator<(other) || operator==(other);
  437. }
  438. inline bool operator > (const Fixed32& other) {
  439. return bits > other.bits;
  440. }
  441. inline bool operator >= (const Fixed32& other) {
  442. return operator>(other) || operator==(other);
  443. }
  444. };