Shop OBEX P1 Docs P2 Docs Learn Events
Multiply Longs — Parallax Forums

Multiply Longs

Beavis3215Beavis3215 Posts: 229
edited 2018-08-01 11:23 in Propeller 1
deg30 := speedtix ** $1555_5555                              ' speedtix / 360 * 30


I have looked in the help files and on the internet on how to implement this Spin example in C, and there are so many conflicting answers I can't make sense of them.

Can anyone please help?

Comments

  • If you're ever stuck trying to convert some Spin code to C, try using spin2cpp. For example,
    pub mulx(speedtix) : deg30
      deg30 := speedtix ** $1555_5555
    
    is converted by spin2cpp into:
    #define Highmult__(X, Y) ( ( (X) * (int64_t)(Y) ) >> 32 )
    int32_t foo::mulx(int32_t speedtix)
    {
      int32_t deg30 = 0;
      deg30 = Highmult__(speedtix, 357913941);
      return deg30;
    }
    

    This simplifies manually to:
    deg30 = (speedtix * (int64_t)0x15555555) >> 32;
    
  • Thats pretty cool.
    Thankyou :)
  • AribaAriba Posts: 2,682
    Or just like that:
    deg30 = speedtix / 12;                              // speedtix / 360 * 30
    
  • Isn't the assignment in this line redundant?
      int32_t deg30 = 0;
    

    I mean, why set the variable to zero, when you're only going to change it in the next line?

    -Phil
  • Isn't the assignment in this line redundant?
      int32_t deg30 = 0;
    

    I mean, why set the variable to zero, when you're only going to change it in the next line?

    Yes, it's redundant in this case. In some other cases (e.g. if the assignment below were conditional) then it would be necessary. spin2cpp is an automatic tool, so sometimes it outputs code that isn't as tidy as a human would write. The C compiler will optimize away the redundant assignment, so I didn't put any particular effort into preventing it.
Sign In or Register to comment.