1 # CORDIC Implementations
3 From <https://github.com/suyashmahar/cordic-algorithm-python>
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)))
17 def rotation_mode(x, y, z, coord_mode, iterations):
18 a = 0.607252935; # = 1/K
25 i = 0; # Keeps count on number of iterations
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
33 if (coord_mode == hyperbolic):
41 while (i < iterations):
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)
54 x_val_list.append(current_x)
55 y_val_list.append(current_y)
56 z_val_list.append(current_z)
58 iterations_list.append(i)
60 if (coord_mode == hyperbolic):
61 if ((i != 4) & (i != 13) & (i!=40)):
70 return { 'x':x_val_list, 'y':y_val_list, 'z':z_val_list,
71 'iteration':iterations_list, }
73 def vector_mode(x, y, z, coord_mode, iterations):
81 i = 0; # Keeps count on number of iterations
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
89 # This is neccesary since result for i=0 doesn't exists for hyperbolic
91 if (coord_mode == hyperbolic):
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)
109 x_val_list.append(current_x)
110 y_val_list.append(current_y)
111 z_val_list.append(current_z)
113 iterations_list.append(i)
115 if (coord_mode == hyperbolic):
116 if ((i != 4) & (i != 13) & (i!=40)):
125 return { 'x':x_val_list, 'y':y_val_list, 'z':z_val_list,
126 'iteration':iterations_list }
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?
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.