1
0

Fixed.h 22 KB

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