User Tools

Site Tools


en:pfw:dlshift_drshift

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:pfw:dlshift_drshift [2024-04-03 23:06] – [Contributions] alberten: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 and RSHIFT =====
 <code> <code>
-decimal+decimal 
 : DLSHIFT ( lo hi n -- lo' hi' )    \ n in [0,32*2] : DLSHIFT ( lo hi n -- lo' hi' )    \ n in [0,32*2]
-    dup 32 2* u< +    tuck lshift >r          \ hi' 
-    if 0 ?do d2* loop exit +    2dup 32  
-    then 2drop drop 0. ;+    2dup   lshift >r        \ lower lo to upper hi 
 +    negate rshift >r        \ upper lo to lower hi 
 +    lshift                  \ lo' 
 +    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]
-    dup 32 2* u< +    tuck 2dup rshift >r     \ hi' 
-    if 0 ?do d2/  [ -1 1 rshift ] literal and loop exit +    32 - 
-    then 2drop drop 0. ;+    2dup   rshift >r        \ upper hi to lower lo 
 +    negate lshift >r        \ lower hi to upper lo 
 +    rshift 2r> or or        \ compose lo' 
 +    r>                    \ hi'
 </code> </code>
-===== 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 shift --> 0,   for not in [0,32-1]. More precise: for n in [-32,-1] or in [32,32*2] 
-    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 ( x n -- x) dup 32 u< if lshift exit then 2drop 0 ;
-    lshift                      \ lo' +
-    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' 
-</code> 
-===== Take care ===== 
-We assume for single cell LSHIFT and RSHIFT: 
-<code> 
-1) x n shift --> x     for n=0 
-2) x n shift --> 0     for n not in [0,32-1]  (test your forth) 
-   To be precise: result zero for n in [-32,-1] or in [32,32*2] 
-</code> 
 ===== Test code ===== ===== Test code =====
 <code> <code>
 decimal decimal
-DUHEX. ( ud -- ) <# 32 2/ 0 do # loop #> type space ; +DU.HEX ( ud -- ) base @ >r hex 
-: (TEST) ( ud ud -- ) 2over 2over duhexduhex.+    <# 32 2/ 0 do # loop #>   type space r> base ! 
 +: (TEST) ( ud ud -- ) 2over 2over du.hex du.hex
     cr ." #shifts dlshift--------- drshift---------"     cr ." #shifts dlshift--------- drshift---------"
-    32 2* +   -2+    32 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 duhex. i drshift duhex.+        2over 2over i dlshift du.hex i drshift du.hex
     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) ;
 </code> </code>
 ===== Test results (noForth t) ===== ===== Test results (noForth t) =====
 <code> <code>
-@)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  OK.0+
 </code> </code>
 <code> <code>
-@)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 +     41 0000000000000000 0000000000000000  OK.0
-     42 0000000000000000 0000000000000000  OK.0+
 </code> </code>
 ===== Contributions ===== ===== Contributions =====
Line 243: Line 231:
 <html><h2 style="background-color:yellow">Alternative Implementations</h2></html> <html><h2 style="background-color:yellow">Alternative Implementations</h2></html>
 ===== Coded with D2* and D2/ ===== ===== Coded with D2* and D2/ =====
-Remember that D2/ is in fact an arithmic shift.+an.02apr2024  
 <code> <code>
 decimal decimal
Line 255: Line 244:
     then 2drop drop 0. ;     then 2drop drop 0. ;
 </code> </code>
 +Remember that D2/ is in fact an arithmetic shift.
en/pfw/dlshift_drshift.1712178375.txt.gz · Last modified: 2024-04-03 23:06 by albert