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.

Using inline assembly together with C code

This is a function for addition with saturation, given by a third party:

/*
* operation:         s32 + s32
* saturation:        Yes
* rounding mode:     N/A 
*/
inline int32_T c28x_add_s32_s32_s32_sat(int32_T a, int32_T b)
{
	int32_T result;	
	asm("	SETC OVM");
	result = a + b;
	asm("	CLRC OVM");
	return result;
}

It will not return correct results if -O1-4 is enabled, because the addition is not performed between SETC OVM and CLRC OVM. I suppose the compiler is free to move these statements around.

  1. Where is this behavior documented?
  2. Do you have a suggested workaround?
  • From the function name I presume you using a C28xx device.  

    Stian Soevik said:
    Where is this behavior documented?

    Please see the section titled Use Caution With asm Statements in Optimized Code in the C28x compiler manual.

    Stian Soevik said:
    Do you have a suggested workaround?

    All the direct workarounds I can think of are worse than just disabling optimization.  

    As for an indirect workaround ... I don't know for sure, but it is a good guess that you could use the IQmath library to do something similar.

    Thanks and regards,

    -George

  • Thanks. Correct, I use 2809.

    Is anything guaranteed about the internal order of the asm statements? If one can expect them to keep this ordering, maybe the addition could be performed in assembly instead.

  • You don't want to go that route.  There is no way for the asm statements to access the function arguments or return a result.

    If no IQmath function does what you want, then you have to go with a routine entirely written in assembly.

    Thanks and regards,

    -George

  •  OK, I suppose the C intrinsics are there because of this?

    I got a new solution from the vendor  using __sat, but as this depends on previous overflows it still would not work. How is __sat intended to be used in C?

    Do you have a guide to writing assembly functions that are called from C? (Parameters, registers to use, return values etc) 

    Regards,

    Stian

     

  • Stian Soevik said:
    Do you have a guide to writing assembly functions that are called from C?

    Please see the section titled Interfacing C and C++ With Assembly Language in the C2000 compiler manual.

    Thanks and regards,

    -George