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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [lib/] [cntlz.v] - Blame information for rev 70

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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