ieee754.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright (C) 1992-2019 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. #include <float.h>
  19. __BEGIN_DECLS
  20. union ieee754_float
  21. {
  22. float f;
  23. /* This is the IEEE 754 single-precision format. */
  24. struct
  25. {
  26. #if __BYTE_ORDER == __BIG_ENDIAN
  27. unsigned int negative:1;
  28. unsigned int exponent:8;
  29. unsigned int mantissa:23;
  30. #endif /* Big endian. */
  31. #if __BYTE_ORDER == __LITTLE_ENDIAN
  32. unsigned int mantissa:23;
  33. unsigned int exponent:8;
  34. unsigned int negative:1;
  35. #endif /* Little endian. */
  36. } ieee;
  37. /* This format makes it easier to see if a NaN is a signalling NaN. */
  38. struct
  39. {
  40. #if __BYTE_ORDER == __BIG_ENDIAN
  41. unsigned int negative:1;
  42. unsigned int exponent:8;
  43. unsigned int quiet_nan:1;
  44. unsigned int mantissa:22;
  45. #endif /* Big endian. */
  46. #if __BYTE_ORDER == __LITTLE_ENDIAN
  47. unsigned int mantissa:22;
  48. unsigned int quiet_nan:1;
  49. unsigned int exponent:8;
  50. unsigned int negative:1;
  51. #endif /* Little endian. */
  52. } ieee_nan;
  53. };
  54. #define IEEE754_FLOAT_BIAS 0x7f /* Added to exponent. */
  55. union ieee754_double
  56. {
  57. double d;
  58. /* This is the IEEE 754 double-precision format. */
  59. struct
  60. {
  61. #if __BYTE_ORDER == __BIG_ENDIAN
  62. unsigned int negative:1;
  63. unsigned int exponent:11;
  64. /* Together these comprise the mantissa. */
  65. unsigned int mantissa0:20;
  66. unsigned int mantissa1:32;
  67. #endif /* Big endian. */
  68. #if __BYTE_ORDER == __LITTLE_ENDIAN
  69. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  70. unsigned int mantissa0:20;
  71. unsigned int exponent:11;
  72. unsigned int negative:1;
  73. unsigned int mantissa1:32;
  74. # else
  75. /* Together these comprise the mantissa. */
  76. unsigned int mantissa1:32;
  77. unsigned int mantissa0:20;
  78. unsigned int exponent:11;
  79. unsigned int negative:1;
  80. # endif
  81. #endif /* Little endian. */
  82. } ieee;
  83. /* This format makes it easier to see if a NaN is a signalling NaN. */
  84. struct
  85. {
  86. #if __BYTE_ORDER == __BIG_ENDIAN
  87. unsigned int negative:1;
  88. unsigned int exponent:11;
  89. unsigned int quiet_nan:1;
  90. /* Together these comprise the mantissa. */
  91. unsigned int mantissa0:19;
  92. unsigned int mantissa1:32;
  93. #else
  94. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  95. unsigned int mantissa0:19;
  96. unsigned int quiet_nan:1;
  97. unsigned int exponent:11;
  98. unsigned int negative:1;
  99. unsigned int mantissa1:32;
  100. # else
  101. /* Together these comprise the mantissa. */
  102. unsigned int mantissa1:32;
  103. unsigned int mantissa0:19;
  104. unsigned int quiet_nan:1;
  105. unsigned int exponent:11;
  106. unsigned int negative:1;
  107. # endif
  108. #endif
  109. } ieee_nan;
  110. };
  111. #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
  112. #if LDBL_MANT_DIG == 113
  113. union ieee854_long_double
  114. {
  115. long double d;
  116. /* This is the IEEE 854 quad-precision format. */
  117. struct
  118. {
  119. #if __BYTE_ORDER == __BIG_ENDIAN
  120. unsigned int negative:1;
  121. unsigned int exponent:15;
  122. /* Together these comprise the mantissa. */
  123. unsigned int mantissa0:16;
  124. unsigned int mantissa1:32;
  125. unsigned int mantissa2:32;
  126. unsigned int mantissa3:32;
  127. #endif /* Big endian. */
  128. #if __BYTE_ORDER == __LITTLE_ENDIAN
  129. /* Together these comprise the mantissa. */
  130. unsigned int mantissa3:32;
  131. unsigned int mantissa2:32;
  132. unsigned int mantissa1:32;
  133. unsigned int mantissa0:16;
  134. unsigned int exponent:15;
  135. unsigned int negative:1;
  136. #endif /* Little endian. */
  137. } ieee;
  138. /* This format makes it easier to see if a NaN is a signalling NaN. */
  139. struct
  140. {
  141. #if __BYTE_ORDER == __BIG_ENDIAN
  142. unsigned int negative:1;
  143. unsigned int exponent:15;
  144. unsigned int quiet_nan:1;
  145. /* Together these comprise the mantissa. */
  146. unsigned int mantissa0:15;
  147. unsigned int mantissa1:32;
  148. unsigned int mantissa2:32;
  149. unsigned int mantissa3:32;
  150. #endif /* Big endian. */
  151. #if __BYTE_ORDER == __LITTLE_ENDIAN
  152. /* Together these comprise the mantissa. */
  153. unsigned int mantissa3:32;
  154. unsigned int mantissa2:32;
  155. unsigned int mantissa1:32;
  156. unsigned int mantissa0:15;
  157. unsigned int quiet_nan:1;
  158. unsigned int exponent:15;
  159. unsigned int negative:1;
  160. #endif /* Little endian. */
  161. } ieee_nan;
  162. };
  163. #define IEEE854_LONG_DOUBLE_BIAS 0x3fff /* Added to exponent. */
  164. #elif LDBL_MANT_DIG == 64
  165. union ieee854_long_double
  166. {
  167. long double d;
  168. /* This is the IEEE 854 double-extended-precision format. */
  169. struct
  170. {
  171. #if __BYTE_ORDER == __BIG_ENDIAN
  172. unsigned int negative:1;
  173. unsigned int exponent:15;
  174. unsigned int empty:16;
  175. unsigned int mantissa0:32;
  176. unsigned int mantissa1:32;
  177. #endif
  178. #if __BYTE_ORDER == __LITTLE_ENDIAN
  179. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  180. unsigned int exponent:15;
  181. unsigned int negative:1;
  182. unsigned int empty:16;
  183. unsigned int mantissa0:32;
  184. unsigned int mantissa1:32;
  185. # else
  186. unsigned int mantissa1:32;
  187. unsigned int mantissa0:32;
  188. unsigned int exponent:15;
  189. unsigned int negative:1;
  190. unsigned int empty:16;
  191. # endif
  192. #endif
  193. } ieee;
  194. /* This is for NaNs in the IEEE 854 double-extended-precision format. */
  195. struct
  196. {
  197. #if __BYTE_ORDER == __BIG_ENDIAN
  198. unsigned int negative:1;
  199. unsigned int exponent:15;
  200. unsigned int empty:16;
  201. unsigned int one:1;
  202. unsigned int quiet_nan:1;
  203. unsigned int mantissa0:30;
  204. unsigned int mantissa1:32;
  205. #endif
  206. #if __BYTE_ORDER == __LITTLE_ENDIAN
  207. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  208. unsigned int exponent:15;
  209. unsigned int negative:1;
  210. unsigned int empty:16;
  211. unsigned int mantissa0:30;
  212. unsigned int quiet_nan:1;
  213. unsigned int one:1;
  214. unsigned int mantissa1:32;
  215. # else
  216. unsigned int mantissa1:32;
  217. unsigned int mantissa0:30;
  218. unsigned int quiet_nan:1;
  219. unsigned int one:1;
  220. unsigned int exponent:15;
  221. unsigned int negative:1;
  222. unsigned int empty:16;
  223. # endif
  224. #endif
  225. } ieee_nan;
  226. };
  227. #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
  228. #elif LDBL_MANT_DIG == 53
  229. union ieee854_long_double
  230. {
  231. long double d;
  232. /* This is the IEEE 754 double-precision format. */
  233. struct
  234. {
  235. #if __BYTE_ORDER == __BIG_ENDIAN
  236. unsigned int negative:1;
  237. unsigned int exponent:11;
  238. /* Together these comprise the mantissa. */
  239. unsigned int mantissa0:20;
  240. unsigned int mantissa1:32;
  241. #endif /* Big endian. */
  242. #if __BYTE_ORDER == __LITTLE_ENDIAN
  243. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  244. unsigned int mantissa0:20;
  245. unsigned int exponent:11;
  246. unsigned int negative:1;
  247. unsigned int mantissa1:32;
  248. # else
  249. /* Together these comprise the mantissa. */
  250. unsigned int mantissa1:32;
  251. unsigned int mantissa0:20;
  252. unsigned int exponent:11;
  253. unsigned int negative:1;
  254. # endif
  255. #endif /* Little endian. */
  256. } ieee;
  257. /* This format makes it easier to see if a NaN is a signalling NaN. */
  258. struct
  259. {
  260. #if __BYTE_ORDER == __BIG_ENDIAN
  261. unsigned int negative:1;
  262. unsigned int exponent:11;
  263. unsigned int quiet_nan:1;
  264. /* Together these comprise the mantissa. */
  265. unsigned int mantissa0:19;
  266. unsigned int mantissa1:32;
  267. #else
  268. # if __FLOAT_WORD_ORDER == __BIG_ENDIAN
  269. unsigned int mantissa0:19;
  270. unsigned int quiet_nan:1;
  271. unsigned int exponent:11;
  272. unsigned int negative:1;
  273. unsigned int mantissa1:32;
  274. # else
  275. /* Together these comprise the mantissa. */
  276. unsigned int mantissa1:32;
  277. unsigned int mantissa0:19;
  278. unsigned int quiet_nan:1;
  279. unsigned int exponent:11;
  280. unsigned int negative:1;
  281. # endif
  282. #endif
  283. } ieee_nan;
  284. };
  285. #define IEEE854_LONG_DOUBLE_BIAS 0x3ff /* Added to exponent. */
  286. #endif /* LDBL_MANT_DIG == 53 */
  287. __END_DECLS
  288. #endif /* ieee754.h */