annotate python/ppci/bitfun.py @ 377:9667d78ba79e

Switched to xml for project description
author Windel Bouwman
date Fri, 11 Apr 2014 15:47:50 +0200
parents 98ff43cfdd36
children
rev   line source
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
1
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
2
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
3 def rotate_right(v, n):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
4 """ bit-wise Rotate right n times """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
5 mask = (2**n) - 1
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
6 mask_bits = v & mask
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
7 return (v >> n) | (mask_bits << (32 - n))
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
8
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
9 def rotate_left(v, n):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
10 assert n >= 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
11 assert n < 32
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
12 return rotate_right(v, 32 - n)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
13
365
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
14 def encode_imm32(v):
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
15 """ Bundle 32 bit value into 4 bits rotation and 8 bits value
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
16 """
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
17 for i in range(0, 16):
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
18 v2 = rotate_left(v, i*2)
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
19 if (v2 & 0xFFFFFF00) == 0:
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
20 rotation = i
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
21 val = v2 & 0xFF
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
22 x = (rotation << 8) | val
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
23 return x
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 346
diff changeset
24 raise Exception("Invalid value {}".format(v))
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents:
diff changeset
25