mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Improved bitsFromValue implementation.
This commit is contained in:
parent
7fea159d27
commit
cfe630c717
@ -180,15 +180,20 @@ ValueConstraint ValueConstraint::valueFromBits(u256 _minBits, u256 _maxBits)
|
|||||||
|
|
||||||
ValueConstraint ValueConstraint::bitsFromValue(u256 _minValue, u256 _maxValue)
|
ValueConstraint ValueConstraint::bitsFromValue(u256 _minValue, u256 _maxValue)
|
||||||
{
|
{
|
||||||
if (_minValue == _maxValue)
|
// Summary of algorithm: Retain the comon higher order bit prefix between
|
||||||
return constant(_minValue);
|
// _minValue and _maxValue and starting from the point of divergence,
|
||||||
|
// set all following _maxBits bits to 1, and all following _minBits bits to 0.
|
||||||
int highestBit = highestBitSet(_maxValue);
|
// Reasoning: If we count up from _minValue to _maxValue, then the common upper
|
||||||
|
// bits will not change.
|
||||||
|
int divergence = highestBitSet(_minValue ^ _maxValue);
|
||||||
|
u256 lowerBits = u256(-1) >> (255 - divergence);
|
||||||
|
u256 minBits = _minValue & (~lowerBits);
|
||||||
|
u256 maxBits = _maxValue | lowerBits;
|
||||||
return ValueConstraint{
|
return ValueConstraint{
|
||||||
move(_minValue),
|
move(_minValue),
|
||||||
move(_maxValue),
|
move(_maxValue),
|
||||||
0,
|
move(minBits),
|
||||||
u256(-1) >> (255 - highestBit)
|
move(maxBits)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user