12.12Bitfields. 4tH 4tH Forth compiler
Anúncio
Anúncio
CHAPTER 12. STANDARD LIBRARIES
A
LGORITHM
Bubble sort
Cocktail sort
Cocktail sort
(improved)
Simple sort
Insertion sort
Selection sort
Cycle sort
Pancake sort
Stooge sort
Heap sort
Slow sort
Quicksort
Quicksort
(unsafe)
Comb sort
Shell sort
Shellsort
(improved)
Merge sort
A
LGORITHM
Heap sort
Comb sort
Selection sort
Bubble sort
Gnome sort
Gnome sort
(improved)
L
IBRARY bublsort.4th
cocksort.4th
coc2sort.4th
simpsort.4th
instsort.4th
selcsort.4th
cyclsort.4th
pancsort.4th
stoosort.4th
hea2sort.4th
slowsort.4th
qsort.4th
qsort.4th
C
OMBINE
?
No
No
No
No
No
No
No
No
No
No
No
No
No com2sort.4th
shelsort.4th
shelsort.4th
No
No
No
3.4
2.6
4.1
mergsort.4th
No 2.6
Table 12.3: 4tH sorting algorithms (address based)
L
IBRARY heapsort.4th
combsort.4th
sel2sort.4th
bub2sort.4th
gnomsort.4th
gno2sort.4th
C
OMBINE
?
Yes
Yes
Yes
Yes
Yes
Yes
S
IZE
3.0
3.2
1.3
1.4
1.3
1.8
S
IZE
1.4
2.6
2.0
2.4
2.7
3.9
2.3
1.0
1.5
1.2
2.7
4.0
3.0
Table 12.4: 4tH sorting algorithms (index based)
S
PEED
3.0
2.5
3900
6000
6000
4200
Apart from their ”family” characteristics each and every algorithm has its own strengths and weaknesses. Which one is optimal for your specific problem depends on a lot of things, but there more than adequate resources for you to find out. As a rule of the thumb you can say that the larger library members are faster, but the table gives you more accurate timings. The factor ”Speed” gives you an indication of how fast they are. 1.0 is the reference speed, higher numbers are proportionally slower
10
. The same for the ”Size” factor - higher numbers are proportionally bigger.
179
S
PEED
2500
2800
1800
1800
1500
1200
4100
3600
>500,000
2.5
∞
1.0
1.0
2.5
2.5
2.0
10
12.12
Bitfields
Bitfields are always a bit of a controversial subject, since many complain that they are not portable or simply not worth the effort. Anyway, there is a library included if you should ever need them. In its most basic form they are quite simple: you provide the number and a certain number of bits are extracted from a certain position:
10
For a 100,000 element cell array
CHAPTER 12. STANDARD LIBRARIES 180 include lib/bitfield.4th
[binary] 00011000 [decimal]
3 2 bn@ .
\ include the library
\ put a number on the stack
\ start at bit 3, 2 bits long
This will return ”3” - or ”11” binary, if you prefer. Counting starts at the rightmost bit
(which is bit 0) so this expression returns bits 3 and 4. Saving it is very easy as well, just add a new value: include lib/bitfield.4th
\ include the library
\ store ”2” into bit 3 and 4
2 [binary] 00011000 [decimal] 3 2 bn!
Of course, you can also store a pattern into a string or cell variable: include lib/bitfield.4th
variable myvar
\ include the library
\ define a variable
0 myvar !
3 myvar 3 2 b!
myvar 3 2 b@ . cr
\ initialize it
\ store ”3” into bit 3 and 4
\ print the bitfield
Its character counterparts are named ”BC@” and ”BC!”, which may not be all that surprising.
Some may argue that these are not true bitfields, because you always have to provide the starting bit and the number of bits. With a bit of effort though we can make these bitfields behave like the real thing. First, include constant.4th in addition to the bitfield library: include lib/bitfield.4th
include lib/constant.4th
\ include the library
\ include "cell-bits"
Let’s define the fields in the usual ”starting bit, length in bits” form and add the expression
”CELL-BITS * +”. After that, turn the whole thing into a constant. Just make sure the bitfields don’t overlap:
0 3 cell-bits * + constant ink does> cell-bits /mod ;
3 3 cell-bits * + constant paper does> cell-bits /mod ;
6 1 cell-bits * + constant bright does> cell-bits /mod ;
7 1 cell-bits * + constant flash does> cell-bits /mod ;
This will scale the bitfield ”length” parameter in such a way, that it stays arithmetically separated from the ”starting bit” parameter. Finally, wrap it into a simple ’DOES>’ definition.
This will split the constant into the two components again when it’s used. Now let’s use it in a real world example:
0 enum black enum enum red enum blue magenta
\ enumerate all colors enum green enum cyan enum yellow constant white
24 constant /lines
32 constant /rows
\ length of the display
\ width of the display
\ allocate display buffer
/lines /rows * buffer: attributes does> swap rot /rows * + chars + ;
\ make it a two dim. array white 5 6 attributes -> ink bc!
red 5 6 attributes -> paper bc!
true 5 6 attributes -> bright bc!
false 5 6 attributes -> flash bc!
\ set colors at line 5, row 6
Download
Anúncio