Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.forth    |    Forth programmers eat a lot of Bratwurst    |    117,927 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 117,034 of 117,927    |
|    Anton Ertl to Paul Rubin    |
|    Re: Back & Forth - Co-routines    |
|    01 Feb 25 07:26:11    |
   
   From: anton@mips.complang.tuwien.ac.at   
      
   Paul Rubin writes:   
   >This seems more in the locals spirit:   
   >   
   >: blend { a x b -- n } 100 b x - b a - */ ;   
   >: tri_mf3.1 { x a b c -- mf }   
   > a x <= x b < AND IF b x a blend EXIT THEN   
   > b x <= x c < AND IF b x c blend EXIT THEN   
   > 0 ;   
      
   Forth has a word WITHIN that should come in handy here:   
      
   : tri_mf3.1 { x a b c -- mf }   
    x a b within IF b x a blend EXIT THEN   
    x b c within IF b x c blend EXIT THEN   
    0 ;   
      
   Another alternative, assuming a<=b<=c:   
      
   : tri_mf3.1 { x a b c -- mf }   
    case   
    x a < ?of 0 endof   
    x b < ?of b x a blend endof   
    x c < ?of b x c blend endof   
    0 0   
    endcase ;   
      
   The disadvantage in the latter case is that the above and below cases   
   are separate, needing another branch and possibly increasing the   
   number of mispredictions. This can be addressed with:   
      
   [undefined] select [if]   
   : select ( u1 u2 f -- u )   
    if swap then nip ;   
   [then]   
      
   : tri_mf3.1 { x a b c -- mf }   
    x a c within if   
    b x x b < a c select blend   
    else   
    0   
    then ;   
      
   In Gforth (development) SELECT is a primitive which hopefully does not   
   branch; in that case you have only one branch here.   
      
   - anton   
   --   
   M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html   
   comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html   
    New standard: https://forth-standard.org/   
   EuroForth 2023 proceedings: http://www.euroforth.org/ef23/papers/   
   EuroForth 2024 proceedings: http://www.euroforth.org/ef24/papers/   
      
   --- SoupGate-DOS v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca