Discussion:
convenient stack operation: a b c -- b a c
Add Reply
Krishna Myneni
2023-11-30 18:55:04 UTC
Reply
Permalink
I'm in need of a convenient way to do the following for the fp stack,
but we can pose the problem for the data stack:

a b c -- b a c

Two ways to do this on the data stack are

ROT SWAP
R SWAP R>
( the latter can't be done from the interpreter, portably at least ).

Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?

SWAP-UNDER
ROTSWAP
RISE

For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.

Maybe a generic swap operation at depth u, called RISE, is useful e.g.


0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
etc.


Comments?

--
Krishna Myneni
Eric Griswold
2023-11-30 20:34:10 UTC
Reply
Permalink
Post by Krishna Myneni
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
Speaking entirely as a wholly inexpert Forth aficionado, I like both the
name 'RISE' and function a lot.

Eric
Krishna Myneni
2023-11-30 21:31:16 UTC
Reply
Permalink
Post by Eric Griswold
Post by Krishna Myneni
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
Speaking entirely as a wholly inexpert Forth aficionado, I like both the
name 'RISE' and function a lot.
It occurred to me after I posted the above, that maybe 0 RISE should do
nothing, 1 RISE should be the same as SWAP, etc. in order to correspond
to the meaning of RISE. The argument to rise is the depth of the element
relative to the top of the stack (TOS) which should move closer by 1
cell to TOS.

--
Krishna
dxf
2023-12-01 00:51:28 UTC
Reply
Permalink
Post by Eric Griswold
Post by Krishna Myneni
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
Speaking entirely as a wholly inexpert Forth aficionado, I like both the
name 'RISE' and function a lot.
That would almost certainly give rise to accusations of 'stack juggling' :)
mhx
2023-11-30 20:39:31 UTC
Reply
Permalink
In a corpus of about 8000 iForth files, "FROT FSWAP" appears 9 times:

Searching for: FROT FSWAP
D:dfwforthincludecomplex.frt(231): : X- FROT FSWAP F- <i1> <r1> <i2> <r2> --- <i3> <r3>
D:dfwforthincludecplx_fsl.frt(175): FROT FSWAP F- -FROT
D:dfwforthincludecplx_fsl.frt(210): FROT FSWAP FOVER F*
D:dfwforthincludefft.frt(66): : X- FROT FSWAP F- -FROT F- FSWAP ; PRIVATE
D:dfwforthincludemiscutil.frt(1761): FROT FSWAP F- -FROT F- FSWAP ;
D:dfwforthincludepcylfun.frt(259): FROT FSWAP FOVER F+
D:dfwforthincludepcylfun.frt(270): FROT FSWAP FOVER F+
D:dfwforthexamplesgraphicshenon.frt(71): y{ 0 1 :: }range[] FROT FSWAP SET-GWINDOW
D:dfwforthexamplesgraphicskrawczyk.frt(127): y{ 0 1 :: }range[] FROT FSWAP SET-GWINDOW

-marcel
Krishna Myneni
2023-11-30 21:12:23 UTC
Reply
Permalink
Post by mhx
Searching for: FROT FSWAP
D:dfwforthincludecomplex.frt(231): : X- FROT FSWAP F-  <i1> <r1> <i2>
<r2> --- <i3> <r3>
D:dfwforthincludecplx_fsl.frt(175): FROT FSWAP F- -FROT
D:dfwforthincludecplx_fsl.frt(210): FROT FSWAP FOVER F*
D:dfwforthincludefft.frt(66): : X-    FROT FSWAP F- -FROT F- FSWAP ;
PRIVATE
D:dfwforthincludemiscutil.frt(1761): FROT FSWAP F- -FROT F- FSWAP ;
D:dfwforthincludepcylfun.frt(259): FROT FSWAP FOVER F+
D:dfwforthincludepcylfun.frt(270): FROT FSWAP FOVER F+
D:dfwforthexamplesgraphicshenon.frt(71): y{ 0 1 :: }range[] FROT FSWAP SET-GWINDOW
D:dfwforthexamplesgraphicskrawczyk.frt(127): y{ 0 1 :: }range[] FROT FSWAP SET-GWINDOW
That's not as many instances as I would have expected. I expect you
don't see it as much because you are probably using floating point locals?

--
Krishna
minforth
2023-12-01 01:53:18 UTC
Reply
Permalink
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
a b c -- b a c
Two ways to do this on the data stack are
ROT SWAP
R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
In a 64-bit Forth, if the return stack does not allow it, you could
easily use the data stack to hold 64-bit fp numbers (a union in C parlance).
Otherwise I use F>R and FR> from time to time, or would use a scratch
FVARIABLE, or fp locals. Performance-wise, FROT FSWAP should be the fastest
for such a tiny task.
Ron AARON
2023-12-01 07:19:26 UTC
Reply
Permalink
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
   a b c -- b a c
Two ways to do this on the data stack are
  ROT SWAP
  >R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE   \ same as SWAP
1 RISE   \ i*x a b c -- i*x b a c
2 RISE   \ i*x a b c d -- i*x b a c d
etc.
Comments?
--
Krishna Myneni
In 8th there's "_swap" which does "a b c -- b a c"
minforth
2023-12-01 08:26:25 UTC
Reply
Permalink
Post by Ron AARON
In 8th there's "_swap" which does "a b c -- b a c"
With my ouput locals I could define it quickly by

: _SWAP { a b c == b a c } ;

or for fp numbers

: _FSWAP { f: a b c == b a c } ;

If used more often, a new primitive would be better.
none) (albert
2023-12-01 10:04:44 UTC
Reply
Permalink
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
a b c -- b a c
Two ways to do this on the data stack are
ROT SWAP
R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
etc.
Comments?
It is insane. I'm reading through your programs and I
encounter SWAP-UNDER .
It involves stack operations, so there is no way you
can understand the code unless you have looked up
this words.

Remember the washing machine example of Brodie?
All stack manipulations are hidden.
The program proceeds with normal words that describe
the actions that are done.

You already have three items on the stack, that is
too much. Unless it is actally a double item,
say a string, and a single item.
Post by Krishna Myneni
--
Krishna Myneni
Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -
dxf
2023-12-02 00:39:15 UTC
Reply
Permalink
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
a b c -- b a c
Two ways to do this on the data stack are
ROT SWAP
R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
etc.
[...]
I'm reading through your programs and I encounter SWAP-UNDER .
It involves stack operations, so there is no way you
can understand the code unless you have looked up
this words.
Looking at various sources, including my own, ROT SWAP is not a common
sequence. Indeed, I would say it is exceptional. Scanning SwiftForth
sources and libs it is used once. In my case it was a setup for CMOVE
which I used on a couple occasions:

( dest src len) <process-source>
( dest src' len') ROT SWAP CMOVE ( save result)

I could have written:

( src len dst) >R <process-source>
( src' len') R> SWAP CMOVE ( save result)

but it would have been costlier. So I've no regrets.
Remember the washing machine example of Brodie?
All stack manipulations are hidden.
The program proceeds with normal words that describe
the actions that are done.
If you mean factoring as a way of improving readability and hiding
'noise words', yes. But we're told define words used only once is
a 'waste' of namespace. This may be a learned response. If one
sees an algorithm written in C in one chunk, the temptation is to
duplicate that.
You already have three items on the stack, that is
too much. Unless it is actally a double item,
say a string, and a single item.
The presence of ROT SWAP ought to raise a red flag as it suggests
something is awry. After examining the code we may conclude its
use in this case was the least worst option.
mhx
2024-04-14 15:23:52 UTC
Reply
Permalink
Post by dxf
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
[..]
Post by dxf
The presence of ROT SWAP ought to raise a red flag as it suggests
something is awry. After examining the code we may conclude its
use in this case was the least worst option.
I can only find it 50 times in 1352 files. It seems to pop up
when there are 3 strong-coupled parameters for a certain operation
(like your MOVE).

The classic HANOI demo uses it:

hanoi.frt(53): -rot swap r@ 1- recurse \ extra dest source N-1
hanoi.frt(55): -rot swap r>
hanoi.frt(87): >r -rot swap r> 1- recurse exit \ extra dest source N-1
hanoi.frt(359): -ROT SWAP R@ 1- RECURSE \ EXTRA DEST SOURCE n-1
hanoi.frt(361): -ROT SWAP R>
hanoi.frt(375): >R -ROT SWAP R> 1- RECURSE \ EXTRA DEST SOURCE n-1
hanoi.frt(507): . -ROT SWAP R@ 1- RECURSE \ EXTRA DEST SOURCE n-1
hanoi.frt(509): . -ROT SWAP R>

The TAK test has the same coupling problem:

tak.frt(8): : tak2 ( x y z -- y1- z x ) swap 1- -rot swap ;

This classic utility (almost) mirrors your example:

blocks\editor.frt(109): -ROT SWAP CMOVE

Classic integer FFT

numeric\fft.frt(22): : X- ROT SWAP - -ROT - SWAP ; ( cplx1 cplx2 -- cplx3 )

Somebody remembers SOD64 ?

sod64\kernel.frt(236): R> ROT ROT SWAP >R >R >R ;
sod64\kernel.frt(240): OVER OVER - IF R> ROT ROT SWAP >R >R 8 + >R
sod64\kernel.frt(814): 1+ ROT ROT SWAP 1- SWAP
sod64\kernel.frt(825): 1+ ROT ROT SWAP 1- SWAP

Too low a frequency to make it a new word (and then having to look it up).

-marcel
Hans Bezemer
2024-04-21 13:51:18 UTC
Reply
Permalink
Post by mhx
I can only find it 50 times in 1352 files. It seems to pop up
when there are 3 strong-coupled parameters for a certain operation
(like your MOVE).
Same thing here -even lower- about 20 times in 900 files. Curiously, 25%
of them as a part of "-rot swap". With that frequency, I see little use
of adding a word like "RISE". As a metaphor for certain stack
operations, I don't think it's very compelling.

I mean - a stack operator is like telling a story:
- drop the 2OS item;
- duplicate 2OS to 2OS;
- duplicate TOS to 3OS;
- swap 3OS with TOS.

I can all easily imagine uses for that. "SWAP 3OS with 2OS" isn't
telling me such a story. Sorry.

Hans Bezemer
Hans Bezemer
2024-05-17 12:08:30 UTC
Reply
Permalink
Post by Hans Bezemer
Post by mhx
I can only find it 50 times in 1352 files. It seems to pop up
when there are 3 strong-coupled parameters for a certain operation
(like your MOVE).
Same thing here -even lower- about 20 times in 900 files. Curiously, 25%
of them as a part of "-rot swap". With that frequency, I see little use
of adding a word like "RISE". As a metaphor for certain stack
operations, I don't think it's very compelling.
- drop the 2OS item;
- duplicate 2OS to 2OS;
- duplicate TOS to 3OS;
- swap 3OS with TOS.
I can all easily imagine uses for that. "SWAP 3OS with 2OS" isn't
telling me such a story. Sorry.
Hans Bezemer
Yes, both Marcel and I saw a LARGE part of the "ROT SWAP" sequence was
part of "-ROT SWAP". I recently introduced : SPIN SWAP ROT ; in 4tH.

You know what? It's equivalent to "-ROT SWAP"..
I'll leave you to your thoughts..

Hans Bezemer

NN
2024-05-04 15:14:08 UTC
Reply
Permalink
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
   a b c -- b a c
Two ways to do this on the data stack are
  ROT SWAP
  >R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE   \ same as SWAP
1 RISE   \ i*x a b c -- i*x b a c
2 RISE   \ i*x a b c d -- i*x b a c d
etc.
Comments?
--
Krishna Myneni
Since no one suggested it, in factor they have called it swapd ( x y z
-- y x z )

https://docs.factorcode.org/content/word-swapd%2Ckernel.html

Using the same convention you could call it fswapd for operating on the
floating stack.
Krishna Myneni
2024-05-04 17:13:43 UTC
Reply
Permalink
Post by NN
Post by Krishna Myneni
I'm in need of a convenient way to do the following for the fp stack,
    a b c -- b a c
Two ways to do this on the data stack are
   ROT SWAP
   >R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R
and FR> to push and pop from the floating point stack to/from the
return stack. A significant reason for suggesting the F>R FR> pair is
to deal with this situation where having to use FROT FSWAP would be
inefficient on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE   \ same as SWAP
1 RISE   \ i*x a b c -- i*x b a c
2 RISE   \ i*x a b c d -- i*x b a c d
etc.
Comments?
--
Krishna Myneni
Since no one suggested it, in factor they have called it swapd ( x y z
-- y x z )
https://docs.factorcode.org/content/word-swapd%2Ckernel.html
Using the same convention you could call it fswapd for operating on the
floating stack.
Thanks for the suggestion. It's nice to have a word name with some
history of use.

--
KM
Loading...