Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22268

Failing comparison of floats NaNs on STM32F407 CORTEX-M4 [duplicate]

$
0
0

I'm trying to compare two floats returned from nanf("1") and the program is not entering if block.

int main(void)
{
    volatile float f;
    volatile float ff;
    uint32_t* view1;
    uint32_t* view2;

    view1 = ((uint32_t*)&f);
    view2 = ((uint32_t*)&ff);

    f = nanf("1");
    ff = nanf("1");

    if(f == ff)
    {
        f = 0;
    }

    while(1);
}

Debugger shows f and ff (through view1 and view2) variable has quiet NaN values (QNaN == 0x7FC00001).

Compiler:

gcc version 4.8.3 20131129 (release) [ARM/embedded-4_8-branch revision 205641] (GNU Tools for ARM Embedded Processors)

Flags for compiler:

-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -std=c99 -DSTM32F407VETx -DSTM32 -DSTM32F4 -DDEBUG -O0 -g3 -Wall -fmessage-length=0 -ffunction-sections -c

Flags for Linker:

-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -lm

And here is the output assembly (can't paste this in code block):

if(f == ff)
080129b4:   ldr     r3, [pc, #32]   ; (0x80129d8 <main+92>)
080129b6:   vldr    s14, [r3]
080129ba:   ldr     r3, [pc, #36]   ; (0x80129e0 <main+100>)
080129bc:   vldr    s15, [r3]
080129c0:   vcmp.f32        s14, s15
080129c4:   vmrs    APSR_nzcv, fpscr
080129c8:   bne.n   0x80129d2 <main+86>

f = 0;
080129ca:   ldr     r3, [pc, #12]   ; (0x80129d8 <main+92>)
080129cc:   mov.w   r2, #0
080129d0:   str     r2, [r3, #0]

while(1);
080129d2:   b.n     0x80129d2 <main+86>

Viewing all articles
Browse latest Browse all 22268

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>