Fixed.h 16 KB

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