Fixed.h 19 KB

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