lib: Starting thinning out the dependency on math.h by using compiler builtins
where available.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@144785 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/divdc3.c b/lib/divdc3.c
index 95f8c93..620b8ca 100644
--- a/lib/divdc3.c
+++ b/lib/divdc3.c
@@ -13,6 +13,7 @@
  */
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the quotient of (a + ib) / (c + id) */
@@ -22,7 +23,7 @@
 {
     int __ilogbw = 0;
     double __logbw = logb(fmax(fabs(__c), fabs(__d)));
-    if (isfinite(__logbw))
+    if (crt_isfinite(__logbw))
     {
         __ilogbw = (int)__logbw;
         __c = scalbn(__c, -__ilogbw);
@@ -32,24 +33,26 @@
     double _Complex z;
     __real__ z = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
     __imag__ z = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
-        if ((__denom == 0.0) && (!isnan(__a) || !isnan(__b)))
+        if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
         {
             __real__ z = copysign(INFINITY, __c) * __a;
             __imag__ z = copysign(INFINITY, __c) * __b;
         }
-        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
+        else if ((crt_isinf(__a) || crt_isinf(__b)) &&
+                 crt_isfinite(__c) && crt_isfinite(__d))
         {
-            __a = copysign(isinf(__a) ? 1.0 : 0.0, __a);
-            __b = copysign(isinf(__b) ? 1.0 : 0.0, __b);
+            __a = copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
+            __b = copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
             __real__ z = INFINITY * (__a * __c + __b * __d);
             __imag__ z = INFINITY * (__b * __c - __a * __d);
         }
-        else if (isinf(__logbw) && __logbw > 0.0 && isfinite(__a) && isfinite(__b))
+        else if (crt_isinf(__logbw) && __logbw > 0.0 &&
+                 crt_isfinite(__a) && crt_isfinite(__b))
         {
-            __c = copysign(isinf(__c) ? 1.0 : 0.0, __c);
-            __d = copysign(isinf(__d) ? 1.0 : 0.0, __d);
+            __c = copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
+            __d = copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
             __real__ z = 0.0 * (__a * __c + __b * __d);
             __imag__ z = 0.0 * (__b * __c - __a * __d);
         }
diff --git a/lib/divsc3.c b/lib/divsc3.c
index 50ca81e..88e2e09 100644
--- a/lib/divsc3.c
+++ b/lib/divsc3.c
@@ -13,6 +13,7 @@
  */
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the quotient of (a + ib) / (c + id) */
@@ -22,7 +23,7 @@
 {
     int __ilogbw = 0;
     float __logbw = logbf(fmaxf(fabsf(__c), fabsf(__d)));
-    if (isfinite(__logbw))
+    if (crt_isfinite(__logbw))
     {
         __ilogbw = (int)__logbw;
         __c = scalbnf(__c, -__ilogbw);
@@ -32,24 +33,26 @@
     float _Complex z;
     __real__ z = scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
     __imag__ z = scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
-        if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
+        if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
         {
             __real__ z = copysignf(INFINITY, __c) * __a;
             __imag__ z = copysignf(INFINITY, __c) * __b;
         }
-        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
+        else if ((crt_isinf(__a) || crt_isinf(__b)) &&
+                 crt_isfinite(__c) && crt_isfinite(__d))
         {
-            __a = copysignf(isinf(__a) ? 1 : 0, __a);
-            __b = copysignf(isinf(__b) ? 1 : 0, __b);
+            __a = copysignf(crt_isinf(__a) ? 1 : 0, __a);
+            __b = copysignf(crt_isinf(__b) ? 1 : 0, __b);
             __real__ z = INFINITY * (__a * __c + __b * __d);
             __imag__ z = INFINITY * (__b * __c - __a * __d);
         }
-        else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
+        else if (crt_isinf(__logbw) && __logbw > 0 &&
+                 crt_isfinite(__a) && crt_isfinite(__b))
         {
-            __c = copysignf(isinf(__c) ? 1 : 0, __c);
-            __d = copysignf(isinf(__d) ? 1 : 0, __d);
+            __c = copysignf(crt_isinf(__c) ? 1 : 0, __c);
+            __d = copysignf(crt_isinf(__d) ? 1 : 0, __d);
             __real__ z = 0 * (__a * __c + __b * __d);
             __imag__ z = 0 * (__b * __c - __a * __d);
         }
diff --git a/lib/divxc3.c b/lib/divxc3.c
index 32364d6..66111db 100644
--- a/lib/divxc3.c
+++ b/lib/divxc3.c
@@ -14,6 +14,7 @@
 #if !_ARCH_PPC
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the quotient of (a + ib) / (c + id) */
@@ -23,7 +24,7 @@
 {
     int __ilogbw = 0;
     long double __logbw = logbl(fmaxl(fabsl(__c), fabsl(__d)));
-    if (isfinite(__logbw))
+    if (crt_isfinite(__logbw))
     {
         __ilogbw = (int)__logbw;
         __c = scalbnl(__c, -__ilogbw);
@@ -33,24 +34,26 @@
     long double _Complex z;
     __real__ z = scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
     __imag__ z = scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
-        if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
+        if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
         {
             __real__ z = copysignl(INFINITY, __c) * __a;
             __imag__ z = copysignl(INFINITY, __c) * __b;
         }
-        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
+        else if ((crt_isinf(__a) || crt_isinf(__b)) &&
+                 crt_isfinite(__c) && crt_isfinite(__d))
         {
-            __a = copysignl(isinf(__a) ? 1 : 0, __a);
-            __b = copysignl(isinf(__b) ? 1 : 0, __b);
+            __a = copysignl(crt_isinf(__a) ? 1 : 0, __a);
+            __b = copysignl(crt_isinf(__b) ? 1 : 0, __b);
             __real__ z = INFINITY * (__a * __c + __b * __d);
             __imag__ z = INFINITY * (__b * __c - __a * __d);
         }
-        else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
+        else if (crt_isinf(__logbw) && __logbw > 0 &&
+                 crt_isfinite(__a) && crt_isfinite(__b))
         {
-            __c = copysignl(isinf(__c) ? 1 : 0, __c);
-            __d = copysignl(isinf(__d) ? 1 : 0, __d);
+            __c = copysignl(crt_isinf(__c) ? 1 : 0, __c);
+            __d = copysignl(crt_isinf(__d) ? 1 : 0, __d);
             __real__ z = 0 * (__a * __c + __b * __d);
             __imag__ z = 0 * (__b * __c - __a * __d);
         }
diff --git a/lib/int_math.h b/lib/int_math.h
new file mode 100644
index 0000000..856889d
--- /dev/null
+++ b/lib/int_math.h
@@ -0,0 +1,28 @@
+/* ===-- int_math.h - internal math inlines ---------------------------------===
+ *
+ *                     The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===-----------------------------------------------------------------------===
+ *
+ * This file is not part of the interface of this library.
+ *
+ * This file defines substitutes for the libm functions used in some of the
+ * compiler-rt implementations, defined in such a way that there is not a direct
+ * dependency on libm or math.h. Instead, we use the compiler builtin versions
+ * where available. This reduces our dependencies on the system SDK by foisting
+ * the responsibility onto the compiler.
+ *
+ * ===-----------------------------------------------------------------------===
+ */
+
+#ifndef INT_MATH_H
+#define INT_MATH_H
+
+#define crt_isfinite(x) __builtin_isfinite((x))
+#define crt_isinf(x) __builtin_isinf((x))
+#define crt_isnan(x) __builtin_isnan((x))
+
+#endif /* INT_MATH_H */
diff --git a/lib/muldc3.c b/lib/muldc3.c
index d92ee7a..0f3be9e 100644
--- a/lib/muldc3.c
+++ b/lib/muldc3.c
@@ -13,6 +13,7 @@
  */
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the product of a + ib and c + id */
@@ -27,39 +28,39 @@
     double _Complex z;
     __real__ z = __ac - __bd;
     __imag__ z = __ad + __bc;
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
         int __recalc = 0;
-        if (isinf(__a) || isinf(__b))
+        if (crt_isinf(__a) || crt_isinf(__b))
         {
-            __a = copysign(isinf(__a) ? 1 : 0, __a);
-            __b = copysign(isinf(__b) ? 1 : 0, __b);
-            if (isnan(__c))
+            __a = copysign(crt_isinf(__a) ? 1 : 0, __a);
+            __b = copysign(crt_isinf(__b) ? 1 : 0, __b);
+            if (crt_isnan(__c))
                 __c = copysign(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysign(0, __d);
             __recalc = 1;
         }
-        if (isinf(__c) || isinf(__d))
+        if (crt_isinf(__c) || crt_isinf(__d))
         {
-            __c = copysign(isinf(__c) ? 1 : 0, __c);
-            __d = copysign(isinf(__d) ? 1 : 0, __d);
-            if (isnan(__a))
+            __c = copysign(crt_isinf(__c) ? 1 : 0, __c);
+            __d = copysign(crt_isinf(__d) ? 1 : 0, __d);
+            if (crt_isnan(__a))
                 __a = copysign(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysign(0, __b);
             __recalc = 1;
         }
-        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
-                          isinf(__ad) || isinf(__bc)))
+        if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
+                          crt_isinf(__ad) || crt_isinf(__bc)))
         {
-            if (isnan(__a))
+            if (crt_isnan(__a))
                 __a = copysign(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysign(0, __b);
-            if (isnan(__c))
+            if (crt_isnan(__c))
                 __c = copysign(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysign(0, __d);
             __recalc = 1;
         }
diff --git a/lib/mulsc3.c b/lib/mulsc3.c
index 28f9d33..ec0411a 100644
--- a/lib/mulsc3.c
+++ b/lib/mulsc3.c
@@ -13,6 +13,7 @@
  */
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the product of a + ib and c + id */
@@ -27,39 +28,39 @@
     float _Complex z;
     __real__ z = __ac - __bd;
     __imag__ z = __ad + __bc;
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
         int __recalc = 0;
-        if (isinf(__a) || isinf(__b))
+        if (crt_isinf(__a) || crt_isinf(__b))
         {
-            __a = copysignf(isinf(__a) ? 1 : 0, __a);
-            __b = copysignf(isinf(__b) ? 1 : 0, __b);
-            if (isnan(__c))
+            __a = copysignf(crt_isinf(__a) ? 1 : 0, __a);
+            __b = copysignf(crt_isinf(__b) ? 1 : 0, __b);
+            if (crt_isnan(__c))
                 __c = copysignf(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysignf(0, __d);
             __recalc = 1;
         }
-        if (isinf(__c) || isinf(__d))
+        if (crt_isinf(__c) || crt_isinf(__d))
         {
-            __c = copysignf(isinf(__c) ? 1 : 0, __c);
-            __d = copysignf(isinf(__d) ? 1 : 0, __d);
-            if (isnan(__a))
+            __c = copysignf(crt_isinf(__c) ? 1 : 0, __c);
+            __d = copysignf(crt_isinf(__d) ? 1 : 0, __d);
+            if (crt_isnan(__a))
                 __a = copysignf(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysignf(0, __b);
             __recalc = 1;
         }
-        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
-                          isinf(__ad) || isinf(__bc)))
+        if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
+                          crt_isinf(__ad) || crt_isinf(__bc)))
         {
-            if (isnan(__a))
+            if (crt_isnan(__a))
                 __a = copysignf(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysignf(0, __b);
-            if (isnan(__c))
+            if (crt_isnan(__c))
                 __c = copysignf(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysignf(0, __d);
             __recalc = 1;
         }
diff --git a/lib/mulxc3.c b/lib/mulxc3.c
index a7f2355..ae59d05 100644
--- a/lib/mulxc3.c
+++ b/lib/mulxc3.c
@@ -15,6 +15,7 @@
 #if !_ARCH_PPC
 
 #include "int_lib.h"
+#include "int_math.h"
 #include <math.h>
 
 /* Returns: the product of a + ib and c + id */
@@ -29,39 +30,39 @@
     long double _Complex z;
     __real__ z = __ac - __bd;
     __imag__ z = __ad + __bc;
-    if (isnan(__real__ z) && isnan(__imag__ z))
+    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     {
         int __recalc = 0;
-        if (isinf(__a) || isinf(__b))
+        if (crt_isinf(__a) || crt_isinf(__b))
         {
-            __a = copysignl(isinf(__a) ? 1 : 0, __a);
-            __b = copysignl(isinf(__b) ? 1 : 0, __b);
-            if (isnan(__c))
+            __a = copysignl(crt_isinf(__a) ? 1 : 0, __a);
+            __b = copysignl(crt_isinf(__b) ? 1 : 0, __b);
+            if (crt_isnan(__c))
                 __c = copysignl(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysignl(0, __d);
             __recalc = 1;
         }
-        if (isinf(__c) || isinf(__d))
+        if (crt_isinf(__c) || crt_isinf(__d))
         {
-            __c = copysignl(isinf(__c) ? 1 : 0, __c);
-            __d = copysignl(isinf(__d) ? 1 : 0, __d);
-            if (isnan(__a))
+            __c = copysignl(crt_isinf(__c) ? 1 : 0, __c);
+            __d = copysignl(crt_isinf(__d) ? 1 : 0, __d);
+            if (crt_isnan(__a))
                 __a = copysignl(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysignl(0, __b);
             __recalc = 1;
         }
-        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
-                          isinf(__ad) || isinf(__bc)))
+        if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
+                          crt_isinf(__ad) || crt_isinf(__bc)))
         {
-            if (isnan(__a))
+            if (crt_isnan(__a))
                 __a = copysignl(0, __a);
-            if (isnan(__b))
+            if (crt_isnan(__b))
                 __b = copysignl(0, __b);
-            if (isnan(__c))
+            if (crt_isnan(__c))
                 __c = copysignl(0, __c);
-            if (isnan(__d))
+            if (crt_isnan(__d))
                 __d = copysignl(0, __d);
             __recalc = 1;
         }
diff --git a/lib/ppc/divtc3.c b/lib/ppc/divtc3.c
index d41f621..d718d5a 100644
--- a/lib/ppc/divtc3.c
+++ b/lib/ppc/divtc3.c
@@ -3,16 +3,17 @@
  */
 
 #include "DD.h"
+#include "../int_math.h"
 #include <math.h>
 
 #if !defined(INFINITY) && defined(HUGE_VAL)
 #define INFINITY HUGE_VAL
 #endif /* INFINITY */
 
-#define makeFinite(x)	{ \
-							(x).s.hi = __builtin_copysign(isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi); \
-							(x).s.lo = 0.0; \
-						}
+#define makeFinite(x) { \
+    (x).s.hi = __builtin_copysign(crt_isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi); \
+    (x).s.lo = 0.0;                                                     \
+  }
 
 long double __gcc_qadd(long double, long double);
 long double __gcc_qsub(long double, long double);
@@ -28,7 +29,7 @@
 	int ilogbw = 0;
 	const double logbw = logb(__builtin_fmax( __builtin_fabs(cDD.s.hi), __builtin_fabs(dDD.s.hi) ));
 	
-	if (isfinite(logbw))
+	if (crt_isfinite(logbw))
 	{
 		ilogbw = (int)logbw;
 		
@@ -50,13 +51,14 @@
 	imag.s.hi = scalbn(imag.s.hi, -ilogbw);
 	imag.s.lo = scalbn(imag.s.lo, -ilogbw);
 	
-	if (isnan(real.s.hi) && isnan(imag.s.hi))
+	if (crt_isnan(real.s.hi) && crt_isnan(imag.s.hi))
 	{
 		DD aDD = { .ld = a };
 		DD bDD = { .ld = b };
 		DD rDD = { .ld = denom };
 		
-		if ((rDD.s.hi == 0.0) && (!isnan(aDD.s.hi) || !isnan(bDD.s.hi)))
+		if ((rDD.s.hi == 0.0) && (!crt_isnan(aDD.s.hi) ||
+                                          !crt_isnan(bDD.s.hi)))
 		{
 			real.s.hi = __builtin_copysign(INFINITY,cDD.s.hi) * aDD.s.hi;
 			real.s.lo = 0.0;
@@ -64,7 +66,8 @@
 			imag.s.lo = 0.0;
 		}
 		
-		else if ((isinf(aDD.s.hi) || isinf(bDD.s.hi)) && isfinite(cDD.s.hi) && isfinite(dDD.s.hi))
+		else if ((crt_isinf(aDD.s.hi) || crt_isinf(bDD.s.hi)) &&
+                         crt_isfinite(cDD.s.hi) && crt_isfinite(dDD.s.hi))
 		{
 			makeFinite(aDD);
 			makeFinite(bDD);
@@ -74,7 +77,8 @@
 			imag.s.lo = 0.0;
 		}
 		
-		else if ((isinf(cDD.s.hi) || isinf(dDD.s.hi)) && isfinite(aDD.s.hi) && isfinite(bDD.s.hi))
+		else if ((crt_isinf(cDD.s.hi) || crt_isinf(dDD.s.hi)) &&
+                         crt_isfinite(aDD.s.hi) && crt_isfinite(bDD.s.hi))
 		{
 			makeFinite(cDD);
 			makeFinite(dDD);
diff --git a/lib/ppc/multc3.c b/lib/ppc/multc3.c
index 9d17a2c..d04a901 100644
--- a/lib/ppc/multc3.c
+++ b/lib/ppc/multc3.c
@@ -3,23 +3,24 @@
  */
 
 #include "DD.h"
+#include "../int_math.h"
 #include <math.h>
 
 #if !defined(INFINITY) && defined(HUGE_VAL)
 #define INFINITY HUGE_VAL
 #endif /* INFINITY */
 
-#define makeFinite(x)	{ \
-							(x).s.hi = __builtin_copysign(isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi); \
-							(x).s.lo = 0.0; \
-						}
+#define makeFinite(x) { \
+    (x).s.hi = __builtin_copysign(crt_isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi); \
+    (x).s.lo = 0.0;                                                     \
+  }
 
-#define zeroNaN(x)		{ \
-							if (isnan((x).s.hi)) { \
-								(x).s.hi = __builtin_copysign(0.0, (x).s.hi); \
-								(x).s.lo = 0.0; \
-							} \
-						}
+#define zeroNaN() { \
+    if (crt_isnan((x).s.hi)) {                                          \
+      (x).s.hi = __builtin_copysign(0.0, (x).s.hi);                     \
+      (x).s.lo = 0.0;                                                   \
+    }                                                                   \
+  }
 
 long double __gcc_qadd(long double, long double);
 long double __gcc_qsub(long double, long double);
@@ -36,7 +37,7 @@
 	DD real = { .ld = __gcc_qsub(ac,bd) };
 	DD imag = { .ld = __gcc_qadd(ad,bc) };
 	
-	if (isnan(real.s.hi) && isnan(imag.s.hi))
+	if (crt_isnan(real.s.hi) && crt_isnan(imag.s.hi))
 	{
 		int recalc = 0;
 		
@@ -45,7 +46,7 @@
 		DD cDD = { .ld = c };
 		DD dDD = { .ld = d };
 		
-		if (isinf(aDD.s.hi) || isinf(bDD.s.hi))
+		if (crt_isinf(aDD.s.hi) || crt_isinf(bDD.s.hi))
 		{
 			makeFinite(aDD);
 			makeFinite(bDD);
@@ -54,7 +55,7 @@
 			recalc = 1;
 		}
 		
-		if (isinf(cDD.s.hi) || isinf(dDD.s.hi))
+		if (crt_isinf(cDD.s.hi) || crt_isinf(dDD.s.hi))
 		{
 			makeFinite(cDD);
 			makeFinite(dDD);
@@ -70,7 +71,8 @@
 			DD adDD = { .ld = ad };
 			DD bcDD = { .ld = bc };
 			
-			if (isinf(acDD.s.hi) || isinf(bdDD.s.hi) || isinf(adDD.s.hi) || isinf(bcDD.s.hi))
+			if (crt_isinf(acDD.s.hi) || crt_isinf(bdDD.s.hi) ||
+                            crt_isinf(adDD.s.hi) || crt_isinf(bcDD.s.hi))
 			{
 				zeroNaN(aDD);
 				zeroNaN(bDD);