-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_speed_limit_entries.py
64 lines (47 loc) · 1.77 KB
/
gen_speed_limit_entries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import math
import numpy as np
INT_MAX = 2147483647
def double_to_dec(db):
"""Convert a float to its decimal representation used by the P4 switch and the forkproxy. """
# return (long) ( Integer.MAX_VALUE/(16*4*Math.PI) * d);
return int(INT_MAX/(16*4*math.pi) * db)
def int_to_binstr(n):
"""Convert a 64-bit integer to a binary literal form used by the P4 language."""
return "0b"+np.binary_repr(n,width=64)
def get_lpm_p(d):
"""Get lpm limit rules for the given positive limit."""
bs = int_to_binstr(double_to_dec(d))[2:]
ps = []
prefix = "0"
for i in range(1,len(bs)):
if bs[i]=="0":
ps.append(prefix+"1")
prefix = prefix + bs[i]
return [ ( p+(64-len(p))*'0' , len(p) ) for p in ps ]
def get_lpm_n(d):
"""Get lpm limit rules for the given negative limit."""
bs = int_to_binstr(double_to_dec(d))[2:]
ps = []
prefix = "1"
for i in range(1,len(bs)):
if bs[i]=="1":
ps.append(prefix+"0")
prefix = prefix + bs[i]
return [ ( p+(64-len(p))*'0' , len(p) ) for p in ps ]
def get_lpm(d):
"""Get the lpm limit rules."""
if d>=0:
return get_lpm_p(d)
else:
return get_lpm_n(d)
def print_lpm_p4(joint_id,d):
"""Print the lpm rules of the given limit according to the P4 syntax."""
for bits, mask in get_lpm(d):
print("(",joint_id,",",'0b'+bits,"&&&", hex(int('1'*mask+'0'*(64-mask),2)),") :","override_speed(",int_to_binstr(double_to_dec(d)),");")
print("// AUTOGENERATED FILE. Do not edit manually!\n")
JOINT_SPEED_LIMITS = [3.5,3.5,3.5,3.5,3.5,3.5]
for i in range(6):
print("// JOINT",i+1,"SPEED LIMIT:",JOINT_SPEED_LIMITS[i])
print_lpm_p4(i,JOINT_SPEED_LIMITS[i])
print_lpm_p4(i,-1*JOINT_SPEED_LIMITS[i])
print()