fpmath.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*-
  2. * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
  3. * Copyright (c) 2002 David Schultz <das@FreeBSD.ORG>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * $FreeBSD: src/lib/libc/include/fpmath.h,v 1.4 2008/12/23 22:20:59 marcel Exp $
  28. */
  29. // Currently assumes Intel platform
  30. #ifdef __LP64__
  31. #include "amd64_fpmath.h"
  32. #else
  33. #include "i386_fpmath.h"
  34. #endif
  35. #ifdef __linux
  36. #include <features.h>
  37. #include <endian.h>
  38. #define _LITTLE_ENDIAN __LITTLE_ENDIAN
  39. #define _BIG_ENDIAN __BIG_ENDIAN
  40. #define _PDP_ENDIAN __PDP_ENDIAN
  41. #define _BYTE_ORDER __BYTE_ORDER
  42. #endif
  43. #ifdef __APPLE__
  44. #include <machine/endian.h>
  45. #define _LITTLE_ENDIAN LITTLE_ENDIAN
  46. #define _BIG_ENDIAN BIG_ENDIAN
  47. #define _PDP_ENDIAN PDP_ENDIAN
  48. #define _BYTE_ORDER BYTE_ORDER
  49. #endif
  50. #ifdef WIN32
  51. #define _LITTLE_ENDIAN 1234
  52. #define _BIG_ENDIAN 4321
  53. #define _PDP_ENDIAN 3412
  54. #define _BYTE_ORDER __LITTLE_ENDIAN
  55. #define _FLOAT_WORD_ORDER __LITTLE_ENDIAN
  56. #define LITTLE_ENDIAN __LITTLE_ENDIAN
  57. #define BIG_ENDIAN __BIG_ENDIAN
  58. #define PDP_ENDIAN __PDP_ENDIAN
  59. #define BYTE_ORDER __BYTE_ORDER
  60. #endif
  61. #ifndef _IEEE_WORD_ORDER
  62. #define _IEEE_WORD_ORDER _BYTE_ORDER
  63. #endif
  64. union IEEEf2bits {
  65. float f;
  66. struct {
  67. #if _BYTE_ORDER == _LITTLE_ENDIAN
  68. unsigned int man :23;
  69. unsigned int exp :8;
  70. unsigned int sign :1;
  71. #else /* _BIG_ENDIAN */
  72. unsigned int sign :1;
  73. unsigned int exp :8;
  74. unsigned int man :23;
  75. #endif
  76. } bits;
  77. };
  78. #define DBL_MANH_SIZE 20
  79. #define DBL_MANL_SIZE 32
  80. union IEEEd2bits {
  81. double d;
  82. struct {
  83. #if _BYTE_ORDER == _LITTLE_ENDIAN
  84. #if _IEEE_WORD_ORDER == _LITTLE_ENDIAN
  85. unsigned int manl :32;
  86. #endif
  87. unsigned int manh :20;
  88. unsigned int exp :11;
  89. unsigned int sign :1;
  90. #if _IEEE_WORD_ORDER == _BIG_ENDIAN
  91. unsigned int manl :32;
  92. #endif
  93. #else /* _BIG_ENDIAN */
  94. unsigned int sign :1;
  95. unsigned int exp :11;
  96. unsigned int manh :20;
  97. unsigned int manl :32;
  98. #endif
  99. } bits;
  100. };