Skip to content

Commit

Permalink
Added some missing C99 float operations (#944)
Browse files Browse the repository at this point in the history
* Added some missing C99 float operations
* Update float.template.mli
* Automatically enable shims on VS 2012 and earlier
* Refactor lib-float test
* Add autoconf test for runnable host exes

When building for Windows, configuring with --build and --host enables
cross-compilation mode but configuring with just --host doesn't (since
the resulting executables can be run on the build machine). Adds an
additional check to see whether executables from the C compiler can be
run _regardless_ of autoconf's cross-compilation mode.

* Workaround broken round on mingw-w64
* Generalise the fma test in configure


Co-authored-by: David Allsopp <david.allsopp@metastack.com>
  • Loading branch information
mmottl and dra27 committed Jun 2, 2021
1 parent b5821db commit 7eaf05b
Show file tree
Hide file tree
Showing 17 changed files with 985 additions and 189 deletions.
11 changes: 11 additions & 0 deletions Changes
Expand Up @@ -150,6 +150,17 @@ Working version

### Standard library:

- #944: Add some missing C99 float operations. `Stdlib` now contains
the inverse hyperbolic functions `acosh`, `asinh`, and `atanh`. These
functions were also added to module `Stdlib.Float` together with `exp2`,
`log2`, `cbrt`, `erf`, and `erfc`. Full support on MSVC requires VS2013+ but
emulated versions are still available (for now) for older compilers.
(Markus Mottl, review by David Allsopp, Olivier Andrieu, Florian Angeletti,
Nicolás Ojeda Bär, Daniel Bünzli, Fabian @copy, Pascal Cuoq, Damien
Doligez, Sébastien Hinderer, Jacques-Henri Jourdan, Xavier Leroy, Guillaume
Melquiond, Perry E. Metzger, @objmagic, Gabriel Scherer, Mark Shinwell,
Bernhard Schommer and Christophe Troestler)

- #9448: Add String.{empty,cat} as dual of Bytes.{empty,cat},
String.{of,to}_bytes as aliases of Bytes.{to,of}_string,
Bytes.split_on_char as dual of String.split_on_char, and binary decoding
Expand Down
9 changes: 9 additions & 0 deletions INSTALL.adoc
Expand Up @@ -164,3 +164,12 @@ and sanity checks that could help you pinpoint the problem.
* On HP 9000/700 machines under HP/UX 9, some versions of `cc` are unable to
compile correctly the runtime system (wrong code is generated for `(x - y)`
where `x` is a pointer and `y` an integer). Fix: use `gcc`.

* In the unlikely case that a platform does not offer all C99 float operations
that the runtime needs, a configuration error will result. Users
can work around this problem by calling `configure` with the flag
`--enable-imprecise-c99-float-ops`. This will enable simple but potentially
imprecise implementations of C99 float operations. Users with exacting
requirements for mathematical accuracy, numerical precision, and proper
handling of mathematical corner cases and error conditions may need to
consider running their code on a platform with better C99 support.
115 changes: 114 additions & 1 deletion aclocal.m4
Expand Up @@ -58,7 +58,8 @@ unknown
#endif]
)],
[AC_CACHE_VAL([ocaml_cv_cc_vendor],
[ocaml_cv_cc_vendor=`grep ['^[a-z]'] conftest.i | tr -s ' ' '-'`])],
[ocaml_cv_cc_vendor=`grep ['^[a-z]'] conftest.i | tr -s ' ' '-' \
| tr -d '\r'`])],
[AC_MSG_FAILURE([unexpected preprocessor failure])])
AC_MSG_RESULT([$ocaml_cv_cc_vendor])
])
Expand Down Expand Up @@ -356,3 +357,115 @@ EOF
OCAML_CC_RESTORE_VARIABLES
])

AC_DEFUN([OCAML_HOST_IS_EXECUTABLE], [
AC_MSG_CHECKING([whether host executables can be run in the build])
old_cross_compiling="$cross_compiling"
cross_compiling='no'
AC_RUN_IFELSE(
[AC_LANG_SOURCE([[int main (void) {return 0;}]])],
[AC_MSG_RESULT([yes])
host_runnable=true],
[AC_MSG_RESULT([no])
host_runnable=false],
# autoconf displays a warning if this parameter is missing, but
# cross-compilation mode was disabled above.
[assert=false])
cross_compiling="$old_cross_compiling"
])

# This is AC_RUN_IFELSE but taking $host_runnable into account (i.e. if the
# program can be run, then it is run)
AC_DEFUN([OCAML_RUN_IFELSE], [
old_cross_compiling="$cross_compiling"
AS_IF([test "x$host_runnable" = 'xtrue'], [cross_compiling='no'])
AC_RUN_IFELSE([$1],[$2],[$3],[$4])
cross_compiling="$old_cross_compiling"
])

AC_DEFUN([OCAML_C99_CHECK_ROUND], [
AC_MSG_CHECKING([whether round works])
OCAML_RUN_IFELSE(
[AC_LANG_SOURCE([[
#include <math.h>
int main (void) {
static volatile double d = 0.49999999999999994449;
return (fpclassify(round(d)) != FP_ZERO);
}
]])],
[AC_MSG_RESULT([yes])
AC_DEFINE([HAS_WORKING_ROUND])],
[AC_MSG_RESULT([no])
AS_CASE([$enable_imprecise_c99_float_ops,$target],
[no,*], [hard_error=true],
[yes,*], [hard_error=false],
[*,x86_64-w64-mingw32], [hard_error=false],
[hard_error=true])
AS_IF([test x"$hard_error" = "xtrue"],
[AC_MSG_ERROR(m4_normalize([
round does not work, enable emulation with
--enable-imprecise-c99-float-ops]))],
[AC_MSG_WARN(m4_normalize([
round does not work; emulation enabled]))])],
[AS_CASE([$target],
[x86_64-w64-mingw32],[AC_MSG_RESULT([cross-compiling; assume not])],
[AC_MSG_RESULT([cross-compiling; assume yes])
AC_DEFINE([HAS_WORKING_ROUND])])])
])

AC_DEFUN([OCAML_C99_CHECK_FMA], [
AC_MSG_CHECKING([whether fma works])
OCAML_RUN_IFELSE(
[AC_LANG_SOURCE([[
#include <math.h>
int main (void) {
/* Tests 264-266 from testsuite/tests/fma/fma.ml. These tests trigger the
broken implementations of Cygwin64, mingw-w64 (x86_64) and VS2013-2017.
The static volatile variables aim to thwart GCC's constant folding. */
static volatile double x, y, z;
double t264, t265, t266;
x = 0x3.bd5b7dde5fddap-496;
y = 0x3.bd5b7dde5fddap-496;
z = -0xd.fc352bc352bap-992;
t264 = fma(x, y, z);
x = 0x3.bd5b7dde5fddap-504;
y = 0x3.bd5b7dde5fddap-504;
z = -0xd.fc352bc352bap-1008;
t265 = fma(x, y, z);
x = 0x8p-540;
y = 0x4p-540;
z = 0x4p-1076;
t266 = fma(x, y, z);
return (!(t264 == 0x1.0989687cp-1044 ||
t264 == 0x0.000004277ca1fp-1022 || /* Acceptable emulated values */
t264 == 0x0.00000428p-1022)
|| !(t265 == 0x1.0988p-1060 ||
t265 == 0x0.0000000004278p-1022 || /* Acceptable emulated values */
t265 == 0x0.000000000428p-1022)
|| !(t266 == 0x8p-1076));
}
]])],
[AC_MSG_RESULT([yes])
AC_DEFINE([HAS_WORKING_FMA])],
[AC_MSG_RESULT([no])
AS_CASE([$enable_imprecise_c99_float_ops,$target],
[no,*], [hard_error=true],
[yes,*], [hard_error=false],
[*,x86_64-w64-mingw32|*,x86_64-*-cygwin*], [hard_error=false],
[AS_CASE([$ocaml_cv_cc_vendor],
[msvc-*], [AS_IF([test "${ocaml_cv_cc_vendor#msvc-}" -lt 1920 ],
[hard_error=false],
[hard_error=true])],
[hard_error=true])])
AS_IF([test x"$hard_error" = "xtrue"],
[AC_MSG_ERROR(m4_normalize([
fma does not work, enable emulation with
--enable-imprecise-c99-float-ops]))],
[AC_MSG_WARN(m4_normalize([
fma does not work; emulation enabled]))])],
[AS_CASE([$target],
[x86_64-w64-mingw32|x86_64-*-cygwin*],
[AC_MSG_RESULT([cross-compiling; assume not])],
[AC_MSG_RESULT([cross-compiling; assume yes])
AC_DEFINE([HAS_WORKING_FMA])])])
])

0 comments on commit 7eaf05b

Please sign in to comment.