maint: update copyright
[gnulib.git] / lib / ilogb.c
1 /* Floating-point exponent.
2    Copyright (C) 2012-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #if ! (defined USE_LONG_DOUBLE || defined USE_FLOAT)
18 # include <config.h>
19 #endif
20
21 /* Specification.  */
22 #include <math.h>
23
24 #include <limits.h>
25
26 #ifdef USE_LONG_DOUBLE
27 # define ILOGB ilogbl
28 # define DOUBLE long double
29 # define L_(literal) literal##L
30 # define FREXP frexpl
31 # define ISNAN isnanl
32 #elif ! defined USE_FLOAT
33 # define ILOGB ilogb
34 # define DOUBLE double
35 # define L_(literal) literal
36 # define FREXP frexp
37 # define ISNAN isnand
38 #else /* defined USE_FLOAT */
39 # define ILOGB ilogbf
40 # define DOUBLE float
41 # define L_(literal) literal##f
42 # define FREXP frexpf
43 # define ISNAN isnanf
44 #endif
45
46 int
47 ILOGB (DOUBLE x)
48 {
49   if (isfinite (x))
50     {
51       if (x == L_(0.0))
52         return FP_ILOGB0;
53       else
54         {
55           int e;
56
57           (void) FREXP (x, &e);
58           return e - 1;
59         }
60     }
61   else
62     {
63       if (ISNAN (x))
64         return FP_ILOGBNAN;
65       else
66         return INT_MAX;
67     }
68 }