Пример #1
0
 def load_other(self, other):
     if isinstance(other, cint):
         assert(self.n == other.size)
         self.conv_regint_by_bit(self.n, self, other.to_regint(1))
     elif isinstance(other, int):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         assert self.unit == 64
         n_units = int(math.ceil(self.n / self.unit))
         n_convs = min(other.size, n_units)
         for i in range(n_convs):
             x = self[i]
             y = other[i]
             self.conv_regint(min(self.unit, self.n - i * self.unit), x, y)
         for i in range(n_convs, n_units):
             inst.ldbits(self[i], min(self.unit, self.n - i * self.unit), 0)
     elif (isinstance(self, type(other)) or isinstance(other, type(self))) \
          and self.n == other.n:
         for i in range(math.ceil(self.n / self.unit)):
             self.mov(self[i], other[i])
     elif isinstance(other, sint) and isinstance(self, sbits):
         self.mov(self, sbitvec(other, self.n).elements()[0])
     else:
         try:
             bits = other.bit_decompose()
             bits = bits[:self.n] + [sbit(0)] * (self.n - len(bits))
             other = self.bit_compose(bits)
             assert(isinstance(other, type(self)))
             assert(other.n == self.n)
             self.load_other(other)
         except:
             raise CompilerError('cannot convert %s/%s from %s to %s' % \
                                 (str(other), repr(other), type(other), type(self)))
Пример #2
0
 def xor_int(self, other):
     if other == 0:
         return self
     self_bits = self.bit_decompose()
     other_bits = util.bit_decompose(other, max(self.n, util.int_len(other)))
     extra_bits = [self.new(b, n=1) for b in other_bits[self.n:]]
     return self.bit_compose([~x if y else x \
                              for x,y in zip(self_bits, other_bits)] \
                             + extra_bits)
Пример #3
0
 def __mul__(self, other):
     if isinstance(other, cbits):
         return NotImplemented
     else:
         try:
             res = cbits(n=min(self.max_length, self.n+util.int_len(other)))
             inst.mulcbi(res, self, other)
             return res
         except TypeError:
             return NotImplemented
Пример #4
0
 def xor_int(self, other):
     if other == 0:
         return self
     self_bits = self.bit_decompose()
     other_bits = util.bit_decompose(other, max(self.n,
                                                util.int_len(other)))
     extra_bits = [self.new(b, n=1) for b in other_bits[self.n:]]
     return self.bit_compose([~x if y else x \
                              for x,y in zip(self_bits, other_bits)] \
                             + extra_bits)
Пример #5
0
 def __mul__(self, other):
     if isinstance(other, cbits):
         return NotImplemented
     else:
         try:
             res = cbits(n=min(self.max_length, self.n+util.int_len(other)))
             inst.mulci(res, self, other)
             return res
         except TypeError:
             return NotImplemented
Пример #6
0
 def load_other(self, other):
     if isinstance(other, (int, long)):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         self.conv_regint(self.n, self, other)
     elif isinstance(self, type(other)) or isinstance(other, type(self)):
         self.mov(self, other)
     else:
         raise CompilerError('cannot convert from %s to %s' % \
                             (type(other), type(self)))
Пример #7
0
 def mul_int(self, other):
     if other == 0:
         return 0
     elif other == 1:
         return self
     elif self.n == 1:
         bits = util.bit_decompose(other, util.int_len(other))
         zero = sbit(0)
         mul_bits = [self if b else zero for b in bits]
         return self.bit_compose(mul_bits)
     else:
         print self.n, other
         return NotImplemented
Пример #8
0
 def mul_int(self, other):
     if other == 0:
         return 0
     elif other == 1:
         return self
     elif self.n == 1:
         bits = util.bit_decompose(other, util.int_len(other))
         zero = sbit(0)
         mul_bits = [self if b else zero for b in bits]
         return self.bit_compose(mul_bits)
     else:
         print self.n, other
         return NotImplemented
Пример #9
0
 def load_other(self, other):
     if isinstance(other, int):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         self.conv_regint(self.n, self, other)
     elif isinstance(self, type(other)) or isinstance(other, type(self)):
         self.mov(self, other)
     else:
         try:
             other = self.bit_compose(other.bit_decompose())
             self.mov(self, other)
         except:
             raise CompilerError('cannot convert from %s to %s' % \
                                 (type(other), type(self)))
Пример #10
0
 def load_other(self, other):
     if isinstance(other, (int, long)):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         self.conv_regint(self.n, self, other)
     elif isinstance(self, type(other)) or isinstance(other, type(self)):
         self.mov(self, other)
     else:
         try:
             other = self.bit_compose(other.bit_decompose())
             self.mov(self, other)
         except:
             raise CompilerError('cannot convert from %s to %s' % \
                                 (type(other), type(self)))
Пример #11
0
 def load_other(self, other):
     if isinstance(other, cint):
         size = other.size
         other = sum(x << i for i, x in enumerate(other))
         other = other.to_regint(size)
     if isinstance(other, int):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         assert(other.size == 1)
         self.conv_regint(self.n, self, other)
     elif isinstance(self, type(other)) or isinstance(other, type(self)):
         self.mov(self, other)
     else:
         try:
             other = self.bit_compose(other.bit_decompose())
             self.mov(self, other)
         except:
             raise CompilerError('cannot convert from %s to %s' % \
                                 (type(other), type(self)))
Пример #12
0
 def load_other(self, other):
     if isinstance(other, cint):
         assert(self.n == other.size)
         self.conv_regint_by_bit(self.n, self, other.to_regint(1))
     elif isinstance(other, int):
         self.set_length(self.n or util.int_len(other))
         self.load_int(other)
     elif isinstance(other, regint):
         assert(other.size == math.ceil(self.n / self.unit))
         for i, (x, y) in enumerate(zip(self, other)):
             self.conv_regint(min(self.unit, self.n - i * self.unit), x, y)
     elif isinstance(self, type(other)) or isinstance(other, type(self)):
         assert(self.n == other.n)
         for i in range(math.ceil(self.n / self.unit)):
             self.mov(self[i], other[i])
     else:
         try:
             other = self.bit_compose(other.bit_decompose())
             self.load_other(other)
         except:
             raise CompilerError('cannot convert from %s to %s' % \
                                 (type(other), type(self)))
Пример #13
0
 def load_other(self, other):
     if isinstance(other, (int, long)):
         self.n = util.int_len(other)
         self.load_int(other)
     else:
         self.conv_regint(self, regint.conv(other))