codon icon indicating copy to clipboard operation
codon copied to clipboard

Operation between data of different bit lengths

Open iSunfield opened this issue 2 years ago • 2 comments

I've tried performing a multiplication operation with integers (Int[N]) of different bit lengths and encountered an error. It seems that operations between different bit lengths are not currently supported for Int[N]. Is it possible to create a new class that supports operations between differing bit lengths? If so, how should I specify the bit length of each argument in the definition of the operation function?

a = Int[12](20)
b = Int[8](15)
c = a * b
print(c)

 error: unsupported operand type(s) for *: 'Int[12]' and 'Int[8]'

iSunfield avatar Jun 13 '23 06:06 iSunfield

@iSunfield You have to create a new Int[12].

a = Int[12](20)
b = Int[8](15)
c = Int[12](int(a) * int(b))
print(c)  # 300

elisbyberi avatar Jun 13 '23 20:06 elisbyberi

Currently we do not have a overload for arbitrary width multiplications. Until we add that (planned), you can look at intn.codon and maybe roll your own. For example:

@extend Int:
  def __add__(self: Int[N], b: Int[X], X: Static[int]):
     Z = (N if N > X else X) + 1
     return Int[Z](self) + Int[Z](b)

inumanag avatar Jul 26 '23 13:07 inumanag