Fix _copysign is not a member of std (Windows) (#35199)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/35199
While running `test_cpp_extensions_aot_no_ninja` and building `rng_extension.cpp` compilation fails with:
[C:\Users\circleci\project\build\win_tmp\build\torch\include\ATen/native/Math.h(82): error C2039: '_copysign': is not a member of 'std'](https://app.circleci.com/pipelines/github/pytorch/pytorch/144367/workflows/f939ad40-273f-4492-a19e-3f602509f6f5/jobs/4907947)
this PR should fix it based on [MSDN](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/copysign-copysignf-copysignl-copysign-copysignf-copysignl?view=vs-2019)
Test Plan: Imported from OSS
Differential Revision: D20591607
Pulled By: pbelevich
fbshipit-source-id: 4d61245cfeb37c074f0ee89027b60c581b5e08b9
diff --git a/aten/src/ATen/native/Math.h b/aten/src/ATen/native/Math.h
index b2ea680..f9efc28 100644
--- a/aten/src/ATen/native/Math.h
+++ b/aten/src/ATen/native/Math.h
@@ -2,6 +2,7 @@
#include <cstdlib>
#include <cmath>
+#include <cfloat>
#include <limits>
#include <type_traits>
#include <c10/util/math_compat.h>
@@ -79,7 +80,12 @@
T d[2]={ 3.543889200, 1.637067800};
T y_abs = std::abs(y);
if(y_abs > 1.0) return std::numeric_limits<T>::quiet_NaN();
+#ifdef _WIN32
+ // error C2039: '_copysign': is not a member of 'std'
+ if(y_abs == 1.0) return copysign(std::numeric_limits<T>::infinity(), y);
+#else
if(y_abs == 1.0) return std::copysign(std::numeric_limits<T>::infinity(), y);
+#endif
if(y_abs <= static_cast<T>(CENTRAL_RANGE)) {
z = y * y;
num = (((a[3]*z + a[2])*z + a[1])*z + a[0]);
@@ -90,7 +96,12 @@
z = std::sqrt(-std::log((static_cast<T>(1.0)-y_abs)/static_cast<T>(2.0)));
num = ((c[3]*z + c[2])*z + c[1]) * z + c[0];
dem = (d[1]*z + d[0])*z + static_cast<T>(1.0);
+#ifdef _WIN32
+ // error C2039: '_copysign': is not a member of 'std'
+ x = copysign(num, y) / dem;
+#else
x = std::copysign(num, y) / dem;
+#endif
}
/* Two steps of Newton-Raphson correction */
x = x - (std::erf(x) - y) / ((static_cast<T>(2.0)/static_cast<T>(std::sqrt(M_PI)))*std::exp(-x*x));