Fixed.h 15 KB

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