Fixed.h 22 KB

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