473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float to DWORD conversion bug

Running this piece of code while having the sse optimization turned on
(vcnet2003), something really strange happens I cannot explain. Without sse
it is giving me the right results.

float f = 0.8;
f *= UINT_MAX;
DWORD dw = (DWORD)f;

Basically the result in dw should be 0xcccccccc (or 0xcccccd00 because of
float's inaccuracy), but instead I get 0x80000000 for anything above f =
0.5!

Here is the same code with the assembly instructions inlined copied from the
debugger's view. According to the watch window, f still holds the right
value after f *= UINT_MAX, and only becomes wrong after the third line.

float f = 0.8;

movss xmm0,dword ptr [__real@3f4ccccd (102261C4h)]
movss dword ptr [f],xmm0

f *= UINT_MAX;

movss xmm0,dword ptr [f]
mulss xmm0,dword ptr [__real@4f800000 (102260ECh)]
movss dword ptr [f],xmm0

DWORD dw = (DWORD)f;

fld dword ptr [f]
fnstcw word ptr [ebp-10Ah]
movzx eax,word ptr [ebp-10Ah]
or ah,0Ch
mov dword ptr [ebp-110h],eax
fldcw word ptr [ebp-110h]
fistp dword ptr [ebp-114h]
fldcw word ptr [ebp-10Ah]
mov eax,dword ptr [ebp-114h]
mov dword ptr [dw],eax
Nov 17 '05 #1
1 4702
Hm, no one has commented on this yet?

Anyway, I just noticed the problem is not there with the sse opt, but only
with sse2. The sse code seems to call __ftol2 which still works. So as it
looks, the current sse2 option in vc2k3 is quite useless and _dangerous_ if
someone wants to use floating point calculations in his program too.
DWORD dw = (DWORD)f;

sse:

fld dword ptr [f]
call @ILT+1375(__fto l2) (411564h)
mov dword ptr [dw],eax

sse2

fld dword ptr [f]
fnstcw word ptr [ebp-0DAh]
movzx eax,word ptr [ebp-0DAh]
or ah,0Ch
mov dword ptr [ebp-0E0h],eax
fldcw word ptr [ebp-0E0h]
fistp dword ptr [ebp-0E4h]
fldcw word ptr [ebp-0DAh]
mov eax,dword ptr [ebp-0E4h]
mov dword ptr [dw],eax
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4359
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either (1) or (2) will assign dword value to not dword aligned address. The question is - will this code be fatal on some systems??
6
2098
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
54
8380
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
3
1619
by: van6 | last post by:
code frag: int main() { float fval1 = 1.0f; float fval2 = 2.0f; double dsum =0.0; // here is the point ; // Are fval1 and fval2 both promoted to type of double prior to adding
3
41238
by: pipe.jack | last post by:
I'm trying to convert string to float and my float after conversion is 0 (zero). Here is my code: $c = $currencies->format($cart->show_total()); echo gettype($c); echo (float)$c; $c = 39.59 gettype($c) = string echo (float)$c; = 0
3
1661
by: Alexandru Mosoi | last post by:
Hi Recently I've made a stupid bug. Here is a snippet: long long a, b; .... int c = min(a, b); But this bug could have been prevented if g++ would warn me about loss of precision. Do you know any flag that can turn on this kind of
8
14169
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val = 3.14159; u_long nw_val = *((u_long *) &val); than the nw_val equal to 1078530000, I made such conversion: float d_val = *((float*)&nw_val);
6
3716
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0) File "C:\Program Files\Python30\lib\fractions.py", line 97, in __new__
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.