summaryrefslogtreecommitdiff
path: root/lib/ilogb.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2012-04-03 18:59:33 +0200
committerBruno Haible <bruno@clisp.org>2012-04-03 21:14:09 +0200
commit1ab17d9f64900d991443e712f636c9ef6ad543b2 (patch)
treee1a8a3720fbea6455aaf2a40fb01ba0610db69cc /lib/ilogb.c
parent9e7a6e9093ca226915933ed14029ba7efea0efca (diff)
downloadgnulib-1ab17d9f64900d991443e712f636c9ef6ad543b2.tar.gz
New module 'ilogb'.
* lib/math.in.h (ilogb): New declaration. * lib/ilogb.c: New file. * m4/ilogb.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whether ilogb is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_ILOGB, HAVE_ILOGB, REPLACE_ILOGB. * modules/math (Makefile.am): Substitute GNULIB_ILOGB, HAVE_ILOGB, REPLACE_ILOGB. * modules/ilogb: New file. * tests/test-math-c++.cc: Check the declaration of ilogb. * doc/posix-functions/ilogb.texi: Mention the new module.
Diffstat (limited to 'lib/ilogb.c')
-rw-r--r--lib/ilogb.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/ilogb.c b/lib/ilogb.c
new file mode 100644
index 0000000000..2b92a08890
--- /dev/null
+++ b/lib/ilogb.c
@@ -0,0 +1,68 @@
+/* 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>
+
+#include <limits.h>
+
+#ifdef USE_LONG_DOUBLE
+# define ILOGB ilogbl
+# define DOUBLE long double
+# define L_(literal) literal##L
+# define FREXP frexpl
+# define ISNAN isnanl
+#elif ! defined USE_FLOAT
+# define ILOGB ilogb
+# define DOUBLE double
+# define L_(literal) literal
+# define FREXP frexp
+# define ISNAN isnand
+#else /* defined USE_FLOAT */
+# define ILOGB ilogbf
+# define DOUBLE float
+# define L_(literal) literal##f
+# define FREXP frexpf
+# define ISNAN isnanf
+#endif
+
+int
+ILOGB (DOUBLE x)
+{
+ if (isfinite (x))
+ {
+ if (x == L_(0.0))
+ return FP_ILOGB0;
+ else
+ {
+ int e;
+
+ (void) FREXP (x, &e);
+ return e - 1;
+ }
+ }
+ else
+ {
+ if (ISNAN (x))
+ return FP_ILOGBNAN;
+ else
+ return INT_MAX;
+ }
+}