Skip to content

Commit

Permalink
Autumn cleaning part 4 - split test/common.py (kuznia-rdzeni/corebloc…
Browse files Browse the repository at this point in the history
  • Loading branch information
lekcyjna123 authored Oct 29, 2023
1 parent 2a11ca9 commit 1015e15
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion transactron/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Concatenate, Optional, TypeAlias, TypeGuard, TypeVar
from collections.abc import Callable, Iterable, Mapping
from amaranth import *
from coreblocks.utils._typing import LayoutLike
from coreblocks.utils._typing import LayoutLike, ShapeLike
from coreblocks.utils import OneHotSwitchDynamic

__all__ = [
Expand Down Expand Up @@ -164,3 +164,64 @@ def get_caller_class_name(default: Optional[str] = None) -> tuple[Optional[Elabo
return None, default
else:
raise RuntimeError("Not called from a method")


def data_layout(val: ShapeLike) -> LayoutLike:
return [("data", val)]


def neg(x: int, xlen: int) -> int:
"""
Computes the negation of a number in the U2 system.
Parameters
----------
x: int
Number in U2 system.
xlen : int
Bit width of x.
Returns
-------
return : int
Negation of x in the U2 system.
"""
return (-x) & (2**xlen - 1)


def int_to_signed(x: int, xlen: int) -> int:
"""
Converts a Python integer into its U2 representation.
Parameters
----------
x: int
Signed Python integer.
xlen : int
Bit width of x.
Returns
-------
return : int
Representation of x in the U2 system.
"""
return x & (2**xlen - 1)


def signed_to_int(x: int, xlen: int) -> int:
"""
Changes U2 representation into Python integer
Parameters
----------
x: int
Number in U2 system.
xlen : int
Bit width of x.
Returns
-------
return : int
Representation of x as signed Python integer.
"""
return x | -(x & (2 ** (xlen - 1)))

0 comments on commit 1015e15

Please sign in to comment.