Shop OBEX P1 Docs P2 Docs Learn Events
Spin Golf Challenge ... and an interesting exception to operator precedence. (S - Page 2 — Parallax Forums

Spin Golf Challenge ... and an interesting exception to operator precedence. (S

2»

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-17 06:06
    After further investigation it's clear that, in Spin, the "right-associativity" of assignment operators is preserved. What this means is that the assignment has to take place first before its value can be used in an expression. This prevents the nonsense interpretation alluded to above. Again, this should be footnoted in the manual (unless I've missed it), since precedence alone is not the whole story.

    -Phil
  • BradCBradC Posts: 2,601
    edited 2010-02-17 06:29
    Bobb Fwed said...
    If all we care about is written characters, you could change that "z := 0" to "z~". It does the same thing, but it is slightly slower.
    By the way, it being slightly slower seems like a compiler issue to me. They should perform identical actions. And seeing as we can easily test which is faster, the compiler should be changed so it compiles "z~" to the same byte code as "z := 0"

    Assuming Z is a long -

    Z := 0 translates to :
    Push 0 on the stack
    Pop the stack into Z
    (Read byte from hub, write long to stack, read long from stack, write long to hub)


    Z~ translates to
    Post-Clear Z.
    (read 2 bytes from hub, write long to hub)

    The bytecode is the same length, but the former performs extra stack actions. Having said that, the assignment code path in the interpreter is quite complex.

    There are certainly little things you can do as a programmer to keep your time critical spin code fast, but I'm not sure it's the job of the compiler to second guess the programmer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
  • mparkmpark Posts: 1,305
    edited 2010-02-17 06:53
    Phil Pilgrim (PhiPi) said...
    After further investigation it's clear that, in Spin, the "right-associativity" of assignment operators is preserved. What this means is that the assignment has to take place first before its value can be used in an expression. This prevents the nonsense interpretation alluded to above. Again, this should be footnoted in the manual (unless I've missed it), since precedence alone is not the whole story.

    -Phil

    (Emphasis added.) Right you are, Phil. This made me slightly nuts when I first encountered it.
  • Kal_ZakkathKal_Zakkath Posts: 72
    edited 2010-02-17 06:56
    Here's the best I could do:

        z~  {2}                   
        repeat a  {7}
          z += 1 & a {6}
          a /= 2      {4} {19}
    

    Unfortunately it only works if a >= 0, to handle negative numbers the last line should be a >>= 1, which brings the total up to 20 characters the same as Phil's.

    (Also I'm assuming that 'repeat -1' will eventually terminate [noparse][[/noparse]I didn't have the patience to check it but I assume it will run the loop 2^32-1 times, not a very efficient way of doing things but that's not really the point here is it tongue.gif ])
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-17 07:11
    Kal,

    LOL! I laud your creativity! smile.gif And, you're, right: time efficiency does not matter here. But you can still get your 19:

      z~                        {2}
      repeat a                  {7}
        z += 1 & a ->= 1        {10} {19}
    
    
    


    -Phil

    Update: Oops! The above does not always work! (Why not is left as an exercise for the reader.)

    Post Edited (Phil Pilgrim (PhiPi)) : 2/17/2010 7:20:25 AM GMT
  • rokickirokicki Posts: 1,000
    edited 2010-02-17 07:44
    How about:
    z~ {2}
    repeat 32 {8}
       z-=a->=7 {8} {18}
    
    

    Post Edited (rokicki) : 2/17/2010 7:51:57 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-17 08:29
    Tom,

    Wow. Just wow. Of course it works. Every bit occurs in every position, with the net effect of subtracting -1 for each one. Brilliant!

    Any particular reason for choosing 7? Any number that's relatively prime to 32 (i.e. any odd number, including 1) would have the same effect, right?

    -Phil
  • rokickirokicki Posts: 1,000
    edited 2010-02-17 15:09
    Hey, Phil!

    Yeah, the 7 was just to throw people off. Anyway I'm just building on what
    other people have already done. I expect Beau to knock off another few
    chars.

    -tom
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-02-17 21:01
    Finding the middle or median value peaked my interest for use as a data filter.· In line code took a little over 40 microseconds with a 80,000,000 Hz clock.· But I wondered if this could be extended to finding the median of 5 values.· I found a method that takes 148 microseconds.· The way was

    · z1 = median of a, b, & c

    · z2 = median of b, c, & d

    · z3 = median of c, d & e

    · z = median of z1, z2, & z3

    To make it more useful I need to rewrite it as a function that accepts the address of the first data value.· That will add some additional time; hopefully not too much.



    John Abshier
  • rokickirokicki Posts: 1,000
    edited 2010-02-17 21:35
    Unfortunately, this median-of-five algorithm fails for input such as (0,1,5,4,3).
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-02-17 21:40
    Thanks rokicki. Speed doesn't matter if it doesn't give correct answers all the time. Twenty sets of random numbers was not a sufficient test.

    John Abshier
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-18 03:56
    rokicki,

    "I expect Beau to knock off another few chars." - lol, I think you have me beat, I would have come in at 19 for my next try ... but I think I understand the mechanism that allows us to compact the equations into one line.

    For example we'll take your latest solution...

    z-=a->=7
    
    



    below is the same as the operation above...

    z-=a
       a->=7
    
    



    same as above but expanded ... I aligned the 'a' so that you can see where they would abut

    z := z - a
             a := a->7
    
    



    below is the equivalent operation expanded and abutted ...

    z := z - a := a->7
    
    



    ... the ':=' is used to seperate/isolate each equation and allows us to to other things like the code below that you often see when starting a new cog...

    a := b := c := 100
    
    



    ...it is this mechanism that allows this to work. Left to right the equations are still evaluated properly, but where there is an implied ':=' you can almost treat it as a new equation on the next line.


    Hope this makes sense.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Kal_ZakkathKal_Zakkath Posts: 72
    edited 2010-02-18 20:42
    Beau Schwabe said...
    ...where there is an implied ':=' you can almost treat it as a new equation on the next line.
    If I'm not mistaken, it is actually the previous line, not the next line.
    So the example:
    z-=a->=7
    

    becomes:

    a->=7
    z-=a
    

    as it happens, this makes no difference in rokicki's code (it still works), but modifying my previous answer to:

    z += 1 & a /= 2   
    

    is equivalent to:

    a /= 2
    z += 1 & a
    


    The assignments (:=), implied or otherwise, are done right-to-left, not left-to-right, this is also shown in your last example, if it was left-to-right you would get this:
    a := b
    b := c
    c := 100
    
    


    but right-to-left gives what we expect (or at least, this is what I expect to happen, at work now so can't check)

    c := 100
    b := c
    a := b
    
    
    
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-19 00:32
    Kal_Zakkath,

    Yes, you are correct, the equations done between the implied ":=" are evaluated right-to-left as if they are on separate lines.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.