Shop OBEX P1 Docs P2 Docs Learn Events
Spin2 Operator Syntax - Page 3 — Parallax Forums

Spin2 Operator Syntax

1356718

Comments

  • Isn't the PRNG (GETRND) free running now?
  • jmgjmg Posts: 15,171
    cgracey wrote: »
    Here are all the aliases (updated):
    !	NOT
    &&	AND
    ||	OR
    ^	XOR
    <<	SHL
    %	MOD
    =<	<=
    =>	>=
    <>	!=
    
    That all looks good, I have no problems reading both forms in anyone's code.

  • cgraceycgracey Posts: 14,134
    ozpropdev wrote: »
    Isn't the PRNG (GETRND) free running now?

    Yes, but it would be good to have a reversible PRNG available in software for programming, where you want to be able to seed it, in order to get a repeatable pattern. Reversibility is nice, too. This could just be an LFSR, or something better, working on a long variable.

    The hardware PRNG is the Xoroshiro128+ and that thing might as well be a TRNG.
  • cgraceycgracey Posts: 14,134
    jmg wrote: »
    cgracey wrote: »
    Here are all the aliases (updated):
    !	NOT
    &&	AND
    ||	OR
    ^	XOR
    <<	SHL
    %	MOD
    =<	<=
    =>	>=
    <>	!=
    
    That all looks good, I have no problems reading both forms in anyone's code.

    Me neither.

    The next big thing we'll have to work out is precedence.
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2017-04-11 02:18
    Chip, you are missing the sign extend operators. Unless I am just blind? Also, I think keeping the aliases is perfectly fine.

    jmg, in C >> is implementation defined, but every compiler I have ever used has it as an arithmetic shift. And Chip has it has arithmetic. He could add the SAR alias to it.
    Also, << is always not arithmetic (being non-sign preserving).

  • cgraceycgracey Posts: 14,134
    edited 2017-04-11 02:16
    Roy Eltham wrote: »
    Chip, you are missing the sign extend operators. Unless I am just blind? Also, I think keeping the aliases is perfectly fine.

    jmg, in C >> is implementation defined, but every compiler I have ever used has it as an arithmetic shift. Also, << is always not arithmetic (being non-sign preserving).

    There is now EXTS, which is a general case, where you supply the bit number to extend from.

    We could call it SEXT, which is more natural, but awfully distracting. EXTS looks like "extension?!?".
  • signext ? sgnx? extend?
  • cgraceycgracey Posts: 14,134
    edited 2017-04-11 02:22
    Roy Eltham wrote: »
    signext ? sgnx? extend?

    SIGNX, maybe?

    I though of SIGNEXT, too, but it reads like "signal next".

    I'll change it to SIGNX.
  • What about these as aliases
    EXTB 'sign extend byte
    EXTW Sign extend word
  • jmgjmg Posts: 15,171
    edited 2017-04-11 02:27
    cgracey wrote: »
    LOG and EXP are base-2, what would you suggest? LOG2 and EXP2, maybe?
    Hmm, if they are not Natural, or base 10, it would pay to avoid those names, to allow user functions to user those names.
    If they work base2, then LOG2, EXP2 seems ok

    addit:
    Yes, Google finds instances of LOG2 and EXP2 that do what they suggest.
    cgracey wrote: »
    SHR shifts 0's into the MSB's, whereas ">>" does an arithmetic shift, as one would expect from other languages.

    This link suggests >> shifts in 0 for C ?
    http://www.c4learn.com/c-programming/c-bitwise-right-shift/

    but then this gets murkier
    https://msdn.microsoft.com/en-us/library/336xbhcz.aspx

    Right Shifts
    The right-shift operator causes the bit pattern in shift-expression to be shifted to the right by the number of positions specified by additive-expression. For unsigned numbers, the bit positions that have been vacated by the shift operation are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.
    Important :
    The result of a right-shift of a signed negative number is implementation-dependent. Although Visual C++ uses the sign bit to fill vacated bit positions, there is no guarantee that other implementations also do so.


    ugh.

    Sounds like SHR and SAR are the needed aliases to give predictable outcomes.
    ie if someone tries to port >> that used the 'other convention', there is a way to get the expected result.
  • cgraceycgracey Posts: 14,134
    edited 2017-04-11 02:27
    ozpropdev wrote: »
    What about these as aliases
    EXTB 'sign extend byte
    EXTW Sign extend word

    Those would be special cases, but we could do that.
  • cgraceycgracey Posts: 14,134
    jmg wrote: »
    cgracey wrote: »
    LOG and EXP are base-2, what would you suggest? LOG2 and EXP2, maybe?
    Hmm, if they are not Natural, or base 10, it would pay to avoid those names, to allow user functions to user those names.
    If they work base2, then LOG2, EXP2 seems ok

    addit:
    Yes, Google finds instances of LOG2 and EXP2 that do what they suggest.
    cgracey wrote: »
    SHR shifts 0's into the MSB's, whereas ">>" does an arithmetic shift, as one would expect from other languages.

    This link suggests >> shifts in 0 for C ?
    http://www.c4learn.com/c-programming/c-bitwise-right-shift/

    but then this gets murkier
    https://msdn.microsoft.com/en-us/library/336xbhcz.aspx

    Right Shifts
    The right-shift operator causes the bit pattern in shift-expression to be shifted to the right by the number of positions specified by additive-expression. For unsigned numbers, the bit positions that have been vacated by the shift operation are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.
    Important :
    The result of a right-shift of a signed negative number is implementation-dependent. Although Visual C++ uses the sign bit to fill vacated bit positions, there is no guarantee that other implementations also do so.


    ugh.

    Sounds like SHR and SAR are the needed aliases to give predictable outcomes.
    ie if someone tries to port >> that used the 'other convention', there is a way to get the expected result.

    Good point, I just added an alias, SAR, to >>.
  • jmg, gcc & clang also do SAR for signed numbers.

    Chip, SIGNX works well enough. I don't think we need extb or extw, signx can do those plus any other size.
  • cgraceycgracey Posts: 14,134
    Roy Eltham wrote: »
    jmg, gcc & clang also do SAR for signed numbers.

    Chip, SIGNX works well enough. I don't think we need extb or extw, signx can do those plus any other size.

    Done.
  • cgracey wrote:
    So, which would you keep, "AND" or "&&"?
    Both. They're two different operators. Both are lazy: i.e. if they fail partway through, the rest of the expression is not evaluated. AND has lower precedence than &&, helping to reduce the need for parens.

    Same with || and OR. Both are lazy operators. They stop evaluating upon the first TRUE result. OR has lower precedence than ||.

    Just keep !; forget NOT, same with << and >>. Self-explanatory.

    ^ and XOR are two separate operators: ^ is bitwise; XOR is Boolean. Both are useful. If you wanted to, you could add ^^ as an analog of && and ||.

    Regarding >= and =>, just make up your mind, and pick one! An alias there just confuses things further.

    Signed shifts and rotates: keep what you had in Spin 1.

    IOW, forget aliases altogether. Pick the most C-like of each pair and jettison the other.

    Thanks,
    -Phil
  • Phil, AND and && are the same, not sure why you think they are different. Same with || and OR.
    & is different from && and AND, just like | is from || and OR
  • ozpropdev wrote: »
    cgracey wrote: »
    Here are all the aliases:
    !	NOT
    &&	AND
    ||	OR
    ^	XOR
    <<	SHL
    =<	<=
    =>	>=
    <>	!=
    
    I like all of them. :)

    Seconded. Me too

  • jmgjmg Posts: 15,171
    cgracey wrote: »
    Roy Eltham wrote: »
    signext ? sgnx? extend?

    SIGNX, maybe?

    I though of SIGNEXT, too, but it reads like "signal next".

    I'll change it to SIGNX.

    SIGNX sounds good, or maybe SIGNEX, or is that still too close to signal next ?

  • cgraceycgracey Posts: 14,134
    edited 2017-04-11 05:55
    How do you guys feel about C operator precedence? Should we go with it, or does it need any fixing?

    I would like NOT to have a low precedence, so it's useful with AND and OR, without needing parentheses.
  • cgraceycgracey Posts: 14,134
    XSIGN ?
  • I like C operator precedence, but I am biased since I use it all day for work. I would not change it away from what C has if you go with it.
  • Heater.Heater. Posts: 21,230
    Not sure I like all those aliases. I can see the "gurus" using the cryptic symbols thus confusing the newbies trying to read their code. I rather all the C operators were C style cryptic symbols and the other Spin specific operators as textual acronyms only.

    It make sense to use C style operator precedence if you are using C style operators.
  • jmgjmg Posts: 15,171
    Heater. wrote: »
    I can see the "gurus" using the cryptic symbols thus confusing the newbies trying to read their code. .

    Why do you imagine that ?
    Surely a true Guru recognizes the audience, and creates the clearest code, where they know newbies will be reading.

  • cgracey wrote: »
    David Betz wrote: »
    Looks good but I would get rid of the aliases. They will just make it harder to read someone else's code.

    So, which would you keep, "AND" or "&&"?
    Well, I would have chosen && just because I'm used to C syntax but I don't see anything wrong with choosing AND for Spin.

  • Cluso99Cluso99 Posts: 18,069
    edited 2017-04-11 09:43
    I dislike (actually hate) the cryptic symbols of both C and spin.

    That is why it has been said that C was invented by programmers who were scared of losing their jobs, so they made C able to be as obtuse as possible.

    And by this, I don't mean the simple things like
    x := (a+b)*(c+d)
    x++

    but I do mean things like
    x <= 5
    and long convoluted expressions where precedence is used, rather than using parenthesis to ensure correct interpretation (not the simple * / + - which everyone should know).

    Just MHO.
  • Heater.Heater. Posts: 21,230
    David Betz,
    Why do you imagine that ?
    Surely a true Guru recognizes the audience, and creates the clearest code, where they know newbies will be reading.
    I know what you mean.

    But how does a guru know who is audience is? He writes his code, he sticks in in OBEX or github etc. Every newbie in the world get's to read it. Which is almost everybody as very few people know Spin.

    I can also imagine source emerging that is a soup of cryptic operators and acronyms all mixed up in a blinding mess!

    Having cryptic symbols and acronyms is just more junk someone has to learn.

    I'd go with &&. If we are going to have C style we should have C style.

  • dMajodMajo Posts: 855
    edited 2017-04-11 10:23
    cgracey wrote: »
    Here are all the aliases (updated):
    !	NOT
    &&	AND
    ||	OR
    ^	XOR
    <<	SHL
    %	MOD
    [b]=<	<=
    =>	>=[/b]
    <>	!=
    
    Looks good.

    As minor suggestion:
    - I will remove the aliases for equality operators (greater or equal and less or equal) and
    - add the OR and AND alias also for | and & if the compiler can understand how to discriminate between boolean and bitwise.


    What about SGNEX for sign extension?
  • Heater. wrote: »
    David Betz,
    Why do you imagine that ?
    Surely a true Guru recognizes the audience, and creates the clearest code, where they know newbies will be reading.
    I know what you mean.

    But how does a guru know who is audience is? He writes his code, he sticks in in OBEX or github etc. Every newbie in the world get's to read it. Which is almost everybody as very few people know Spin.

    I can also imagine source emerging that is a soup of cryptic operators and acronyms all mixed up in a blinding mess!

    Having cryptic symbols and acronyms is just more junk someone has to learn.

    I'd go with &&. If we are going to have C style we should have C style.
    That wasn't my quote but I agree with you. :-)
  • jmg wrote: »
    cgracey wrote: »
    Here are all the aliases (updated):
    !	NOT
    &&	AND
    ||	OR
    ^	XOR
    <<	SHL
    %	MOD
    =<	<=
    =>	>=
    <>	!=
    
    That all looks good, I have no problems reading both forms in anyone's code.
    This reminds me of the usually hated practice of using #define to change C syntax:
    #define AND &&
    

    I am glad though that <= and >= are no longer assignment operators.


  • cgraceycgracey Posts: 14,134
    What about supporting both <> and != for inequality? Just go with != ?
Sign In or Register to comment.