Fixed.h 21 KB

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