MinGW and MSVCRT Conflict Causes Floating-Point Value Corruption
jones_supa writes: If you are working on a C++ program where you need very accurate floating point numbers, you might have decided to use long double data type for the extra precision. After a few calculations, you happen to print your number. To your shock, instead of the number being 123.456789, it is printed out as -6.518427 × 10^264 (or 2.745563 depending on your computer). This is actually a bug in some versions of MinGW g++ 4.8.1 (MinGW is a port of GNU programming tools for Windows). Microsoft's C++ runtime library reserves 80 bits for double and long double. When MinGW uses the Microsoft DLL to print out the value, the number is interpreted as using only 64 bits. This discrepancy causes garbage results to be output.
Compiler bugs are news ?
Let me guess, you are running this in boot camp on an intel bad d apple.
And he's runnig this all through bootcamp on a macbook air.
Nope he used MinGW that uses libraries without looking at any documentation, instead it uses them like they worked in its drug fueled pipe dream.
You used MinGW? What a fool!
FTA
This is actually a bug in some versions of MinGW g++ 4.8.1.
The title implies that the floating point value becomes corrupt. Without looking into it, it sounds like the value does not become corrupt but rather is just not output correctly. The underlying value is still intact.
ok?
The fine article actually pins it on gcc, and mentions that the later MinGW doesn't have this problem, so the whole /. article isn't very interesting ...
You like *read* the summary right? It says in the summary what bit of code is wrong. The open source code BTW if you do not want to read...
This is a conversion bug. Not a 'windows' bug. It is an easy mistake to make. I have made it myself many times on all the platforms I have used. Bug classification is important. Do not willy nilly blame things. It really makes it hard to fix things. As you may start fixing from the wrong assumption. Then may end up fixing one thing and creating 10 new bugs.
The sage of Wikipedia states
So the program implementation assumed a behavior that was not guaranteed, and was burned when it used an outside library which was specification compliant but not in the same way as that particular implementation.
A poor workman blames his tools. And in this case I'm not referring to MinGW, which is admirably neutral in its reporting.
Try doing the same thing on an original Pentium.
Get free satoshi (Bitcoin) and Dogecoins
This is well known. I had a bug in a tree class due to this. The key stored in the instance was 64 bit, but the compare class evaluated and compared it in 80 bits. One of the most difficult bugs I ever encountered. Highly recursive calls to the compare function failing once in about a billion calls... But that was almost 10 or 12 years ago.
But one thing. GCC handled the truncations correctly. It allows the 80 bit evaluations turned off by compiler options. I don't mix GCC with msvcrt so I am not sure how old / new this is. My 80 bit adventure was in Linux on Intel chips.
sed -e 's/Chuck Norris/Rajnikant/g' joke > fact
It's an ABI mismatch, and the summary is nonsense, saying almost the exact opposite of TFA (which I actually read, because the summary is obvious nonsense). The issue is that the Windows ABI defines long double as being a 64-bit floating point value (which is fine, because the only requirement for long double is that it have no less precision than double. If you're using it and expecting some guaranteed precision for vaguely portable code then you're an idiot). For some reason, MinGW (which aims to be ABI-compatible with MS, at least for C libraries) uses 80-bit x87 values for long double, so you get truncation. I forget the exact calling conventions for Windows i386, but I believe that in some cases this will be silently hidden, as the value will be passed in x87 register and so be transparently extended to 80 bits in the caller and truncated in the callee anyway. It's only if it's passed on the stack (or indirectly via a pointer) that it's a problem.
It's not obvious which definition of long double is better. On modern x86, you'll use SSE for 32- and 64-bit values, and may lose precision moving between x87 and SSE registers. You also get worse IEEE compliance out of the x87 unit, which may matter more than the extra 16 bits of precision. 80-bit floats are not available on any platform other than x86 (128-bit is more common, though PowerPC has its own special non-IEEE version of these and on some other platforms they're entirely done in software), so they're a bad choice if you want portable code that generates the same output on different platforms.
I am TheRaven on Soylent News
... this is better than posting stories about SourceForge getting caught highjacking the dev accounts of major OSS projects I guess.
64 bits should be enough for anybody
You used GNU's compiler (on Windows) for floating point maths and got the wrong answer? Surely not!
FTFY.
Also earlier versions of MinGW do not have the problem. It's a regression.
Which means it's a 100% windows bug
No. The MinGW version of GCC allowed to compile programs against the Microsoft C++ runtime library, but the compiler created code which did not follow the spec of the Microsoft library. There really isn't anything to blame about Windows here.
But once I've debugged my software and uploaded it to SourceForge can I be sure it won't have an advertising spyware package added to the installer by DICE?
saying almost the exact opposite of TFA (which I actually read, because the summary is obvious nonsense).
Oops, indeed... 80-bit for GCC, 64-bit for MSVCRT. I stand corrected.
It's nice to know we can rely on the Anonymous Cow-patty to be wrong and insulting at the same time.
Go kill yourself. You'll feel better.
No, boot camp doesn't. From the older versions of Bootcamp which were essentailly Bios emulation over EFI 1.1 with non-standard extensions, to the new EFI version that actively turns hardware off if you don't boot into OS X, Boot camp doesn not give access to the Intel hardware. In point of fact, one o the things it turns off is the Intel integrated graphics, on dual GPU Macs.
the summary is nonsense, saying almost the exact opposite of TFA (which I actually read, because the summary is obvious nonsense).
Apparently NOT.
The current bug appears to be a regression bug for this version of g++
You are correct about the ABI mismatch. But the article clearly puts the blame on MinGW. He even gives links to the patch.
Again you are not being clear but trying to be. You are being misleading. Doing that causes bugs. I have hundreds of bugs where I chased down a rabbit hole because someone speculated in the bug summary. I have seen hundreds of bugs 'fixed' but were not fixed because someone 'fixed' the wrong thing because of a bad first assumption.
Basically the G++ compiler was not following the spec. The visual studio compiler was using the IEEE format. Do not mix them if you do not like odd results... It is a very old land mine in compiling things. You sometimes need to know what your compiler does. It is one of those things that you just need to watch for. For example a good land mine (and he briefly touched on it in the article) is printf. printf varies wildly from platform to platform. With different bits implemented in different ways. In some platforms feeding too little or too much data can cause stack overflow/underflow errors. Because you are dealing across platforms they will have assumptions about what it means. You need to be careful when using external APIs they do not always match the OS you are using.
You will not see this, as much, on other platforms because of their monolithic nature with the compiler. They tend to use one compiler chain. But you can sometimes get oddities like this between compiler releases. I can think of at least 5 different compiler chains you can use on windows. You can also get this oddity when porting code. As that code may have accidentally/intentionally used some side effect of the original compiler. But that may no longer exist on the new platform.
You also get worse IEEE compliance out of the x87 unit, which may matter more than the extra 16 bits of precision.
As far as I am aware, the x87 was fully IEEE compliant so long as you asked for 64-bit (or 32-bit) rounding after every operation (which was implicit if you write all operations back to memory) until Intel decided that precision didnt matter as much as benchmark performance. It was about mid-2014 that Intels newest precision issues made the frontpage of slashdot (Where Intel Processors Fail At Math (Again)
"His name was James Damore."
Maybe it wasn't sarcasm or being a jerk, but that he actually meant that Microsoft's own compiler does a proper job here.
For 64-bit apps, Microsoft defaults to full precision (80-bit long doubles, that is, 64-bit mantissa).
Check it in float.h (the _PC_53 part, as in Precision Control):
#if defined (_M_IX86)
#define _CW_DEFAULT ( _RC_NEAR + _PC_53 + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL)
#elif defined (_M_X64) || defined (_M_ARM)
#define _CW_DEFAULT ( _RC_NEAR + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL)
#endif
I don't see what makes it worth a Slashdot posting reading the blog post, it's actually 1. specific to an old g++ version (4.8), and does not occur in the two more recent versions (4.9 and 5.1); 2. specific to a certain build of g++ on mingw; others are not impacted.
In short: much ado about very little.
There is no precision loss when moving from SSE to x87, unless you set a very strange rounding mode. The x87 is IEEE compliant for all but underflow/overflow behavior, if you set the post-operation rounding mode appropiate. There are other platforms supporting the 80bit Intel float format.
Gnu C "long double" is 16 bytes long and most decidedly does not fit into 80 bytes.
Most ACs are not even worth the keystrokes to insult them. Be generically insulted by this and ignored otherwise.
That should be "80 bits"...
Most ACs are not even worth the keystrokes to insult them. Be generically insulted by this and ignored otherwise.
But the article clearly puts the blame on MinGW.
Not really...
He even gives links to the patch.
... and this patch makes MinGW use its own implementation of printf (and family...), rather than trusting Microsoft's buggy version.
We are on 4.9.2 even on windoze... just sayin... old news is old news...
It's the other way around. long double is 80 bits long and most decidedly does fit into 16 bytes, which is does so presumably for alignment purposes.
systemd is Roko's Basilisk.
SourceForge, the code repository site owned by Slashdot Media, has apparently seized control of the account hosting GIMP for Windows on the service, according to e-mails and discussions amongst members of the GIMP community—locking out GIMP's lead Windows developer. And now anyone downloading the Windows version of the open source image editing tool from SourceForge gets the software wrapped in an installer replete with advertisements.
http://arstechnica.com/informa...
The GIMP developers aren't happy at all about this. They say that Sourceforge impersonated the GIMP developers, and abused the trademarks owned by the GNOME foundation.
https://mail.gnome.org/archive...
The 80-bit long double is also available on the 68881, 68882 coprocessors and later 68K family members that incorporate the FPU. The Itanium also supports the 80-bit format.
But yeah... those aren't particularly common these days.
Program Intellivision!
It's all TheRaven64's fault.
and this post will not change my mind. Even if I had to use a proprietary but otherwise free compiler on windows, I would definitely go with pellesc and not msvc.
As far as c++ is concerned, I am idiot and cannot understand it. So I avoid it.
the question is whether this bug affects dmd or pocc.
here is no precision loss when moving from SSE to x87, unless you set a very strange rounding mode.
and unless you're using long double on a non-Windows (read: Linux) x64 platform, where sizeof(long double) = 16, i.e. 128 bits.
a datatype of "long double" is truly irrelevant, and an utter representation of stupidity.
WTF is a "short double"?
The true problem is a mhoronic presumtion that MSVC is relevant. FALSE.
A double is 64-bits. A float is is 32-bits. A half-float is 16 bits (only pertinent to ship-dits that believe that 80 bit 'floats' are useful.)
Also, ABI mismatches are common in the MinGW world, so we know to look out for them. Sometimes MinGW even breaks compatibility with itself, like that time when they suddenly without telling anybody changed the standard calling convention of one of its shared (!) DLLs from stdcall to cdecl (if memory serves me correctly). This created a compatibility and support nightmare that cannot really be fixed. I contacted the devs and asked about it, and apparently one of them just sort of ‘flicked the switch’, not for a reason but just because he felt like it, without even thinking of thinking the consequences through. Amateurs.
x87 can produce IEEE 754 compliant results if the compiler either sets the correct rounding mode before each operation OR if it stores and reloads the results of each operation into memory (forcing the correct rounding).
However, both are expensive to do, performance wise, and no compiler does so by default.
Instead, x87 is normally used a way which is not IEEE 754 compliant, although it's actually a bit more accurate: internally, everything is done with 80 bit precision.
This results from the fact the x87 unit actually predates the final version of the IEEE 754 standard.
The IEEE 754 standard only covers a few operations: add, subtract, multiply, divide, FMA,
The transcendals (sin, cos, tag, exp, pow, etc) functions have never been part of the IEEE 754. Historically, most x87 FPUs have had errors larger than 1 ulp, at least for some part of the range.it
If I am not mistaken, only the AMD K5 FPU actually provided errors of less than 1 ulp for the entire range of inputs. And please, take this with a large grain of salt.
I am perfectly happy with MingW to port my Linux programs.
C standard is not the end-all be-all, there are all specifications that define the details of ABI for a particular architecture and platform. In case of VC++, it is well-documented that long double is the same as double, so any compiler that has an explicit stated goal of being ABI-compatible must respect that.
It has to do that because arrays of types are required to not have gaps between elements (so that address arithmetic works), and on the other hand they all have to be properly aligned. For modern Intel CPUs, the proper alignment for an 80-bit float is 8 bytes. Hence the smallest value that is equal to or greater than 10 bytes (80 bits) that is divisible by 8 - 16 bytes.
See subject: From http://slashdot.org/comments.p... ?
Your "DNS lookup" b.s.?
1st: Hosts exceed SLOWER remote DNS lookup (Exploit ridden by Kaminsky redirect flaw & 99.999% of ISP DNS != patched vs. it!) - how?
I avoid DNS putting WHERE I SPEND 95%++ ONLINE TIME @ TOP OF HOSTS via 30 favs!
2nd: AdBlocking gains speed!
BOTH exceed remote DNS lag indexed lookup post query/turnaround on resolution (do the math binary search) over 3++ million records w/ a most efficient blocking format = better load + internal parse & no bloat in hosts cached in LOCAL RAM via 2 kernelmode subsystems (diskcache & ip stack = no usermode context switch overhead like Windows' faulty w/ large hosts files usermode dns cache service) vs. remote DNS for utmost in speed, efficiency + reliability (my program keeps hardcodes current) vs. downed DNS too.
* Hosts = MORE SPEED + EFFICIENCY & ease of maintenance (via http://start64.com/index.php?o...) versus.:
1.) Remote DNS & hosts do so w/ less resource use + added on app complexity/room for breakdown & exploit w/ added CPU & power use w/ a local setup DNS (worse if separate system) + complexity of deny rules vs. hosts simple entries
+ vs.
2.) "Almost ALL Ads Blocked": Hosts are far more efficient doing more w/ less vs. AdBlock's BLOAT & regex complexity vs. hosts simple entries. Addons add overheads layered over slower browsers in usermode increasing messagepassing overheads vs. hosts in kernelmode (run some addons concurrently see what I mean). Addons do more added I/O operations + consume more memory & create CPU overuse + complexity (regex vs. hosts entries) bolted-on in SLOW usemode vs. hosts in PURE kernelmode via a high cpu serviced layer of ops by IP stack. Addons = easily detected by native browser methods + clarityray shuts 'em down (hosts aren't).
APK
P.S.=> + Hosts != bribed (like AdBlock/ABP NOT DOING THE 1 JOB IT HAD by default)... apk
Well, Microsoft's version is not buggy if you use it as Microsoft intended. After all, the VC++ compiler doesn't produce incorrect results. But here's the catch: MSVCRT wasn't designed as a reusable component, so the burden is on MinGW. If reuse of some parts of MSVCRT is too difficult, it's entirely legitimate to rewrite just those.
IIRC, the third option for IEEE compliance is to use 80 bits for storage too. Again, expensive. The x87 non-conformance is caused by silently mixing 64 bits and 80 bits FP numbers.
On an unrelated note, I'm not sure what you mean by "IEEE 754 covering only a few operations, x87 being incorrect". It covers many, many more. However, the core 5 (+-/* and FMA) are required to be exact within 1 ulp. The others have an unspecified error, and at the time it wasn't known which functions could reasonably be specified down to 1 ulp error.
I think you mean denormalized numbers when you discuss "some part of the range". Those are tricky because 1 ulp isn't well-defined for those numbers. I.e. for 32 bits FP, 1 ulp normally is about 0.001% but the smallest denormal number *is* exacly one ulp (so 100%).