1 | /* |
---|
2 | Operace v pevne radove carce |
---|
3 | |
---|
4 | 9.10.2005 |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | #include "fixed.h" |
---|
9 | |
---|
10 | long prevod(double x, unsigned int posun); |
---|
11 | // prevod "x" do systemu "Qposun" |
---|
12 | double zprevod(long x, unsigned int posun); |
---|
13 | // prevod "x" ze systemu "Qposun" do DOUBLE |
---|
14 | long nasob(int x, int y,unsigned int posun); |
---|
15 | // (x*y)>>posun |
---|
16 | int deleni15(int x, int y); |
---|
17 | // (x*Q15/y) se saturaci |
---|
18 | int deleni(int x, int y, int posun1, int posun2, int scale, int posun_fin); |
---|
19 | // obecne deleni: x_"Qposun1"*"Qscale"/y_"Qposun2" -> "Q_posun_fin" |
---|
20 | |
---|
21 | |
---|
22 | long prevod(double x, unsigned int posun) |
---|
23 | { |
---|
24 | long y,nasobic; |
---|
25 | int znamenko; |
---|
26 | |
---|
27 | nasobic=(long)1<<posun; |
---|
28 | if (x>=0) znamenko=1; |
---|
29 | else znamenko=-1; |
---|
30 | y=(long)(x*nasobic+znamenko*0.5); |
---|
31 | |
---|
32 | return y; |
---|
33 | } |
---|
34 | |
---|
35 | double zprevod(long x, unsigned int posun) |
---|
36 | { |
---|
37 | long nasobic; |
---|
38 | double y; |
---|
39 | |
---|
40 | nasobic=(long)1<<posun; |
---|
41 | y=(double)x/nasobic; |
---|
42 | |
---|
43 | return y; |
---|
44 | } |
---|
45 | |
---|
46 | long nasob(int x, int y,unsigned int posun) |
---|
47 | { |
---|
48 | long z,zaokrouhl; // zaokrouhl ... zaokrouhlovani vysledku pricteni 0.5 |
---|
49 | int znamenko; |
---|
50 | |
---|
51 | if (posun>0) |
---|
52 | zaokrouhl=(long)1<<(posun-1); |
---|
53 | else zaokrouhl=0; |
---|
54 | z=(long)x*y; |
---|
55 | if (z>=0) znamenko=1; |
---|
56 | else znamenko=-1; |
---|
57 | z=(z+znamenko*zaokrouhl)>>posun; |
---|
58 | |
---|
59 | return z; |
---|
60 | } |
---|
61 | |
---|
62 | int deleni15(int x, int y) // deleni Q15/Q15 se saturaci |
---|
63 | { |
---|
64 | long z; |
---|
65 | |
---|
66 | z=(long)x*32768/y; |
---|
67 | if (z>32767) z=32767; |
---|
68 | if (z<-32768) z=-32768; |
---|
69 | |
---|
70 | return (int)z; |
---|
71 | } |
---|
72 | |
---|
73 | int deleni(int x, int y, int posun1, int posun2, int scale, int posun_fin) |
---|
74 | /* |
---|
75 | x/y ... podil |
---|
76 | posun1 ... posun x (scale faktor) |
---|
77 | posun2 ... posun y |
---|
78 | scale ... posun pri deleni - pomocny |
---|
79 | posun_fin ... system, ve kterem ma byt vysledek (scale faktor vysledku) |
---|
80 | */ |
---|
81 | { |
---|
82 | int z; |
---|
83 | int bit_posun; |
---|
84 | long zz, scale_fakt, scale_fin; |
---|
85 | |
---|
86 | scale_fakt=(long)1<<scale; |
---|
87 | bit_posun=(posun1+scale-posun2)-posun_fin; |
---|
88 | |
---|
89 | zz=(long)x*scale_fakt/y; |
---|
90 | |
---|
91 | if (bit_posun>=0) |
---|
92 | zz=(int)(zz>>bit_posun); |
---|
93 | else |
---|
94 | { bit_posun*=-1; |
---|
95 | zz=(int)(zz<<bit_posun); |
---|
96 | } |
---|
97 | |
---|
98 | // Saturace vystupu |
---|
99 | if (zz>32767) |
---|
100 | z=32767; |
---|
101 | else if (zz<-32768) |
---|
102 | z=-32768; |
---|
103 | else |
---|
104 | z=zz; |
---|
105 | |
---|
106 | return z; |
---|
107 | } |
---|