*/t/*.py: Fix the various testing scripts for Python 3 compatibility.
[mLib] / utils / t / bits-testgen.py
CommitLineData
7cf5c72a
MW
1#! /usr/bin/python
2### -*-python-*-
3###
4### Generate input script and expected output for bit manipulation,
5### particularly 64-bit arithmetic.
6
7import sys as SYS
8import random as R
9
37342632
MW
10if SYS.version_info >= (3,): xrange = range
11
7cf5c72a
MW
12NVEC = 64
13WD = 64
14LIMIT = 1 << WD
15MASK = LIMIT - 1
16
e243edbc 17ARGS = SYS.argv[1:]; ARGS.reverse()
7cf5c72a 18def arg(default = None):
e243edbc
MW
19 if len(ARGS): return ARGS.pop()
20 else: return default
7cf5c72a
MW
21
22R.seed(None)
29922fc0
MW
23seed = arg()
24if seed is None: SEED = R.randrange(0, 1 << 32)
25else: SEED = int(seed, 0)
7cf5c72a
MW
26R.seed(SEED)
27
37342632
MW
28print('### Test vectors for 64-bit arithmetic macros')
29print('### [generated; seed = 0x%08x]' % SEED)
7cf5c72a
MW
30
31def rol(x, n): return ((x << n) | (x >> (WD - n))) & MASK
32def ror(x, n): return ((x >> n) | (x << (WD - n))) & MASK
37342632 33def put(x): return '%0*x' % (WD//4, x)
7cf5c72a
MW
34
35for name, func in [('lsl', lambda x, n: x << n),
36 ('lsr', lambda x, n: x >> n),
37 ('rol', rol),
38 ('ror', ror)]:
37342632 39 print('\n%s64 {' % name)
7cf5c72a
MW
40 for i in xrange(NVEC):
41 x = R.randrange(LIMIT)
42 sh = R.randrange(0, 70) & 63
37342632 43 print(' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK)))
7cf5c72a
MW
44 for i in xrange(4):
45 x = R.randrange(LIMIT)
46 sh = 32
37342632
MW
47 print(' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK)))
48 print('}')
7cf5c72a
MW
49
50for name, func in [('add', lambda x, y: x + y),
51 ('sub', lambda x, y: x - y)]:
37342632 52 print('\n%s64 {' % name)
7cf5c72a
MW
53 for i in xrange(NVEC):
54 x = R.randrange(LIMIT)
55 y = R.randrange(LIMIT)
37342632
MW
56 print(' %s %s %s;' % (put(x), put(y), put(func(x, y) & MASK)))
57 print('}')