summaryrefslogtreecommitdiff
path: root/lib/logb.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2012-04-03 02:51:42 +0200
committerBruno Haible <bruno@clisp.org>2012-04-03 13:58:10 +0200
commit77e408383e7944caaf77cb068e670238aa7ee905 (patch)
tree51b65542ea5dd5fefd8a9d0e74e7829d4c2e02d9 /lib/logb.c
parentcd2e4d4bfbf4bfbc8d2155969a63605759f8f243 (diff)
downloadgnulib-77e408383e7944caaf77cb068e670238aa7ee905.tar.gz
logb: Provide replacement and workarounds.
* lib/math.in.h (logb): Ensure declaration. Replace if REPLACE_LOGB is 1. * lib/logb.c: New file. * m4/logb.m4 (gl_FUNC_LOGB_WORKS): New macro. (gl_FUNC_LOGB): Invoke it. Set HAVE_LOGB, REPLACE_LOGB. * m4/math_h.m4 (gl_MATH_H_DEFAULTS): Initialize REPLACE_LOGB. * modules/math (Makefile.am): Substitute REPLACE_LOGB. * modules/logb (Files): Add lib/logb.c. (Depends-on): Add isfinite, frexp, isnand. (configure.ac): Compile the replacement code logb.c if needed. * tests/test-math-c++.cc: Check the declaration of logb. * doc/posix-functions/logb.texi: Mention the replacement and the bug with subnormal numbers.
Diffstat (limited to 'lib/logb.c')
-rw-r--r--lib/logb.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/logb.c b/lib/logb.c
new file mode 100644
index 0000000000..d56be557a7
--- /dev/null
+++ b/lib/logb.c
@@ -0,0 +1,71 @@
+/* Floating-point exponent.
+ Copyright (C) 2012 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#if ! (defined USE_LONG_DOUBLE || defined USE_FLOAT)
+# include <config.h>
+#endif
+
+/* Specification. */
+#include <math.h>
+
+#ifdef USE_LONG_DOUBLE
+# define LOGB logbl
+# define DOUBLE long double
+# define L_(literal) literal##L
+# define HUGEVAL HUGE_VALL
+# define FREXP frexpl
+# define ISNAN isnanl
+#elif ! defined USE_FLOAT
+# define LOGB logb
+# define DOUBLE double
+# define L_(literal) literal
+# define HUGEVAL HUGE_VAL
+# define FREXP frexp
+# define ISNAN isnand
+#else /* defined USE_FLOAT */
+# define LOGB logbf
+# define DOUBLE float
+# define L_(literal) literal##f
+# define HUGEVAL HUGE_VALF
+# define FREXP frexpf
+# define ISNAN isnanf
+#endif
+
+DOUBLE
+LOGB (DOUBLE x)
+{
+ if (isfinite (x))
+ {
+ if (x == L_(0.0))
+ /* Return -Infinity. */
+ return - HUGEVAL;
+ else
+ {
+ int e;
+
+ (void) FREXP (x, &e);
+ return (DOUBLE) (e - 1);
+ }
+ }
+ else
+ {
+ if (ISNAN (x))
+ return x; /* NaN */
+ else
+ /* Return +Infinity. */
+ return HUGEVAL;
+ }
+}