Support negative-valued action parameters (#483)

by converting them in the Python helper code into a positive value
whose bit pattern is the 2's complement representation of the negative
value.
This commit is contained in:
Andy Fingerhut 2023-06-24 12:00:32 -04:00 committed by GitHub
parent e855fa7dfd
commit bf5433b7c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,7 +48,17 @@ def bitwidthToBytes(bitwidth):
def encodeNum(number, bitwidth):
byte_len = bitwidthToBytes(bitwidth)
# If number is negative, calculate the positive number that its
# 2's complement encoding would look like in 'bitwidth' bits.
orig_number = number
if number < 0:
if number < -(2 ** (bitwidth-1)):
raise Exception("Negative number, %d, has 2's complement representation that does not fit in %d bits" % (number, bitwidth))
number = (2 ** bitwidth) + number
num_str = '%x' % number
if orig_number < 0:
print("CONVERT_NEGATIVE_NUMBER debug: orig_number=%s number=%s bitwidth=%d num_str='%s'"
"" % (orig_number, number, bitwidth, num_str))
if number >= 2 ** bitwidth:
raise Exception("Number, %d, does not fit in %d bits" % (number, bitwidth))
return bytes.fromhex('0' * (byte_len * 2 - len(num_str)) + num_str)