From simon.urbanek at r-project.org  Mon Sep 14 15:27:57 2015
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Mon, 14 Sep 2015 09:27:57 -0400
Subject: [Rd] Optimization bug when byte compiling with gcc 5.2.0 on
	windows
In-Reply-To: <CABFfbXsdT2-jBdaqr4oCtZ2AvBWZ++tQHa49DyWc=m3G_qE7bw@mail.gmail.com>
References: <CABFfbXsdT2-jBdaqr4oCtZ2AvBWZ++tQHa49DyWc=m3G_qE7bw@mail.gmail.com>
Message-ID: <2E775529-1E44-480C-97B6-AB847BDB6928@r-project.org>

Jeroen,

the difference is that level 3 is using the internal implementation of sqrt in the compiler instead of calling the sqrt function. The internal path goes to R_sqrt which is defined as

# define R_sqrt(x) (ISNAN(x) ? x : sqrt(x))

so you could check if that is where the problem happens. It theory NA should yield itself, but perhaps the compiler thinks all NANs are equal and will return a constant one. If that's not it, then something else in the direct FastMath1(R_sqrt, R_SqrtSym) path...

Cheers,
Simon


> On Sep 14, 2015, at 8:49 AM, Jeroen Ooms <jeroen.ooms at stat.ucla.edu> wrote:
> 
> When building R-devel with gcc 5.2.0 (mingw-w64 v4) on Windows, make
> check fails reg-tests-1b.R at the following check:
> 
>  x <- c(1:2, NA)
>  sx <- sd(x)
>  !is.nan(sx)
> 
> Here 'sx' should be 'NA' but it is 'NaN'. It turns out this problem
> only appears when the function is byte compiled with optimization
> level 3:
> 
>  mysd <- function (x, na.rm = FALSE)
>    sqrt(var(if (is.vector(x)) x else as.double(x), na.rm = na.rm))
> 
>  mysd(x)
>  # [1] NA
>  compiler::cmpfun(mysd, list(optimize = 1L))(x)
>  # [1] NA
>  compiler::cmpfun(mysd, list(optimize = 2L))(x)
>  # [1] NA
>  compiler::cmpfun(mysd, list(optimize = 3L))(x)
>  # [1] NaN
> 
> The problem does not appear with gcc 5.2.0 on Debian, and also not
> with gcc 4.9.3 on Windows. Where do I start debugging this? The
> disassembled output from the compiled functions is here:
> https://gist.github.com/jeroenooms/3206945a6db6680a9c5c
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] Optimization bug when byte compiling with gcc 5.2.0 on windows
Simon Urbanek