fpmath.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*-
  2. * Copyright (c) 2003 Mike Barcroft <[email protected]>
  3. * Copyright (c) 2002 David Schultz <[email protected]>
  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. #ifndef _FPMATH_H_
  30. #define _FPMATH_H_
  31. // Currently assumes Intel platform
  32. #if defined (__i386__) || defined(__x86_64__)
  33. #ifdef __LP64__
  34. #include "amd64_fpmath.h"
  35. #else
  36. #include "i386_fpmath.h"
  37. #endif
  38. #endif
  39. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
  40. /* Definitions provided directly by GCC and Clang. */
  41. #define _LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  42. #define _BIG_ENDIAN __ORDER_BIG_ENDIAN__
  43. #define _PDP_ENDIAN __ORDER_PDP_ENDIAN__
  44. #define _BYTE_ORDER __BYTE_ORDER__
  45. #elif defined(__linux)
  46. #include <features.h>
  47. #include <endian.h>
  48. #define _LITTLE_ENDIAN __LITTLE_ENDIAN
  49. #define _BIG_ENDIAN __BIG_ENDIAN
  50. #define _PDP_ENDIAN __PDP_ENDIAN
  51. #define _BYTE_ORDER __BYTE_ORDER
  52. #elif defined(__APPLE__)
  53. #include <machine/endian.h>
  54. #define _LITTLE_ENDIAN LITTLE_ENDIAN
  55. #define _BIG_ENDIAN BIG_ENDIAN
  56. #define _PDP_ENDIAN PDP_ENDIAN
  57. #define _BYTE_ORDER BYTE_ORDER
  58. #elif defined(__FreeBSD__)
  59. #include <machine/endian.h>
  60. #elif defined(_WIN32)
  61. #define _LITTLE_ENDIAN 1234
  62. #define _BIG_ENDIAN 4321
  63. #define _PDP_ENDIAN 3412
  64. #define _BYTE_ORDER _LITTLE_ENDIAN
  65. #define _FLOAT_WORD_ORDER _LITTLE_ENDIAN
  66. #define LITTLE_ENDIAN _LITTLE_ENDIAN
  67. #define BIG_ENDIAN _BIG_ENDIAN
  68. #define PDP_ENDIAN _PDP_ENDIAN
  69. #define BYTE_ORDER _BYTE_ORDER
  70. #endif
  71. #ifndef _IEEE_WORD_ORDER
  72. #define _IEEE_WORD_ORDER _BYTE_ORDER
  73. #endif
  74. union IEEEf2bits {
  75. float f;
  76. struct {
  77. #if _BYTE_ORDER == _LITTLE_ENDIAN
  78. unsigned int man :23;
  79. unsigned int exp :8;
  80. unsigned int sign :1;
  81. #else /* _BIG_ENDIAN */
  82. unsigned int sign :1;
  83. unsigned int exp :8;
  84. unsigned int man :23;
  85. #endif
  86. } bits;
  87. };
  88. #define DBL_MANH_SIZE 20
  89. #define DBL_MANL_SIZE 32
  90. union IEEEd2bits {
  91. double d;
  92. struct {
  93. #if _BYTE_ORDER == _LITTLE_ENDIAN
  94. #if _IEEE_WORD_ORDER == _LITTLE_ENDIAN
  95. unsigned int manl :32;
  96. #endif
  97. unsigned int manh :20;
  98. unsigned int exp :11;
  99. unsigned int sign :1;
  100. #if _IEEE_WORD_ORDER == _BIG_ENDIAN
  101. unsigned int manl :32;
  102. #endif
  103. #else /* _BIG_ENDIAN */
  104. unsigned int sign :1;
  105. unsigned int exp :11;
  106. unsigned int manh :20;
  107. unsigned int manl :32;
  108. #endif
  109. } bits;
  110. };
  111. #endif