"""HUGR integer types and operations."""from__future__importannotationsfromdataclassesimportdataclass,fieldfromtypingimportTYPE_CHECKING,ClassVarfromtyping_extensionsimportSelfimporthugr.modelasmodelfromhugrimportext,tys,valfromhugr.opsimportAsExtOp,DataflowOp,ExtOp,RegisteredOpfromhugr.stdimport_load_extensionifTYPE_CHECKING:fromhugr.opsimportCommand,ComWireCONVERSIONS_EXTENSION=_load_extension("arithmetic.conversions")INT_TYPES_EXTENSION=_load_extension("arithmetic.int.types")_INT_PARAM=tys.BoundedNatParam(7)INT_T_DEF=INT_TYPES_EXTENSION.types["int"]
[docs]defint_t(width:int)->tys.ExtType:"""Create an integer type with a fixed log bit width. Args: width: The log bit width of the integer. Returns: The integer type. Examples: >>> int_t(5).type_def.name # 32 bit integer 'int' """returnINT_T_DEF.instantiate([tys.BoundedNatArg(n=width)],)
INT_OPS_EXTENSION=_load_extension("arithmetic.int")@dataclass(frozen=True)class_DivModDef(RegisteredOp):"""DivMod operation, has two inputs and two outputs."""width:int=5const_op_def:ClassVar[ext.OpDef]=INT_OPS_EXTENSION.operations["idivmod_u"]deftype_args(self)->list[tys.TypeArg]:return[tys.BoundedNatArg(n=self.width)]defcached_signature(self)->tys.FunctionType|None:row:list[tys.Type]=[int_t(self.width)]*2returntys.FunctionType.endo(row)@classmethoddeffrom_ext(cls,custom:ExtOp)->Self|None:ifcustom.op_def()!=cls.op_def():returnNonematchcustom.args:case[tys.BoundedNatArg(n=a1)]:returncls(width=a1)case_:msg=f"Invalid args: {custom.args}"raiseAsExtOp.InvalidExtOp(msg)def__call__(self,a:ComWire,b:ComWire)->Command:returnDataflowOp.__call__(self,a,b)#: DivMod operation.DivMod=_DivModDef()