[libre-riscv-dev] TLB

Luke Kenneth Casson Leighton lkcl at lkcl.net
Mon Apr 22 14:02:21 BST 2019


hiya jacob, spotted another code-size reduction:

max_exponent = 0
replace with
self.max_exponent = 0

then
                if exponent > max_exponent:
                    max_exponent = exponent
replace with
            self.max_exponent = max(e, self.max_exponent)

remove the function (reduces indentation), remove declaration "nonlocal"...

    def __init__(self, exponents=()):
        self.max_exponent = 0
        elements = {0} # 0 is always required
        for e in exponents:
            if not isinstance(e, int):
                raise TypeError("exponent %s must be an integer" % repr(e))
            if e < 0:
                raise ValueError("exponent %d must not be negative" % e)
            self.max_exponent = max(e, self.max_exponent)
            elements.add(e) # uniquifies additions (it's a set!)
        set.__init__(self, elements)

also, it suddenly occurred to me: the elements are... a set!  so... no
need to do the test for zero.

actually... that in turn means that this can be done:

    def __init__(self, exponents=None):
        self.max_exponent = 0
        exponents = exponents or {0} # default is zero set
        assert (0 not in exponents), "0 must be in the exponents"
        for e in exponents:
            if not isinstance(e, int):
                raise TypeError("exponent %s must be an integer" % repr(e))
            if e < 0:
                raise ValueError("exponent %d must not be negative" % e)
            self.max_exponent = max(e, self.max_exponent)
        set.__init__(self, set(exponents))

and, further:

    def __init__(self, exponents=None):
        exponents = exponents or {0} # default is zero set
       assert (0 not in exponents), "0 must be in the exponents"
        for e in exponents:
            if not isinstance(e, int):
                raise TypeError("exponent %s must be an integer" % repr(e))
            if e < 0:
                raise ValueError("exponent %d must not be negative" % e)
       self.max_exponent = max(exponents)
       set.__init__(self, set(exponents))

ok so... actually, that didn't work, so went with this:

    def __init__(self, exponents=()):
        exponents = set(exponents)
        exponents.add(0) # must contain zero
        for e in exponents:
            assert isinstance(e, int), TypeError("%s must be an int" % repr(e))
            assert (e >= 0), ValueError("%d must not be negative" % e)
        self.max_exponent = max(exponents)
        set.__init__(self, exponents)

so:

* make a set out of the incoming parameter
* add 0 to it (just to make sure)
* validate the set members
* set the max...

actually... *now* we have an excuse to have max_exponent as a property:

class LFSRPolynomial:
    def __init__(self, exponents=()):
        exponents = set(exponents)
        exponents.add(0) # must contain zero
        for e in exponents:
            assert isinstance(e, int), TypeError("%s must be an int" % repr(e))
            assert (e >= 0), ValueError("%d must not be negative" % e)
        set.__init__(self, exponents)

    @property
    def max_exponent(self):
        return max(self)

so, getting there.

l.



More information about the libre-riscv-dev mailing list