This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

how do intrinsics __llmax() and __llmin() work?

// When I use __llmax() and __llmin() to saturate a value, I don't expect the
// order of input parameters to make a difference. But apparently they do.
//
// Can someone explain why a becomes 4294967286 in the first calculation and
// 0 (as expected) in the second?

volatile long long int a, b, c;

void main(void)
{
        b  = 10LL;
        c  = -1LL;
 
        a = __llmax(__llmin(b*c, 100LL), 0LL);
        //a = 4294967286
 
        a = __llmax(0LL, __llmin(100LL, b*c));
        //a = 0
}
  • Hi Ruben,

    how do intrinsics __llmax() and __llmin() work?

    In short you're getting a garbage value at first iteration! Try initializing a, b,c to some initial value say 0.

    Regards,

    Gautam

  • b an c are initialized to 10 and -1 respectively. Initializing even a (to 0) does not help.

    I think it is curious that 4294967286 happens to be equal to "(unsigned long int)-10", which in turn is the result of b*c

    I stepped through the following code:

    volatile long long int a, b, c;
    volatile unsigned long int test;

    void main(void)
    {
      test = (unsigned long int)-10;
      // at this point test = 4294967286
      a = 0LL;
      b = 10LL;
      c = -1LL;

      a = __llmax(__llmin(b*c, 100LL), 0LL);
      // I would expect:
      // b*c = -10:
      // min(-10, 100) = -10
      // max(-10, 0) = 0
      //
      // The actual result is:
      // a = 4294967286

      a = __llmax(0LL, __llmin(100LL, b*c));
      // Here the result is correct:
      // a = 0
    }