• convenient stack operation: a b c -- b a c

    From Krishna Myneni@21:1/5 to All on Thu Nov 30 12:55:04 2023
    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
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to mhx on Thu Nov 30 15:12:23 2023
    On 11/30/23 14:39, mhx wrote:
    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


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to All on Thu Nov 30 20:39:31 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Griswold@21:1/5 to Krishna Myneni on Thu Nov 30 12:34:10 2023
    Krishna Myneni <krishna.myneni@ccreweb.org> writes:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Eric Griswold on Thu Nov 30 15:31:16 2023
    On 11/30/23 14:34, Eric Griswold wrote:
    Krishna Myneni <krishna.myneni@ccreweb.org> writes:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Krishna Myneni on Fri Dec 1 01:53:18 2023
    Krishna Myneni wrote:

    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 ).

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ron AARON@21:1/5 to Krishna Myneni on Fri Dec 1 09:19:26 2023
    On 30/11/2023 20:55, Krishna Myneni wrote:
    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
    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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Ron AARON on Fri Dec 1 08:26:25 2023
    Ron AARON wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to krishna.myneni@ccreweb.org on Fri Dec 1 11:04:44 2023
    In article <ukalqa$1fmfg$1@dont-email.me>,
    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    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
    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.

    --
    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 -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to dxf on Sun Apr 14 15:23:52 2024
    dxf wrote:

    On 1/12/2023 9:04 pm, albert wrote:
    In article <ukalqa$1fmfg$1@dont-email.me>,
    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    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:
    [..]
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to All on Sat May 4 12:13:43 2024
    On 5/4/24 10:14, NN wrote:
    On 30/11/2023 6:55 pm, Krishna Myneni wrote:
    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



    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)