Because of the holidays, TI E2E™ design support forum responses will be delayed from Dec. 25 through Jan. 2. Thank you for your patience.

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.

DMC Library: Question about macro extension, pointer and optimization

Hello everybody,

the DMC Library makes extensive use of macro expansions. It works fine and I use this programming technique for my custom software modules as well. But there are two things i just couldn't figure out and I hope someone of you can tell me:

 

1. What is the purpose of the "User defined Data type of pointer to the module"? e.g. in svgen_dq module:

At first the clumsy expression with the struct gets replaced by the symbol SVGENDQ. A new datatype is born. With this new datatype one can define variables e.g. svgen_dq1. So far so good, but then the Symbol SVGENDQ gets replaced by a pointer: 

 typedef SVGENDQ *SVGENDQ_handle;

As far as i know this pointer is never used, neither in the documention of the softwaremodule nor in any of the code examples. What is its purpose?

 

2. Why a macro extension can be smaller and faster than an inline function, and what are the certain circumstances for further optimization of the macro expansion mentioned below? 

From the http://focus.ti.com/lit/an/spraak2/spraak2.pdf :

 

To inline the function, the code is placed in the client file itself, and is qualified with the inline keyword, 

as shown in Example 6.

Another optimization is sometimes possible; that is, to turn the function into a macro expansion. This

allows the compiler to optimize further under certain circumstances and produces less code, and

consumes fewer cycles to execute. Such a version of the PID controller was tested, and the results are

documented in Step 8, in Table 1. The code for such a version of the PID controller is shown in

Example 7.

 

 

Thanks for any help and greetings from Germany

Thomas

 

 

  • I believe I can answer your question 2.

    When using pointers, the compiler is less efficient than when using direct reference of objects.

    For example, take the code:

    typedef struct
    {
        int a;
        int b;
    } MyStruct_t;
    
    extern MyStruct_t s;
    extern MyStruct_t *sp;
    
    void test( void )
    {
    
        s.a = 10;
        sp->b = 80;
    }
    


    s.a = 10
    will compile into

            MOVW      DP,#_s
            MOVB      @_s,#10,UNC
    


    sp->b = 80
    will compile into

            MOVW      DP,#_sp
            MOVL      XAR4,@_sp             
            MOVB      *+XAR4[1],#80,UNC    
    

    Example 6 of spraak2 uses pointers whereas Example 7 directly references objects.

    Mit freundlichen Grüßen

    -quark 

  • Thanks for your answer quark. I was not aware of the fact, that the use of pointers extends execution time. I'am just not familiar with machine code that much.

    If they used the directly referenced object in both examples, would there be any difference left between inline function and macro? Or is this just a tic of the programmer which one of these two he prefers?

    Sorry for my persistency, but I gonna use this in my master thesis and therefore I need to understand it properly.

  • Hi Thomas,

    I may have an answer to the first question. The type pointer to an object with general name of OBJECT_handle (in your case SVGENDQ_handle) was created as it was used correctly in some versions of some modules. Thus function svgendq_calc(SVGENDQ *v) should really be svgendq_calc(SVGENDQ_handle v). This was done to keep the programing style close to recommendations of OO programing in C. The problem was/is that TI programers do not have a consistent style, so the typdef, if not used can be (or maybe even should be) removed.

    Cheers, Mitja