NO

Author Topic: Incorrect value after multiplying size_t and a float  (Read 2638 times)

neo313

  • Guest
Incorrect value after multiplying size_t and a float
« on: October 27, 2014, 11:54:12 PM »
I managed to hunt this one out of a large project( it certainly wasn't easy ). Especially punishing is the fact that the variables are declared with const, yet they get incorrectly changed, which as you can guess, is very misleading when you search for the problem.

Optimization bug. Disabling optimizations or if the function gets inlined, gives correct results. Latest version v6. 32 bit, C99, Win7

Assembly:
The problem is after the call ___ftoul, the result is loaded into eax and then from eax into edx. Edx is used as count_low and has the correct new value. Instead of reloading the value from count, eax holding the new value is used as count in future operations.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

void Bug( const size_t count )
{
const size_t count_low = count * 0.5f ;

printf("%zu != %zu\n" , count_low , count ) ;
}

int main ( void )
{
const size_t c = 345U ;
Bug( c ) ;

return 0 ;
}


« Last Edit: October 28, 2014, 01:19:35 AM by neo313 »