aboutsummaryrefslogtreecommitdiff
path: root/semestr-3/anm/cordic/csin.cpp
blob: bcc9af4bc6a8482b78902f22bdec500dc1e53aae (plain)
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
#include "CORDICtable.c"
#include <iostream>
// angle is radians multiplied by CORDIC multiplication factor M
// modulus can be set to CORDIC inverse gain 1/F to avoid post-division
void CORDICsincos(int a, int m, int *s, int *c)
{
  int k, tx, x = m, y = 0, z = a, fl = 0;
  if (z > +CORDIC_HALFPI)
  {
    fl = +1;
    z = (+CORDIC_PI) - z;
  }
  else if (z < -CORDIC_HALFPI)
  {
    fl = +1;
    z = (-CORDIC_PI) - z;
  }
  for (k = 0; k < CORDIC_MAXITER; k++)
  {
    std::cout << x << " " << y << " " << z << "\n";
    tx = x;
    if (z >= 0)
    {
      x -= (y >> k);
      y += (tx >> k);
      z -= CORDIC_ZTBL[k];
    }
    else
    {
      x += (y >> k);
      y -= (tx >> k);
      z += CORDIC_ZTBL[k];
    }
  }
  if (fl)
    x = -x;
  *c = x; // m*cos(a) multiplied by gain F and factor M
  *s = y; // m*sin(a) multiplied by gain F and factor M
}

int main()
{
  double x;
  std::cin >> x;
  int sinus, cosinus;
  CORDICsincos(x * CORDIC_MUL, CORDIC_1F, &sinus, &cosinus);
  std::cout << sinus << " " << cosinus << "\n";
  std::cout << (double)sinus / (double)CORDIC_MUL << " " << (double)cosinus / (double)CORDIC_MUL << "\n";
}