-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquant.cpp
More file actions
244 lines (188 loc) · 6.58 KB
/
quant.cpp
File metadata and controls
244 lines (188 loc) · 6.58 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include "ap_int.h"
#include "math.h"
#include "conv.h"
#define PARA 16
void rand_(uchar_* lfsr) {
ubit_ b_7 = (*lfsr).get_bit(7);
ubit_ b_5 = (*lfsr).get_bit(5);
ubit_ b_4 = (*lfsr).get_bit(4);
ubit_ b_3 = (*lfsr).get_bit(3);
bool new_bit = b_7 ^ b_5 ^ b_4 ^ b_3;
(*lfsr) = (*lfsr) << 1;
(*lfsr).set_bit(0, new_bit);
}
void memcpy_int(int_* to, int_* from, int size)
{
for(int i=0;i<size;i++)
to[i] = from[i];
}
void memset_int(int_* to, int size)
{
for(int i=0;i<size;i++)
to[i] = 0;
}
ubit_ round_(uchar_ value, uchar_* lfsr)
{
rand_(lfsr);
return value>(*lfsr);
}
char_ input_qt( int_ input, int s_in_pre, int s_in, ubit_ * larger, ubit_ * smaller)
{
int_ tmp_int;
if(s_in + 2 > s_in_pre)
tmp_int = input << (s_in + 2 -s_in_pre);
else
tmp_int = input >> (-(s_in + 2 -s_in_pre));
int_ tmp_int0 = tmp_int.range(31,1) + tmp_int.range(0,0);
int_ tmp_int1 = tmp_int.range(31,2) + tmp_int.range(1,1);
if(tmp_int1>127 || tmp_int1<-128)
*larger = 1;
if(tmp_int0>127 || tmp_int0<-128)
*smaller = 0;
if(tmp_int1 > 127)
tmp_int1 = 127;
else if (tmp_int1 < -128)
tmp_int1 = -128;
return tmp_int1.range(7,0);
}
char_ outdiff_qt( int_ outdiff, int s_outdif_pre, int s_out, ubit_ * larger, ubit_ *smaller, uchar_ *seed)
{
int_ tmp_int;
if(s_out+9>s_outdif_pre)
tmp_int = outdiff << (s_out+9-s_outdif_pre);
else
tmp_int = outdiff >> (-(s_out+9-s_outdif_pre));
int_ tmp_int0 = tmp_int.range(31,8) + round_(tmp_int.range(7,0), seed);
int_ tmp_int1 = tmp_int.range(31,9) + round_(tmp_int.range(8,1), seed);
if(tmp_int1>127 || tmp_int1<-128)
{
*larger = 1;
}
if(tmp_int0>127 || tmp_int0<-128)
{
*smaller = 0;
}
if(tmp_int1>127)
tmp_int1 = 127;
else if (tmp_int1<-128)
tmp_int1 = -128;
return tmp_int1.range(7,0);
}
int_ momentb_qt( int_ moment_buf, int_ weight_buf, int s_moment, int s_moment_pre, int s_weight, ubit_ * larger, ubit_ * smaller)
{
long_ tmp0, tmp00;
if(s_moment > (s_weight))
tmp0 = (((long_)weight_buf) << (s_moment - s_weight));
else
tmp0 = (weight_buf) >> (s_weight - s_moment);
if(s_moment > s_moment_pre)
tmp00 = moment_buf << (s_moment - s_moment_pre);
else
tmp00 = moment_buf >> (s_moment_pre - s_moment);
long_ tmp1 = ((tmp00*9 + tmp0)<<2)/10;
long_ tmp2 = tmp1.range(63,1) + tmp1.range(0,0);
long_ tmp3 = tmp1.range(63,2) + tmp1.range(1,1);
if(tmp3> 2e32-1 || tmp3<-2e32)
{
*larger = 1;
}
if(tmp2>2e32-1 || tmp2<-2e32)
{
*smaller = 0;
}
if(tmp3 > 2e32-1)
tmp3 = 2e32-1;
else if(tmp3 < -2e32)
tmp3 = -2e32;
return tmp3.range(31, 0);
}
int_ moment_qt( int_ moment_buf, int_ weight_buf, int s_moment, int s_moment_pre, int s_weight, ubit_ * larger, ubit_ * smaller)
{
long_ tmp0, tmp00;
if(s_moment > (s_weight))
tmp0 = (((long_)weight_buf) << (s_moment - s_weight))/img_num;
else
tmp0 = (weight_buf/img_num) >> (s_weight - s_moment);
if(s_moment > s_moment_pre)
tmp00 = moment_buf << (s_moment - s_moment_pre);
else
tmp00 = moment_buf >> (s_moment_pre - s_moment);
long_ tmp1 = ((tmp00*9 + tmp0)<<2)/10;
long_ tmp2 = tmp1.range(63,1) + tmp1.range(0,0);
long_ tmp3 = tmp1.range(63,2) + tmp1.range(1,1);
if(tmp3> 2e32-1 || tmp3<-2e32)
{
*larger = 1;
}
if(tmp2>2e32-1 || tmp2<-2e32)
{
*smaller = 0;
}
if(tmp3 > 2e32-1)
tmp3 = 2e32-1;
else if(tmp3 < -2e32)
tmp3 = -2e32;
return tmp3.range(31, 0);
}
char_ weight_qt( int_ moment, char_ weight, int s_moment, int s_w_pre, int s_w, ubit_ * larger, ubit_ * smaller, int rate, int decay )
{
int w_decay = decay - 1;
long_ tmp_int, tmpw, tmpg;
if(s_w+2 > s_w_pre)
tmpw = (long_)weight << (s_w + 2 - s_w_pre);
else
tmpw = weight >> (s_w_pre - s_w -2);
tmpw = tmpw * w_decay/decay;
if(s_w+2 > s_moment)
tmpg = (((long_)moment) << (s_w + 2 - s_moment))/rate;
else
tmpg = (moment/rate) >> (s_moment - 2 - s_w);
tmp_int = tmpw - tmpg;
int_ tmp_int0 = tmp_int.range(31,1) + tmp_int.range(0,0);
int_ tmp_int1 = tmp_int.range(31,2) + tmp_int.range(1,1);
if(tmp_int1>127 || tmp_int1<-128)
{
*larger = 1;
}
if(tmp_int0>127 || tmp_int0<-128)
{
*smaller = 0;
}
if(tmp_int1>127)
tmp_int1 = 127;
else if(tmp_int1<-128)
tmp_int1 = -128;
return tmp_int1.range(7,0);
}
char_ bias_qt( int_ moment, char_ weight, int s_moment, int s_w_pre, int s_w, ubit_ * larger, ubit_ * smaller, int rate)
{
long_ tmp_int, tmpw, tmpg;
if(s_w+2 > s_w_pre)
tmpw = (long_)weight << (s_w + 2 - s_w_pre);
else
tmpw = weight >> (s_w_pre - s_w -2);
if(s_w+2 > s_moment)
tmpg = (((long_)moment) << (s_w + 2 - s_moment))/rate;
else
tmpg = (moment/rate) >> (s_moment - 2 - s_w);
tmp_int = tmpw - tmpg;
int_ tmp_int0 = tmp_int.range(31,1) + tmp_int.range(0,0);
int_ tmp_int1 = tmp_int.range(31,2) + tmp_int.range(1,1);
if(tmp_int1>127 || tmp_int1<-128)
{
*larger = 1;
}
if(tmp_int0>127 || tmp_int0<-128)
{
*smaller = 0;
}
if(tmp_int1>127)
tmp_int1 = 127;
else if(tmp_int1<-128)
tmp_int1 = -128;
return tmp_int1.range(7,0);
}