exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
13.96k stars 498 forks source link

Operation between data of different bit lengths #408

Open iSunfield opened 1 year ago

iSunfield commented 1 year ago

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]'
elisbyberi commented 1 year ago

@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
inumanag commented 11 months ago

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)