fmod() incorrectly returns NaN for (some?) denormalized inputs
Bug #1000498 reported by
Thomas Bushnell, BSG
This bug affects 2 people
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
eglibc |
Fix Released
|
Medium
|
|||
eglibc (Ubuntu) |
Fix Released
|
Medium
|
Adam Conrad | ||
Precise |
Fix Released
|
Medium
|
Adam Conrad | ||
Quantal |
Fix Released
|
Medium
|
Adam Conrad |
Bug Description
[Impact]
When using fmod, some inputs will cause incorrect results.
[Fix]
An upstream eglibc patch (c5bfe3d5ba29d3
[Test Case]
See comment https:/
[Regression Potential]
This adds the fixes for fmod and adds the appropriate test cases in eglibc.
[Original Report]
See http://
Changed in eglibc: | |
importance: | Unknown → Medium |
status: | Unknown → Fix Released |
Changed in eglibc (Ubuntu): | |
assignee: | nobody → Chris J Arges (christopherarges) |
importance: | Undecided → Medium |
Changed in eglibc (Ubuntu Precise): | |
status: | New → Confirmed |
importance: | Undecided → Medium |
assignee: | nobody → Chris J Arges (christopherarges) |
Changed in eglibc (Ubuntu Precise): | |
assignee: | Chris J Arges (christopherarges) → Adam Conrad (adconrad) |
Changed in eglibc (Ubuntu Quantal): | |
assignee: | Chris J Arges (christopherarges) → Adam Conrad (adconrad) |
Changed in eglibc (Ubuntu Quantal): | |
status: | In Progress → Fix Committed |
To post a comment you must log in.
I've come across a case where fmod() does not return the expected result. Reduced repro:
------
#include <math.h>
#include <stdio.h>
#include <iostream>
int main() { 01e-308;
double x = 2.2250738585072
double y = 5e-324;
double z = fmod(x, y);
// printf("result: %g\n", z); // see [1] below.
std::cout << "result: " << z << std::endl;
return 0;
}
------
Expected result: 0
Actual result: -nan
I can repro the bug on both a Gentoo (gcc-4.5.3, kernel 3.3.4) and an Ubuntu Precise (gcc-4.6.3, kernel 3.2.5) system, which both have glibc-2.15, and both are x86_64. It works correctly on Ubuntu Lucid (glibc-2.11, gcc-4.4.2, kernel 2.6.38.8).
Further, it works correctly when compiling with either of -O1, -O2, -O3. It also works correctly when removing the "std::cout << ..." and "#include <iostream>" lines, and using the "printf(..." line (marked [1]) instead.
I've looked at the generated machine code. In the buggy case, glibc's fmod is called directly:
main():
400744: 55 push rbp
400745: 48 89 e5 mov rbp,rsp
400748: 48 83 ec 20 sub rsp,0x20
40074c: 48 b8 ff ff ff ff ff movabs rax,0xfffffffffffff
400753: ff 0f 00
400756: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
40075a: b8 01 00 00 00 mov eax,0x1
40075f: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
400763: f2 0f 10 4d f0 movsd xmm1,QWORD PTR [rbp-0x10]
400768: f2 0f 10 45 f8 movsd xmm0,QWORD PTR [rbp-0x8]
40076d: e8 de fe ff ff call 400650 <fmod@plt>
400772: f2 0f 11 45 e8 movsd QWORD PTR [rbp-0x18],xmm0
400777: f2 0f 10 45 e8 movsd xmm0,QWORD PTR [rbp-0x18]
40077c: bf dc 08 40 00 mov edi,0x4008dc
400781: b8 01 00 00 00 mov eax,0x1
400786: e8 75 fe ff ff call 400600 <printf@plt>
40078b: b8 00 00 00 00 mov eax,0x0
400790: c9 leave
400791: c3 ret
When I do any of the changes that make it work (e.g. remove the iostream include), g++ decides to inline an FPU-based implementation of fmod (which seems to work as expected) and only calls out to glibc as a fallback:
main():
400604: 55 push rbp
400605: 48 89 e5 mov rbp,rsp
400608: 48 83 ec 40 sub rsp,0x40
40060c: 48 b8 ff ff ff ff ff movabs rax,0xfffffffffffff
400613: ff 0f 00
400616: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
40061a: b8 01 00 00 00 mov eax,0x1
40061f: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
400623: dd 45 f8 fld QWORD PTR [rbp-0x8]
400626: dd 45 f0 fld QWORD PTR [rbp-0x10]
400629: d9 c0 fld st(0)
40062b: d9 c2 fld st(2)
40062d: d9 f8 fprem
40062f: df e0 fnstsw ax
400631: f6 c4 04 test ah,0x4
400634: 75 f7 jne 40062d <main+0x29>
400636: dd d9 fstp st(1)
400638: dd 5d d8 fstp QWORD PTR [rbp-0x28]
40063b: f2 0f 10 45 d8 movsd xmm0,QWORD PTR [rbp-0x28]
400640: 66 0f 2e c0 ucomisd xmm0,xmm0
...