en:pfw:dlshift_drshift
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:pfw:dlshift_drshift [2024-04-03 23:02] – [The code] albert | en:pfw:dlshift_drshift [2024-04-09 14:41] (current) – [Test results (noForth t)] albert | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Shifting doubles ===== | + | ===== Shifting doubles: DLSHIFT and DRSHIFT |
an.02apr2024 | an.02apr2024 | ||
Line 6: | Line 6: | ||
The code is for 32bit cells, to keep it readable. | The code is for 32bit cells, to keep it readable. | ||
For other cell widths adjust the number 32 everywhere. | For other cell widths adjust the number 32 everywhere. | ||
- | ===== Coded with D2* and D2/ ===== | + | |
- | Remember that D2/ is in fact an arithmic shift. | + | ===== Coded with LSHIFT |
< | < | ||
- | decimal | + | decimal |
: DLSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | : DLSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | ||
- | | + | |
- | | + | 2dup 32 - |
- | | + | |
+ | | ||
+ | lshift | ||
+ | 2r> r> or or ; \ compose hi' | ||
+ | | ||
: DRSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | : DRSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | ||
- | | + | |
- | | + | |
- | | + | 2dup |
+ | negate lshift >r \ lower hi to upper lo | ||
+ | | ||
+ | | ||
</ | </ | ||
- | ===== Coded with LSHIFT and RSHIFT | + | ===== Possibly a problem |
- | <code> | + | We assume for single cell LSHIFT and RSHIFT: |
- | decimal : DLSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | + | - x n shift --> x, for n=0 |
- | tuck lshift >r \ hi' | + | - x n shift --> 0, |
- | 2dup 32 - negate rshift >r \ upper lo to lower hi | + | Test your forth. If 2. does not hold: |
- | 2dup 32 - lshift >r \ lower lo to upper hi | + | |
- | lshift | + | |
- | 2r> r> or or ; \ build hi' | + | |
- | decimal : DRSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | ||
- | tuck 2dup rshift >r \ hi' | ||
- | 2dup 32 - negate lshift >r \ lower hi to upper lo | ||
- | 32 - rshift >r \ upper hi to lower lo | ||
- | rshift 2r> or or \ build lo' | ||
- | r> ; \ hi' | ||
- | </ | ||
- | ===== Take care ===== | ||
- | We assume for single cell LSHIFT and RSHIFT: | ||
- | < | ||
- | 1) x n shift --> x for n=0 | ||
- | 2) x n shift --> 0 for n not in [0, | ||
- | To be precise: result zero for n in [-32,-1] or in [32,32*2] | ||
- | </ | ||
===== Test code ===== | ===== Test code ===== | ||
< | < | ||
decimal | decimal | ||
- | : DUHEX. ( ud -- ) <# 32 2/ 0 do # loop #> type space ; | + | : DU.HEX ( ud -- ) base @ >r hex |
- | : (TEST) ( ud ud -- ) 2over 2over duhex. duhex. | + | |
+ | : (TEST) ( ud ud -- ) 2over 2over du.hex du.hex | ||
cr ." #shifts dlshift--------- drshift---------" | cr ." #shifts dlshift--------- drshift---------" | ||
- | 32 2* 3 + -2 | + | 32 2* 2 + -2 |
do cr i 3 and 0= if cr then i 7 .r space | do cr i 3 and 0= if cr then i 7 .r space | ||
- | 2over 2over i dlshift | + | 2over 2over i dlshift |
loop 2drop 2drop ; | loop 2drop 2drop ; | ||
- | : TEST1 -1. 2dup (test) ; | + | : TEST1 ( -- ) -1. 2dup (test) ; |
- | : TEST2 0 -1 dup 1 rshift xor 1. (test) ; | + | : TEST2 ( -- ) 0 -1 dup 1 rshift xor 1. (test) ; |
</ | </ | ||
===== Test results (noForth t) ===== | ===== Test results (noForth t) ===== | ||
< | < | ||
- | @)hex OK.0 | ||
@)test1 FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF | @)test1 FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF | ||
#shifts dlshift--------- drshift--------- | #shifts dlshift--------- drshift--------- | ||
Line 145: | Line 136: | ||
40 0000000000000000 0000000000000000 | 40 0000000000000000 0000000000000000 | ||
- | 41 0000000000000000 0000000000000000 | + | 41 0000000000000000 0000000000000000 OK.0 |
- | 42 0000000000000000 0000000000000000 | + | |
</ | </ | ||
< | < | ||
- | @)hex OK.0 | ||
@)test2 0000000000000001 8000000000000000 | @)test2 0000000000000001 8000000000000000 | ||
#shifts dlshift--------- drshift--------- | #shifts dlshift--------- drshift--------- | ||
Line 236: | Line 225: | ||
40 0000000000000000 0000000000000000 | 40 0000000000000000 0000000000000000 | ||
- | | + | 41 0000000000000000 0000000000000000 |
- | | + | |
</ | </ | ||
===== Contributions ===== | ===== Contributions ===== | ||
< | < | ||
+ | ===== Coded with D2* and D2/ ===== | ||
+ | an.02apr2024 | ||
+ | < | ||
+ | decimal | ||
+ | : DLSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | ||
+ | dup 32 2* u< | ||
+ | if 0 ?do d2* loop exit | ||
+ | then 2drop drop 0. ; | ||
+ | : DRSHIFT ( lo hi n -- lo' hi' ) \ n in [0,32*2] | ||
+ | dup 32 2* u< | ||
+ | if 0 ?do d2/ [ -1 1 rshift ] literal and loop exit | ||
+ | then 2drop drop 0. ; | ||
+ | </ | ||
+ | Remember that D2/ is in fact an arithmetic shift. |
en/pfw/dlshift_drshift.1712178153.txt.gz · Last modified: 2024-04-03 23:02 by albert