Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "BitFieldTie 3" | |
132 | .TH BitFieldTie 3 "2003-01-23" "perl v5.8.0" "User Contributed Perl Documentation" | |
133 | .SH "NAME" | |
134 | BitFieldTie \- Tie interface for bitfield operations | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use BitFieldTie; # or use TRELoad 'BitFieldTie'; | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 1 | |
142 | \& tie %num, 'BitFieldTie'; | |
143 | .Ve | |
144 | .PP | |
145 | .Vb 2 | |
146 | \& $num{'31:0'} = hex('0x1234'); | |
147 | \& $num{'63:32'} = hex('0xabcd'); | |
148 | .Ve | |
149 | .PP | |
150 | .Vb 2 | |
151 | \& print "low byte is $num{'7:0'}\en"; | |
152 | \& print "MSB is $num{63}\en"; | |
153 | .Ve | |
154 | .PP | |
155 | .Vb 2 | |
156 | \& my $obj = tied %num; # get object | |
157 | \& print "Num is $obj\en"; # object prints as hex num | |
158 | .Ve | |
159 | .SH "ABSTRACT" | |
160 | .IX Header "ABSTRACT" | |
161 | .Vb 3 | |
162 | \& This is a thin wrapper for Bit::Vector that presents a tie interface for | |
163 | \& bit vectors. The bit vector itself can be of arbitrary size, but the | |
164 | \& chunk size (the size of an individual bit field) is limited to 32 bits. | |
165 | .Ve | |
166 | .SH "DESCRIPTION" | |
167 | .IX Header "DESCRIPTION" | |
168 | This module allows users to access bit fields with a hash interface. | |
169 | .Sh "Introduction" | |
170 | .IX Subsection "Introduction" | |
171 | This module provides two components. The first is a class, | |
172 | BitFieldTie, that allows users to manipulate bit vectors of arbitrary | |
173 | size using object methods. The second is a tie interface. When a | |
174 | hash is tied to a BitFieldTie object, a hash interface can be used to | |
175 | set or exampine bit ranges in the vector. | |
176 | .Sh "Hash Interface" | |
177 | .IX Subsection "Hash Interface" | |
178 | This subsection describes using the tied hash interface. | |
179 | .PP | |
180 | \fISetting up a bitfield\fR | |
181 | .IX Subsection "Setting up a bitfield" | |
182 | .PP | |
183 | When you tie a hash to this module, the hash becomes a representation | |
184 | of bitfields of a number. By default, a 64\-bit integer is created and | |
185 | initialized to zero. You can provide optional arguments to the tie | |
186 | command to set a different size and initial value, as in: | |
187 | .PP | |
188 | .Vb 1 | |
189 | \& tie %num, 'BitFieldTie', 32, '0x1234abcd'; | |
190 | .Ve | |
191 | .PP | |
192 | The first optional agument is the size in bits, and the second is the | |
193 | initial value \s-1IN\s0 \s-1HEX\s0. | |
194 | .PP | |
195 | \fIUsing a bitfield\fR | |
196 | .IX Subsection "Using a bitfield" | |
197 | .PP | |
198 | You can then access fields of the hash. Hash keys can either be a | |
199 | single number for single-bit access, or a range in the form of | |
200 | <high>:<low>. The values in the hash are integers, so | |
201 | for istance aftre the above initialization, the value of \f(CW$num\fR{'3:0'} | |
202 | would be 13 (decimal for 0xd). The hash provides both read and write | |
203 | access. \fBThe major restriction is that the size of the bit range | |
204 | (i.e., high\-low+1) cannot exceed 32\-bits.\fR To access larger ranges, | |
205 | you need to break it up into separate accesses. The main reason for | |
206 | that restriction is that if the module allowed larger chunks, it could | |
207 | not use integers to represent bit fields and performance would suffer | |
208 | considerably. | |
209 | .PP | |
210 | \fIPrinting the bitfield\fR | |
211 | .IX Subsection "Printing the bitfield" | |
212 | .PP | |
213 | Unfortunately, the tied-hash mechanism does not lend itself to object | |
214 | methods to do un-hash-like things like pretty\-printing. You must | |
215 | therefore use the object interface, and there is a little bit of | |
216 | syntax involved. | |
217 | .PP | |
218 | .Vb 1 | |
219 | \& $obj = tied %num; | |
220 | .Ve | |
221 | .PP | |
222 | This sets \f(CW$obj\fR to the underlying object for the tied hash. The object | |
223 | does know how to print itself (among other things). | |
224 | .PP | |
225 | .Vb 1 | |
226 | \& print "Num is $obj\en"; | |
227 | .Ve | |
228 | .PP | |
229 | The above statement will print \f(CW%num\fR as a hexidecimal number. | |
230 | .PP | |
231 | You can also interpolate the hash directly with a little bit of funny | |
232 | syntax: | |
233 | .PP | |
234 | .Vb 1 | |
235 | \& print "Num is @{[tied %num]}\en"; | |
236 | .Ve | |
237 | .PP | |
238 | This is just a clever perl hack to do the same thing without explictly | |
239 | referencing \f(CW$obj\fR. | |
240 | .Sh "Object interface" | |
241 | .IX Subsection "Object interface" | |
242 | Objects of type BitFieldTie can be created in 3 ways. The first is if | |
243 | a hash is tied to a BitFieldTie object, but no object is specified (as | |
244 | is the case in the previous examples), one will be created. This | |
245 | object can then be referenced by using the 'tied' operator on the | |
246 | hash, as shown in the previous section. | |
247 | .PP | |
248 | Objects can also be created with the \fInew()\fR or \fIclone()\fR methods, as | |
249 | described in the section on Object Methods. | |
250 | .PP | |
251 | Once an object is created, it can be easily manipulated as shown in | |
252 | the next section. | |
253 | .PP | |
254 | \fIMath with Bitfields\fR | |
255 | .IX Subsection "Math with Bitfields" | |
256 | .PP | |
257 | BitFieldTie ties a hash object to an object. This allows you to use | |
258 | convenient hash syntax to access bit fields. To do math, however, you | |
259 | need to manipulate the object directly. The perl builtin-function | |
260 | tied will give you the object associated with a tied hash. | |
261 | .PP | |
262 | .Vb 2 | |
263 | \& my %v1; | |
264 | \& tie %v1, 'BitFieldTie', 64, '0x0000ffff0000cccc'; | |
265 | .Ve | |
266 | .PP | |
267 | .Vb 1 | |
268 | \& my $v1 = tied %v1; | |
269 | .Ve | |
270 | .PP | |
271 | The above code creates a new 64\-bit number tied to the hash \f(CW%v1\fR. The | |
272 | underlying object is assigned to \f(CW$v1\fR. Say we had a similar definition | |
273 | for v2: | |
274 | .PP | |
275 | .Vb 3 | |
276 | \& my %v2; | |
277 | \& tie %v2, 'BitFieldTie', 64, '0xffff333300003333'; | |
278 | \& my $v2 = tied %v2; | |
279 | .Ve | |
280 | .PP | |
281 | You can still access bitfields using hash syntax on \f(CW%v1\fR and \f(CW%v2\fR. You | |
282 | can now also call object methods on \f(CW$v1\fR and \f(CW$v2\fR. For instance: | |
283 | .PP | |
284 | .Vb 2 | |
285 | \& $v2->bitwise_and($v1); | |
286 | \& print "$v2"; | |
287 | .Ve | |
288 | .PP | |
289 | The above prints: \*(L"0000333300000000\*(R". Keep in mind that as mentioned | |
290 | above, when you convert an underlying BitFieldTie object to a string | |
291 | (as in the print statement), the string is a hexadecimal | |
292 | representation of the number. | |
293 | .PP | |
294 | \fIObject methods\fR | |
295 | .IX Subsection "Object methods" | |
296 | .PP | |
297 | The following are the object methods that BitFieldTie objects respond to. | |
298 | .ie n .IP "new($size, $hexstring) \s-1OR\s0 new($obj)" 4 | |
299 | .el .IP "new($size, \f(CW$hexstring\fR) \s-1OR\s0 new($obj)" 4 | |
300 | .IX Item "new($size, $hexstring) OR new($obj)" | |
301 | Class method that creates a new object and returns it. Arguments are | |
302 | optional, if a \f(CW$size\fR and/or \f(CW$hexstring\fR is specified, it works just as | |
303 | the argument list to tie. If an object is provided, that object is | |
304 | cloned, and the clone is returned. | |
305 | .Sp | |
306 | \&\fInew()\fR can also be called as an object method. So the following two | |
307 | statements are identical (assuming \f(CW$obj\fR is a BitFieldTie): | |
308 | .Sp | |
309 | .Vb 2 | |
310 | \& $new = $obj->new(); | |
311 | \& $new = BitFieldTie->new($obj); | |
312 | .Ve | |
313 | .ie n .IP "new_dec($size, $decimal)" 4 | |
314 | .el .IP "new_dec($size, \f(CW$decimal\fR)" 4 | |
315 | .IX Item "new_dec($size, $decimal)" | |
316 | Same as new, except that the second argument is treated as a decimal | |
317 | argument, instead of a hex string. | |
318 | .IP "\fIclone()\fR" 4 | |
319 | .IX Item "clone()" | |
320 | Returns a new BitFieldTie object that is identical to the old one | |
321 | \&\s-1EXCEPT\s0 that it is not tied to any hash. | |
322 | .IP "\fIstringify()\fR" 4 | |
323 | .IX Item "stringify()" | |
324 | Returns hexadecimal object as a string. This is called automatically | |
325 | when you include a BitFieldTie object in double\-quotes. | |
326 | .ie n .IP "extract($hi, $low)" 4 | |
327 | .el .IP "extract($hi, \f(CW$low\fR)" 4 | |
328 | .IX Item "extract($hi, $low)" | |
329 | Returns the specified bit range from the object as an integer. Since | |
330 | the return value is an integer, the size (i.e., \f(CW$hi\fR \- \f(CW$low\fR + 1) must | |
331 | be <= 32. | |
332 | .ie n .IP "store($hi, $low\fR, \f(CW$value)" 4 | |
333 | .el .IP "store($hi, \f(CW$low\fR, \f(CW$value\fR)" 4 | |
334 | .IX Item "store($hi, $low, $value)" | |
335 | Stores the \f(CW$value\fR (an integer!) in the specified bit range in the | |
336 | object. Since the return value is an integer, the size (i.e., \f(CW$hi\fR \- | |
337 | \&\f(CW$low\fR + 1) must be <= 32. Also, the \f(CW$value\fR must be an integer, not a string. | |
338 | .IP "\fIclear()\fR" 4 | |
339 | .IX Item "clear()" | |
340 | Sets all bits in the bit vector to 0. | |
341 | .IP "\fIsize()\fR, size($numbits)" 4 | |
342 | .IX Item "size(), size($numbits)" | |
343 | Sets/Gets the size (in bits) of the number, depending on whether or | |
344 | not an argument is given. | |
345 | .IP "left_shift($numbits)" 4 | |
346 | .IX Item "left_shift($numbits)" | |
347 | Left shifts the number. | |
348 | .IP "right_shift($numbits)" 4 | |
349 | .IX Item "right_shift($numbits)" | |
350 | Right shifts the number. | |
351 | .IP "bitwise_and($obj)" 4 | |
352 | .IX Item "bitwise_and($obj)" | |
353 | Does a bitwise and between the calling object and \f(CW$obj\fR. Stores the | |
354 | result in the calling object. For example: | |
355 | .Sp | |
356 | .Vb 1 | |
357 | \& $v1->bitwise_and($v2); | |
358 | .Ve | |
359 | .Sp | |
360 | has the C equivalent of: | |
361 | .Sp | |
362 | .Vb 1 | |
363 | \& v1 &= v2; | |
364 | .Ve | |
365 | .IP "bitwise_or($obj)" 4 | |
366 | .IX Item "bitwise_or($obj)" | |
367 | Same as bitwise_and, except it performs an \s-1OR\s0 function. | |
368 | .IP "bitwise_xor($obj)" 4 | |
369 | .IX Item "bitwise_xor($obj)" | |
370 | Same as bitwise_and and bitwise_or, except it performs an \s-1XOR\s0 function. | |
371 | .IP "\fIbitwise_not()\fR" 4 | |
372 | .IX Item "bitwise_not()" | |
373 | Flips every bit in the number. | |
374 | .ie n .IP "divide($obj, $remainder)" 4 | |
375 | .el .IP "divide($obj, \f(CW$remainder\fR)" 4 | |
376 | .IX Item "divide($obj, $remainder)" | |
377 | Divides the calling object by \f(CW$obj\fR and stores the result in the | |
378 | calling object (i.e., /=). \f(CW$remainder\fR is initialized to the | |
379 | remainder. \f(CW$obj\fR can be an integer, in which case an object the same | |
380 | size as the calling object is created for it. | |
381 | .IP "multiply($obj)" 4 | |
382 | .IX Item "multiply($obj)" | |
383 | Multiplies the calling object by \f(CW$obj\fR and stores the result in the | |
384 | calling object (i.e., *=). \f(CW$obj\fR can be an integer, in which | |
385 | case an object the same size as the calling object is created for it. | |
386 | .IP "add($obj)" 4 | |
387 | .IX Item "add($obj)" | |
388 | Adds \f(CW$obj\fR to the calling object. \f(CW$obj\fR can be an integer, in which | |
389 | case an object the same size as the calling object is created for it. | |
390 | .IP "subtract($obj)" 4 | |
391 | .IX Item "subtract($obj)" | |
392 | Subtracts \f(CW$obj\fR from the calling object. \f(CW$obj\fR can be an integer, in which | |
393 | case an object the same size as the calling object is created for it. | |
394 | .IP "compare($obj)" 4 | |
395 | .IX Item "compare($obj)" | |
396 | Does a comparison on the calling object and \f(CW$obj\fR (which may be an | |
397 | integer). Returns \-1 if the calling object is smaller, 0 if they are | |
398 | equal, and 1 if the calling object is greater that \f(CW$obj\fR. Both the | |
399 | calling object and \f(CW$obj\fR are treated as \s-1SIGNED\s0 integers for the | |
400 | purposes of comparison. | |
401 | .IP "ucompare($obj)" 4 | |
402 | .IX Item "ucompare($obj)" | |
403 | Same as compare, but the calling object and \f(CW$obj\fR are treated as | |
404 | \&\s-1UNSIGNED\s0 integers. | |
405 | .Sh "Tying an Existing Object to a Hash" | |
406 | .IX Subsection "Tying an Existing Object to a Hash" | |
407 | If you create a BitFieldTie object with \fInew()\fR or \fIclone()\fR, it begins | |
408 | life not tied to any hash. You can manipulate it with object methods, | |
409 | but if you want to access bit fields with hash syntax, you will need | |
410 | to tie it to a hash first. Here is an example | |
411 | .PP | |
412 | .Vb 2 | |
413 | \& my $obj = BitFieldTie->new(64, '0xdeadbeefcafe0123'); | |
414 | \& tie %h, 'BitFieldTie', $obj; | |
415 | .Ve | |
416 | .PP | |
417 | The contents of \f(CW$h\fR{'15:0'} would then be hex('0123'); | |
418 | .Sh "\s-1EXPORT\s0" | |
419 | .IX Subsection "EXPORT" | |
420 | None. Object modules do not export any symbols. | |
421 | .SH "SEE ALSO" | |
422 | .IX Header "SEE ALSO" | |
423 | \&\fIBit::Vector\fR\|(3). |