Upgrade libm.

This brings us up to date with FreeBSD HEAD, fixes various bugs, unifies
the set of functions we support on ARM, MIPS, and x86, fixes "long double",
adds ISO C99 support, and adds basic unit tests.

It turns out that our "long double" functions have always been broken
for non-normal numbers. This patch fixes that by not using the upstream
implementations and just forwarding to the regular "double" implementation
instead (since "long double" on Android is just "double" anyway, which is
what BSD doesn't support).

All the tests pass on ARM, MIPS, and x86, plus glibc on x86-64.

Bug: 3169850
Bug: 8012787
Bug: https://code.google.com/p/android/issues/detail?id=6697
Change-Id: If0c343030959c24bfc50d4d21c9530052c581837
diff --git a/libc/arch-arm/include/machine/_types.h b/libc/arch-arm/include/machine/_types.h
index 1521a4c..0a8b610 100644
--- a/libc/arch-arm/include/machine/_types.h
+++ b/libc/arch-arm/include/machine/_types.h
@@ -87,6 +87,8 @@
 /* Standard system types */
 typedef int			__clock_t;
 typedef int			__clockid_t;
+typedef double			__double_t;
+typedef float			__float_t;
 typedef long			__ptrdiff_t;
 typedef	int			__time_t;
 typedef int			__timer_t;
diff --git a/libc/arch-x86/include/machine/_types.h b/libc/arch-x86/include/machine/_types.h
index d217822..3806f78 100644
--- a/libc/arch-x86/include/machine/_types.h
+++ b/libc/arch-x86/include/machine/_types.h
@@ -87,6 +87,8 @@
 /* Standard system types */
 typedef int			__clock_t;
 typedef int			__clockid_t;
+typedef double			__double_t;
+typedef float			__float_t;
 typedef long			__ptrdiff_t;
 typedef	int			__time_t;
 typedef int			__timer_t;
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index ca81cb6..937abe0 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -177,6 +177,8 @@
 #define	__unused	/* delete */
 #endif
 
+#define __pure2 __attribute__((__const__)) /* Android-added: used by FreeBSD libm */
+
 #if __GNUC_PREREQ__(3, 1)
 #define	__used		__attribute__((__used__))
 #else
@@ -313,6 +315,12 @@
 #define __purefunc
 #endif
 
+#if __GNUC_PREREQ__(3, 1)
+#define __always_inline __attribute__((__always_inline__))
+#else
+#define __always_inline
+#endif
+
 /*
  * Macros for manipulating "link sets".  Link sets are arrays of pointers
  * to objects, which are gathered up by the linker.
@@ -510,4 +518,11 @@
 #define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
 #endif
 
+/* Android-added: for FreeBSD's libm. */
+#define __weak_reference(sym,alias) \
+    __asm__(".weak " #alias); \
+    __asm__(".equ "  #alias ", " #sym)
+#define __strong_reference(sym,aliassym) \
+    extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
+
 #endif /* !_SYS_CDEFS_H_ */
diff --git a/libc/tools/check-symbols.py b/libc/tools/check-symbols.py
new file mode 100755
index 0000000..0922548
--- /dev/null
+++ b/libc/tools/check-symbols.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python
+
+import glob
+import os
+import re
+import string
+import subprocess
+import sys
+
+toolchain = os.environ['ANDROID_TOOLCHAIN']
+arch = re.sub(r'.*/linux-x86/([^/]+)/.*', r'\1', toolchain)
+
+sys.stderr.write('Checking symbols for arch "%s"...\n' % arch)
+
+def GetSymbols(library, functions_or_variables):
+  api = '9'
+  if library == 'libm' and arch == 'arm':
+    api = '3'
+  path = '%s/development/ndk/platforms/android-%s/arch-%s/symbols/%s.so.%s.txt' % (os.environ['ANDROID_BUILD_TOP'], api, arch, library, functions_or_variables)
+  symbols = set()
+  for line in open(path, 'r'):
+    symbols.add(line.rstrip())
+  #sys.stdout.write('%d %s in %s for %s\n' % (len(symbols), functions_or_variables, library, arch))
+  return symbols
+
+def CheckSymbols(library, functions_or_variables):
+  expected_symbols = GetSymbols(library, functions_or_variables)
+
+  so_file = '%s/system/lib/%s.so' % (os.environ['ANDROID_PRODUCT_OUT'], library)
+
+  # Example readelf output:
+  #   264: 0001623c     4 FUNC    GLOBAL DEFAULT    8 cabsf
+  #   266: 00016244     4 FUNC    GLOBAL DEFAULT    8 dremf
+  #   267: 00019018     4 OBJECT  GLOBAL DEFAULT   11 __fe_dfl_env
+  #   268: 00000000     0 FUNC    GLOBAL DEFAULT  UND __aeabi_dcmplt
+
+
+  r = re.compile(r' +\d+: [0-9a-f]+ +\d+ (FUNC|OBJECT) +\S+ +\S+ +\d+ (\S+)')
+
+  actual_symbols = set()
+  for line in subprocess.check_output(['readelf', '--dyn-syms', so_file]).split('\n'):
+    m = r.match(line)
+    if m:
+      if m.group(1) == 'FUNC' and functions_or_variables == 'functions':
+        actual_symbols.add(m.group(2))
+      elif m.group(1) == 'OBJECT' and functions_or_variables == 'variables':
+        actual_symbols.add(m.group(2))
+    #else:
+      #print 'ignoring: ' % line
+
+  missing = expected_symbols - actual_symbols
+  if len(missing) > 0:
+    sys.stderr.write('%d missing %s in %s for %s:\n' % (len(missing), functions_or_variables, library, arch))
+    for miss in sorted(missing):
+      sys.stderr.write('  %s\n' % miss)
+
+  return len(missing) == 0
+
+CheckSymbols("libc", "functions")
+CheckSymbols("libc", "variables")
+CheckSymbols("libm", "functions")
+CheckSymbols("libm", "variables")
+
+sys.exit(0)