ieee754.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 1992-2021 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. <https://www.gnu.org/licenses/>. */
  14. #ifndef _IEEE754_H
  15. #define _IEEE754_H 1
  16. #include <features.h>
  17. #include <bits/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. /* Together these comprise the mantissa. */
  69. unsigned int mantissa1:32;
  70. unsigned int mantissa0:20;
  71. unsigned int exponent:11;
  72. unsigned int negative:1;
  73. #endif /* Little endian. */
  74. } ieee;
  75. /* This format makes it easier to see if a NaN is a signalling NaN. */
  76. struct
  77. {
  78. #if __BYTE_ORDER == __BIG_ENDIAN
  79. unsigned int negative:1;
  80. unsigned int exponent:11;
  81. unsigned int quiet_nan:1;
  82. /* Together these comprise the mantissa. */
  83. unsigned int mantissa0:19;
  84. unsigned int mantissa1:32;
  85. #else
  86. /* Together these comprise the mantissa. */
  87. unsigned int mantissa1:32;
  88. unsigned int mantissa0:19;
  89. unsigned int quiet_nan:1;
  90. unsigned int exponent:11;
  91. unsigned int negative:1;
  92. #endif
  93. } ieee_nan;
  94. };
  95. #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
  96. union ieee854_long_double
  97. {
  98. long double d;
  99. /* This is the IEEE 854 quad-precision format. */
  100. struct
  101. {
  102. #if __BYTE_ORDER == __BIG_ENDIAN
  103. unsigned int negative:1;
  104. unsigned int exponent:15;
  105. /* Together these comprise the mantissa. */
  106. unsigned int mantissa0:16;
  107. unsigned int mantissa1:32;
  108. unsigned int mantissa2:32;
  109. unsigned int mantissa3:32;
  110. #endif /* Big endian. */
  111. #if __BYTE_ORDER == __LITTLE_ENDIAN
  112. /* Together these comprise the mantissa. */
  113. unsigned int mantissa3:32;
  114. unsigned int mantissa2:32;
  115. unsigned int mantissa1:32;
  116. unsigned int mantissa0:16;
  117. unsigned int exponent:15;
  118. unsigned int negative:1;
  119. #endif /* Little endian. */
  120. } ieee;
  121. /* This format makes it easier to see if a NaN is a signalling NaN. */
  122. struct
  123. {
  124. #if __BYTE_ORDER == __BIG_ENDIAN
  125. unsigned int negative:1;
  126. unsigned int exponent:15;
  127. unsigned int quiet_nan:1;
  128. /* Together these comprise the mantissa. */
  129. unsigned int mantissa0:15;
  130. unsigned int mantissa1:32;
  131. unsigned int mantissa2:32;
  132. unsigned int mantissa3:32;
  133. #else
  134. /* Together these comprise the mantissa. */
  135. unsigned int mantissa3:32;
  136. unsigned int mantissa2:32;
  137. unsigned int mantissa1:32;
  138. unsigned int mantissa0:15;
  139. unsigned int quiet_nan:1;
  140. unsigned int exponent:15;
  141. unsigned int negative:1;
  142. #endif
  143. } ieee_nan;
  144. };
  145. #define IEEE854_LONG_DOUBLE_BIAS 0x3fff /* Added to exponent. */
  146. __END_DECLS
  147. #endif /* ieee754.h */