mirror of
				https://github.com/ethereum/solidity
				synced 2023-10-03 13:03:40 +00:00 
			
		
		
		
	Merge pull request #4277 from ethereum/signedRightShift
Signed Right Shift: Additional test and more explanation.
This commit is contained in:
		
						commit
						e2f4a9fcf4
					
				| @ -1088,8 +1088,13 @@ TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, Typ | |||||||
| 				else | 				else | ||||||
| 				{ | 				{ | ||||||
| 					if (m_value.numerator() < 0) | 					if (m_value.numerator() < 0) | ||||||
| 						// add 1 to the negative value before dividing to get a result that is strictly too large
 | 						// Add 1 to the negative value before dividing to get a result that is strictly too large,
 | ||||||
| 						// subtract 1 afterwards to round towards negative infinity
 | 						// then subtract 1 afterwards to round towards negative infinity.
 | ||||||
|  | 						// This is the same algorithm as used in ExpressionCompiler::appendShiftOperatorCode(...).
 | ||||||
|  | 						// To see this note that for negative x, xor(x,all_ones) = (-x-1) and
 | ||||||
|  | 						// therefore xor(div(xor(x,all_ones), exp(2, shift_amount)), all_ones) is
 | ||||||
|  | 						// -(-x - 1) / 2^shift_amount - 1, which is the same as
 | ||||||
|  | 						// (x + 1) / 2^shift_amount - 1.
 | ||||||
| 						value = rational((m_value.numerator() + 1) / boost::multiprecision::pow(bigint(2), exponent) - bigint(1), 1); | 						value = rational((m_value.numerator() + 1) / boost::multiprecision::pow(bigint(2), exponent) - bigint(1), 1); | ||||||
| 					else | 					else | ||||||
| 						value = rational(m_value.numerator() / boost::multiprecision::pow(bigint(2), exponent), 1); | 						value = rational(m_value.numerator() / boost::multiprecision::pow(bigint(2), exponent), 1); | ||||||
|  | |||||||
| @ -10508,6 +10508,39 @@ BOOST_AUTO_TEST_CASE(shift_right_garbled) | |||||||
| 	ABI_CHECK(callContractFunction("f(uint8,uint8)", u256(0x0), u256(0x1004)), encodeArgs(u256(0xf))); | 	ABI_CHECK(callContractFunction("f(uint8,uint8)", u256(0x0), u256(0x1004)), encodeArgs(u256(0xf))); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | BOOST_AUTO_TEST_CASE(shift_right_garbled_signed) | ||||||
|  | { | ||||||
|  | 	char const* sourceCode = R"( | ||||||
|  | 			contract C { | ||||||
|  | 				function f(int8 a, uint8 b) returns (int) { | ||||||
|  | 					assembly { | ||||||
|  | 						a := 0xfffffff0 | ||||||
|  | 					} | ||||||
|  | 					// Higher bits should be signextended before the shift
 | ||||||
|  | 					return a >> b; | ||||||
|  | 				} | ||||||
|  | 				function g(int8 a, uint8 b) returns (int) { | ||||||
|  | 					assembly { | ||||||
|  | 						a := 0xf0 | ||||||
|  | 					} | ||||||
|  | 					// Higher bits should be signextended before the shift
 | ||||||
|  | 					return a >> b; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		)"; | ||||||
|  | 	compileAndRun(sourceCode, 0, "C"); | ||||||
|  | 	ABI_CHECK(callContractFunction("f(int8,uint8)", u256(0x0), u256(3)), encodeArgs(u256(-2))); | ||||||
|  | 	ABI_CHECK(callContractFunction("f(int8,uint8)", u256(0x0), u256(4)), encodeArgs(u256(-1))); | ||||||
|  | 	ABI_CHECK(callContractFunction("f(int8,uint8)", u256(0x0), u256(0xFF)), encodeArgs(u256(-1))); | ||||||
|  | 	ABI_CHECK(callContractFunction("f(int8,uint8)", u256(0x0), u256(0x1003)), encodeArgs(u256(-2))); | ||||||
|  | 	ABI_CHECK(callContractFunction("f(int8,uint8)", u256(0x0), u256(0x1004)), encodeArgs(u256(-1))); | ||||||
|  | 	ABI_CHECK(callContractFunction("g(int8,uint8)", u256(0x0), u256(3)), encodeArgs(u256(-2))); | ||||||
|  | 	ABI_CHECK(callContractFunction("g(int8,uint8)", u256(0x0), u256(4)), encodeArgs(u256(-1))); | ||||||
|  | 	ABI_CHECK(callContractFunction("g(int8,uint8)", u256(0x0), u256(0xFF)), encodeArgs(u256(-1))); | ||||||
|  | 	ABI_CHECK(callContractFunction("g(int8,uint8)", u256(0x0), u256(0x1003)), encodeArgs(u256(-2))); | ||||||
|  | 	ABI_CHECK(callContractFunction("g(int8,uint8)", u256(0x0), u256(0x1004)), encodeArgs(u256(-1))); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| BOOST_AUTO_TEST_CASE(shift_right_uint32) | BOOST_AUTO_TEST_CASE(shift_right_uint32) | ||||||
| { | { | ||||||
| 	char const* sourceCode = R"( | 	char const* sourceCode = R"( | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user