1
0

Fixed.h 22 KB

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