-
-
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.
2.12.4 - Add integer clamping for scaled values
- Loading branch information
Showing
10 changed files
with
65 additions
and
9 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
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,5 +1,5 @@ | ||
--- | ||
major: 2 | ||
minor: 12 | ||
patch: 3 | ||
patch: 4 | ||
entry: runtimepy |
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
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
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,30 @@ | ||
""" | ||
A module implementing an interface for keeping track of primitive-integer | ||
bounds (based on bit width). | ||
""" | ||
|
||
# built-in | ||
from typing import NamedTuple | ||
|
||
|
||
class IntegerBounds(NamedTuple): | ||
"""A container for integer bounds.""" | ||
|
||
min: int | ||
max: int | ||
|
||
def clamp(self, val: int) -> int: | ||
""" | ||
Ensure that 'val' is between min and max, use the min or max value | ||
instead of the provided value if it exceeds these bounds. | ||
""" | ||
|
||
return max(self.min, min(val, self.max)) | ||
|
||
@staticmethod | ||
def create(byte_count: int, signed: bool) -> "IntegerBounds": | ||
"""Compute maximum and minimum values given size and signedness.""" | ||
|
||
min_val = 0 if not signed else -1 * (2 ** (byte_count * 8 - 1)) | ||
width = 8 * byte_count if not signed else 8 * byte_count - 1 | ||
return IntegerBounds(min_val, (2**width) - 1) |
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