Back to index

nagios-plugins  1.4.16
float+.h
Go to the documentation of this file.
00001 /* Supplemental information about the floating-point formats.
00002    Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
00003    Written by Bruno Haible <bruno@clisp.org>, 2007.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #ifndef _FLOATPLUS_H
00020 #define _FLOATPLUS_H
00021 
00022 #include <float.h>
00023 #include <limits.h>
00024 
00025 /* Number of bits in the mantissa of a floating-point number, including the
00026    "hidden bit".  */
00027 #if FLT_RADIX == 2
00028 # define FLT_MANT_BIT FLT_MANT_DIG
00029 # define DBL_MANT_BIT DBL_MANT_DIG
00030 # define LDBL_MANT_BIT LDBL_MANT_DIG
00031 #elif FLT_RADIX == 4
00032 # define FLT_MANT_BIT (FLT_MANT_DIG * 2)
00033 # define DBL_MANT_BIT (DBL_MANT_DIG * 2)
00034 # define LDBL_MANT_BIT (LDBL_MANT_DIG * 2)
00035 #elif FLT_RADIX == 16
00036 # define FLT_MANT_BIT (FLT_MANT_DIG * 4)
00037 # define DBL_MANT_BIT (DBL_MANT_DIG * 4)
00038 # define LDBL_MANT_BIT (LDBL_MANT_DIG * 4)
00039 #endif
00040 
00041 /* Bit mask that can be used to mask the exponent, as an unsigned number.  */
00042 #define FLT_EXP_MASK ((FLT_MAX_EXP - FLT_MIN_EXP) | 7)
00043 #define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
00044 #define LDBL_EXP_MASK ((LDBL_MAX_EXP - LDBL_MIN_EXP) | 7)
00045 
00046 /* Number of bits used for the exponent of a floating-point number, including
00047    the exponent's sign.  */
00048 #define FLT_EXP_BIT \
00049   (FLT_EXP_MASK < 0x100 ? 8 : \
00050    FLT_EXP_MASK < 0x200 ? 9 : \
00051    FLT_EXP_MASK < 0x400 ? 10 : \
00052    FLT_EXP_MASK < 0x800 ? 11 : \
00053    FLT_EXP_MASK < 0x1000 ? 12 : \
00054    FLT_EXP_MASK < 0x2000 ? 13 : \
00055    FLT_EXP_MASK < 0x4000 ? 14 : \
00056    FLT_EXP_MASK < 0x8000 ? 15 : \
00057    FLT_EXP_MASK < 0x10000 ? 16 : \
00058    FLT_EXP_MASK < 0x20000 ? 17 : \
00059    FLT_EXP_MASK < 0x40000 ? 18 : \
00060    FLT_EXP_MASK < 0x80000 ? 19 : \
00061    FLT_EXP_MASK < 0x100000 ? 20 : \
00062    FLT_EXP_MASK < 0x200000 ? 21 : \
00063    FLT_EXP_MASK < 0x400000 ? 22 : \
00064    FLT_EXP_MASK < 0x800000 ? 23 : \
00065    FLT_EXP_MASK < 0x1000000 ? 24 : \
00066    FLT_EXP_MASK < 0x2000000 ? 25 : \
00067    FLT_EXP_MASK < 0x4000000 ? 26 : \
00068    FLT_EXP_MASK < 0x8000000 ? 27 : \
00069    FLT_EXP_MASK < 0x10000000 ? 28 : \
00070    FLT_EXP_MASK < 0x20000000 ? 29 : \
00071    FLT_EXP_MASK < 0x40000000 ? 30 : \
00072    FLT_EXP_MASK <= 0x7fffffff ? 31 : \
00073    32)
00074 #define DBL_EXP_BIT \
00075   (DBL_EXP_MASK < 0x100 ? 8 : \
00076    DBL_EXP_MASK < 0x200 ? 9 : \
00077    DBL_EXP_MASK < 0x400 ? 10 : \
00078    DBL_EXP_MASK < 0x800 ? 11 : \
00079    DBL_EXP_MASK < 0x1000 ? 12 : \
00080    DBL_EXP_MASK < 0x2000 ? 13 : \
00081    DBL_EXP_MASK < 0x4000 ? 14 : \
00082    DBL_EXP_MASK < 0x8000 ? 15 : \
00083    DBL_EXP_MASK < 0x10000 ? 16 : \
00084    DBL_EXP_MASK < 0x20000 ? 17 : \
00085    DBL_EXP_MASK < 0x40000 ? 18 : \
00086    DBL_EXP_MASK < 0x80000 ? 19 : \
00087    DBL_EXP_MASK < 0x100000 ? 20 : \
00088    DBL_EXP_MASK < 0x200000 ? 21 : \
00089    DBL_EXP_MASK < 0x400000 ? 22 : \
00090    DBL_EXP_MASK < 0x800000 ? 23 : \
00091    DBL_EXP_MASK < 0x1000000 ? 24 : \
00092    DBL_EXP_MASK < 0x2000000 ? 25 : \
00093    DBL_EXP_MASK < 0x4000000 ? 26 : \
00094    DBL_EXP_MASK < 0x8000000 ? 27 : \
00095    DBL_EXP_MASK < 0x10000000 ? 28 : \
00096    DBL_EXP_MASK < 0x20000000 ? 29 : \
00097    DBL_EXP_MASK < 0x40000000 ? 30 : \
00098    DBL_EXP_MASK <= 0x7fffffff ? 31 : \
00099    32)
00100 #define LDBL_EXP_BIT \
00101   (LDBL_EXP_MASK < 0x100 ? 8 : \
00102    LDBL_EXP_MASK < 0x200 ? 9 : \
00103    LDBL_EXP_MASK < 0x400 ? 10 : \
00104    LDBL_EXP_MASK < 0x800 ? 11 : \
00105    LDBL_EXP_MASK < 0x1000 ? 12 : \
00106    LDBL_EXP_MASK < 0x2000 ? 13 : \
00107    LDBL_EXP_MASK < 0x4000 ? 14 : \
00108    LDBL_EXP_MASK < 0x8000 ? 15 : \
00109    LDBL_EXP_MASK < 0x10000 ? 16 : \
00110    LDBL_EXP_MASK < 0x20000 ? 17 : \
00111    LDBL_EXP_MASK < 0x40000 ? 18 : \
00112    LDBL_EXP_MASK < 0x80000 ? 19 : \
00113    LDBL_EXP_MASK < 0x100000 ? 20 : \
00114    LDBL_EXP_MASK < 0x200000 ? 21 : \
00115    LDBL_EXP_MASK < 0x400000 ? 22 : \
00116    LDBL_EXP_MASK < 0x800000 ? 23 : \
00117    LDBL_EXP_MASK < 0x1000000 ? 24 : \
00118    LDBL_EXP_MASK < 0x2000000 ? 25 : \
00119    LDBL_EXP_MASK < 0x4000000 ? 26 : \
00120    LDBL_EXP_MASK < 0x8000000 ? 27 : \
00121    LDBL_EXP_MASK < 0x10000000 ? 28 : \
00122    LDBL_EXP_MASK < 0x20000000 ? 29 : \
00123    LDBL_EXP_MASK < 0x40000000 ? 30 : \
00124    LDBL_EXP_MASK <= 0x7fffffff ? 31 : \
00125    32)
00126 
00127 /* Number of bits used for a floating-point number: the mantissa (not
00128    counting the "hidden bit", since it may or may not be explicit), the
00129    exponent, and the sign.  */
00130 #define FLT_TOTAL_BIT ((FLT_MANT_BIT - 1) + FLT_EXP_BIT + 1)
00131 #define DBL_TOTAL_BIT ((DBL_MANT_BIT - 1) + DBL_EXP_BIT + 1)
00132 #define LDBL_TOTAL_BIT ((LDBL_MANT_BIT - 1) + LDBL_EXP_BIT + 1)
00133 
00134 /* Number of bytes used for a floating-point number.
00135    This can be smaller than the 'sizeof'.  For example, on i386 systems,
00136    'long double' most often have LDBL_MANT_BIT = 64, LDBL_EXP_BIT = 16, hence
00137    LDBL_TOTAL_BIT = 80 bits, i.e. 10 bytes of consecutive memory, but
00138    sizeof (long double) = 12 or = 16.  */
00139 #define SIZEOF_FLT ((FLT_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
00140 #define SIZEOF_DBL ((DBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
00141 #define SIZEOF_LDBL ((LDBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
00142 
00143 /* Verify that SIZEOF_FLT <= sizeof (float) etc.  */
00144 typedef int verify_sizeof_flt[2 * (SIZEOF_FLT <= sizeof (float)) - 1];
00145 typedef int verify_sizeof_dbl[2 * (SIZEOF_DBL <= sizeof (double)) - 1];
00146 typedef int verify_sizeof_ldbl[2 * (SIZEOF_LDBL <= sizeof (long double)) - 1];
00147 
00148 #endif /* _FLOATPLUS_H */