1 /*
2  * Copyright (c) 2001 Mark Fullmer and The Ohio State University
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $Id: bit1024.c,v 1.6 2003/02/13 02:38:41 maf Exp $
27  */
28 
29 #include "ftinclude.h"
30 #include "ftlib.h"
31 
32 int bit1024_reverse[] = {
33 
34 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
35 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
36 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4,
37 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5,
38 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2,
39 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,
40 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5,
41 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
42 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5,
43 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
44 
45 unsigned int bit1024_pow2[] = {
46 1U, 2U, 4U, 8U, 16U, 32U, 64U, 128U, 256U, 512U, 1024U, 2048U, 4096U, 8192U,
47 16384U, 32768U, 65536U, 131072U, 262144U, 524288U, 1048576U, 2097152U,
48 4194304U, 8388608U, 16777216U, 33554432U, 67108864U, 134217728U, 268435456U,
49 536870912U, 1073741824U, 2147483648U,
50 };
51 
52 
bit1024_store(int d,struct bit1024 * old)53 void bit1024_store(int d, struct bit1024 *old)
54 {
55   old->n[(31 - (d/32))] |= bit1024_pow2[d%32];
56 } /* bit1024_store */
57 
bit1024_count(struct bit1024 * b)58 int bit1024_count(struct bit1024 *b)
59 {
60   int i, c;
61 
62   for (i = 0, c = 0; i < 32; ++i) {
63     c += bit1024_reverse[(b->n[i]&0x000000FF)]+
64 		bit1024_reverse[(b->n[i]&0x0000FF00)>>8]+
65 		bit1024_reverse[(b->n[i]&0x00FF0000)>>16]+
66 		bit1024_reverse[(b->n[i]&0xFF000000)>>24];
67   }
68 
69   return c;
70 
71 } /* bit1024_count */
72 
bit1024_print(FILE * FP,struct bit1024 * b)73 void bit1024_print(FILE *FP, struct bit1024 *b)
74 {
75   int i, j, x;
76 
77   if (!(x = bit1024_count(b))) {
78     return;
79   }
80 
81   fprintf(FP, "P %d", x);
82 
83   for (i = 31; i >= 0; --i) {
84     for (j = 0; j < 32; ++j) {
85       if (b->n[i] & bit1024_pow2[j]) {
86         x = (31 - i);
87 		fprintf(FP, " %d", x*32+j);
88       }
89     }
90   }
91   fprintf(FP, "\n");
92 
93 } /* bit1024_print */
94 
95