Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #34092

Re: revisiting a prgramming problem

From mhx@iae.nl (Marcel Hendrix)
Subject Re: revisiting a prgramming problem
Newsgroups comp.lang.forth
Message-ID <78081413938433@frunobulax.edu> (permalink)
Date 2014-10-22 21:27 +0200
References <m28hte$gie$1@dont-email.me>
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


"Alex McDonald" writes Re: revisiting a prgramming problem
[..]
> Or (annoying several others in the process)

> : rot {: a b c :} b c a ;

> Optimization should turn both into the same code; it's maxiamlly 4 moves
> and minimally none if moves are optimised away.

The moves would be dropped when ROT is used, not in ROT itself.

Hopefully annoying all trolls and a few local-haters ...

-marcel

-- -----------
ANEW -optims 

     \ *** note equivalent code 

: rot2 ( a b c -- b c a ) >r swap r> swap ;
FORTH> ' rot2 idis
$01221480  : rot2
$0122148A  pop           rbx
$0122148B  pop           rdi
$0122148C  pop           rax
$0122148D  push          rdi
$0122148E  push          rbx
$0122148F  push          rax
$01221490  ;

: rot3 ( a b c -- b c a ) PARAMS| a b c |  b c a ;
FORTH> ' rot3 idis
$01223D00  : rot3
$01223D0A  pop           rbx
$01223D0B  pop           rdi
$01223D0C  pop           rax
$01223D0D  push          rdi
$01223D0E  push          rbx
$01223D0F  push          rax
$01223D10  ;

      \ *** various sum-of-squares proposals

      \ 28 ins, 2 compares

: 2SORT 2DUP < IF SWAP THEN ;
: 2MAX 2SORT ROT 2SORT DROP ;
: SQSUM 2MAX DUP * SWAP DUP * + ;

FORTH> ' SQSUM idis
$01223EC0  : SQSUM
$01223ECA  pop           rbx
$01223ECB  pop           rdi
$01223ECC  cmp           rbx, rdi
$01223ECF  push          rdi
$01223ED0  jle           $01223EDE offset NEAR
$01223ED6  pop           rdi
$01223ED7  mov           rcx, rbx
$01223EDA  mov           rbx, rdi
$01223EDD  push          rcx
$01223EDE  pop           rdi
$01223EDF  pop           rax
$01223EE0  cmp           rax, rbx
$01223EE3  push          rdi
$01223EE4  push          rbx
$01223EE5  mov           rbx, rcx
$01223EE8  mov           rcx, rax
$01223EEB  mov           rbx, rcx
$01223EEE  jle           $01223EFC offset NEAR
$01223EF4  pop           rdi
$01223EF5  mov           rcx, rbx
$01223EF8  mov           rbx, rdi
$01223EFB  push          rcx
$01223EFC  pop           rbx
$01223EFD  imul          rbx, rbx
$01223F01  pop           rdi
$01223F02  imul          rdi, rdi
$01223F06  lea           rbx, [rbx rdi*1] qword
$01223F0A  push          rbx
$01223F0B  ;

      \ 30 ins, 2 jumps

: ?swap ( a b -- smaller bigger ) 2dup > if swap then ;
: 2biggest ( a b c -- n n ) ?swap >r ?swap nip r> ;
: ^2 ( n -- n ) dup * ;
: sos ( a b c -- n ) 2biggest ^2 swap ^2 + ; \ Paul Rubin

FORTH> ' sos idis
$01224140  : sos
$0122414A  pop           rbx
$0122414B  pop           rdi
$0122414C  cmp           rbx, rdi
$0122414F  push          rdi
$01224150  jge           $0122415E offset NEAR
$01224156  pop           rdi
$01224157  mov           rcx, rbx
$0122415A  mov           rbx, rdi
$0122415D  push          rcx
$0122415E  pop           rdi
$0122415F  pop           rax
$01224160  cmp           rdi, rax
$01224163  lea           rbp, [rbp -8 +] qword
$01224167  mov           [rbp 0 +] qword, rbx
$0122416B  push          rax
$0122416C  mov           rbx, rcx
$0122416F  mov           rcx, rdi
$01224172  mov           rbx, rcx
$01224175  jge           $01224183 offset NEAR
$0122417B  pop           rdi
$0122417C  mov           rcx, rbx
$0122417F  mov           rbx, rdi
$01224182  push          rcx
$01224183  pop           rdi
$01224184  mov           rdi, [rbp 0 +] qword
$01224188  lea           rbp, [rbp 8 +] qword
$0122418C  imul          rdi, rdi
$01224190  imul          rbx, rbx
$01224194  lea           rbx, [rdi rbx*1] qword
$01224198  push          rbx
$01224199  ;

      \ 26 ins, 1 jump

: sumofsquare 2dup > if swap then over max dup * >r max dup * r> + ; \ Beez
FORTH> ' sumofsquare idis
$01224200  : sumofsquare
$0122420A  pop           rbx
$0122420B  pop           rdi
$0122420C  cmp           rbx, rdi
$0122420F  push          rdi
$01224210  jge           $0122421E offset NEAR
$01224216  pop           rdi
$01224217  mov           rcx, rbx
$0122421A  mov           rbx, rdi
$0122421D  push          rcx
$0122421E  mov           rdi, [rsp] qword
$01224222  mov           rcx, rbx
$01224225  mov           rbx, rdi
$01224228  cmp           rbx, rcx
$0122422B  cmovl         rbx, rcx
$0122422F  imul          rbx, rbx
$01224233  lea           rbp, [rbp -8 +] qword
$01224237  mov           [rbp 0 +] qword, rbx
$0122423B  pop           rbx
$0122423C  pop           rcx
$0122423D  cmp           rbx, rcx
$01224240  cmovl         rbx, rcx
$01224244  imul          rbx, rbx
$01224248  mov           rdi, [rbp 0 +] qword
$0122424C  lea           rbp, [rbp 8 +] qword
$01224250  lea           rbx, [rbx rdi*1] qword
$01224254  push          rbx
$01224255  ;

: msquare ( n -- d )  \ mixed-precision integer square
  dup m* ;

: minmax ( n1 n2 -- n1 n2 | n2 n1 )  \ bigger comes out atop
  2dup > if swap then ;

: msum_of_squares ( n1 n2 n3 -- d )  \ mixed-precision solution
  minmax >r        \ move bigger of n2 and n3 to r-stack
  max              \ discard smaller (=smallest overall) number
  msquare r> msquare d+  \ calculate result
; \ Alexander Wegel

      \ 35 ins, 1 jump and double precision output

FORTH> ' msum_of_squares idis
$012243C0  : msum_of_squares
$012243CA  pop           rbx
$012243CB  pop           rdi
$012243CC  cmp           rbx, rdi
$012243CF  push          rdi
$012243D0  jge           $012243DE offset NEAR
$012243D6  pop           rdi
$012243D7  mov           rcx, rbx
$012243DA  mov           rbx, rdi
$012243DD  push          rcx
$012243DE  lea           rbp, [rbp -8 +] qword
$012243E2  mov           [rbp 0 +] qword, rbx
$012243E6  pop           rbx
$012243E7  pop           rcx
$012243E8  cmp           rbx, rcx
$012243EB  cmovl         rbx, rcx
$012243EF  mov           rax, rbx
$012243F2  mov           rdi, rbx
$012243F5  xchg          rdx, rdi
$012243F8  imul          rdx
$012243FB  xchg          rdx, rdi
$012243FE  mov           rbx, [rbp 0 +] qword
$01224402  lea           rbp, [rbp 8 +] qword
$01224406  push          rax
$01224407  push          rdi
$01224408  mov           rcx, rbx
$0122440B  mov           rax, rcx
$0122440E  imul          rbx
$01224411  mov           rbx, rdx
$01224414  mov           rcx, rax
$01224417  pop           rdi
$01224418  pop           rax
$01224419  add           rax, rcx
$0122441C  adc           rbx, rdi
$0122441F  push          rax
$01224420  push          rbx
$01224421  ;

But the 'best' (defined as: no jumps/compares) is of course:

      \ 22 ins, 0 jumps

: sqr ( a -- a^2 ) dup * ;
: suosq ( a b c -- d ) 
  sqr swap sqr rot sqr 3dup ( -- a^2 b^2 c^2 a^2 b^2 c^2 )
  + >r + r> max 
    >r + r> max ;

FORTH> ' suosq idis
$01224E40  : suosq
$01224E4A  pop           rbx
$01224E4B  imul          rbx, rbx
$01224E4F  pop           rdi
$01224E50  imul          rdi, rdi
$01224E54  pop           rax
$01224E55  imul          rax, rax
$01224E59  push          rbx
$01224E5A  push          rdi
$01224E5B  lea           rbx, [rax rbx*1] qword
$01224E5F  push          rbx
$01224E60  lea           rbx, [rdi rax*1] qword
$01224E64  pop           rcx
$01224E65  cmp           rbx, rcx
$01224E68  cmovl         rbx, rcx
$01224E6C  pop           rdi
$01224E6D  pop           rax
$01224E6E  lea           rdi, [rax rdi*1] qword
$01224E72  push          rdi
$01224E73  pop           rcx
$01224E74  cmp           rbx, rcx
$01224E77  cmovl         rbx, rcx
$01224E7B  push          rbx
$01224E7C  ;


Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

revisiting a prgramming problem gavino_himself <visploveslisp@gmail.com> - 2014-10-14 15:55 -0700
  Re: revisiting a prgramming problem hughaguilar96@yahoo.com - 2014-10-14 18:52 -0700
    Re: revisiting a prgramming problem Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 17:58 +0200
      Re: revisiting a prgramming problem Paul Rubin <no.email@nospam.invalid> - 2014-10-21 09:09 -0700
        Re: revisiting a prgramming problem Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 22:18 +0200
          Re: revisiting a prgramming problem rickman <gnuarm@gmail.com> - 2014-10-23 15:44 -0400
            Re: revisiting a prgramming problem "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-23 17:01 -0400
            Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 21:33 -0800
              Re: revisiting a prgramming problem rickman <gnuarm@gmail.com> - 2014-11-29 20:40 -0500
                Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-12-01 22:43 -0800
                Re: revisiting a prgramming problem rickman <gnuarm@gmail.com> - 2014-12-02 02:47 -0500
                Re: revisiting a prgramming problem Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-02 11:53 +0000
                Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-12-02 05:23 -0800
                Re: revisiting a prgramming problem rickman <gnuarm@gmail.com> - 2014-12-02 09:01 -0500
        Re: revisiting a prgramming problem Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 22:25 +0200
          Re: revisiting a prgramming problem Paul Rubin <no.email@nospam.invalid> - 2014-10-21 16:10 -0700
            Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 21:41 -0800
        Re: revisiting a prgramming problem Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 22:30 +0200
          Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-12-01 22:45 -0800
        Re: revisiting a prgramming problem Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 23:04 +0200
      Re: revisiting a prgramming problem Mark Wills <markwills1970@gmail.com> - 2014-10-21 10:17 -0700
        Re: revisiting a prgramming problem "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-22 02:13 -0400
          Re: revisiting a prgramming problem Paul Rubin <no.email@nospam.invalid> - 2014-10-21 23:26 -0700
            Re: revisiting a prgramming problem "Alex McDonald" <blog@rivadpm.com> - 2014-10-22 16:18 +0100
              Re: revisiting a prgramming problem mhx@iae.nl (Marcel Hendrix) - 2014-10-22 21:27 +0200
                Re: revisiting a prgramming problem albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-24 10:30 +0000
                Optimal comparisons for m maximums from n items  (was Re: revisiting a prgramming problem) "Alex McDonald" <blog@rivadpm.com> - 2014-10-24 16:29 +0100
                Re: Optimal comparisons for m maximums from n items  (was Re: revisiting a prgramming problem) albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-25 08:27 +0000
                Re: Optimal comparisons for m maximums from n items (was Re: revisiting a prgramming problem) "Alex McDonald" <blog@rivadpm.com> - 2014-10-25 18:54 +0100
                Re: revisiting a prgramming problem mhx@iae.nl (Marcel Hendrix) - 2014-10-25 13:28 +0200
                Re: revisiting a prgramming problem mhx@iae.nl (Marcel Hendrix) - 2014-10-25 14:28 +0200
                Re: revisiting a prgramming problem mhx@iae.nl (Marcel Hendrix) - 2014-10-25 15:17 +0200
            Re: revisiting a prgramming problem Gerry Jackson <spam@qlikz.org> - 2014-10-22 21:57 +0100
              Re: revisiting a prgramming problem Paul Rubin <no.email@nospam.invalid> - 2014-10-22 14:06 -0700
                Re: revisiting a prgramming problem Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-23 00:03 +0200
                Re: revisiting a prgramming problem albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-23 14:46 +0000
                Re: revisiting a prgramming problem Paul Rubin <no.email@nospam.invalid> - 2014-10-23 08:16 -0700
                Re: revisiting a prgramming problem "Alex McDonald" <blog@rivadpm.com> - 2014-10-23 19:21 +0100
                Re: revisiting a prgramming problem "Alex McDonald" <blog@rivadpm.com> - 2014-10-23 20:02 +0100
                Re: revisiting a prgramming problem albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-23 20:29 +0000
                Re: revisiting a prgramming problem Coos Haak <chforth@hccnet.nl> - 2014-10-23 22:23 +0200
              Re: revisiting a prgramming problem awegel@arcor.de (Alexander Wegel) - 2014-10-23 00:36 +0200
                Re: revisiting a prgramming problem Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-24 10:44 +0100
              Re: revisiting a prgramming problem hughaguilar96@yahoo.com - 2014-10-22 17:26 -0700
                Re: revisiting a prgramming problem Michael Barry <barrym95838@yahoo.com> - 2014-10-23 00:02 -0700
          Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 23:10 -0800
        Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 21:42 -0800
  Re: revisiting a prgramming problem Roger Levy <roger.levy@gmail.com> - 2014-10-15 08:18 -0700
    Re: revisiting a prgramming problem Roger Levy <roger.levy@gmail.com> - 2014-10-15 08:18 -0700
      Re: revisiting a prgramming problem november.nihal@gmail.com - 2014-10-15 08:57 -0700
      Re: revisiting a prgramming problem Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-15 17:10 +0100
      Re: revisiting a prgramming problem "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-17 21:53 -0400
    Re: revisiting a prgramming problem Roger Levy <roger.levy@gmail.com> - 2014-10-15 08:19 -0700
  Re: revisiting a prgramming problem awegel@arcor.de (Alexander Wegel) - 2014-10-22 00:30 +0200
    Re: revisiting a prgramming problem hughaguilar96@yahoo.com - 2014-10-21 16:07 -0700
      Re: revisiting a prgramming problem awegel@arcor.de (Alexander Wegel) - 2014-10-22 03:23 +0200
        Re: revisiting a prgramming problem "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-22 02:15 -0400
          Re: revisiting a prgramming problem awegel@arcor.de (Alexander Wegel) - 2014-10-22 12:52 +0200
        Re: revisiting a prgramming problem hughaguilar96@yahoo.com - 2014-10-22 17:19 -0700
          Re: revisiting a prgramming problem awegel@arcor.de (Alexander Wegel) - 2014-10-23 16:10 +0200
      Re: revisiting a prgramming problem "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-22 02:14 -0400
      Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 23:21 -0800
      Re: revisiting a prgramming problem "WJ" <w_a_x_man@yahoo.com> - 2014-12-06 02:34 +0000
      Re: revisiting a prgramming problem "WJ" <w_a_x_man@yahoo.com> - 2014-12-27 08:00 +0000
        Re: revisiting a prgramming problem Mark Wills <markwills1970@gmail.com> - 2014-12-27 05:15 -0800
  Re: revisiting a prgramming problem "WJ" <w_a_x_man@yahoo.com> - 2014-11-29 17:20 +0000
    Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-11-29 12:00 -0800
    Re: revisiting a prgramming problem "WJ" <w_a_x_man@yahoo.com> - 2014-12-02 16:15 +0000
  Re: revisiting a prgramming problem Paul Sephton <prsephton@gmail.com> - 2014-12-02 05:06 -0800
    Re: revisiting a prgramming problem Paul Sephton <prsephton@gmail.com> - 2014-12-02 05:14 -0800
    Re: revisiting a prgramming problem the_gavino_himself <visphatesjava@gmail.com> - 2014-12-02 05:24 -0800

csiph-web