Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #10305 nim cpp is now nan-correct at CT #10310

Merged
merged 2 commits into from
Jan 15, 2019

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Jan 15, 2019

I finally figured out root cause of #10305, but it was a rather painful debug session

Here's a minimal test case to explain what's going on:

#include <stdio.h>

#define CASE_VOLATILE // otherwise const folding optimizer kicks in
int main (int argc, char *argv[]) {
  #ifdef CASE_VOLATILE
    volatile double a1 = 0.0;
  #else
    double a1 = 0.0;
  #endif
  double a2 = 0.0;
  double a3 = a1 / a2;
  int t = a3 == 0.0;
  int t2 = a3 == a3;
  int t3 = a3 != a3;
  printf("a3=%g,t=%d,t2=%d,t3=%d\n", a3, t, t2, t3);
  return 0;
}
clang++ -o /tmp/D20181107T193702 -ffast-math -O3 $timn_D/tests/nim/all/t0103.cpp
/tmp/D20181107T193702
a3=nan,t=1,t2=1,t3=0 # BUG

without  -ffast-math
a3=nan,t=0,t2=0,t3=1

so -ffast-math affects Nan correctness;
note that :

$nimc_D/config/nim.cfg contains

clang.options.speed = "-O3"

so that explains why this issue was only affecting nim cpp (since $nimc_D/compiler/extccomp.nim defined optSpeed: " -O3 -ffast-math " for both c and cpp, but c was overridden by $nimc_D/config/nim.cfg)

note

there's a tradeoff between speed and accuracy at play here; but -d:release should be correct by default; but I added a note in doc/nimc.rst regarding more aggressive options for speed, including --passC:-ffast-math

@timotheecour timotheecour requested a review from Araq January 15, 2019 07:17
tests/float/tfloatnan.nim Outdated Show resolved Hide resolved
@timotheecour
Copy link
Member Author

PTAL, fixed b3 unused

@timotheecour
Copy link
Member Author

PTAL

@Araq Araq merged commit 4355f23 into nim-lang:devel Jan 15, 2019
@timotheecour timotheecour deleted the pr_fix_10305_nan_ct branch January 15, 2019 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nim cpp treats Nan as 0.0 (during compile time)
2 participants