forked from linhbkhn95/int256
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new utilities function, changed package name and moved to space…
… based formating isntead of tab
- Loading branch information
0xsimulacra
committed
Jan 17, 2025
1 parent
5a96414
commit 1f49de0
Showing
11 changed files
with
2,303 additions
and
2,220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,63 @@ | ||
package int256 | ||
|
||
import ( | ||
"math/big" | ||
"math/big" | ||
|
||
"github.com/holiman/uint256" | ||
"github.com/holiman/uint256" | ||
) | ||
|
||
var negativeOneBigInt = big.NewInt(-1) | ||
var zero = big.NewInt(0) | ||
|
||
func (z *Int) ToBig() *big.Int { | ||
b := z.abs.ToBig() | ||
if z.neg { | ||
return b.Mul(b, negativeOneBigInt) | ||
} | ||
return b | ||
b := z.abs.ToBig() | ||
if z.neg { | ||
return b.Mul(b, negativeOneBigInt) | ||
} | ||
return b | ||
} | ||
|
||
func MustFromBig(x *big.Int) *Int { | ||
big, overflow := FromBig(x) | ||
if overflow { | ||
panic("cannot parsing from big.Int") | ||
} | ||
return big | ||
big, overflow := FromBig(x) | ||
if overflow { | ||
panic("cannot parsing from big.Int") | ||
} | ||
return big | ||
} | ||
|
||
func FromBig(x *big.Int) (*Int, bool) { | ||
num := x | ||
neg := false | ||
if x.Cmp(zero) == -1 { | ||
num = new(big.Int).Mul(x, negativeOneBigInt) | ||
neg = true | ||
} | ||
abs, overflow := uint256.FromBig(num) | ||
/* | ||
type Int [4]uint64 | ||
Currently, uint26 has maxWord is 4 | ||
bigInt has len(words) that can great than 4. So we can receive overflow error. | ||
words := b.Bits() | ||
overflow := len(words) > maxWords | ||
ref from uint256 code | ||
https://github.com/holiman/uint256/blob/master/conversion.go#L202 | ||
*/ | ||
if overflow { | ||
abs, err := uint256.FromDecimal(x.String()) | ||
if err != nil { | ||
return nil, overflow | ||
} | ||
neg = x.Sign() < 0 | ||
return &Int{ | ||
abs, | ||
neg, | ||
}, true | ||
} | ||
return &Int{ | ||
abs, | ||
neg, | ||
}, false | ||
num := x | ||
neg := false | ||
if x.Cmp(zero) == -1 { | ||
num = new(big.Int).Mul(x, negativeOneBigInt) | ||
neg = true | ||
} | ||
abs, overflow := uint256.FromBig(num) | ||
/* | ||
type Int [4]uint64 | ||
Currently, uint26 has maxWord is 4 | ||
bigInt has len(words) that can great than 4. So we can receive overflow error. | ||
words := b.Bits() | ||
overflow := len(words) > maxWords | ||
ref from uint256 code | ||
https://github.com/holiman/uint256/blob/master/conversion.go#L202 | ||
*/ | ||
if overflow { | ||
abs, err := uint256.FromDecimal(x.String()) | ||
if err != nil { | ||
return nil, overflow | ||
} | ||
neg = x.Sign() < 0 | ||
return &Int{ | ||
abs, | ||
neg, | ||
}, true | ||
} | ||
return &Int{ | ||
abs, | ||
neg, | ||
}, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.