Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail X-Newsreader: iForth 2.0 console (October 21, 2006) From: mhx@iae.nl (Marcel Hendrix) Subject: Re: revisiting a prgramming problem Newsgroups: comp.lang.forth Message-ID: <07078910938433@frunobulax.edu> Date: Sat, 25 Oct 2014 14:28:28 +0200 References: <544a2a5e$0$6868$e4fe514c@dreader36.news.xs4all.nl> Lines: 82 Organization: SunSITE.dk - Supporting Open source NNTP-Posting-Host: 82.157.147.127 X-Trace: news.sunsite.dk DXC=>D11T In article <78081413938433@frunobulax.edu>, Marcel Hendrix wrote: > >>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