Home | History | Annotate | Download | only in lib
      1 /* Supplemental information about the floating-point formats.
      2    Copyright (C) 2007, 2009-2012 Free Software Foundation, Inc.
      3    Written by Bruno Haible <bruno (at) clisp.org>, 2007.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3, or (at your option)
      8    any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #ifndef _FLOATPLUS_H
     19 #define _FLOATPLUS_H
     20 
     21 #include <float.h>
     22 #include <limits.h>
     23 
     24 /* Number of bits in the mantissa of a floating-point number, including the
     25    "hidden bit".  */
     26 #if FLT_RADIX == 2
     27 # define FLT_MANT_BIT FLT_MANT_DIG
     28 # define DBL_MANT_BIT DBL_MANT_DIG
     29 # define LDBL_MANT_BIT LDBL_MANT_DIG
     30 #elif FLT_RADIX == 4
     31 # define FLT_MANT_BIT (FLT_MANT_DIG * 2)
     32 # define DBL_MANT_BIT (DBL_MANT_DIG * 2)
     33 # define LDBL_MANT_BIT (LDBL_MANT_DIG * 2)
     34 #elif FLT_RADIX == 16
     35 # define FLT_MANT_BIT (FLT_MANT_DIG * 4)
     36 # define DBL_MANT_BIT (DBL_MANT_DIG * 4)
     37 # define LDBL_MANT_BIT (LDBL_MANT_DIG * 4)
     38 #endif
     39 
     40 /* Bit mask that can be used to mask the exponent, as an unsigned number.  */
     41 #define FLT_EXP_MASK ((FLT_MAX_EXP - FLT_MIN_EXP) | 7)
     42 #define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
     43 #define LDBL_EXP_MASK ((LDBL_MAX_EXP - LDBL_MIN_EXP) | 7)
     44 
     45 /* Number of bits used for the exponent of a floating-point number, including
     46    the exponent's sign.  */
     47 #define FLT_EXP_BIT \
     48   (FLT_EXP_MASK < 0x100 ? 8 : \
     49    FLT_EXP_MASK < 0x200 ? 9 : \
     50    FLT_EXP_MASK < 0x400 ? 10 : \
     51    FLT_EXP_MASK < 0x800 ? 11 : \
     52    FLT_EXP_MASK < 0x1000 ? 12 : \
     53    FLT_EXP_MASK < 0x2000 ? 13 : \
     54    FLT_EXP_MASK < 0x4000 ? 14 : \
     55    FLT_EXP_MASK < 0x8000 ? 15 : \
     56    FLT_EXP_MASK < 0x10000 ? 16 : \
     57    FLT_EXP_MASK < 0x20000 ? 17 : \
     58    FLT_EXP_MASK < 0x40000 ? 18 : \
     59    FLT_EXP_MASK < 0x80000 ? 19 : \
     60    FLT_EXP_MASK < 0x100000 ? 20 : \
     61    FLT_EXP_MASK < 0x200000 ? 21 : \
     62    FLT_EXP_MASK < 0x400000 ? 22 : \
     63    FLT_EXP_MASK < 0x800000 ? 23 : \
     64    FLT_EXP_MASK < 0x1000000 ? 24 : \
     65    FLT_EXP_MASK < 0x2000000 ? 25 : \
     66    FLT_EXP_MASK < 0x4000000 ? 26 : \
     67    FLT_EXP_MASK < 0x8000000 ? 27 : \
     68    FLT_EXP_MASK < 0x10000000 ? 28 : \
     69    FLT_EXP_MASK < 0x20000000 ? 29 : \
     70    FLT_EXP_MASK < 0x40000000 ? 30 : \
     71    FLT_EXP_MASK <= 0x7fffffff ? 31 : \
     72    32)
     73 #define DBL_EXP_BIT \
     74   (DBL_EXP_MASK < 0x100 ? 8 : \
     75    DBL_EXP_MASK < 0x200 ? 9 : \
     76    DBL_EXP_MASK < 0x400 ? 10 : \
     77    DBL_EXP_MASK < 0x800 ? 11 : \
     78    DBL_EXP_MASK < 0x1000 ? 12 : \
     79    DBL_EXP_MASK < 0x2000 ? 13 : \
     80    DBL_EXP_MASK < 0x4000 ? 14 : \
     81    DBL_EXP_MASK < 0x8000 ? 15 : \
     82    DBL_EXP_MASK < 0x10000 ? 16 : \
     83    DBL_EXP_MASK < 0x20000 ? 17 : \
     84    DBL_EXP_MASK < 0x40000 ? 18 : \
     85    DBL_EXP_MASK < 0x80000 ? 19 : \
     86    DBL_EXP_MASK < 0x100000 ? 20 : \
     87    DBL_EXP_MASK < 0x200000 ? 21 : \
     88    DBL_EXP_MASK < 0x400000 ? 22 : \
     89    DBL_EXP_MASK < 0x800000 ? 23 : \
     90    DBL_EXP_MASK < 0x1000000 ? 24 : \
     91    DBL_EXP_MASK < 0x2000000 ? 25 : \
     92    DBL_EXP_MASK < 0x4000000 ? 26 : \
     93    DBL_EXP_MASK < 0x8000000 ? 27 : \
     94    DBL_EXP_MASK < 0x10000000 ? 28 : \
     95    DBL_EXP_MASK < 0x20000000 ? 29 : \
     96    DBL_EXP_MASK < 0x40000000 ? 30 : \
     97    DBL_EXP_MASK <= 0x7fffffff ? 31 : \
     98    32)
     99 #define LDBL_EXP_BIT \
    100   (LDBL_EXP_MASK < 0x100 ? 8 : \
    101    LDBL_EXP_MASK < 0x200 ? 9 : \
    102    LDBL_EXP_MASK < 0x400 ? 10 : \
    103    LDBL_EXP_MASK < 0x800 ? 11 : \
    104    LDBL_EXP_MASK < 0x1000 ? 12 : \
    105    LDBL_EXP_MASK < 0x2000 ? 13 : \
    106    LDBL_EXP_MASK < 0x4000 ? 14 : \
    107    LDBL_EXP_MASK < 0x8000 ? 15 : \
    108    LDBL_EXP_MASK < 0x10000 ? 16 : \
    109    LDBL_EXP_MASK < 0x20000 ? 17 : \
    110    LDBL_EXP_MASK < 0x40000 ? 18 : \
    111    LDBL_EXP_MASK < 0x80000 ? 19 : \
    112    LDBL_EXP_MASK < 0x100000 ? 20 : \
    113    LDBL_EXP_MASK < 0x200000 ? 21 : \
    114    LDBL_EXP_MASK < 0x400000 ? 22 : \
    115    LDBL_EXP_MASK < 0x800000 ? 23 : \
    116    LDBL_EXP_MASK < 0x1000000 ? 24 : \
    117    LDBL_EXP_MASK < 0x2000000 ? 25 : \
    118    LDBL_EXP_MASK < 0x4000000 ? 26 : \
    119    LDBL_EXP_MASK < 0x8000000 ? 27 : \
    120    LDBL_EXP_MASK < 0x10000000 ? 28 : \
    121    LDBL_EXP_MASK < 0x20000000 ? 29 : \
    122    LDBL_EXP_MASK < 0x40000000 ? 30 : \
    123    LDBL_EXP_MASK <= 0x7fffffff ? 31 : \
    124    32)
    125 
    126 /* Number of bits used for a floating-point number: the mantissa (not
    127    counting the "hidden bit", since it may or may not be explicit), the
    128    exponent, and the sign.  */
    129 #define FLT_TOTAL_BIT ((FLT_MANT_BIT - 1) + FLT_EXP_BIT + 1)
    130 #define DBL_TOTAL_BIT ((DBL_MANT_BIT - 1) + DBL_EXP_BIT + 1)
    131 #define LDBL_TOTAL_BIT ((LDBL_MANT_BIT - 1) + LDBL_EXP_BIT + 1)
    132 
    133 /* Number of bytes used for a floating-point number.
    134    This can be smaller than the 'sizeof'.  For example, on i386 systems,
    135    'long double' most often have LDBL_MANT_BIT = 64, LDBL_EXP_BIT = 16, hence
    136    LDBL_TOTAL_BIT = 80 bits, i.e. 10 bytes of consecutive memory, but
    137    sizeof (long double) = 12 or = 16.  */
    138 #define SIZEOF_FLT ((FLT_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
    139 #define SIZEOF_DBL ((DBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
    140 #define SIZEOF_LDBL ((LDBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
    141 
    142 /* Verify that SIZEOF_FLT <= sizeof (float) etc.  */
    143 typedef int verify_sizeof_flt[SIZEOF_FLT <= sizeof (float) ? 1 : -1];
    144 typedef int verify_sizeof_dbl[SIZEOF_DBL <= sizeof (double) ? 1 : - 1];
    145 typedef int verify_sizeof_ldbl[SIZEOF_LDBL <= sizeof (long double) ? 1 : - 1];
    146 
    147 #endif /* _FLOATPLUS_H */
    148