Пример #1
0
def test_factorial():
    def fact(n):
        result = 1
        while n > 1:
            result *= n
            n -= 1
        return result
    rundemo(fact, 10)
Пример #2
0
def test_factorial():
    def fact(n):
        result = 1
        while n > 1:
            result *= n
            n -= 1
        return result

    rundemo(fact, 10)
Пример #3
0
def test_pseudofactorial():
    def pseudofact(n):
        result = 1
        while n > 1:
            if n & 1:
                result *= n
            n -= 1
        return result
    rundemo(pseudofact, 10)
Пример #4
0
def test_pseudofactorial():
    def pseudofact(n):
        result = 1
        while n > 1:
            if n & 1:
                result *= n
            n -= 1
        return result

    rundemo(pseudofact, 10)
Пример #5
0
def test_f1():
    def f1(n):
        "Arbitrary test function."
        i = 0
        x = 1
        while i<n:
            j = 0
            while j<=i:
                j = j + 1
                x = x + (i&j)
            i = i + 1
        return x
    #rundemo(f1, 2117)
    rundemo(f1, 217)
Пример #6
0
def test_f1():
    def f1(n):
        "Arbitrary test function."
        i = 0
        x = 1
        while i < n:
            j = 0
            while j <= i:
                j = j + 1
                x = x + (i & j)
            i = i + 1
        return x

    #rundemo(f1, 2117)
    rundemo(f1, 217)
Пример #7
0
def accept(lines):
    g = open('zgen.py', 'w')
    print >> g, '''from pypy.rlib.rarithmetic import intmask

def dummyfn(counter, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z):
  goto = 0
  while True:
    '''

    for line in lines:
        print >> g, line

    print >> g, '''
  return intmask(a*-468864544+b*-340864157+c*-212863774+d*-84863387+e*43136996+f*171137383+g*299137766+h*427138153+i*555138536+j*683138923+k*811139306+l*939139693+m*1067140076+n*1195140463+o*1323140846+p*1451141233+q*1579141616+r*1707142003+s*1835142386+t*1963142773+u*2091143156+v*-2075823753+w*-1947823370+x*-1819822983+y*-1691822600+z*-1563822213)

'''
    g.close()

    #ok = os.system("py.test zgen.py --seed=6661 -s") == 0

    from pypy.jit.codegen.demo import conftest as demo_conftest
    demo_conftest.option.randomseed = SEED
    demo_conftest.option.backend = BACKEND
    from pypy.jit.codegen.demo.support import rundemo

    d = {}
    execfile('zgen.py', d)
    dummyfn = d['dummyfn']

    childpid = os.fork()
    if childpid == 0:  # in child
        rundemo(dummyfn, ITERATIONS, *ARGS)
        os._exit(0)

    _, status = os.waitpid(childpid, 0)
    ok = status == 0

    if ok:
        return True  # accept
    else:
        os.system("cp -f zgen.py zsample.py")
        global progress
        progress = True
        globals()['lines'][:] = lines
        return False
Пример #8
0
def accept(lines):
    g = open('zgen.py', 'w')
    print >> g, '''from pypy.rlib.rarithmetic import intmask

def dummyfn(counter, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z):
  goto = 0
  while True:
    '''

    for line in lines:
        print >> g, line

    print >> g, '''
  return intmask(a*-468864544+b*-340864157+c*-212863774+d*-84863387+e*43136996+f*171137383+g*299137766+h*427138153+i*555138536+j*683138923+k*811139306+l*939139693+m*1067140076+n*1195140463+o*1323140846+p*1451141233+q*1579141616+r*1707142003+s*1835142386+t*1963142773+u*2091143156+v*-2075823753+w*-1947823370+x*-1819822983+y*-1691822600+z*-1563822213)

'''
    g.close()

    #ok = os.system("py.test zgen.py --seed=6661 -s") == 0

    from pypy.jit.codegen.demo import conftest as demo_conftest
    demo_conftest.option.randomseed = SEED
    demo_conftest.option.backend = BACKEND
    from pypy.jit.codegen.demo.support import rundemo

    d = {}
    execfile('zgen.py', d)
    dummyfn = d['dummyfn']

    childpid = os.fork()
    if childpid == 0:     # in child
        rundemo(dummyfn, ITERATIONS, *ARGS)
        os._exit(0)

    _, status = os.waitpid(childpid, 0)
    ok = status == 0

    if ok:
        return True     # accept
    else:
        os.system("cp -f zgen.py zsample.py")
        global progress
        progress = True
        globals()['lines'][:] = lines
        return False
Пример #9
0
def test_random_function(nb_blocks=demo_conftest.option.nb_blocks,
                         max_block_length=demo_conftest.option.max_block_length):
    #py.test.skip("in-progress")
    blocklabels = range(nb_blocks)
    r = Random()
    vars = list("abcdefghijklmnopqrstuvwxyz"[:demo_conftest.option.n_vars])
    varlist = ', '.join(vars)
    magicsum = '+'.join(['%s*%d' % (v, hash(v)) for v in vars])
    operations = ['%s + %s',
                  '%s + %s',
                  '%s - %s',
                  '%s - %s',
                  '%s * %s',
                  '%s & %s',
                  '%s | %s',
                  '%s ^ %s',
                  '%s << (%s & 0x0000067f)',
                  '%s >> (%s & 0x1234567f)',
                  'abs(%s)',
                  '-%s',
                  '~%s',
                  '%s // ((%s & 0xfffff) + 1)',
                  '%s // (-((%s & 0xfffff) + 2))',
                  '%s %% ((%s & 0xfffff) + 1)',
                  '%s %% (-((%s & 0xfffff) + 2))',
                  '!%s or %s',
                  '!%s and %s',
                  '!not %s',
                  '!bool(%s)',
                  '!%s <  %s',
                  '!%s <= %s',
                  '!%s == %s',
                  '!%s != %s',
                  '!%s >  %s',
                  '!%s >= %s',
                  ]
    lines = ["def dummyfn(counter, %(varlist)s):" % locals(),
             "  goto = 0",
             "  while True:",
             ]
    for blocklabel in blocklabels:
        lines.append("    if goto == %d:" % blocklabel)
        for j in range(r.randrange(0, max_block_length)):
            v1 = r.choice(vars)
            constbytes = r.randrange(-15, 5)
            if constbytes <= 0:
                v2 = r.choice(vars)
                op = r.choice(operations)
                if op.count('%s') == 1:
                    op = op % (v2,)
                else:
                    v3 = r.choice(vars)
                    op = op % (v2, v3)
                if op.startswith('!'):
                    op = op[1:]
                else:
                    op = 'intmask(%s)' % op
                lines.append("      %s = %s" % (v1, op))
            else:
                constant = r.randrange(-128, 128)
                for i in range(1, constbytes):
                    constant = constant << 8 | r.randrange(0, 256)
                lines.append("      %s = %d" % (v1, constant))
        v1 = r.choice(vars)
        for line in ["      if %s:" % v1,
                     "      else:"]:
            lines.append(line)
            j = r.choice(blocklabels)
            if j <= blocklabel:
                lines.append("        counter -= 1")
                lines.append("        if not counter: break")
            lines.append("        goto = %d" % j)
    lines.append("  return intmask(%(magicsum)s)" % locals())

    args = [r.randrange(-99, 100) for v1 in vars]

    src = py.code.Source('\n'.join(lines))
    print src
    udir.join('generated.py').write(
        'from pypy.rlib.rarithmetic import intmask\n\n'
        '%s\n\n'
        'args=%r\n'
        'print dummyfn(10000, *args)\n' % (src, args))
    exec src.compile()

    if demo_conftest.option.iterations != 0:
        iterations = demo_conftest.option.iterations
    else:
        if demo_conftest.option.backend in demo_conftest.very_slow_backends:
            iterations = 50
        else:
            iterations = 10000

    rundemo(dummyfn, iterations, *args)
Пример #10
0
def test_random_function(
        nb_blocks=demo_conftest.option.nb_blocks,
        max_block_length=demo_conftest.option.max_block_length):
    #py.test.skip("in-progress")
    blocklabels = range(nb_blocks)
    r = Random()
    vars = list("abcdefghijklmnopqrstuvwxyz"[:demo_conftest.option.n_vars])
    varlist = ', '.join(vars)
    magicsum = '+'.join(['%s*%d' % (v, hash(v)) for v in vars])
    operations = [
        '%s + %s',
        '%s + %s',
        '%s - %s',
        '%s - %s',
        '%s * %s',
        '%s & %s',
        '%s | %s',
        '%s ^ %s',
        '%s << (%s & 0x0000067f)',
        '%s >> (%s & 0x1234567f)',
        'abs(%s)',
        '-%s',
        '~%s',
        '%s // ((%s & 0xfffff) + 1)',
        '%s // (-((%s & 0xfffff) + 2))',
        '%s %% ((%s & 0xfffff) + 1)',
        '%s %% (-((%s & 0xfffff) + 2))',
        '!%s or %s',
        '!%s and %s',
        '!not %s',
        '!bool(%s)',
        '!%s <  %s',
        '!%s <= %s',
        '!%s == %s',
        '!%s != %s',
        '!%s >  %s',
        '!%s >= %s',
    ]
    lines = [
        "def dummyfn(counter, %(varlist)s):" % locals(),
        "  goto = 0",
        "  while True:",
    ]
    for blocklabel in blocklabels:
        lines.append("    if goto == %d:" % blocklabel)
        for j in range(r.randrange(0, max_block_length)):
            v1 = r.choice(vars)
            constbytes = r.randrange(-15, 5)
            if constbytes <= 0:
                v2 = r.choice(vars)
                op = r.choice(operations)
                if op.count('%s') == 1:
                    op = op % (v2, )
                else:
                    v3 = r.choice(vars)
                    op = op % (v2, v3)
                if op.startswith('!'):
                    op = op[1:]
                else:
                    op = 'intmask(%s)' % op
                lines.append("      %s = %s" % (v1, op))
            else:
                constant = r.randrange(-128, 128)
                for i in range(1, constbytes):
                    constant = constant << 8 | r.randrange(0, 256)
                lines.append("      %s = %d" % (v1, constant))
        v1 = r.choice(vars)
        for line in ["      if %s:" % v1, "      else:"]:
            lines.append(line)
            j = r.choice(blocklabels)
            if j <= blocklabel:
                lines.append("        counter -= 1")
                lines.append("        if not counter: break")
            lines.append("        goto = %d" % j)
    lines.append("  return intmask(%(magicsum)s)" % locals())

    args = [r.randrange(-99, 100) for v1 in vars]

    src = py.code.Source('\n'.join(lines))
    print src
    udir.join('generated.py').write(
        'from pypy.rlib.rarithmetic import intmask\n\n'
        '%s\n\n'
        'args=%r\n'
        'print dummyfn(10000, *args)\n' % (src, args))
    exec src.compile()

    if demo_conftest.option.iterations != 0:
        iterations = demo_conftest.option.iterations
    else:
        if demo_conftest.option.backend in demo_conftest.very_slow_backends:
            iterations = 50
        else:
            iterations = 10000

    rundemo(dummyfn, iterations, *args)