-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
21 additions
and
22 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
2 changes: 1 addition & 1 deletion
2
...mpiler/pipeline/controlflowanalysis/expression_simplification/rules/positive_constants.py
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def normalize_int(v: int, size: int, signed: bool) -> int: | ||
""" | ||
Normalizes an integer value to a specific size and signedness. | ||
This function takes an integer value 'v' and normalizes it to fit within | ||
the specified 'size' in bits by discarding overflowing bits. If 'signed' is | ||
true, the value is treated as a signed integer, i.e. interpreted as a two's complement. | ||
Therefore the return value will be negative iff 'signed' is true and the most-significant bit is set. | ||
:param v: The value to be normalized. | ||
:param size: The desired bit size for the normalized integer. | ||
:param signed: True if the integer should be treated as signed. | ||
:return: The normalized integer value. | ||
""" | ||
value = v & ((1 << size) - 1) | ||
if signed and value & (1 << (size - 1)): | ||
return value - (1 << size) | ||
else: | ||
return value |