Comments (21)
: struct 0 ;
: end-struct constant ;
: field create over , + does> @ + ;
: superfield create , does> @ + ;
: union 0 ;
: end-union dup constant + ;
: unionfield create max does> @ + ;
( sample usage )
struct
4 field part1
9 superfield part2_3_4
4 field part2
4 field part3
1 field part4
4 superfield part5_6
2 field part5
2 field part6
union
4 unionfield part7
1 unionfield part8
end-union myunion-size
union
2 unionfield part9
end-union myunion2-size
end-struct mystruct-size
: buffer: create allot ;
mystruct-size buffer: mybuf
$AAAAAAAA mybuf part1 !
$BBBBBBBB mybuf part2 !
$CCCCCCCC mybuf part3 !
$DD mybuf part4 !
$EEEE mybuf part5 !
$FFFF mybuf part6 !
mybuf part5 @ hex. cr ( prints EEEE )
mybuf part6 @ hex. cr ( prints FFFF )
$DEADBEEF mybuf part5_6 !
mybuf part5_6 @ hex. cr ( prints DEADBEEF )
EDIT: Fixed to include implementations of actual union
s supporing differently sized members as @forthy42 described them.
from discussion.
I know this is an old post but I will add something in case it is of use to someone.
Lowfatcomputing's struct/union example is so cool. Thanks
This is not bit structures but arrays of bits, but it might provide some ideas for managing bit fields. It seems to work on 16,32 and 64 bit platforms. It is factored for understanding. It's not real fast from using /MOD at runtime, but it's not too bad with native code compilers.
https://github.com/bfox9900/CAMEL99-V2/blob/master/LIB.ITC/BOOLEAN.FTH
Edit: Fixed this link
from discussion.
Nice and a different way to factor it with the BIT word.
Looks very efficient.
2** is 2 to the exponent ** ?
How is that implemented?
It would seem to be similar to RSHIFT, no?
from discussion.
@rdrop-exit, I like that \<name>
convention. I use the -<name>
convention for negating or removing a "name", but \
works in another dimension.
from discussion.
Flags are bits in a cell in Forth. A Nybble is 4 bits. Depends on where
your flags reside in the quibble!
On Tue, Dec 29, 2015 at 10:31 PM, Rick Carlino [email protected]
wrote:
FORTH 200X introduced structs
https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Forth200x-Structures.html.
I can't find any documentation on online or in documentation for the best
way to perform struct packing, though.Is it possible in FORTH (GForth, specifically)? I'm trying build an MQTT
message packet which requires setting flags that are smaller than one byte.—
Reply to this email directly or view it on GitHub
#20.
from discussion.
I remember doing bit fields in the polyForth DataBase Support System (see https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's been a while, and I'm not sure what happened to that support. I'll keep looking.
from discussion.
My ARM Forth includes:
SETBITS [SETBITS](addr val --)
Logical OR the value-bits with the contents of addr using read, modify,
write
method.
( Create and initialize variable TEST_SETBITS )
VARIABLE TEST_SETBITS 110b TEST_SETBITS !
( Use SETBITS to modify TEST_SETBITS )
TEST_SETBITS 10001b SETBITS
( View results )
TEST_SETBITS @ .B ( 10111 )
See CLRBITS and ANDBITS
On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer [email protected]
wrote:
I remember doing bit fields in the polyForth DataBase Support System (see
https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's
been a while, and I'm not sure what happened to that support. I'll keep
looking.—
Reply to this email directly or view it on GitHub
#20 (comment).
from discussion.
Also:
[CLRBITS](addr value)
Logical AND then NOT the value-bits with the contents of addr using read,
modify, write method.
( Create and initialize variable TEST_CLRBITS )
VARIABLE TEST_CLRBITS 110b TEST_CLRBITS !
( Use ANDBITS to modify TEST_CLRBITS )
TEST_CLRBITS 10b CLRBITS
( View results )
TEST_CLRBITS @ .B ( 100 )
See ANDBITS and SETBITS
CMOVE
On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer [email protected]
wrote:
I remember doing bit fields in the polyForth DataBase Support System (see
https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's
been a while, and I'm not sure what happened to that support. I'll keep
looking.—
Reply to this email directly or view it on GitHub
#20 (comment).
from discussion.
Forth83 has !BITS
and @BITS
for storing and fetching bit fields.
from discussion.
I suppose one could come up with a design to support something like this:
begin-structure foo
begin-bitfield
1 +bits a
1 +bits b
2 +bits c
4 +bits d
end-bitfield
end-structure
from discussion.
I have added something like this to mecrisp-stellaris:
http://hub.darcs.net/pointfree/mecrisp-stellaris/browse/mk20dx256/bitstruct-example.txt
http://hub.darcs.net/pointfree/mecrisp-stellaris/browse/mk20dx256/bitstruct.txt
It was based on this Mitch Bradley's, Structured Data with Bit Fields but extended to support fields that contain subfields (I called them superfields).
from discussion.
I like superbits. I feel they should have the superpower to solve any programming problem!
from discussion.
Am Mittwoch, 30. Dezember 2015, 00:06:17 schrieb Lars Brinkhoff:
I suppose one could come up with a design to support something like this:
begin-structure foo begin-bitfield 1 +bits a 1 +bits b 2 +bits c 4 +bits d end-bitfield end-structure
Since we have no nested structs, a bitfield should just be an entity of its
own. But having such bit fields is indeed a good idea.
Union is also just some individual structures, and the size of the union is
simply the maximum of those sizes (no syntactical support).
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o ID: kQusJzA;7_?t=uy@X}1GWr!+0qqp_Cn176t4(dQ_
http://bernd-paysan.de/
from discussion.
The obvious omission to BOOLEAN.FTH is:
: BTOGGLE ( bit# addr[] -- )
BITFLD \ -- bit# addr
SWAP BITMASK >R \ save mask
DUP @ \ -- addr bits
R> XOR SWAP ! ; \ toggle bit, store back in addr
The file has been updated on GITHub
from discussion.
: BTOGGLE ( bit# addr[] -- ) ...
Below is is the factoring that I use for bit toggling , it takes a mask as an argument rather than a particular bit number, so that if needed I can toggle multiple bits simultaneously in a cell.
0 shadow Bitwise Miscellanea 4|4
1
2 set Set <mask> bits in cell at <a>.
3
4 reset Reset <mask> bits in cell at <a>.
5
6 toggle Xor <mask> bits in cell at <a>.
0 source Bitwise Miscellanea 4|4
1
2 : set ( mask a -- ) tuck @ or \! ;inline
3 : reset ( mask a -- ) tuck @ cand \! ;inline
4 : toggle ( mask a -- ) tuck @ xor \! ;inline
(\!
is a primitive instruction equivalent to swap !
)
When I want to use a particular bit number, as is the case with your btoggle
above, rather than a mask, I write <bit#> bit <addr> toggle
, where bit
is defined as follows:
: bit ( # -- bit ) 2** ;inline
I also have mask
defined as:
: mask ( bit -- mask ) 1- ;inline
So I can write <#bits> bit mask <addr> toggle
to toggle the low <#bits>.
from discussion.
Thanks.
2** is 2 to the exponent ** ?
That's right, it raises 2 to a power.
How is that implemented?
It would seem to be similar to RSHIFT, no?
It's a primitive on my Forth, but in high-level Forth it would be equivalent to:
: 2** ( u -- 1<<u ) 1 swap lshift ;
from discussion.
from discussion.
from discussion.
from discussion.
O.T.: Mark, is your ! word (in your post, quoted
below) in common usage (even if it's not adopted into any
standard), with that name? I ask because I use the same primitive
in my 65816 kernel, but called SWAP! . It's headerless
and is not in any applications so far, so I could easily change
the name to whatever is in common usage.
Garth
I generally use \<name>
to indicate an alternative 'reversed' version of the primitive <name>
where the normal argument order is either reversed or permuted in some way. It's not common usage as far as I know. It could be that I copied it from some other Forth long ago, but I can't recall.
Some other examples of this convention would be reverse subtraction \- ( x1 x2 -- x2-x1 )
, and two different 2-to-1 mux primitives mux ( x1 x2 mask -- x )
and \mux ( mask x1 x2 -- x )
.
from discussion.
from discussion.
Related Issues (20)
- Thinking Forth
- EuroForth 2021 Invitation: 10.-12. September
- A peculiar and fun Forth like compiler targeting bash functions, recently made available. HOT 48
- Exception handling via a condition system : possible? HOT 2
- JForth reboot?
- ChatFORTH - chat with OpenAI ChatGPT with power of FORTH HOT 2
- Invitation: 39th EuroForth in Roma, 15.-17. September 2023
- Looking for Super Forth 64 Disk images (D64/G64) HOT 1
- Conditionals design and implementation HOT 8
- Desktop Forth system requirements HOT 2
- Any good formatters? HOT 5
- Poll: preference of case-sensitivity HOT 22
- Are there any Posit implementations out there? HOT 6
- Is the bot StarForth off ? HOT 1
- Compilation JIT HOT 13
- Why Forth Isn't Slow (Forth Dimension Volume 06 Number 5) (archive.org)
- [META] GitHub Discussions are now generally available HOT 12
- Minimal set of low level words to build forth HOT 48
- Macintosh Forths
- VolksForth HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discussion.