1/* Supplemental information about the floating-point formats. 2 Copyright (C) 2007 Free Software Foundation, Inc. 3 Written by Bruno Haible <bruno@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 2, 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, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 19#ifndef _FLOATPLUS_H 20#define _FLOATPLUS_H 21 22#include <float.h> 23#include <limits.h> 24 25/* Number of bits in the mantissa of a floating-point number, including the 26 "hidden bit". */ 27#if FLT_RADIX == 2 28# define FLT_MANT_BIT FLT_MANT_DIG 29# define DBL_MANT_BIT DBL_MANT_DIG 30# define LDBL_MANT_BIT LDBL_MANT_DIG 31#elif FLT_RADIX == 4 32# define FLT_MANT_BIT (FLT_MANT_DIG * 2) 33# define DBL_MANT_BIT (DBL_MANT_DIG * 2) 34# define LDBL_MANT_BIT (LDBL_MANT_DIG * 2) 35#elif FLT_RADIX == 16 36# define FLT_MANT_BIT (FLT_MANT_DIG * 4) 37# define DBL_MANT_BIT (DBL_MANT_DIG * 4) 38# define LDBL_MANT_BIT (LDBL_MANT_DIG * 4) 39#endif 40 41/* Bit mask that can be used to mask the exponent, as an unsigned number. */ 42#define FLT_EXP_MASK ((FLT_MAX_EXP - FLT_MIN_EXP) | 7) 43#define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7) 44#define LDBL_EXP_MASK ((LDBL_MAX_EXP - LDBL_MIN_EXP) | 7) 45 46/* Number of bits used for the exponent of a floating-point number, including 47 the exponent's sign. */ 48#define FLT_EXP_BIT \ 49 (FLT_EXP_MASK < 0x100 ? 8 : \ 50 FLT_EXP_MASK < 0x200 ? 9 : \ 51 FLT_EXP_MASK < 0x400 ? 10 : \ 52 FLT_EXP_MASK < 0x800 ? 11 : \ 53 FLT_EXP_MASK < 0x1000 ? 12 : \ 54 FLT_EXP_MASK < 0x2000 ? 13 : \ 55 FLT_EXP_MASK < 0x4000 ? 14 : \ 56 FLT_EXP_MASK < 0x8000 ? 15 : \ 57 FLT_EXP_MASK < 0x10000 ? 16 : \ 58 FLT_EXP_MASK < 0x20000 ? 17 : \ 59 FLT_EXP_MASK < 0x40000 ? 18 : \ 60 FLT_EXP_MASK < 0x80000 ? 19 : \ 61 FLT_EXP_MASK < 0x100000 ? 20 : \ 62 FLT_EXP_MASK < 0x200000 ? 21 : \ 63 FLT_EXP_MASK < 0x400000 ? 22 : \ 64 FLT_EXP_MASK < 0x800000 ? 23 : \ 65 FLT_EXP_MASK < 0x1000000 ? 24 : \ 66 FLT_EXP_MASK < 0x2000000 ? 25 : \ 67 FLT_EXP_MASK < 0x4000000 ? 26 : \ 68 FLT_EXP_MASK < 0x8000000 ? 27 : \ 69 FLT_EXP_MASK < 0x10000000 ? 28 : \ 70 FLT_EXP_MASK < 0x20000000 ? 29 : \ 71 FLT_EXP_MASK < 0x40000000 ? 30 : \ 72 FLT_EXP_MASK <= 0x7fffffff ? 31 : \ 73 32) 74#define DBL_EXP_BIT \ 75 (DBL_EXP_MASK < 0x100 ? 8 : \ 76 DBL_EXP_MASK < 0x200 ? 9 : \ 77 DBL_EXP_MASK < 0x400 ? 10 : \ 78 DBL_EXP_MASK < 0x800 ? 11 : \ 79 DBL_EXP_MASK < 0x1000 ? 12 : \ 80 DBL_EXP_MASK < 0x2000 ? 13 : \ 81 DBL_EXP_MASK < 0x4000 ? 14 : \ 82 DBL_EXP_MASK < 0x8000 ? 15 : \ 83 DBL_EXP_MASK < 0x10000 ? 16 : \ 84 DBL_EXP_MASK < 0x20000 ? 17 : \ 85 DBL_EXP_MASK < 0x40000 ? 18 : \ 86 DBL_EXP_MASK < 0x80000 ? 19 : \ 87 DBL_EXP_MASK < 0x100000 ? 20 : \ 88 DBL_EXP_MASK < 0x200000 ? 21 : \ 89 DBL_EXP_MASK < 0x400000 ? 22 : \ 90 DBL_EXP_MASK < 0x800000 ? 23 : \ 91 DBL_EXP_MASK < 0x1000000 ? 24 : \ 92 DBL_EXP_MASK < 0x2000000 ? 25 : \ 93 DBL_EXP_MASK < 0x4000000 ? 26 : \ 94 DBL_EXP_MASK < 0x8000000 ? 27 : \ 95 DBL_EXP_MASK < 0x10000000 ? 28 : \ 96 DBL_EXP_MASK < 0x20000000 ? 29 : \ 97 DBL_EXP_MASK < 0x40000000 ? 30 : \ 98 DBL_EXP_MASK <= 0x7fffffff ? 31 : \ 99 32) 100#define LDBL_EXP_BIT \ 101 (LDBL_EXP_MASK < 0x100 ? 8 : \ 102 LDBL_EXP_MASK < 0x200 ? 9 : \ 103 LDBL_EXP_MASK < 0x400 ? 10 : \ 104 LDBL_EXP_MASK < 0x800 ? 11 : \ 105 LDBL_EXP_MASK < 0x1000 ? 12 : \ 106 LDBL_EXP_MASK < 0x2000 ? 13 : \ 107 LDBL_EXP_MASK < 0x4000 ? 14 : \ 108 LDBL_EXP_MASK < 0x8000 ? 15 : \ 109 LDBL_EXP_MASK < 0x10000 ? 16 : \ 110 LDBL_EXP_MASK < 0x20000 ? 17 : \ 111 LDBL_EXP_MASK < 0x40000 ? 18 : \ 112 LDBL_EXP_MASK < 0x80000 ? 19 : \ 113 LDBL_EXP_MASK < 0x100000 ? 20 : \ 114 LDBL_EXP_MASK < 0x200000 ? 21 : \ 115 LDBL_EXP_MASK < 0x400000 ? 22 : \ 116 LDBL_EXP_MASK < 0x800000 ? 23 : \ 117 LDBL_EXP_MASK < 0x1000000 ? 24 : \ 118 LDBL_EXP_MASK < 0x2000000 ? 25 : \ 119 LDBL_EXP_MASK < 0x4000000 ? 26 : \ 120 LDBL_EXP_MASK < 0x8000000 ? 27 : \ 121 LDBL_EXP_MASK < 0x10000000 ? 28 : \ 122 LDBL_EXP_MASK < 0x20000000 ? 29 : \ 123 LDBL_EXP_MASK < 0x40000000 ? 30 : \ 124 LDBL_EXP_MASK <= 0x7fffffff ? 31 : \ 125 32) 126 127/* Number of bits used for a floating-point number: the mantissa (not 128 counting the "hidden bit", since it may or may not be explicit), the 129 exponent, and the sign. */ 130#define FLT_TOTAL_BIT ((FLT_MANT_BIT - 1) + FLT_EXP_BIT + 1) 131#define DBL_TOTAL_BIT ((DBL_MANT_BIT - 1) + DBL_EXP_BIT + 1) 132#define LDBL_TOTAL_BIT ((LDBL_MANT_BIT - 1) + LDBL_EXP_BIT + 1) 133 134/* Number of bytes used for a floating-point number. 135 This can be smaller than the 'sizeof'. For example, on i386 systems, 136 'long double' most often have LDBL_MANT_BIT = 64, LDBL_EXP_BIT = 16, hence 137 LDBL_TOTAL_BIT = 80 bits, i.e. 10 bytes of consecutive memory, but 138 sizeof (long double) = 12 or = 16. */ 139#define SIZEOF_FLT ((FLT_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT) 140#define SIZEOF_DBL ((DBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT) 141#define SIZEOF_LDBL ((LDBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT) 142 143/* Verify that SIZEOF_FLT <= sizeof (float) etc. */ 144typedef int verify_sizeof_flt[2 * (SIZEOF_FLT <= sizeof (float)) - 1]; 145typedef int verify_sizeof_dbl[2 * (SIZEOF_DBL <= sizeof (double)) - 1]; 146typedef int verify_sizeof_ldbl[2 * (SIZEOF_LDBL <= sizeof (long double)) - 1]; 147 148#endif /* _FLOATPLUS_H */ 149