NXC: inline or safecall for frequently used Btn functions?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: inline or safecall for frequently used Btn functions?

Post 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;
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

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

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

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

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests