maint: update copyright
[gnulib.git] / lib / logb.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 #ifdef USE_LONG_DOUBLE
25 # define LOGB logbl
26 # define DOUBLE long double
27 # define L_(literal) literal##L
28 # define HUGEVAL HUGE_VALL
29 # define FREXP frexpl
30 # define ISNAN isnanl
31 #elif ! defined USE_FLOAT
32 # define LOGB logb
33 # define DOUBLE double
34 # define L_(literal) literal
35 # define HUGEVAL HUGE_VAL
36 # define FREXP frexp
37 # define ISNAN isnand
38 #else /* defined USE_FLOAT */
39 # define LOGB logbf
40 # define DOUBLE float
41 # define L_(literal) literal##f
42 # define HUGEVAL HUGE_VALF
43 # define FREXP frexpf
44 # define ISNAN isnanf
45 #endif
46
47 DOUBLE
48 LOGB (DOUBLE x)
49 {
50   if (isfinite (x))
51     {
52       if (x == L_(0.0))
53         /* Return -Infinity.  */
54         return - HUGEVAL;
55       else
56         {
57           int e;
58
59           (void) FREXP (x, &e);
60           return (DOUBLE) (e - 1);
61         }
62     }
63   else
64     {
65       if (ISNAN (x))
66         return x; /* NaN */
67       else
68         /* Return +Infinity.  */
69         return HUGEVAL;
70     }
71 }