Skip to content

Commit

Permalink
Fix converting int to int256 (#925)
Browse files Browse the repository at this point in the history
Co-authored-by: SpyCheese <[email protected]>
  • Loading branch information
EmelyanenkoK and SpyCheese authored Mar 4, 2024
1 parent 310dd6d commit b09f910
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
27 changes: 21 additions & 6 deletions crypto/common/bigint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ class PropagateConstSpan {
size_t size_{0};
};

struct Normalize {};

template <class Tr = BigIntInfo>
class AnyIntView {
public:
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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 << ")";
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions crypto/common/refint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions crypto/common/refint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crypto/vm/cells/CellSlice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/regression-tests.ans
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b09f910

Please sign in to comment.