Fixed.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #ifndef MANDEL_FIXED128_H
  2. #define MANDEL_FIXED128_H
  3. #include <cinttypes>
  4. #include <cmath>
  5. #include <utility>
  6. #include <array>
  7. #include <vector>
  8. #include <boost/multiprecision/cpp_int.hpp>
  9. #include <boost/multiprecision/cpp_bin_float.hpp>
  10. struct Fixed512
  11. {
  12. using Once = boost::multiprecision::int512_t;
  13. using Twice = boost::multiprecision::int1024_t;
  14. Once body;
  15. inline explicit Fixed512(const Once& body) :
  16. body{ body }
  17. {}
  18. using Float256 = boost::multiprecision::number<
  19. boost::multiprecision::backends::cpp_bin_float<
  20. 240, boost::multiprecision::backends::digit_base_2, void, boost::int16_t, -16382, 16383>,
  21. boost::multiprecision::et_off>;
  22. inline Fixed512(const Float256& val)
  23. {
  24. body = Once{ val * boost::multiprecision::pow(Float256{2}, 512 - 32) };
  25. }
  26. inline Fixed512(double val)
  27. {
  28. body = Once{ boost::multiprecision::pow(Float256{2}, 512 - 32) * val };
  29. }
  30. inline operator Float256(void) const {
  31. return boost::multiprecision::pow(Float256{ 0.5 }, 512 - 32) * Float256{ body };
  32. }
  33. inline Fixed512& operator += (const Fixed512& other) {
  34. body += other.body;
  35. return *this;
  36. }
  37. inline Fixed512 operator + (const Fixed512& other) const {
  38. return Fixed512{ body + other.body };
  39. }
  40. inline Fixed512& operator -= (const Fixed512& other) {
  41. body -= other.body;
  42. return *this;
  43. }
  44. inline Fixed512 operator - (const Fixed512& other) const {
  45. return Fixed512{ body - other.body };
  46. }
  47. inline Fixed512 operator * (const Fixed512& other) const {
  48. auto prod = Twice{ this->body } * other.body;
  49. return Fixed512{ Once{ prod >> (512 - 64) } };
  50. }
  51. inline Fixed512& operator *= (const Fixed512& other) {
  52. auto prod = Twice{ this->body } * other.body;
  53. body = Once{ prod >> (512 - 64) };
  54. return *this;
  55. }
  56. inline bool operator > (const Fixed512& other) {
  57. return this->body > other.body;
  58. }
  59. };
  60. struct Fixed128
  61. {
  62. uint64_t upper;
  63. uint64_t lower;
  64. Fixed128(const Fixed128&) = default;
  65. ~Fixed128() = default;
  66. inline Fixed128(uint64_t upper, uint64_t lower) :
  67. upper{ upper }, lower{ lower }
  68. {
  69. }
  70. inline Fixed128(uint32_t a, uint32_t b, uint32_t c, uint32_t d) :
  71. upper{ (uint64_t(a) << 32) | b }, lower{ (uint64_t(c) << 32) | d }
  72. {
  73. }
  74. inline Fixed128(double x)
  75. {
  76. const double twoToThe32 = double(0x100000000ULL);
  77. upper = uint64_t(int64_t(x * twoToThe32));
  78. double remainder = x - double(upper) / twoToThe32;
  79. lower = uint64_t(int64_t(remainder * twoToThe32 * twoToThe32 * twoToThe32));
  80. /*int integerPart = ::floor(x);
  81. double fractionalPart = x - integerPart;
  82. upper = int64_t(integerPart) << 32;
  83. upper |= uint64_t(fractionalPart * (1ULL << 32)) & 0xFFFFFFFFULL;
  84. lower = 0;// uint64_t(fractionalPart * (1ULL << 32) * (1ULL << 63) * 2);*/
  85. }
  86. inline Fixed128 operator + (const Fixed128& other) const {
  87. uint64_t lowerAdded = lower + other.lower;
  88. uint64_t upperAdded = upper + other.upper + (lowerAdded < lower);
  89. return Fixed128{ upperAdded, lowerAdded };
  90. }
  91. inline Fixed128& operator +=(const Fixed128& other) {
  92. uint64_t lowerAdded = lower + other.lower;
  93. upper += other.upper + (lowerAdded < lower);
  94. lower = lowerAdded;
  95. return *this;
  96. }
  97. inline Fixed128 operator - (const Fixed128& other) const {
  98. uint64_t lowerSubbed = lower - other.lower;
  99. uint64_t upperSubbed = upper - other.upper - (lowerSubbed > lower);
  100. return Fixed128{ upperSubbed, lowerSubbed };
  101. }
  102. inline Fixed128 operator - (void) const {
  103. return this->operator~() + Fixed128{ 0, 0, 0, 1 };
  104. }
  105. //private:
  106. static inline std::pair<uint64_t, uint64_t> mul64(int64_t a, int64_t b) {
  107. int32_t aa[2] = { int32_t(a >> 32), int32_t(a & 0xFFFFFFFF) };
  108. int32_t bb[2] = { int32_t(b >> 32), int32_t(b & 0xFFFFFFFF) };
  109. int32_t res[4];
  110. int64_t temp = int64_t(aa[1]) * bb[1];
  111. res[3] = temp & 0xFFFFFFFF;
  112. int32_t carry = temp >> 32;
  113. temp = int64_t(aa[0]) * bb[1] + int64_t(aa[1]) * bb[0] + carry;
  114. res[2] = temp & 0xFFFFFFFF;
  115. carry = temp >> 32;
  116. temp = int64_t(aa[0]) * bb[0] + carry;
  117. res[1] = temp & 0xFFFFFFFF;
  118. res[0] = temp >> 32;
  119. return std::make_pair(uint64_t((int64_t(res[0]) << 32) | res[1]), uint64_t((int64_t(res[2]) << 32) | res[3]));
  120. }
  121. static inline std::pair<uint64_t, uint64_t> mulu64(uint64_t a, uint64_t b) {
  122. uint32_t aa[2] = { uint32_t(a >> 32), uint32_t(a & 0xFFFFFFFF) };
  123. uint32_t bb[2] = { uint32_t(b >> 32), uint32_t(b & 0xFFFFFFFF) };
  124. uint32_t res[4];
  125. uint64_t temp = uint64_t(aa[1]) * bb[1];
  126. res[3] = temp & 0xFFFFFFFF;
  127. uint32_t carry = temp >> 32;
  128. temp = uint64_t(aa[0]) * bb[1] + uint64_t(aa[1]) * bb[0] + carry;
  129. res[2] = temp & 0xFFFFFFFF;
  130. carry = temp >> 32;
  131. temp = uint64_t(aa[0]) * bb[0] + carry;
  132. res[1] = temp & 0xFFFFFFFF;
  133. res[0] = temp >> 32;
  134. return std::make_pair((uint64_t(res[0]) << 32) | res[1], (uint64_t(res[2]) << 32) | res[3] );
  135. }
  136. public:
  137. inline Fixed128 operator * (const Fixed128& other) const {
  138. if (this->operator!=(Fixed128{ 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }) && isNegative()) {
  139. return -(other * this->operator-());
  140. }
  141. if (other.isNegative()) {
  142. return -((-other) * (*this));
  143. }
  144. auto [uuc, uu] = mulu64(upper, other.upper);
  145. auto [ulc, ul] = mulu64(upper, other.lower);
  146. auto [luc, lu] = mulu64(lower, other.upper);
  147. auto [llc, ll] = mulu64(lower, other.lower);
  148. uint64_t res[4] = { 0, 0, 0, 0 };
  149. res[3] = ll;
  150. res[2] += lu;
  151. res[2] += ul;
  152. if (res[2] < ul)
  153. res[1]++;
  154. res[2] += llc;
  155. if (res[2] < llc)
  156. res[1]++;
  157. res[1] += uu;
  158. if (res[1] < uu)
  159. res[0]++;
  160. res[1] += ulc;
  161. if (res[1] < ulc)
  162. res[0]++;
  163. res[1] += luc;
  164. if (res[1] < luc)
  165. res[0]++;
  166. res[0] += uuc;
  167. 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) };
  168. /*if (isNegative()) {
  169. return -(this->operator-() * other);
  170. }
  171. if (other.isNegative()) {
  172. return -(*this * (-other));
  173. }
  174. bool otherNegative = other.isNegative();
  175. uint32_t quarters[4] = {
  176. (upper >> 32) & 0xFFFFFFFF,
  177. upper & 0xFFFFFFFF,
  178. (lower >> 32) & 0xFFFFFFFF,
  179. lower & 0xFFFFFFFF
  180. };
  181. auto [a, ra] = other.mul(quarters[0]);
  182. auto [b, rb] = other.mul(quarters[1]);
  183. auto [c, rc] = other.mul(quarters[2]);
  184. auto [d, rd] = other.mul(quarters[3]);
  185. b.arshift(1);
  186. c.arshift(2);
  187. d.arshift(3);
  188. Fixed128 carries = { uint32_t(rb), uint32_t(rc), uint32_t(rd), 0 };
  189. Fixed128 result = a + b + c + d + carries;
  190. return result;*/
  191. }
  192. inline std::pair<Fixed128, uint32_t> mul(uint32_t factor) const {
  193. uint32_t quarters[4] = {
  194. uint32_t(upper >> 32) & 0xFFFFFFFF,
  195. uint32_t(upper) & 0xFFFFFFFF,
  196. uint32_t(lower >> 32) & 0xFFFFFFFF,
  197. uint32_t(lower) & 0xFFFFFFFF
  198. };
  199. uint32_t newQ[4];
  200. uint32_t carry = 0;
  201. for (int i = 3; i >= 0; i--) {
  202. int64_t prod = int64_t(quarters[i]) * factor + carry;
  203. newQ[i] = prod & 0xFFFFFFFF;
  204. carry = prod >> 32;
  205. }
  206. /* newQ[i] = quarters[i] * factor;
  207. uint64_t tempLower = newQ[3];
  208. uint64_t newLower = tempLower + (newQ[2] << 32);
  209. uint64_t newUpper = (newQ[2] >> 32) + newQ[1] + (newQ[0] << 32) + (newLower < tempLower ? 1 : 0);*/
  210. return std::make_pair(Fixed128{ newQ[0], newQ[1], newQ[2], newQ[3] }, carry);
  211. }
  212. /*
  213. inline void arshift(int fac32) {
  214. uint32_t temp = 0;
  215. switch (fac32) {
  216. case 0:
  217. return;
  218. case 1:
  219. temp = upper & 0xFFFFFFFF;
  220. upper = uint64_t(int64_t(upper) >> 32);
  221. lower >>= 32;
  222. lower |= uint64_t(temp) << 32;
  223. case 2:
  224. lower = upper;
  225. upper = uint64_t(int64_t(upper) >> 63);
  226. case 3:
  227. lower = uint64_t(int64_t(upper) >> 32);
  228. upper = uint64_t(int64_t(upper) >> 63);
  229. default:
  230. lower = uint64_t(int64_t(upper) >> 63);
  231. upper = uint64_t(int64_t(upper) >> 63);
  232. }
  233. }*/
  234. /*
  235. inline Fixed128 operator * (const Fixed128& other) const {
  236. int32_t quarters[4] = {
  237. (upper >> 32) & 0xFFFFFFFF,
  238. upper & 0xFFFFFFFF,
  239. (lower >> 32) & 0xFFFFFFFF,
  240. lower & 0xFFFFFFFF
  241. };
  242. int32_t otherQuarters[4] = {
  243. (other.upper >> 32) & 0xFFFFFFFF,
  244. other.upper & 0xFFFFFFFF,
  245. (other.lower >> 32) & 0xFFFFFFFF,
  246. other.lower & 0xFFFFFFFF
  247. };
  248. int64_t prods[4][4];
  249. for (int i = 0; i < 4; i++) {
  250. for (int j = 0; j < 4 && j + i < 5; j++) {
  251. if (i == 0 || j == 0)
  252. prods[i][j] = int64_t(quarters[i]) * int64_t(otherQuarters[j]);
  253. else
  254. prods[i][j] = uint64_t(uint32_t(quarters[i])) * uint64_t(uint32_t(otherQuarters[j]));
  255. }
  256. }
  257. Fixed128 ret = { 0, 0 };
  258. for (int i = 0; i < 4; i++) {
  259. for (int j = 0; j < 4 && j + i < 5; j++) {
  260. if (i == 0 || j == 0)
  261. ret.addSigned(prods[i][j], i + j);
  262. else
  263. ret.add(prods[i][j], i + j);
  264. }
  265. }
  266. return ret;
  267. /*
  268. int64_t x00 = int64_t(quarters[0]) * int64_t(otherQuarters[0]);
  269. int64_t x01 = int64_t(quarters[0]) * int64_t(otherQuarters[1]);
  270. int64_t x02 = int64_t(quarters[0]) * int64_t(otherQuarters[2]);
  271. int64_t x03 = int64_t(quarters[0]) * int64_t(otherQuarters[3]);
  272. int64_t x10 = int64_t(quarters[1]) * int64_t(otherQuarters[0]);
  273. int64_t x11 = int64_t(quarters[1]) * int64_t(otherQuarters[1]);
  274. int64_t x12 = int64_t(quarters[1]) * int64_t(otherQuarters[2]);
  275. int64_t x13 = int64_t(quarters[1]) * int64_t(otherQuarters[3]);
  276. int64_t x20 = int64_t(quarters[2]) * int64_t(otherQuarters[0]);
  277. int64_t x21 = int64_t(quarters[2]) * int64_t(otherQuarters[1]);
  278. int64_t x22 = int64_t(quarters[2]) * int64_t(otherQuarters[2]);
  279. int64_t x30 = int64_t(quarters[3]) * int64_t(otherQuarters[0]);
  280. int64_t x31 = int64_t(quarters[3]) * int64_t(otherQuarters[1]);
  281. Fixed128 ret = { 0, 0 };
  282. /*uint32_t newQuarters[4] = {
  283. x00,
  284. x01 + x10,
  285. x02 + x11 + x20,
  286. x03 + x12 + x21 + x30,
  287. };*//*
  288. ret.add(x00, 0);
  289. ret.add(x01 + x10, 1);
  290. ret.add(x02 + x11 + x20, 2);
  291. ret.add(x03 + x12 + x21 + x30, 3);
  292. ret.add(x13 + x22 + x31, 4);
  293. return ret;*/
  294. /*}*/
  295. private:
  296. inline void add(uint64_t val, int b32offset) {
  297. switch (b32offset) {
  298. case 0:
  299. upper += val << 32;
  300. return;
  301. case 1:
  302. upper += val;
  303. return;
  304. case 2:
  305. upper += val >> 32;
  306. lower += val << 32;
  307. return;
  308. case 3: {
  309. uint64_t newLower = lower + val;
  310. if (newLower < lower) upper++;
  311. lower = newLower;
  312. return;
  313. }
  314. case 4:
  315. uint64_t newLower = lower + (val >> 32);
  316. if (lower > newLower) upper++;
  317. lower += newLower;
  318. return;
  319. }
  320. }
  321. inline void addSigned(int64_t val, int b32offset) {
  322. switch (b32offset) {
  323. case 0:
  324. upper += val << 32;
  325. return;
  326. case 1:
  327. upper += val;
  328. return;
  329. case 2:
  330. upper += val >> 32;
  331. lower += val << 32;
  332. return;
  333. case 3:
  334. lower += val;
  335. if (val < 0) upper--;
  336. return;
  337. case 4: {
  338. uint64_t newLower = lower + (val >> 32);
  339. if (lower > newLower) upper++;
  340. lower = newLower;
  341. return;
  342. }
  343. default:
  344. if (val < 0) {
  345. if (lower == 0) upper--;
  346. lower--;
  347. }
  348. return;
  349. }
  350. }
  351. public:
  352. inline Fixed128 operator / (const Fixed128& other) {
  353. if (this->operator!=(Fixed128{ 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }) && isNegative()) {
  354. return -((-(*this)) / other);
  355. }
  356. if (other != Fixed128{ 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF } && other.isNegative()) {
  357. return -((*this) / (-other));
  358. }
  359. using u256 = std::array<uint64_t, 4>;
  360. u256 bigDividend = { upper, lower, 0, 0 };
  361. u256 bigDivisor = { 0, 0, other.upper, other.lower };
  362. auto twice = [] (u256& x) {
  363. bool carry = false;
  364. for (int i = 3; i >= 0; i--) {
  365. bool oldCarry = carry;
  366. carry = x[i] & 0x1000000000000000ULL;
  367. x[i] <<= 1;
  368. if (oldCarry) x[i] ++;
  369. }
  370. };
  371. auto geq = [] (const u256& a, const u256& b) -> bool {
  372. for (int i = 0; i < 4; i++) {
  373. if (a[i] > b[i])
  374. return true;
  375. if (a[i] < b[i])
  376. return false;
  377. }
  378. return true;
  379. };
  380. auto sub = [] (u256& a, const u256& b) -> bool {
  381. bool carry = false;
  382. for (int i = 3; i >= 0; i--) {
  383. uint64_t oldA = a[i];
  384. a[i] -= b[i];
  385. carry = oldA < a[i];
  386. }
  387. return carry;
  388. };
  389. auto add = [] (u256& a, const u256& b) -> bool {
  390. bool carry = false;
  391. for (int i = 3; i >= 0; i--) {
  392. uint64_t oldA = a[i];
  393. a[i] += b[i];
  394. carry = oldA > a[i];
  395. }
  396. return carry;
  397. };
  398. u256 growingCount = { 0, 0, 0, 1 };
  399. u256 quotient = { 0, 0, 0, 0 };
  400. std::vector<u256> growingStack = { bigDivisor };
  401. while (true) {
  402. u256 beforeSub = bigDividend;
  403. const u256& gr = growingStack[growingStack.size() - 1];
  404. if (!sub(bigDividend, gr)) {
  405. add(quotient, growingCount);
  406. u256 tw = gr; twice(tw);
  407. growingStack.push_back(tw);
  408. }
  409. else if (geq(bigDivisor, bigDividend)) {
  410. break;
  411. }
  412. else {
  413. bigDividend = beforeSub;
  414. growingStack.pop_back();
  415. }
  416. }
  417. return Fixed128{ quotient[2], quotient[3] };
  418. }
  419. bool isNegative(void) const {
  420. return upper >> 63;
  421. }
  422. operator double(void) const {
  423. const int64_t twoToThe32 = 0x100000000ULL;
  424. return double(int64_t(upper)) / twoToThe32 + int64_t(lower) / twoToThe32 / twoToThe32 / twoToThe32;
  425. }
  426. inline Fixed128 operator ~ (void) const {
  427. return Fixed128{ ~upper, ~lower };
  428. }
  429. inline bool operator == (const Fixed128& other) const {
  430. return upper == other.upper && lower == other.lower;
  431. }
  432. inline bool operator != (const Fixed128& other) const {
  433. return !operator==(other);
  434. }
  435. inline bool operator < (const Fixed128& other) const {
  436. return upper < other.upper || (upper == other.upper && lower < other.lower);
  437. }
  438. inline bool operator <= (const Fixed128& other) const {
  439. return operator<(other) || operator==(other);
  440. }
  441. inline bool operator > (const Fixed128& other) const {
  442. return upper > other.upper || (upper == other.upper && lower > other.lower);
  443. }
  444. inline bool operator >= (const Fixed128& other) const {
  445. return operator>(other) || operator==(other);
  446. }
  447. };
  448. struct Fixed64
  449. {
  450. bool sign;
  451. uint64_t bits;
  452. Fixed64(const Fixed64&) = default;
  453. ~Fixed64() = default;
  454. inline Fixed64(uint64_t bits, bool /* dummy */) :
  455. bits{ bits }
  456. {
  457. }
  458. inline Fixed64(double x)
  459. {
  460. if (x < 0) {
  461. sign = true;
  462. x *= -1;
  463. }
  464. else {
  465. sign = false;
  466. }
  467. int integerPart = int(x);
  468. double fractionalPart = x - integerPart;
  469. bits = uint64_t(integerPart) << 32;
  470. bits |= uint64_t(fractionalPart * (1ULL << 32)) & 0xFFFFFFFF;
  471. }
  472. inline Fixed64 operator + (const Fixed64& other) {
  473. return Fixed64{ bits + other.bits, true };
  474. }
  475. inline Fixed64& operator +=(const Fixed64& other) {
  476. bits += other.bits;
  477. return *this;
  478. }
  479. inline Fixed64 operator - (const Fixed64& other) {
  480. return Fixed64{ bits - other.bits, true };
  481. }
  482. inline Fixed64 operator * (const Fixed64& other) {
  483. /*int32_t upper = bits >> 32;
  484. uint32_t lower = uint32_t(bits & 0xFFFFFFFF);
  485. int64_t upup = int64_t(upper) * int64_t(upper);
  486. int64_t loup = int64_t(upper) * int64_t(lower);
  487. int64_t lolo = int64_t(lower) * int64_t(lower);
  488. int32_t newUp = upup & 0xFFFFFFFF + (loup >> 32);
  489. int32_t newLo = loup & 0xFFFFFFFF + (lolo >> 32);*/
  490. double d = int32_t(bits >> 32) + double(uint32_t(bits)) / (1ULL << 32);
  491. double od = int32_t(other.bits >> 32) + double(uint32_t(other.bits)) / (1ULL << 32);
  492. return d * od * ((other.sign != sign) ? -1 : 1);
  493. //return Fixed64{ (uint64_t(newUp) << 32) | newLo, true };
  494. }
  495. inline bool operator == (const Fixed64& other) {
  496. return bits == other.bits;
  497. }
  498. inline bool operator != (const Fixed64& other) {
  499. return !operator==(other);
  500. }
  501. inline bool operator < (const Fixed64& other) {
  502. return bits < other.bits;
  503. }
  504. inline bool operator <= (const Fixed64& other) {
  505. return operator<(other) || operator==(other);
  506. }
  507. inline bool operator > (const Fixed64& other) {
  508. return bits > other.bits;
  509. }
  510. inline bool operator >= (const Fixed64& other) {
  511. return operator>(other) || operator==(other);
  512. }
  513. };
  514. struct Fixed32
  515. {
  516. int32_t bits;
  517. Fixed32(const Fixed32&) = default;
  518. ~Fixed32() = default;
  519. inline Fixed32(int32_t bits, bool dummy) :
  520. bits{ bits }
  521. {
  522. }
  523. inline Fixed32(double x)
  524. {
  525. int integerPart = int(::floor(x));
  526. double fractionalPart = x - integerPart;
  527. /*if (x < 0) {
  528. integerPart--;
  529. fractionalPart = 1.0 - fractionalPart;
  530. }*/
  531. bits = int32_t(integerPart) << 16;
  532. bits |= uint32_t(fractionalPart * (1ULL << 16)) & 0xFFFF;
  533. }
  534. inline Fixed32 operator + (const Fixed32& other) {
  535. return Fixed32{ bits + other.bits, true };
  536. }
  537. inline Fixed32& operator +=(const Fixed32& other) {
  538. bits += other.bits;
  539. return *this;
  540. }
  541. inline Fixed32 operator - (const Fixed32& other) {
  542. return Fixed32{ bits - other.bits, true };
  543. }
  544. inline Fixed32 operator * (const Fixed32& other) {
  545. int64_t prod = int64_t(bits) * int64_t(other.bits);
  546. return Fixed32{ int32_t(prod >> 16), true };
  547. //return Fixed32{ (uint64_t(newUp) << 32) | newLo, true };
  548. }
  549. inline bool operator == (const Fixed32& other) {
  550. return bits == other.bits;
  551. }
  552. inline bool operator != (const Fixed32& other) {
  553. return !operator==(other);
  554. }
  555. inline bool operator < (const Fixed32& other) {
  556. return bits < other.bits;
  557. }
  558. inline bool operator <= (const Fixed32& other) {
  559. return operator<(other) || operator==(other);
  560. }
  561. inline bool operator > (const Fixed32& other) {
  562. return bits > other.bits;
  563. }
  564. inline bool operator >= (const Fixed32& other) {
  565. return operator>(other) || operator==(other);
  566. }
  567. };
  568. #endif // MANDEL_FIXED128_H