]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/2c/bits.c
devproc: can't wait for ourselfs to stop (thanks Shamar)
[plan9front.git] / sys / src / cmd / 2c / bits.c
1 #define EXTERN
2 #include "gc.h"
3
4 /*
5 Bits
6 bor(Bits a, Bits b)
7 {
8         Bits c;
9         int i;
10
11         for(i=0; i<BITS; i++)
12                 c.b[i] = a.b[i] | b.b[i];
13         return c;
14 }
15 */
16
17 /*
18 Bits
19 band(Bits a, Bits b)
20 {
21         Bits c;
22         int i;
23
24         for(i=0; i<BITS; i++)
25                 c.b[i] = a.b[i] & b.b[i];
26         return c;
27 }
28 */
29
30 /*
31 Bits
32 bnot(Bits a)
33 {
34         Bits c;
35         int i;
36
37         for(i=0; i<BITS; i++)
38                 c.b[i] = ~a.b[i];
39         return c;
40 }
41 */
42
43 int
44 bany(Bits *a)
45 {
46         int i;
47
48         for(i=0; i<BITS; i++)
49                 if(a->b[i])
50                         return 1;
51         return 0;
52 }
53
54 /*
55 int
56 beq(Bits a, Bits b)
57 {
58         int i;
59
60         for(i=0; i<BITS; i++)
61                 if(a.b[i] != b.b[i])
62                         return 0;
63         return 1;
64 }
65 */
66
67 int
68 bnum(Bits a)
69 {
70         int i;
71         long b;
72
73         for(i=0; i<BITS; i++)
74                 if(b = a.b[i])
75                         return 32*i + bitno(b);
76         diag(Z, "bad in bnum");
77         return 0;
78 }
79
80 Bits
81 blsh(unsigned n)
82 {
83         Bits c;
84
85         c = zbits;
86         c.b[n/32] = 1L << (n%32);
87         return c;
88 }
89
90 /*
91 int
92 bset(Bits a, unsigned n)
93 {
94         int i;
95
96         if(a.b[n/32] & (1L << (n%32)))
97                 return 1;
98         return 0;
99 }
100 */
101
102 int
103 Bconv(va_list *arg, Fconv *fp)
104 {
105         char str[STRINGSZ], ss[STRINGSZ], *s;
106         Bits bits;
107         int i;
108
109         str[0] = 0;
110         bits = va_arg(*arg, Bits);
111         while(bany(&bits)) {
112                 i = bnum(bits);
113                 if(str[0])
114                         strcat(str, " ");
115                 if(var[i].sym == S) {
116                         sprint(ss, "$%ld", var[i].offset);
117                         s = ss;
118                 } else
119                         s = var[i].sym->name;
120                 if(strlen(str) + strlen(s) + 1 >= STRINGSZ)
121                         break;
122                 strcat(str, s);
123                 bits.b[i/32] &= ~(1L << (i%32));
124         }
125         strconv(str, fp);
126         return 0;
127 }