Examining C-- ( https://en.wikipedia.org/wiki/C-- ) I noticed interesting idea for a word EXCHANGE ( addr1 addr2 — ). Such word swaps values of two variables. The „inventor” even gave it Forth-friendly name: "><".Still it's that tiny it won't hurt to have it „just in case”.
2 VARIABLE TWO ok
8 VARIABLE EIGHT ok
TWO @ . 2 ok
EIGHT @ . 8 ok
TWO EIGHT >< ok
TWO @ . 8 ok
EIGHT @ . 2 ok
Of course implementation in ML is rather trivial. Actually I'm somewhat surprised it somehow didn't find its place in Forth vocabularies. Probably such mutual exchange of variables' (or memory locations, in general) values doesn't happen that often.
Examining C-- ( https://en.wikipedia.org/wiki/C-- ) I noticed interesting idea for a word EXCHANGE ( addr1 addr2 — ). Such word swaps values of two variables. The „inventor” even gave it Forth-friendly name: "><".Still it's that tiny it won't hurt to have it „just in case”.
2 VARIABLE TWO ok
8 VARIABLE EIGHT ok
TWO @ . 2 ok
EIGHT @ . 8 ok
TWO EIGHT >< ok
TWO @ . 8 ok
EIGHT @ . 2 ok
Of course implementation in ML is rather trivial. Actually I'm somewhat surprised it somehow didn't find its place in Forth vocabularies. Probably such mutual exchange of variables' (or memory locations, in general) values doesn't happen that often.
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;
I have seen that word >< used for byte swapping in MaxForth on 16 bit integers.
I have seen that word >< used for byte swapping in MaxForth on 16 bit integers.
So in your case it was something usually called „flip” rather (swapping lo-byte and hi-byte)?
Forth-79 Reference Word Set:
< n1 -- n2 "byte-swap"Swap the high and low bytes within n1.
'Thinking FORTH' naming conventions:
exchange, especially bytes >name< >MOVE<
It appears F83 authors didn't like it and called it 'FLIP'.
In article <6b1c8486-32b2-4704...@googlegroups.com>,
Zbig <zbigni...@gmail.com> wrote:
Examining C-- ( https://en.wikipedia.org/wiki/C-- ) I noticedToo late. EXCHANGE is used in ciforth for swapping areas:
interesting idea for a word EXCHANGE ( addr1 addr2 — ). Such word
swaps values of two variables. The „inventor” even gave it >Forth-friendly name: "><".
(adr1 adr2 length -- )
Compare to MOVE.
Your new word can be had
: zbig-exchange 1 CELL EXCHANGE ;
Examining C-- ( https://en.wikipedia.org/wiki/C-- ) I noticed
interesting idea for a word EXCHANGE ( addr1 addr2 — ). Such word
swaps values of two variables. The „inventor” even gave it
Forth-friendly name: "><".
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;
Too late. EXCHANGE is used in ciforth for swapping areas:
(adr1 adr2 length -- )
Compare to MOVE.
Your new word can be had
: zbig-exchange 1 CELL EXCHANGE ;
On 27/05/2022 08:38, Brian Fox wrote:
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;Depending on how fast is your 2DUP:
: EXCH ( a b -- ) 2DUP @ SWAP ! SWAP @ SWAP ! ;
If EXCH happens to be preceded by a 2DUP (as it would be in a Qsort) then:
( a b ) OVER @ OVER @ 3 PICK ! OVER ! ( a b )
On Friday, 27 May 2022 at 10:39:36 UTC+1, dxforth wrote:
: EXCH ( a b -- ) 2DUP @ SWAP ! SWAP @ SWAP ! ;
2dup @ swap ! <--- you have corrupted 'a' at this point
{ : exch ( a b -- ) 2dup @ swap @ rot ! swap ! ; }
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;
r swap noopaddi s2,s2,-8 mv a5,s1 addi s8,s8,8
sd s1,$0(s3) addi s10,s10,8ld s1,$0(s2) addi s10,s10,8 swap
r str x27, [x25],#-0x8 mov x27, x0sub x22, x22, #0x8 add x26, x26, #0x8 @
ldr x21, [x25,#0x8]! !ldr x27, [x22],#0x8 add x26, x26, #0x8 add x26, x26, #0x8
r swap mov rdx,$08[r14]add r14,$08 mov rax,$08[r14] mov rax,$10[r14]
On 27/05/2022 08:38, Brian Fox wrote:
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;Depending on how fast is your 2DUP:
: EXCH ( a b -- ) 2DUP @ SWAP ! SWAP @ SWAP ! ;
If EXCH happens to be preceded by a 2DUP (as it would be in a Qsort) then:
( a b ) OVER @ OVER @ 3 PICK ! OVER ! ( a b )
Brian Fox <bria...@brianfox.ca> writes:[..]
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;gforth-fast on RISC-V, using these definitions:
: exch1 over @ over @ >r swap ! r> swap ! ;
: exch2 over @ over @ swap rot ! swap ! ;
: exch3 dup @ 2 pick @ rot ! swap ! ;
iForth:
EXCH1: EXCH2: EXCH3:
pop rbx pop rbx mov rbx, [rsp] qword
pop rdi pop rdi push [rbx] qword
mov rax, [rdi] qword mov rax, [rbx] qword mov rbx, [rsp #16 +] qword
mov rdx, [rbx] qword mov rdx, [rdi] qword pop rdi
mov [ebx] dword, rax mov [ebx] dword, rdx pop rax
mov [edi] dword, rdx mov [edi] dword, rax mov rdx, [rbx] qword
; ; mov [eax] dword, rdx
pop rbx
mov [ebx] dword, rdi
;
iForth does not keep the TOS in a register on word boundaries, and
uses RSP as data stack pointer. Apparently it implements 2 PICK by
first dumping the whole stack into memory.
DB 82H,">","<"+80h
ALIGN 2
DW CAT - 6
EXCHG DW $ + 2
POP BX
MOV AX,[BX]
MOV DX,BX
POP BX
XCHG AX,[BX]
MOV BX,DX
MOV [BX],AX
JMP NEXT
On Friday, May 27, 2022 at 11:39:36 AM UTC+2, dxforth wrote:
On 27/05/2022 08:38, Brian Fox wrote:
On Thursday, May 26, 2022 at 5:30:07 PM UTC-4, NN wrote:Depending on how fast is your 2DUP:
: exch ( a b -- ) over @ over @ >r swap ! r> swap ! ;
A minor point but on ITC Forth's this might be a touch faster by removing one call to NEXT.
( It is on my system)
: EXCH ( addr1 addr2 -- ) OVER @ OVER @ SWAP ROT ! SWAP ! ;
: EXCH ( a b -- ) 2DUP @ SWAP ! SWAP @ SWAP ! ;
If EXCH happens to be preceded by a 2DUP (as it would be in a Qsort) then: >>
( a b ) OVER @ OVER @ 3 PICK ! OVER ! ( a b )
In QSORT I use ( d-addr -- ) DUP 2@ ROT D!
On 28/05/2022 04:38, Marcel Hendrix wrote:[..]
In QSORT I use ( d-addr -- ) DUP 2@ ROT D!While you've posted others versions of Qsort, I couldn't find that one.
Do you have a link?
DI should be free in Fig-Forth
On Saturday, May 28, 2022 at 6:57:41 AM UTC+2, dxforth wrote:
On 28/05/2022 04:38, Marcel Hendrix wrote:[..]
In QSORT I use ( d-addr -- ) DUP 2@ ROT D!While you've posted others versions of Qsort, I couldn't find that one.
Do you have a link?
Sorry, it is bubble-s.frt:
...
: bubble ( -- )
cr ." bubbling... "
#elements
1 DO A-list #elements i - cells
bounds DO i 2@ > IF i 2@ i D! THEN
cell +LOOP
LOOP ;
was also used in my implementation of Hans' Circle sort:
\ Circlesort addr/cells H.Bezemer
defer PRECEDES ( x1 x2 -- f ) \ comparison
-? variable s
-? : c ( a1 a2 -- ) 2dup = if 2drop end
2dup swap begin 2dup u> while
over @ over @ precedes if
over @ over @ 3 pick ! over ! s off
then swap cell- swap cell+
repeat rot 2over 2over - + > if 2swap then
recurse recurse ;
: SORT ( adr siz -- ) dup if 1- cells over +
begin s on 2dup c s @ until then 2drop ; behead s c
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 52:42:34 |
Calls: | 6,712 |
Files: | 12,243 |
Messages: | 5,355,184 |