Page 1 of 1

NXC: inline or safecall for frequently used Btn functions?

Posted: 19 Feb 2011, 17:11
by HaWe
hi,
I'm not sure about using inline or safecall for the following frequently used io functions (used also by different tasks) - I'm currently using inline.
Or is neither needed?
What would be better for code performance, executing safety, and compactness?

Code: Select all

inline bool btnhit(){
   return ( ButtonPressed(BTN1, false) || ButtonPressed(BTN2, false)
         || ButtonPressed(BTN3, false) || ButtonPressed(BTN4, false));
}

Code: Select all

inline int cin() {
  int result = -1;
  if (ButtonPressed(BTN1, false))
      result = BTN1;
    else if (ButtonPressed(BTN2, false))
      result = BTN2;
    else if (ButtonPressed(BTN3, false))
      result = BTN3;
    else if (ButtonPressed(BTN4, false))
      result = BTN4;
  return result;
}

Re: NXC: inline or safecall for frequently used Btn functions?

Posted: 20 Feb 2011, 16:16
by afanofosc
The ButtonPressed function outputs a call to acquire and release like this:

Code: Select all

acquire __RBtnMutex
// snipped
release __RBtnMutex
So any use of ButtonPressed on multiple threads will definitely be synchronized. It would not hurt to use safecall rather than inline. That would definitely reduce your code size.

I may change the built-in NXC getchar function that use ButtonPressed to be safecall rather than inline.

John Hansen

Re: NXC: inline or safecall for frequently used Btn functions?

Posted: 20 Feb 2011, 21:42
by muntoo
First, pretend you're only programming for one thread (main), and you don't have any other tasks.

-----

Something simple* like:

Code: Select all

int add(int a, int b)
{
    return(a + b);
}
should be inline, because it's so simple*.

* small may not always be faster

Something like:

Code: Select all

long reallyLongFunction(long long1, long long2)
{
    unsigned long isUnsigned = 9;
    for(unsigned long i = 0; i < 100; i++)
    {
        if(long1 + long2 < i)
            return(i);
    }
    return(isUnsigned * long1 + sind(long2) + sqrt(4 * long2 * pow(long1, PI)));
}
Would be better left alone, because of the long loop, and one function call won't really hurt your speed compared to sind, sqrt, pow, and a 5*100 ops will.

----

Now, if you're doing a multi-tasking program, leave all the inline functions alone (see first code snippet), and convert the rest to safecall (second code snippet).

Well, that's what I would do, anyways.