Skip to content

Commit

Permalink
Merge branch 'packing-32-bit'
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jun 26, 2013
2 parents 2add178 + c608fd0 commit 7d294b3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
13 changes: 13 additions & 0 deletions topaz/objects/bignumobject.py
@@ -1,5 +1,6 @@
from rpython.rlib.rbigint import rbigint from rpython.rlib.rbigint import rbigint
from rpython.rlib.rfloat import INFINITY from rpython.rlib.rfloat import INFINITY
from rpython.rtyper.lltypesystem import lltype, rffi


from topaz.module import ClassDef from topaz.module import ClassDef
from topaz.objects.integerobject import W_IntegerObject from topaz.objects.integerobject import W_IntegerObject
Expand Down Expand Up @@ -37,6 +38,18 @@ def bigint_w(self, space):
def float_w(self, space): def float_w(self, space):
return self.bigint.tofloat() return self.bigint.tofloat()


def intmask_w(self, space):
return rffi.cast(lltype.Signed, self.uintmask_w((space)))

def uintmask_w(self, space):
return self.bigint.uintmask()

def longlongmask_w(self, space):
return rffi.cast(lltype.SignedLongLong, self.ulonglongmask_w((space)))

def ulonglongmask_w(self, space):
return self.bigint.ulonglongmask()

@classdef.method("to_s") @classdef.method("to_s")
def method_to_s(self, space): def method_to_s(self, space):
return space.newstr_fromstr(self.bigint.str()) return space.newstr_fromstr(self.bigint.str())
Expand Down
15 changes: 14 additions & 1 deletion topaz/objects/intobject.py
Expand Up @@ -4,7 +4,8 @@
from rpython.rlib import rfloat from rpython.rlib import rfloat
from rpython.rlib.debug import check_regular_int from rpython.rlib.debug import check_regular_int
from rpython.rlib.objectmodel import specialize from rpython.rlib.objectmodel import specialize
from rpython.rlib.rarithmetic import ovfcheck, LONG_BIT from rpython.rlib.rarithmetic import (r_uint, r_longlong, r_ulonglong,
ovfcheck, LONG_BIT)
from rpython.rlib.rbigint import rbigint from rpython.rlib.rbigint import rbigint
from rpython.rtyper.lltypesystem import lltype, rffi from rpython.rtyper.lltypesystem import lltype, rffi


Expand Down Expand Up @@ -52,6 +53,18 @@ def bigint_w(self, space):
def float_w(self, space): def float_w(self, space):
return float(self.intvalue) return float(self.intvalue)


def intmask_w(self, space):
return self.intvalue

def uintmask_w(self, space):
return r_uint(self.intvalue)

def longlongmask_w(self, space):
return r_longlong(self.intvalue)

def ulonglongmask_w(self, space):
return r_ulonglong(self.intvalue)

def find_instance_var(self, space, name): def find_instance_var(self, space, name):
storage = space.fromcache(FixnumStorage).get_or_create(space, self.intvalue) storage = space.fromcache(FixnumStorage).get_or_create(space, self.intvalue)
return storage.find_instance_var(space, name) return storage.find_instance_var(space, name)
Expand Down
25 changes: 22 additions & 3 deletions topaz/utils/packing/intpacking.py
@@ -1,12 +1,31 @@
from rpython.rtyper.lltypesystem import rffi, lltype


def select_conversion_method(size, signed):
if signed:
if size <= rffi.sizeof(lltype.Signed):
return "intmask_w"
else:
return "longlongmask_w"
else:
if size < rffi.sizeof(lltype.Signed):
return "intmask_w"
elif size == rffi.sizeof(lltype.Signed):
return "uintmask_w"
else:
return "ulonglongmask_w"


def make_int_packer(size, signed, bigendian): def make_int_packer(size, signed, bigendian):
conversion_method = select_conversion_method(size, signed)

def pack_int(space, packer, repetitions): def pack_int(space, packer, repetitions):
if repetitions > len(packer.args_w) - packer.args_index: if repetitions > len(packer.args_w) - packer.args_index:
raise space.error(space.w_ArgumentError, "too few arguments") raise space.error(space.w_ArgumentError, "too few arguments")


for i in xrange(packer.args_index, repetitions + packer.args_index): for i in xrange(packer.args_index, repetitions + packer.args_index):
num = space.int_w( w_num = space.convert_type(packer.args_w[i], space.w_integer, "to_int")
space.convert_type(packer.args_w[i], space.w_integer, "to_int") num = getattr(w_num, conversion_method)(space)
)
if bigendian: if bigendian:
for i in xrange(size - 1, -1, -1): for i in xrange(size - 1, -1, -1):
x = (num >> (8 * i)) & 0xff x = (num >> (8 * i)) & 0xff
Expand Down

0 comments on commit 7d294b3

Please sign in to comment.