ieee754.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright (C) 1992, 1995, 1996, 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _IEEE754_H
  15. #define _IEEE754_H 1
  16. #include <features.h>
  17. #include <endian.h>
  18. __BEGIN_DECLS
  19. union ieee754_float
  20. {
  21. float f;
  22. /* This is the IEEE 754 single-precision format. */
  23. struct
  24. {
  25. #if __BYTE_ORDER == __BIG_ENDIAN
  26. unsigned int negative:1;
  27. unsigned int exponent:8;
  28. unsigned int mantissa:23;
  29. #endif /* Big endian. */
  30. #if __BYTE_ORDER == __LITTLE_ENDIAN
  31. unsigned int mantissa:23;
  32. unsigned int exponent:8;
  33. unsigned int negative:1;
  34. #endif /* Little endian. */
  35. } ieee;
  36. /* This format makes it easier to see if a NaN is a signalling NaN. */
  37. struct
  38. {
  39. #if __BYTE_ORDER == __BIG_ENDIAN
  40. unsigned int negative:1;
  41. unsigned int exponent:8;
  42. unsigned int quiet_nan:1;
  43. unsigned int mantissa:22;
  44. #endif /* Big endian. */
  45. #if __BYTE_ORDER == __LITTLE_ENDIAN
  46. unsigned int mantissa:22;
  47. unsigned int quiet_nan:1;
  48. unsigned int exponent:8;
  49. unsigned int negative:1;
  50. #endif /* Little endian. */
  51. } ieee_nan;
  52. };
  53. #define IEEE754_FLOAT_BIAS 0x7f /* Added to exponent. */
  54. union ieee754_double
  55. {
  56. double d;
  57. /* This is the IEEE 754 double-precision format. */
  58. struct
  59. {
  60. #if __BYTE_ORDER == __BIG_ENDIAN
  61. unsigned int negative:1;
  62. unsigned int exponent:11;
  63. /* Together these comprise the mantissa. */
  64. unsigned int mantissa0:20;
  65. unsigned int mantissa1:32;
  66. #endif /* Big endian. */
  67. #if __BYTE_ORDER == __LITTLE_ENDIAN
  68. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  69. unsigned int mantissa0:20;
  70. unsigned int exponent:11;
  71. unsigned int negative:1;
  72. unsigned int mantissa1:32;
  73. # else
  74. /* Together these comprise the mantissa. */
  75. unsigned int mantissa1:32;
  76. unsigned int mantissa0:20;
  77. unsigned int exponent:11;
  78. unsigned int negative:1;
  79. # endif
  80. #endif /* Little endian. */
  81. } ieee;
  82. /* This format makes it easier to see if a NaN is a signalling NaN. */
  83. struct
  84. {
  85. #if __BYTE_ORDER == __BIG_ENDIAN
  86. unsigned int negative:1;
  87. unsigned int exponent:11;
  88. unsigned int quiet_nan:1;
  89. /* Together these comprise the mantissa. */
  90. unsigned int mantissa0:19;
  91. unsigned int mantissa1:32;
  92. #else
  93. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  94. unsigned int mantissa0:19;
  95. unsigned int quiet_nan:1;
  96. unsigned int exponent:11;
  97. unsigned int negative:1;
  98. unsigned int mantissa1:32;
  99. # else
  100. /* Together these comprise the mantissa. */
  101. unsigned int mantissa1:32;
  102. unsigned int mantissa0:19;
  103. unsigned int quiet_nan:1;
  104. unsigned int exponent:11;
  105. unsigned int negative:1;
  106. # endif
  107. #endif
  108. } ieee_nan;
  109. };
  110. #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
  111. union ieee854_long_double
  112. {
  113. long double d;
  114. /* This is the IEEE 854 double-extended-precision format. */
  115. struct
  116. {
  117. #if __BYTE_ORDER == __BIG_ENDIAN
  118. unsigned int negative:1;
  119. unsigned int exponent:15;
  120. unsigned int empty:16;
  121. unsigned int mantissa0:32;
  122. unsigned int mantissa1:32;
  123. #endif
  124. #if __BYTE_ORDER == __LITTLE_ENDIAN
  125. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  126. unsigned int exponent:15;
  127. unsigned int negative:1;
  128. unsigned int empty:16;
  129. unsigned int mantissa0:32;
  130. unsigned int mantissa1:32;
  131. # else
  132. unsigned int mantissa1:32;
  133. unsigned int mantissa0:32;
  134. unsigned int exponent:15;
  135. unsigned int negative:1;
  136. unsigned int empty:16;
  137. # endif
  138. #endif
  139. } ieee;
  140. /* This is for NaNs in the IEEE 854 double-extended-precision format. */
  141. struct
  142. {
  143. #if __BYTE_ORDER == __BIG_ENDIAN
  144. unsigned int negative:1;
  145. unsigned int exponent:15;
  146. unsigned int empty:16;
  147. unsigned int one:1;
  148. unsigned int quiet_nan:1;
  149. unsigned int mantissa0:30;
  150. unsigned int mantissa1:32;
  151. #endif
  152. #if __BYTE_ORDER == __LITTLE_ENDIAN
  153. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  154. unsigned int exponent:15;
  155. unsigned int negative:1;
  156. unsigned int empty:16;
  157. unsigned int mantissa0:30;
  158. unsigned int quiet_nan:1;
  159. unsigned int one:1;
  160. unsigned int mantissa1:32;
  161. # else
  162. unsigned int mantissa1:32;
  163. unsigned int mantissa0:30;
  164. unsigned int quiet_nan:1;
  165. unsigned int one:1;
  166. unsigned int exponent:15;
  167. unsigned int negative:1;
  168. unsigned int empty:16;
  169. # endif
  170. #endif
  171. } ieee_nan;
  172. };
  173. #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
  174. __END_DECLS
  175. #endif /* ieee754.h */