Shop OBEX P1 Docs P2 Docs Learn Events
SPIN2 min/max operations on unsigned values missing in language? — Parallax Forums

SPIN2 min/max operations on unsigned values missing in language?

roglohrogloh Posts: 5,787
edited 2020-08-16 05:28 in Propeller 2
I just noticed in SPIN2 that it doesn't appear to have an operator to find the min or max of a pair of values using unsigned integers, only signed is defined:

minmax.png

Was there a reason why the unsigned version wasn't also in the language? COG space? I sort of wonder if the language could be extended to also use the +#> and +#< forms for determining MIN and MAX of two unsigned values, like the unsigned divide and comparisons use, or does that break the SPIN2 interpreter size?

In PASM, it would just need to use FLE and FGE instructions instead of FLES and FGES.

I guess we can still do the following for now but this workaround may need to break apart a potentially more complex assignment or test (which may otherwise be capable of more conveniently using a min or max of two unsigned values directly as a term) by using an intermediate and more verbose if statement instead like this:
' For limiting a to MIN(a,b)
if a+>b
  a := b 

' For setting a to MAX(a,b)
if a+<b
  a := b

' For assigning MIN(a,b) to c
if a +< b
  c := a
else
  c := b

' For assigning MAX(a,b) to c
if a +> b
  c := a
else
  c := b

829 x 48 - 15K

Comments

  • RaymanRayman Posts: 14,646
    I think all variables in spin are signed integers, right?
  • cgraceycgracey Posts: 14,155
    edited 2020-08-16 15:02
    It would take six byte codes to support unsigned FLE and FGE operators. Since all byte codes are used, I would have to remove six others to make room.
  • cgraceycgracey Posts: 14,155
    Rayman wrote: »
    I think all variables in spin are signed integers, right?

    Yes, except in cases of unsigned operators.
  • cgraceycgracey Posts: 14,155
    ' For assigning MAX(a,b) to c
    if a +> b
      c := a
    else
      c := b
    

    Instead....

    c := a +> b ? a : b

  • Just because an operator is available in the language doesn't mean that it has to have a single corresponding bytecode. There's nothing wrong with generating a sequence of bytecodes to accommodate something like unsigned min/max.

    -Phil
  • cgraceycgracey Posts: 14,155
    Just because an operator is available in the language doesn't mean that it has to have a single corresponding bytecode. There's nothing wrong with generating a sequence of bytecodes to accommodate something like unsigned min/max.

    -Phil

    True, but it can create a cascade of complexity when something falls outside of normal handling.
  • cgracey wrote:
    ... it can create a cascade of complexity when something falls outside of normal handling.
    Can you elaborate, please? I should think there'd be a way to encapsulate things to avoid such a cascade.

    -Phil
  • cgraceycgracey Posts: 14,155
    cgracey wrote:
    ... it can create a cascade of complexity when something falls outside of normal handling.
    Can you elaborate, please? I should think there'd be a way to encapsulate things to avoid such a cascade.

    -Phil

    Phil, if I were to elaborate, the cascade would begin.
  • cgracey wrote: »
    ' For assigning MAX(a,b) to c
    if a +> b
      c := a
    else
      c := b
    

    Instead....

    c := a +> b ? a : b

    Yeah I posted something else about ternary operators last night and also then made the same connection. This will let us put any unsigned min/max calculations into other complex expressions which is preferable over breaking them up, so I'm happy with that solution. Nice!
  • RaymanRayman Posts: 14,646
    cgracey wrote: »

    Yes, except in cases of unsigned operators.

    Wait, who invited unsigned operators to this party?
    That was a mistake...

Sign In or Register to comment.