There is no implicit assignment from int128_t to int256_t (or in general, the boost multi precision types and __int128_t). TODO: look into the perf of this. I think the boost library is very slow with bitwise ops but reasonably fast with arithmetic ops so different implementations of this could have big perf differences.
| 68 | /// ops but reasonably fast with arithmetic ops so different implementations of this |
| 69 | /// could have big perf differences. |
| 70 | inline int256_t ConvertToInt256(const int128_t& x) { |
| 71 | if (x < 0) { |
| 72 | uint64_t hi = static_cast<uint64_t>(-x >> 64); |
| 73 | uint64_t lo = static_cast<uint64_t>(-x); |
| 74 | int256_t v = hi; |
| 75 | v <<= 64; |
| 76 | v |= lo; |
| 77 | return -v; |
| 78 | } else { |
| 79 | uint64_t hi = static_cast<uint64_t>(x >> 64); |
| 80 | uint64_t lo = static_cast<uint64_t>(x); |
| 81 | int256_t v = hi; |
| 82 | v <<= 64; |
| 83 | v |= lo; |
| 84 | return v; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Converts an int256_t to an int128_t. int256_t does support convert_to<int128_t>() but |
| 89 | /// that produces an approximate int128_t which makes it unusable. |