add comment about vector length / dot-product
[libreriscv.git] / simple_v_extension / vector_ops / discussion.mdwn
1 # CORDIC Implementations
2
3 From <https://github.com/suyashmahar/cordic-algorithm-python>
4
5 circular = 1
6 linear = 0
7 hyperbolic = -1
8
9 def ROM_lookup(iteration, coord_mode):
10 if (coord_mode == circular):
11 return math.degrees(math.atan(2**(-1*iteration)))
12 elif (coord_mode == linear):
13 return 2**(-1*iteration)
14 elif (coord_mode == hyperbolic):
15 return (math.atanh(2**(-1*iteration)))
16
17 def rotation_mode(x, y, z, coord_mode, iterations):
18 a = 0.607252935; # = 1/K
19
20 x_val_list = []
21 y_val_list = []
22 z_val_list = []
23 iterations_list = []
24
25 i = 0; # Keeps count on number of iterations
26
27 current_x = x # Value of X on ith iteration
28 current_y = y # Value of Y on ith iteration
29 current_z = z # Value of Z on ith iteration
30
31 di = 0
32
33 if (coord_mode == hyperbolic):
34 i = 1
35 else:
36 i = 0
37
38 flag = 0
39
40 if (iterations > 0):
41 while (i < iterations):
42 if (current_z < 0):
43 di = -1
44 else:
45 di = +1
46 next_z = current_z - di * ROM_lookup(i, coord_mode)
47 next_x = current_x - coord_mode * di * current_y * (2**(-1*i))
48 next_y = current_y + di * current_x * 2**(-1*i)
49
50 current_x = next_x
51 current_y = next_y
52 current_z = next_z
53
54 x_val_list.append(current_x)
55 y_val_list.append(current_y)
56 z_val_list.append(current_z)
57
58 iterations_list.append(i)
59
60 if (coord_mode == hyperbolic):
61 if ((i != 4) & (i != 13) & (i!=40)):
62 i = i+1
63 elif (flag == 0):
64 flag = 1
65 elif (flag == 1):
66 flag = 0
67 i = i+1
68 else:
69 i = i+1
70 return { 'x':x_val_list, 'y':y_val_list, 'z':z_val_list,
71 'iteration':iterations_list, }
72
73 def vector_mode(x, y, z, coord_mode, iterations):
74 a = 1.2075; # = 1/K
75
76 x_val_list = []
77 y_val_list = []
78 z_val_list = []
79 iterations_list = []
80
81 i = 0; # Keeps count on number of iterations
82
83 current_x = x # Value of X on ith iteration
84 current_y = y # Value of Y on ith iteration
85 current_z = z # Value of Z on ith iteration
86
87 di = 0
88
89 # This is neccesary since result for i=0 doesn't exists for hyperbolic
90 # co-ordinate system.
91 if (coord_mode == hyperbolic):
92 i = 1
93 else:
94 i = 0
95
96 flag = 0
97
98 if (iterations > 0):
99 while (i < iterations):
100 di = -1*math.copysign(1, current_y);#*current_x);
101 next_x = current_x - coord_mode * di * current_y * (2**(-1*i))
102 next_y = current_y + di * current_x * 2**(-1*i)
103 next_z = current_z - di * ROM_lookup(i, coord_mode)
104
105 current_x = next_x
106 current_y = next_y
107 current_z = next_z
108
109 x_val_list.append(current_x)
110 y_val_list.append(current_y)
111 z_val_list.append(current_z)
112
113 iterations_list.append(i)
114
115 if (coord_mode == hyperbolic):
116 if ((i != 4) & (i != 13) & (i!=40)):
117 i = i+1
118 elif (flag == 0):
119 flag = 1
120 elif (flag == 1):
121 flag = 0
122 i = i+1
123 else:
124 i = i+1
125 return { 'x':x_val_list, 'y':y_val_list, 'z':z_val_list,
126 'iteration':iterations_list }
127
128 # Vector Length
129
130 > With VLENGTH being also expressible as dotproduct followed by scalar
131 > sqrt, is it reasonable to have both normalisation as well as VLENGTH
132 > as macro op fused sequences?
133
134 Vector length would presumably involve dotting a vector with itself.
135 The potential advantage I see is that the dot product might be tempted
136 to read that vector twice; whereas the length would only read it once.
137 If some other mechanism eliminates the duplicate read, they would be
138 pretty well equivalent.
139