-
Notifications
You must be signed in to change notification settings - Fork 2
/
bits.fs
51 lines (40 loc) · 1.76 KB
/
bits.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
\ bit vectors, lsb first
\ Authors: Bernd Paysan, Anton Ertl
\ Copyright (C) 2012,2014,2015,2016,2017,2019 Free Software Foundation, Inc.
\ This file is part of Gforth.
\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU General Public License for more details.
\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.
: bits ( n -- n ) 1 swap lshift ;
: >bit ( addr n -- c-addr mask ) 8 /mod under+ bits ;
: +bit ( addr n -- ) >bit over c@ or swap c! ;
: +bit@ ( addr n -- flag ) >bit over c@ 2dup and >r
or swap c! r> 0<> ;
: -bit ( addr n -- ) >bit invert over c@ and swap c! ;
: -bit@ ( addr n -- flag ) >bit over c@ 2dup and >r
invert or invert swap c! r> 0<> ;
: bit! ( flag addr n -- ) rot IF +bit ELSE -bit THEN ;
: bit@ ( addr n -- flag ) >bit swap c@ and 0<> ;
: bittype ( addr base n -- ) bounds +DO
dup I bit@ '+' '-' rot select emit LOOP drop ;
: bit-erase ( addr off len -- )
dup 8 u>= IF
>r dup 7 and >r 3 rshift + r@ bits 1- over c@ and over c!
1+ 8 r> - r> swap -
dup 7 and >r 3 rshift 2dup erase +
0 r> THEN
bounds ?DO dup I -bit LOOP drop ;
: bit-fill ( addr off len -- )
dup 8 u>= IF
>r dup 7 and >r 3 rshift + r@ bits 1- invert over c@ or over c!
1+ 8 r> - r> swap -
dup 7 and >r 3 rshift 2dup $FF fill +
0 r> THEN
bounds ?DO dup I +bit LOOP drop ;