From f4b9cf3beffbcc51b5506a92afa4b7dfb82990ba Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 27 Jan 2010 21:16:49 -0500 Subject: [PATCH] cfloat: Add compatibility wrappers for some C99 functions. Commit 784199fac50d52ee5388abd4757c70892646f02a upstream. It was reported that some C99 math functions are not present on FreeBSD. Some of those functions can be implemented "well enough" fairly easily, and whether or not to build them can be controlled by autoconf. These implementations are very simple, and in some cases don't satisfy the all the requirements of the C99 standard. --- altfloat.cabal | 5 ++++- c99-compat.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 11 +++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 c99-compat.c diff --git a/altfloat.cabal b/altfloat.cabal index dff48a0..75cf502 100644 --- a/altfloat.cabal +++ b/altfloat.cabal @@ -10,7 +10,9 @@ Stability: experimental Category: Numerical Build-Type: Simple Extra-Source-Files: configure.ac, configure, altfloat.buildinfo.in, cfloat.h + config.h.in Extra-Tmp-Files: altfloat.buildinfo, config.status config.log, config.cache + config.h Synopsis: Alternative floating point support for GHC. Description: A replacement for the standard Haskell floating point types and supporting @@ -71,7 +73,8 @@ Library else Build-Depends: integer - C-Sources: cfloat.c + Include-Dirs: . + C-Sources: cfloat.c c99-compat.c Exposed-Modules: Data.Floating.CMath, Data.Floating.Classes, diff --git a/c99-compat.c b/c99-compat.c new file mode 100644 index 0000000..ae87729 --- /dev/null +++ b/c99-compat.c @@ -0,0 +1,54 @@ +/* + * Simple implementations of some C99 library functions. + * Note that the goal of altfloat is not to create a C99 math library: these + * functions are intended to just be "good enough" on platforms where they are + * missing. + * + * Copyright (C) 2010 Nick Bowler. + * + * License BSD2: 2-clause BSD license. See LICENSE for full terms. + * This is free software: you are free to change and redistribute it. + * There is NO WARRANTY, to the extent permitted by law. + */ +#include +#include +#include + +#if NEED_LIBM_NAN +double nan(const char *tagp) +{ + return NAN; +} +#endif + +#if NEED_LIBM_LOG2 +double log2(double x) +{ + return log(x) / log(2); +} +#endif + +#if NEED_LIBM_EXP2 +double exp2(double x) +{ + return pow(2, x); +} +#endif + +#if NEED_LIBM_FMA +double fma(double x, double y, double z) +{ + return x*y + z; +} +#endif + +#if NEED_LIBM_REMQUO +double remquo(double x, double y, int *quo) +{ + unsigned tmp = fabs(round(x/y)); + int sign = signbit(x/y) ? -1 : 1; + + *quo = sign * (int)(tmp % (INT_MAX + 1u)); + return remainder(x, y); +} +#endif diff --git a/configure.ac b/configure.ac index 9e0eee0..89b79d7 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ dnl without any warranty. AC_PREREQ(2.62) AC_INIT([altfloat],[0.1],[nbowler@draconx.ca]) AC_CONFIG_SRCDIR([altfloat.cabal]) +AC_CONFIG_HEADER([config.h]) dnl We don't actually care, but this shuts up the warning. AC_ARG_WITH([compiler], [AS_HELP_STRING([--with-compiler], @@ -18,6 +19,16 @@ dnl Cabal won't let us specify the C compiler, so we need this hack. CC_OPTS=`echo $CC | sed 's/@<:@^@<:@:space:@:>@@:>@*//'` AC_SUBST([CC_OPTS]) +dnl Check for some C library functions. +AC_DEFUN([CHECK_LIBM], [AC_SEARCH_LIBS([$1], [m], [], [dnl + AC_DEFINE([NEED_LIBM_]m4_toupper([$1]), [1], + [Define to 1 if you do not have a working $1 function.])])]) +CHECK_LIBM([nan]) +CHECK_LIBM([log2]) +CHECK_LIBM([exp2]) +CHECK_LIBM([fma]) +CHECK_LIBM([remquo]) + AC_CONFIG_FILES([ altfloat.buildinfo ]) -- 2.11.4.GIT