float+.h 5.5 KB

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