softfloat.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. ===============================================================================
  3. This C header file is part of the SoftFloat IEC/IEEE Floating-point
  4. Arithmetic Package, Release 2.
  5. Written by John R. Hauser. This work was made possible in part by the
  6. International Computer Science Institute, located at Suite 600, 1947 Center
  7. Street, Berkeley, California 94704. Funding was partially provided by the
  8. National Science Foundation under grant MIP-9311980. The original version
  9. of this code was written as part of a project to build a fixed-point vector
  10. processor in collaboration with the University of California at Berkeley,
  11. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  12. is available through the Web page
  13. http://www.jhauser.us/arithmetic/SoftFloat-2b/SoftFloat-source.txt
  14. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  15. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  16. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  17. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  18. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  19. Derivative works are acceptable, even for commercial purposes, so long as
  20. (1) they include prominent notice that the work is derivative, and (2) they
  21. include prominent notice akin to these three paragraphs for those parts of
  22. this code that are retained.
  23. ===============================================================================
  24. */
  25. #ifndef __SOFTFLOAT_H__
  26. #define __SOFTFLOAT_H__
  27. /*
  28. -------------------------------------------------------------------------------
  29. The macro `FLOATX80' must be defined to enable the extended double-precision
  30. floating-point format `floatx80'. If this macro is not defined, the
  31. `floatx80' type will not be defined, and none of the functions that either
  32. input or output the `floatx80' type will be defined.
  33. -------------------------------------------------------------------------------
  34. */
  35. #ifdef CONFIG_FPE_NWFPE_XP
  36. #define FLOATX80
  37. #endif
  38. /*
  39. -------------------------------------------------------------------------------
  40. Software IEC/IEEE floating-point types.
  41. -------------------------------------------------------------------------------
  42. */
  43. typedef u32 float32;
  44. typedef u64 float64;
  45. typedef struct {
  46. #ifdef __ARMEB__
  47. u16 __padding;
  48. u16 high;
  49. #else
  50. u16 high;
  51. u16 __padding;
  52. #endif
  53. u64 low;
  54. } __attribute__ ((packed,aligned(4))) floatx80;
  55. /*
  56. -------------------------------------------------------------------------------
  57. Software IEC/IEEE floating-point underflow tininess-detection mode.
  58. -------------------------------------------------------------------------------
  59. */
  60. extern signed char float_detect_tininess;
  61. enum {
  62. float_tininess_after_rounding = 0,
  63. float_tininess_before_rounding = 1
  64. };
  65. /*
  66. -------------------------------------------------------------------------------
  67. Software IEC/IEEE floating-point rounding mode.
  68. -------------------------------------------------------------------------------
  69. */
  70. //extern int8 float_rounding_mode;
  71. enum {
  72. float_round_nearest_even = 0,
  73. float_round_to_zero = 1,
  74. float_round_down = 2,
  75. float_round_up = 3
  76. };
  77. /*
  78. -------------------------------------------------------------------------------
  79. Software IEC/IEEE floating-point exception flags.
  80. -------------------------------------------------------------------------------
  81. enum {
  82. float_flag_inexact = 1,
  83. float_flag_underflow = 2,
  84. float_flag_overflow = 4,
  85. float_flag_divbyzero = 8,
  86. float_flag_invalid = 16
  87. };
  88. ScottB: November 4, 1998
  89. Changed the enumeration to match the bit order in the FPA11.
  90. */
  91. enum {
  92. float_flag_invalid = 1,
  93. float_flag_divbyzero = 2,
  94. float_flag_overflow = 4,
  95. float_flag_underflow = 8,
  96. float_flag_inexact = 16
  97. };
  98. /*
  99. -------------------------------------------------------------------------------
  100. Routine to raise any or all of the software IEC/IEEE floating-point
  101. exception flags.
  102. -------------------------------------------------------------------------------
  103. */
  104. void float_raise( signed char );
  105. /*
  106. -------------------------------------------------------------------------------
  107. Software IEC/IEEE integer-to-floating-point conversion routines.
  108. -------------------------------------------------------------------------------
  109. */
  110. float32 int32_to_float32( struct roundingData *, signed int );
  111. float64 int32_to_float64( signed int );
  112. #ifdef FLOATX80
  113. floatx80 int32_to_floatx80( signed int );
  114. #endif
  115. /*
  116. -------------------------------------------------------------------------------
  117. Software IEC/IEEE single-precision conversion routines.
  118. -------------------------------------------------------------------------------
  119. */
  120. signed int float32_to_int32( struct roundingData *, float32 );
  121. signed int float32_to_int32_round_to_zero( float32 );
  122. float64 float32_to_float64( float32 );
  123. #ifdef FLOATX80
  124. floatx80 float32_to_floatx80( float32 );
  125. #endif
  126. /*
  127. -------------------------------------------------------------------------------
  128. Software IEC/IEEE single-precision operations.
  129. -------------------------------------------------------------------------------
  130. */
  131. float32 float32_round_to_int( struct roundingData*, float32 );
  132. float32 float32_add( struct roundingData *, float32, float32 );
  133. float32 float32_sub( struct roundingData *, float32, float32 );
  134. float32 float32_mul( struct roundingData *, float32, float32 );
  135. float32 float32_div( struct roundingData *, float32, float32 );
  136. float32 float32_rem( struct roundingData *, float32, float32 );
  137. float32 float32_sqrt( struct roundingData*, float32 );
  138. char float32_eq( float32, float32 );
  139. char float32_le( float32, float32 );
  140. char float32_lt( float32, float32 );
  141. char float32_eq_signaling( float32, float32 );
  142. char float32_le_quiet( float32, float32 );
  143. char float32_lt_quiet( float32, float32 );
  144. char float32_is_signaling_nan( float32 );
  145. /*
  146. -------------------------------------------------------------------------------
  147. Software IEC/IEEE double-precision conversion routines.
  148. -------------------------------------------------------------------------------
  149. */
  150. signed int float64_to_int32( struct roundingData *, float64 );
  151. signed int float64_to_int32_round_to_zero( float64 );
  152. float32 float64_to_float32( struct roundingData *, float64 );
  153. #ifdef FLOATX80
  154. floatx80 float64_to_floatx80( float64 );
  155. #endif
  156. /*
  157. -------------------------------------------------------------------------------
  158. Software IEC/IEEE double-precision operations.
  159. -------------------------------------------------------------------------------
  160. */
  161. float64 float64_round_to_int( struct roundingData *, float64 );
  162. float64 float64_add( struct roundingData *, float64, float64 );
  163. float64 float64_sub( struct roundingData *, float64, float64 );
  164. float64 float64_mul( struct roundingData *, float64, float64 );
  165. float64 float64_div( struct roundingData *, float64, float64 );
  166. float64 float64_rem( struct roundingData *, float64, float64 );
  167. float64 float64_sqrt( struct roundingData *, float64 );
  168. char float64_eq( float64, float64 );
  169. char float64_le( float64, float64 );
  170. char float64_lt( float64, float64 );
  171. char float64_eq_signaling( float64, float64 );
  172. char float64_le_quiet( float64, float64 );
  173. char float64_lt_quiet( float64, float64 );
  174. char float64_is_signaling_nan( float64 );
  175. #ifdef FLOATX80
  176. /*
  177. -------------------------------------------------------------------------------
  178. Software IEC/IEEE extended double-precision conversion routines.
  179. -------------------------------------------------------------------------------
  180. */
  181. signed int floatx80_to_int32( struct roundingData *, floatx80 );
  182. signed int floatx80_to_int32_round_to_zero( floatx80 );
  183. float32 floatx80_to_float32( struct roundingData *, floatx80 );
  184. float64 floatx80_to_float64( struct roundingData *, floatx80 );
  185. /*
  186. -------------------------------------------------------------------------------
  187. Software IEC/IEEE extended double-precision operations.
  188. -------------------------------------------------------------------------------
  189. */
  190. floatx80 floatx80_round_to_int( struct roundingData *, floatx80 );
  191. floatx80 floatx80_add( struct roundingData *, floatx80, floatx80 );
  192. floatx80 floatx80_sub( struct roundingData *, floatx80, floatx80 );
  193. floatx80 floatx80_mul( struct roundingData *, floatx80, floatx80 );
  194. floatx80 floatx80_div( struct roundingData *, floatx80, floatx80 );
  195. floatx80 floatx80_rem( struct roundingData *, floatx80, floatx80 );
  196. floatx80 floatx80_sqrt( struct roundingData *, floatx80 );
  197. char floatx80_eq( floatx80, floatx80 );
  198. char floatx80_le( floatx80, floatx80 );
  199. char floatx80_lt( floatx80, floatx80 );
  200. char floatx80_eq_signaling( floatx80, floatx80 );
  201. char floatx80_le_quiet( floatx80, floatx80 );
  202. char floatx80_lt_quiet( floatx80, floatx80 );
  203. char floatx80_is_signaling_nan( floatx80 );
  204. extern flag floatx80_is_nan(floatx80);
  205. #endif
  206. static inline flag extractFloat32Sign(float32 a)
  207. {
  208. return a >> 31;
  209. }
  210. static inline flag float32_eq_nocheck(float32 a, float32 b)
  211. {
  212. return (a == b) || ((bits32) ((a | b) << 1) == 0);
  213. }
  214. static inline flag float32_lt_nocheck(float32 a, float32 b)
  215. {
  216. flag aSign, bSign;
  217. aSign = extractFloat32Sign(a);
  218. bSign = extractFloat32Sign(b);
  219. if (aSign != bSign)
  220. return aSign && ((bits32) ((a | b) << 1) != 0);
  221. return (a != b) && (aSign ^ (a < b));
  222. }
  223. static inline flag extractFloat64Sign(float64 a)
  224. {
  225. return a >> 63;
  226. }
  227. static inline flag float64_eq_nocheck(float64 a, float64 b)
  228. {
  229. return (a == b) || ((bits64) ((a | b) << 1) == 0);
  230. }
  231. static inline flag float64_lt_nocheck(float64 a, float64 b)
  232. {
  233. flag aSign, bSign;
  234. aSign = extractFloat64Sign(a);
  235. bSign = extractFloat64Sign(b);
  236. if (aSign != bSign)
  237. return aSign && ((bits64) ((a | b) << 1) != 0);
  238. return (a != b) && (aSign ^ (a < b));
  239. }
  240. extern flag float32_is_nan( float32 a );
  241. extern flag float64_is_nan( float64 a );
  242. extern int32 float64_to_uint32( struct roundingData *roundData, float64 a );
  243. extern int32 float64_to_uint32_round_to_zero( float64 a );
  244. #endif