Author Topic: dspic33CH / dspic33CK Compiler __builtin's problems.  (Read 144 times)

0 Members and 1 Guest are viewing this topic.

Offline ndan

  • Contributor
  • Posts: 8
  • Country: ro
dspic33CH / dspic33CK Compiler __builtin's problems.
« on: August 19, 2019, 03:10:10 pm »
Hi guys,

I'm trying to optimize some math operations for an dspic33CK32MP205 motor control application, by using the xc16 v1.36 compiler builtin functions. That is, the builtin's that use the accumulator registers, e.g. __builtin_clr, __builtin_mac, __builtin_mpy.

But, when I try declare an accumulator as in the xc16 compiler user guide, I get an error, namely:
Code: [Select]
volatile register  int result  asm("A");
error: register specified for 'result' isn't suitable for data type

Furthermore, enabling fixed point data types support (-menable-fixed) din't fixed the problem. While using an _Accum declaration, instead, does not seem to be recognized by the builtin functions, as in:
Code: [Select]
int a = 0x00FF;
int b = 0x00FF;
_Accum result; // or volatile _Accum result asm("A");

result = _builtin_mpy(a, b,0,0,0,0,0,0);

error: Expected DSP accumulator result (__builtin_mpy)
Of course, I can write these in inline assembly, but I was really hoping to use the builtin's since the code looks much cleaner.

Does anyone have any idea how to get around this problem?

« Last Edit: August 19, 2019, 06:27:35 pm by ndan »
"In my opinion, we should search for a completely different flying machine, based on other flying principles." - Henri Coanda

Offline ndan

  • Contributor
  • Posts: 8
  • Country: ro
Re: dspic33CH / dspic33CK Compiler __builtin's problems.
« Reply #1 on: August 20, 2019, 02:49:04 pm »

In my case, there were some sources for which the "-menable-fixed" compiler flag was still present in the Makefile. This is due to either an MPLAB X makefile generation screw up or someone edited the makefiles by hand (as the application is part of a larger project).

Anyway, what is not clear in the XC16 C Compiler User’s Guide  is two fold:
1. "-menable-fixed" enables fixed point support for the MCU, NOT the DSP engine (e.g. _Accum referes to a data type operated upon by the MCU core). This can be seen from the compiler listing of the following code:
Code: [Select]
_Accum a = 1,b = 1,c = 1;
a = b*c;

add.w     w14, #0xc, w1
mov.w     [w1], w4
mov.w     [w1+2], w5
mov.b     [w1+4], w6
add.w     w14, #0x6, w3
mov.w     [w3], w0
mov.w     [w3+2], w1
mov.b     [w3+4], w2
rcall     0x608 <___mulda3>
mov.w     w0, [w14++]
mov.w     w1, [w14++]
mov.b     w2, [w14]

2. More importantly, the "-menable-fixed" flag, probably imposes a specific alignment which clobbers the DSP engine usage (though the __builtin's) and the compiler reports the error from the previous post. Plus, the only information I could find about the error message was this statement in the user manual: "Alignment or other restrictions prevent using requested register".

To solve this issue, I removed the "-menable-fixed" flag from all the places it was present in the Makefile and re-imported the project into MPLAB X.
I guess one can't use the MCU supported fixed point types and DSP engine operations at the same time. Or, possibly, it can be done by using inline assembly instead of the __builtin's for the DSP.

Hope this helps!
"In my opinion, we should search for a completely different flying machine, based on other flying principles." - Henri Coanda

Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo