1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00

Fix converting int to int256 (#925)

Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
EmelyanenkoK 2024-03-04 17:37:42 +03:00 committed by GitHub
parent 310dd6dec1
commit b09f910bf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 15 deletions

View file

@ -169,8 +169,6 @@ class PropagateConstSpan {
size_t size_{0};
};
struct Normalize {};
template <class Tr = BigIntInfo>
class AnyIntView {
public:
@ -290,6 +288,7 @@ class BigIntG {
public:
enum { word_bits = Tr::word_bits, word_shift = Tr::word_shift, max_bits = len, word_cnt = len / word_shift + 1 };
typedef typename Tr::word_t word_t;
typedef typename Tr::uword_t uword_t;
typedef Tr Traits;
typedef BigIntG<len * 2, Tr> DoubleInt;
@ -312,9 +311,6 @@ class BigIntG {
BigIntG() : n(0) {
}
explicit BigIntG(word_t x) : n(1) {
digits[0] = x;
}
BigIntG(Normalize, word_t x) : n(1) {
if (x >= -Tr::Half && x < Tr::Half) {
digits[0] = x;
} else if (len <= 1) {
@ -325,6 +321,25 @@ class BigIntG {
digits[n++] = (x >> Tr::word_shift) + (digits[0] < 0);
}
}
explicit BigIntG(uword_t x) : n(1) {
if (x < (uword_t)Tr::Half) {
digits[0] = x;
} else if (len <= 1) {
digits[0] = x;
normalize_bool();
} else {
digits[0] = ((x ^ Tr::Half) & (Tr::Base - 1)) - Tr::Half;
digits[n++] = (x >> Tr::word_shift) + (digits[0] < 0);
}
}
explicit BigIntG(unsigned x) : BigIntG(uword_t(x)) {
}
explicit BigIntG(int x) : BigIntG(word_t(x)) {
}
explicit BigIntG(unsigned long x) : BigIntG(uword_t(x)) {
}
explicit BigIntG(long x) : BigIntG(word_t(x)) {
}
BigIntG(const BigIntG& x) : n(x.n) {
std::memcpy(digits, x.digits, n * sizeof(word_t));
///std::cout << "(BiCC " << (const void*)&x << "->" << (void*)this << ")";
@ -2556,7 +2571,7 @@ typedef BigIntG<257, BigIntInfo> BigInt256;
template <int n = 257>
BigIntG<n, BigIntInfo> make_bigint(long long x) {
return BigIntG<n, BigIntInfo>{Normalize(), x};
return BigIntG<n, BigIntInfo>{x};
}
namespace literals {

View file

@ -261,10 +261,6 @@ int sgn(RefInt256 x) {
return x->sgn();
}
RefInt256 make_refint(long long x) {
return td::RefInt256{true, td::Normalize(), x};
}
RefInt256 zero_refint() {
// static RefInt256 Zero = td::RefInt256{true, 0};
// return Zero;

View file

@ -113,8 +113,6 @@ RefInt256 make_refint(Args&&... args) {
return td::RefInt256{true, std::forward<Args>(args)...};
}
extern RefInt256 make_refint(long long x);
extern RefInt256 zero_refint();
extern RefInt256 bits_to_refint(td::ConstBitPtr bits, int n, bool sgnd = false);

View file

@ -595,7 +595,7 @@ td::RefInt256 CellSlice::fetch_int256(unsigned bits, bool sgnd) {
if (!have(bits)) {
return {};
} else if (bits < td::BigInt256::word_shift) {
return td::make_refint(sgnd ? fetch_long(bits) : fetch_ulong(bits));
return td::make_refint(td::int64(sgnd ? fetch_long(bits) : fetch_ulong(bits)));
} else {
td::RefInt256 res{true};
res.unique_write().import_bits(data_bits(), bits, sgnd);
@ -608,7 +608,7 @@ td::RefInt256 CellSlice::prefetch_int256(unsigned bits, bool sgnd) const {
if (!have(bits)) {
return {};
} else if (bits < td::BigInt256::word_shift) {
return td::make_refint(sgnd ? prefetch_long(bits) : prefetch_ulong(bits));
return td::make_refint(td::int64(sgnd ? prefetch_long(bits) : prefetch_ulong(bits)));
} else {
td::RefInt256 res{true};
res.unique_write().import_bits(data_bits(), bits, sgnd);

View file

@ -1,5 +1,5 @@
abce
Test_Bigint_main_default 0327a04f1252c37f77b6706b902ab2c3235c47738bca3f183c837a2c5d22bb6f
Test_Bigint_main_default 76f38492ec19464a1d0eac51d389023a31ce10396b3894061361d159567ce8cd
Test_Bitstrings_main_default a8b08af3116923c4c2a14e138d168375abd0c059f2f780d3267b294929a1110e
Test_Cells_simple_default 832502642fe4fe5db70de82681aedb7d54d7f3530e0069861fff405fe6f6cf23
Test_Fift_bug_div_default 1ac42861ce96b2896001c587f65e9afe1617db48859f19c2f4e3063a20ea60b0