OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [rtl/] [verilog/] [lib/] [cntpop.v] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 robfinch
/* ===============================================================
2
        (C) 2006  Robert Finch
3
        All rights reserved.
4
        rob@birdcomputer.ca
5
 
6
        cntpop.v
7
                - count number of one bits in a byte
8
                - simple fast approach - lookup table
9
 
10
        This source code is free for use and modification for
11
        non-commercial or evaluation purposes, provided this
12
        copyright statement and disclaimer remains present in
13
        the file.
14
 
15
        If the code is modified, please state the origin and
16
        note that the code has been modified.
17
 
18
        NO WARRANTY.
19
        THIS Work, IS PROVIDEDED "AS IS" WITH NO WARRANTIES OF
20
        ANY KIND, WHETHER EXPRESS OR IMPLIED. The user must assume
21
        the entire risk of using the Work.
22
 
23
        IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24
        ANY INCIDENTAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES
25
        WHATSOEVER RELATING TO THE USE OF THIS WORK, OR YOUR
26
        RELATIONSHIP WITH THE AUTHOR.
27
 
28
        IN ADDITION, IN NO EVENT DOES THE AUTHOR AUTHORIZE YOU
29
        TO USE THE WORK IN APPLICATIONS OR SYSTEMS WHERE THE
30
        WORK'S FAILURE TO PERFORM CAN REASONABLY BE EXPECTED
31
        TO RESULT IN A SIGNIFICANT PHYSICAL INJURY, OR IN LOSS
32
        OF LIFE. ANY SUCH USE BY YOU IS ENTIRELY AT YOUR OWN RISK,
33
        AND YOU AGREE TO HOLD THE AUTHOR AND CONTRIBUTORS HARMLESS
34
        FROM ANY CLAIMS OR LOSSES RELATING TO SUCH UNAUTHORIZED
35
        USE.
36
 
37
        Ref: Webpack 8.1i Spartan3-4 xc3s1000-4ft256
38
        11 LUTs / 6 slices / 12.2 ns
39
 
40
=============================================================== */
41
 
42
module cntpop8(
43
        input [7:0] i,
44
        output reg [3:0] o
45
);
46
 
47
        always @(i)
48
                case (i)
49
                8'b00000000:    o = 0;
50
                8'b00000001:    o = 1;
51
                8'b00000010:    o = 1;
52
                8'b00000011:    o = 2;
53
                8'b00000100:    o = 1;
54
                8'b00000101:    o = 2;
55
                8'b00000110:    o = 2;
56
                8'b00000111:    o = 3;
57
                8'b00001000:    o = 1;
58
                8'b00001001:    o = 2;
59
                8'b00001010:    o = 2;
60
                8'b00001011:    o = 3;
61
                8'b00001100:    o = 2;
62
                8'b00001101:    o = 3;
63
                8'b00001110:    o = 3;
64
                8'b00001111:    o = 4;
65
 
66
                8'b00010000:    o = 1;
67
                8'b00010001:    o = 2;
68
                8'b00010010:    o = 2;
69
                8'b00010011:    o = 3;
70
                8'b00010100:    o = 2;
71
                8'b00010101:    o = 3;
72
                8'b00010110:    o = 3;
73
                8'b00010111:    o = 4;
74
                8'b00011000:    o = 2;
75
                8'b00011001:    o = 3;
76
                8'b00011010:    o = 3;
77
                8'b00011011:    o = 4;
78
                8'b00011100:    o = 3;
79
                8'b00011101:    o = 4;
80
                8'b00011110:    o = 4;
81
                8'b00011111:    o = 5;
82
 
83
                8'b00100000:    o = 1;
84
                8'b00100001:    o = 2;
85
                8'b00100010:    o = 2;
86
                8'b00100011:    o = 3;
87
                8'b00100100:    o = 2;
88
                8'b00100101:    o = 3;
89
                8'b00100110:    o = 3;
90
                8'b00100111:    o = 4;
91
                8'b00101000:    o = 2;
92
                8'b00101001:    o = 3;
93
                8'b00101010:    o = 3;
94
                8'b00101011:    o = 4;
95
                8'b00101100:    o = 3;
96
                8'b00101101:    o = 4;
97
                8'b00101110:    o = 4;
98
                8'b00101111:    o = 5;
99
 
100
                8'b00110000:    o = 2;
101
                8'b00110001:    o = 3;
102
                8'b00110010:    o = 3;
103
                8'b00110011:    o = 4;
104
                8'b00110100:    o = 3;
105
                8'b00110101:    o = 4;
106
                8'b00110110:    o = 4;
107
                8'b00110111:    o = 5;
108
                8'b00111000:    o = 3;
109
                8'b00111001:    o = 4;
110
                8'b00111010:    o = 4;
111
                8'b00111011:    o = 5;
112
                8'b00111100:    o = 4;
113
                8'b00111101:    o = 5;
114
                8'b00111110:    o = 5;
115
                8'b00111111:    o = 6;
116
 
117
                // 44 - 1       
118
                8'b01000000:    o = 1;
119
                8'b01000001:    o = 2;
120
                8'b01000010:    o = 2;
121
                8'b01000011:    o = 3;
122
                8'b01000100:    o = 2;
123
                8'b01000101:    o = 3;
124
                8'b01000110:    o = 3;
125
                8'b01000111:    o = 4;
126
                8'b01001000:    o = 2;
127
                8'b01001001:    o = 3;
128
                8'b01001010:    o = 3;
129
                8'b01001011:    o = 4;
130
                8'b01001100:    o = 3;
131
                8'b01001101:    o = 4;
132
                8'b01001110:    o = 4;
133
                8'b01001111:    o = 5;
134
 
135
                8'b01010000:    o = 2;
136
                8'b01010001:    o = 3;
137
                8'b01010010:    o = 3;
138
                8'b01010011:    o = 4;
139
                8'b01010100:    o = 3;
140
                8'b01010101:    o = 4;
141
                8'b01010110:    o = 4;
142
                8'b01010111:    o = 5;
143
                8'b01011000:    o = 3;
144
                8'b01011001:    o = 4;
145
                8'b01011010:    o = 4;
146
                8'b01011011:    o = 5;
147
                8'b01011100:    o = 4;
148
                8'b01011101:    o = 5;
149
                8'b01011110:    o = 5;
150
                8'b01011111:    o = 6;
151
 
152
                8'b01100000:    o = 2;
153
                8'b01100001:    o = 3;
154
                8'b01100010:    o = 3;
155
                8'b01100011:    o = 4;
156
                8'b01100100:    o = 3;
157
                8'b01100101:    o = 4;
158
                8'b01100110:    o = 4;
159
                8'b01100111:    o = 5;
160
                8'b01101000:    o = 3;
161
                8'b01101001:    o = 4;
162
                8'b01101010:    o = 4;
163
                8'b01101011:    o = 5;
164
                8'b01101100:    o = 4;
165
                8'b01101101:    o = 5;
166
                8'b01101110:    o = 5;
167
                8'b01101111:    o = 6;
168
 
169
                8'b01110000:    o = 3;
170
                8'b01110001:    o = 4;
171
                8'b01110010:    o = 4;
172
                8'b01110011:    o = 5;
173
                8'b01110100:    o = 4;
174
                8'b01110101:    o = 5;
175
                8'b01110110:    o = 5;
176
                8'b01110111:    o = 6;
177
                8'b01111000:    o = 4;
178
                8'b01111001:    o = 5;
179
                8'b01111010:    o = 5;
180
                8'b01111011:    o = 6;
181
                8'b01111100:    o = 5;
182
                8'b01111101:    o = 6;
183
                8'b01111110:    o = 6;
184
                8'b01111111:    o = 7;
185
 
186
                //  - 2 
187
                8'b10000000:    o = 1;
188
                8'b10000001:    o = 2;
189
                8'b10000010:    o = 2;
190
                8'b10000011:    o = 3;
191
                8'b10000100:    o = 2;
192
                8'b10000101:    o = 3;
193
                8'b10000110:    o = 3;
194
                8'b10000111:    o = 4;
195
                8'b10001000:    o = 2;
196
                8'b10001001:    o = 3;
197
                8'b10001010:    o = 3;
198
                8'b10001011:    o = 4;
199
                8'b10001100:    o = 3;
200
                8'b10001101:    o = 4;
201
                8'b10001110:    o = 4;
202
                8'b10001111:    o = 5;
203
 
204
                8'b10010000:    o = 2;
205
                8'b10010001:    o = 3;
206
                8'b10010010:    o = 3;
207
                8'b10010011:    o = 4;
208
                8'b10010100:    o = 3;
209
                8'b10010101:    o = 4;
210
                8'b10010110:    o = 4;
211
                8'b10010111:    o = 5;
212
                8'b10011000:    o = 3;
213
                8'b10011001:    o = 4;
214
                8'b10011010:    o = 4;
215
                8'b10011011:    o = 5;
216
                8'b10011100:    o = 4;
217
                8'b10011101:    o = 5;
218
                8'b10011110:    o = 5;
219
                8'b10011111:    o = 6;
220
 
221
                8'b10100000:    o = 2;
222
                8'b10100001:    o = 3;
223
                8'b10100010:    o = 3;
224
                8'b10100011:    o = 4;
225
                8'b10100100:    o = 3;
226
                8'b10100101:    o = 4;
227
                8'b10100110:    o = 4;
228
                8'b10100111:    o = 5;
229
                8'b10101000:    o = 3;
230
                8'b10101001:    o = 4;
231
                8'b10101010:    o = 4;
232
                8'b10101011:    o = 5;
233
                8'b10101100:    o = 4;
234
                8'b10101101:    o = 5;
235
                8'b10101110:    o = 5;
236
                8'b10101111:    o = 6;
237
 
238
                8'b10110000:    o = 3;
239
                8'b10110001:    o = 4;
240
                8'b10110010:    o = 4;
241
                8'b10110011:    o = 5;
242
                8'b10110100:    o = 4;
243
                8'b10110101:    o = 5;
244
                8'b10110110:    o = 5;
245
                8'b10110111:    o = 6;
246
                8'b10111000:    o = 4;
247
                8'b10111001:    o = 5;
248
                8'b10111010:    o = 5;
249
                8'b10111011:    o = 6;
250
                8'b10111100:    o = 5;
251
                8'b10111101:    o = 6;
252
                8'b10111110:    o = 6;
253
                8'b10111111:    o = 7;
254
 
255
                // 44 - 3       
256
                8'b11000000:    o = 2;
257
                8'b11000001:    o = 3;
258
                8'b11000010:    o = 3;
259
                8'b11000011:    o = 4;
260
                8'b11000100:    o = 3;
261
                8'b11000101:    o = 4;
262
                8'b11000110:    o = 4;
263
                8'b11000111:    o = 5;
264
                8'b11001000:    o = 3;
265
                8'b11001001:    o = 4;
266
                8'b11001010:    o = 4;
267
                8'b11001011:    o = 5;
268
                8'b11001100:    o = 4;
269
                8'b11001101:    o = 5;
270
                8'b11001110:    o = 5;
271
                8'b11001111:    o = 6;
272
 
273
                8'b11010000:    o = 3;
274
                8'b11010001:    o = 4;
275
                8'b11010010:    o = 4;
276
                8'b11010011:    o = 5;
277
                8'b11010100:    o = 4;
278
                8'b11010101:    o = 5;
279
                8'b11010110:    o = 5;
280
                8'b11010111:    o = 6;
281
                8'b11011000:    o = 4;
282
                8'b11011001:    o = 5;
283
                8'b11011010:    o = 5;
284
                8'b11011011:    o = 6;
285
                8'b11011100:    o = 5;
286
                8'b11011101:    o = 6;
287
                8'b11011110:    o = 6;
288
                8'b11011111:    o = 7;
289
 
290
                8'b11100000:    o = 3;
291
                8'b11100001:    o = 4;
292
                8'b11100010:    o = 4;
293
                8'b11100011:    o = 5;
294
                8'b11100100:    o = 4;
295
                8'b11100101:    o = 5;
296
                8'b11100110:    o = 5;
297
                8'b11100111:    o = 6;
298
                8'b11101000:    o = 4;
299
                8'b11101001:    o = 5;
300
                8'b11101010:    o = 5;
301
                8'b11101011:    o = 6;
302
                8'b11101100:    o = 5;
303
                8'b11101101:    o = 6;
304
                8'b11101110:    o = 6;
305
                8'b11101111:    o = 7;
306
 
307
                8'b11110000:    o = 4;
308
                8'b11110001:    o = 5;
309
                8'b11110010:    o = 5;
310
                8'b11110011:    o = 6;
311
                8'b11110100:    o = 5;
312
                8'b11110101:    o = 6;
313
                8'b11110110:    o = 6;
314
                8'b11110111:    o = 7;
315
                8'b11111000:    o = 5;
316
                8'b11111001:    o = 6;
317
                8'b11111010:    o = 6;
318
                8'b11111011:    o = 7;
319
                8'b11111100:    o = 6;
320
                8'b11111101:    o = 7;
321
                8'b11111110:    o = 7;
322
                8'b11111111:    o = 8;
323
 
324
                endcase
325
 
326
 
327
endmodule
328
 
329
 
330
module cntpop16(
331
        input [15:0] i,
332
        output [4:0] o
333
);
334
 
335
        wire [3:0] cnt1, cnt2;
336
 
337
        cntpop8 u1 (i[ 7:0],cnt1);
338
        cntpop8 u2 (i[15:8],cnt2);
339
 
340
        assign o = cnt1 + cnt2;
341
 
342
endmodule
343
 
344
 
345
// 76 slices / 147 LUTs / 19 ns
346
module cntpop32(
347
        input [31:0] i,
348
        output [5:0] o
349
);
350
 
351
        wire [3:0] cnt1, cnt2, cnt3, cnt4;
352
 
353
        // cntpop8 results in faster result than cntpop16
354
        cntpop8 u1 (i[ 7: 0],cnt1);
355
        cntpop8 u2 (i[15: 8],cnt2);
356
        cntpop8 u3 (i[23:16],cnt3);
357
        cntpop8 u4 (i[31:24],cnt4);
358
 
359
        assign o = cnt1+cnt2+cnt3+cnt4;
360
 
361
endmodule
362
 
363
 
364
// 156 slices / 300 LUTs / 22.2 ns
365
module cntpop64(
366
        input [63:0] i,
367
        output [6:0] o
368
);
369
 
370
        wire [4:0] cnt1, cnt2, cnt3, cnt4;
371
 
372
        cntpop16 u1 (i[15: 0],cnt1);
373
        cntpop16 u2 (i[31:16],cnt2);
374
        cntpop16 u3 (i[47:32],cnt3);
375
        cntpop16 u4 (i[63:48],cnt4);
376
 
377
        assign o = cnt1+cnt2+cnt3+cnt4;
378
 
379
endmodule
380
 
381
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.