[Necto 2.0, MicroC ARM] nonME breaks Cmath lib

Fully featured ARM compilers available on Windows, Linux, and macOS.
Post Reply
Author
Message
NoahNilsen
Posts: 5
Joined: 05 Aug 2022 17:45

[Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#1 Post by NoahNilsen » 05 Aug 2022 18:38

Setting 4 bytes integer in MicroC AI settings breaks Cmath. Currently checked modf, pow and log functions. They all return zero.

Example code:

Code: Select all

static double p = 0.188228105404814;
static double rntc = 77318.7772925764;

int main(void)
{
    volatile double coeff = pow(rntc, p);
    (void)coeff;
    
    /* Replace with your application code */
    while (1)
    {
    }

    return 0;
}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#2 Post by filip » 08 Aug 2022 08:33

Hi,

Can you tell me which MCU did you use ?

Regards,
Filip.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#3 Post by hexreader » 08 Aug 2022 08:41

I am just a hobby programmer. Far from an expert.

My suspicion is that your code is being optimised away by the compiler.
The compiler seems to be smart enough to see that the result coeff is not used, so gets rid of what it thinks is pointless code

I think that you need to make use of the result coeff.
I did this by sending coeff to a random PORT. This is a meaningless thing to do for a float value, but just a way to make use of the result coeff.

Code: Select all

// random pow() test code found on forum
// tested using STM32407 on EasyMx PRO v7 board
// necto studio legacy setup

static double p = 0.188228105404814;                           // power value
static double rntc = 77318.7772925764;                         // base value

int main(void)
{
    volatile double coeff = pow(rntc, p);                      // expect about 8.32 as result of approx 77318 ^ 0.188
    (void)coeff;                                               // absolutely no idea what this is for, but seems harmless
    
    GPIO_Digital_Output(&GPIOD_ODR, _GPIO_PINMASK_ALL);        // Set PORTD as all output

    GPIOD_ODR = coeff;                                         // use the result to prevent optimiser removing  
    
    while (1)
    {
    }

    return 0;
}
I am very curious as to what this line of code does

Code: Select all

    (void)coeff;                                               // absolutely no idea what this is for, but seems harmless
I have never seen anything like this.
What is the purpose of this line please?
Last edited by hexreader on 08 Aug 2022 11:13, edited 1 time in total.
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#4 Post by hexreader » 08 Aug 2022 09:16

Well, I did say that I am not an expert, and sure enough I think I got it wrong.

Seems to be display of the local variable coeff that is the problem when using real hardware and debugging with mikroprog
PORTD LEDs correctly show the value 8, so the code seems to be working
Just the local variable coeff and hovering over the word coeff give zero result

Simulator seems to be OK

I have no idea why the behaviour is like this
Start every day with a smile...... (get it over with) :)

NoahNilsen
Posts: 5
Joined: 05 Aug 2022 17:45

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#5 Post by NoahNilsen » 09 Aug 2022 12:23

filip wrote:
08 Aug 2022 08:33
Hi,

Can you tell me which MCU did you use ?

Regards,
Filip.
Hi

STM32L4S5ZI

Additional research make me think that problem in frexp() and ldexp() functions. I changed then to ANSI C functions, and all works correctly.

NoahNilsen
Posts: 5
Joined: 05 Aug 2022 17:45

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#6 Post by NoahNilsen » 09 Aug 2022 12:26

hexreader wrote:
08 Aug 2022 08:41
I am very curious as to what this line of code does

Code: Select all

    (void)coeff;                                               // absolutely no idea what this is for, but seems harmless
I have never seen anything like this.
What is the purpose of this line please?
(void) var; Is to deny compiler optimization. So you could debug this var in this string.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#7 Post by hexreader » 10 Aug 2022 16:01

NoahNilsen wrote:
09 Aug 2022 12:26

(void) var; Is to deny compiler optimization. So you could debug this var in this string.
Ahh clever.

I have learnt something new. Many thanks

Sorry that I don't know how to solve your problem.
Looks to me like a debugger error. The variable seems to work OK, but the debugger fails to show the value correctly

If this behaviour really troubles you, then you could raise a support ticket. This will gain the attention of the MikroE experts.
Start every day with a smile...... (get it over with) :)

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#8 Post by filip » 12 Aug 2022 08:29

Hi,

I have tried your code and seems that it produces the correct result. Can you please elaborate your issue in details, maybe I'm missing something ?

Regards,
Filip.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#9 Post by hexreader » 12 Aug 2022 10:59

Problem appeared to only be with display of local variable, and only when hardware debugging.

Seems to be fixed with new necto version 2.1 :)

All is now well

Maybe OP can confirm?
Start every day with a smile...... (get it over with) :)

NoahNilsen
Posts: 5
Joined: 05 Aug 2022 17:45

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#10 Post by NoahNilsen » 14 Aug 2022 20:20

filip wrote:
12 Aug 2022 08:29
Hi,

I have tried your code and seems that it produces the correct result. Can you please elaborate your issue in details, maybe I'm missing something ?

Regards,
Filip.
Checked in Necto 2.1 - got the same. Linux, Ubuntu 22.04, Intel i5 10gen
MikroC settings
MikroC settings
image_2022-08-14_230941246.png (21.94 KiB) Viewed 1528 times
If I set 4 bytes integer - got coef = 0.0. If I set 2 bytes integer, got coef = 8.319

Also I got another bug in Necto on another PC. It did not change setup options. I had to change them manually in setup.json. Check, if 4 bytes integer really set in your setup.
Attachments
setup.zip
My setup options
(1.77 KiB) Downloaded 37 times

NoahNilsen
Posts: 5
Joined: 05 Aug 2022 17:45

Re: [Necto 2.0, MicroC ARM] nonME breaks Cmath lib

#11 Post by NoahNilsen » 14 Aug 2022 20:23

hexreader wrote:
12 Aug 2022 10:59
Problem appeared to only be with display of local variable, and only when hardware debugging.

Seems to be fixed with new necto version 2.1 :)

All is now well

Maybe OP can confirm?
No, it is not fixed. And this is not display problem. And it is not debugging problem. This code is just working issue example.

Post Reply

Return to “ARM AI Compilers”