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


Groups > comp.lang.forth > #34135

Re: revisiting a prgramming problem

From mhx@iae.nl (Marcel Hendrix)
Subject Re: revisiting a prgramming problem
Newsgroups comp.lang.forth
Message-ID <07078910938433@frunobulax.edu> (permalink)
Date 2014-10-25 14:28 +0200
References <544a2a5e$0$6868$e4fe514c@dreader36.news.xs4all.nl>
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


albert@spenarnc.xs4all.nl (Albert van der Horst) writes Re: revisiting a prgramming problem

> In article <78081413938433@frunobulax.edu>, Marcel Hendrix <mhx@iae.nl> wrote:
> <SNIP>

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

> Correct me of I'm wrong, but getting the squares first,
> seems to fail if any of the numbers is negative.

Who specified that? Anyway, it is only three instructions more:

: suosq ( z y x -- a )
  2dup 
  MAX SQR -rot ( -- z u^2 y x )
  MIN rot  
  MAX SQR + ;

\ 25 ins, no jumps
FORTH> ' suosq idis
$01229480  : sumsq
$0122948A  pop           rbx
$0122948B  mov           rdi, [rsp] qword
$0122948F  push          rbx
$01229490  mov           rcx, rdi
$01229493  cmp           rbx, rcx
$01229496  cmovl         rbx, rcx
$0122949A  imul          rbx, rbx
$0122949E  pop           rdi
$0122949F  pop           rax
$012294A0  push          rbx
$012294A1  mov           rbx, rdi
$012294A4  mov           rcx, rax
$012294A7  cmp           rbx, rcx
$012294AA  cmovg         rbx, rcx
$012294AE  pop           rdi
$012294AF  pop           rax
$012294B0  push          rdi
$012294B1  mov           rcx, rbx
$012294B4  mov           rbx, rax
$012294B7  cmp           rbx, rcx
$012294BA  cmovl         rbx, rcx
$012294BE  imul          rbx, rbx
$012294C2  pop           rdi
$012294C3  lea           rbx, [rdi rbx*1] qword
$012294C7  push          rbx
$012294C8  ;


> As is usual with your benchmarks the conclusion fails.

> What is your opinion, impression or advice based on all the
> work you've done. (Not that it is not appreciated that you
> present results in great detail, and be specific about
> the Forth it runs on. )

My advice is to write critical code as straightforward and clearly as 
possible. Current Forth compilers use all the tricks in the book 
(as demonstrated - remove all stack words, remove branches, remove flags). 
You can't beat these compilers with a CODE definition unless your first 
name is Terje. 
However, always try a few algorithms and check the output of the compiler 
for critical work. For example, I rejected a few algorithms that used 
locals because there were more items than the current iForth compiler 
can handle.

The basic assumption up to now has been that conditional jumps are 
bad for the run-time. I didn't test that here, and it might very well be
wrong. Anyway, I suspect the difference will be so small that it only 
matters for those that are using optimizing compilers (and then in only 
extreme cases).

-marcel

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