softfloat.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* $OpenBSD: softfloat.h,v 1.5 2014/06/10 04:16:57 deraadt Exp $ */
  2. /* $NetBSD: softfloat.h,v 1.1 2001/04/26 03:10:48 ross Exp $ */
  3. /* This is a derivative work. */
  4. /*-
  5. * Copyright (c) 2001 The NetBSD Foundation, Inc.
  6. * All rights reserved.
  7. *
  8. * This code is derived from software contributed to The NetBSD Foundation
  9. * by Ross Harvey.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. ===============================================================================
  34. This C header file is part of the SoftFloat IEC/IEEE Floating-point
  35. Arithmetic Package, Release 2a.
  36. Written by John R. Hauser. This work was made possible in part by the
  37. International Computer Science Institute, located at Suite 600, 1947 Center
  38. Street, Berkeley, California 94704. Funding was partially provided by the
  39. National Science Foundation under grant MIP-9311980. The original version
  40. of this code was written as part of a project to build a fixed-point vector
  41. processor in collaboration with the University of California at Berkeley,
  42. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  43. is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  44. arithmetic/SoftFloat.html'.
  45. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable
  46. effort has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT
  47. WILL AT TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS
  48. RESTRICTED TO PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL
  49. RESPONSIBILITY FOR ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM
  50. THEIR OWN USE OF THE SOFTWARE, AND WHO ALSO EFFECTIVELY INDEMNIFY
  51. (possibly via similar legal warning) JOHN HAUSER AND THE INTERNATIONAL
  52. COMPUTER SCIENCE INSTITUTE AGAINST ALL LOSSES, COSTS, OR OTHER PROBLEMS
  53. ARISING FROM THE USE OF THE SOFTWARE BY THEIR CUSTOMERS AND CLIENTS.
  54. Derivative works are acceptable, even for commercial purposes, so long as
  55. (1) they include prominent notice that the work is derivative, and (2) they
  56. include prominent notice akin to these four paragraphs for those parts of
  57. this code that are retained.
  58. ===============================================================================
  59. */
  60. #include <sys/types.h>
  61. #include "machine/ieeefp.h"
  62. #include <sys/endian.h>
  63. /*
  64. -------------------------------------------------------------------------------
  65. The macro `FLOATX80' must be defined to enable the extended double-precision
  66. floating-point format `floatx80'. If this macro is not defined, the
  67. `floatx80' type will not be defined, and none of the functions that either
  68. input or output the `floatx80' type will be defined. The same applies to
  69. the `FLOAT128' macro and the quadruple-precision format `float128'.
  70. -------------------------------------------------------------------------------
  71. */
  72. /* #define FLOATX80 */
  73. /* #define FLOAT128 */
  74. /*
  75. -------------------------------------------------------------------------------
  76. Software IEC/IEEE floating-point types.
  77. -------------------------------------------------------------------------------
  78. */
  79. typedef u_int32_t float32;
  80. typedef u_int64_t float64;
  81. #ifdef FLOATX80
  82. typedef struct {
  83. #if BYTE_ORDER == BIG_ENDIAN
  84. u_int16_t high;
  85. u_int64_t low;
  86. #else
  87. u_int64_t low;
  88. u_int16_t high;
  89. #endif
  90. } floatx80;
  91. #endif
  92. #ifdef FLOAT128
  93. typedef struct {
  94. u_int64_t high, low;
  95. } float128;
  96. #endif
  97. /*
  98. * Some of the global variables that used to be here have been removed for
  99. * fairly obvious (defopt-MULTIPROCESSOR) reasons. The rest (which don't
  100. * change dynamically) will be removed later. [ross]
  101. */
  102. #define float_rounding_mode() fpgetround()
  103. /*
  104. -------------------------------------------------------------------------------
  105. Software IEC/IEEE floating-point underflow tininess-detection mode.
  106. -------------------------------------------------------------------------------
  107. */
  108. extern int float_detect_tininess;
  109. enum {
  110. float_tininess_after_rounding = 1,
  111. float_tininess_before_rounding = 0
  112. };
  113. /*
  114. -------------------------------------------------------------------------------
  115. Software IEC/IEEE floating-point rounding mode.
  116. -------------------------------------------------------------------------------
  117. */
  118. enum {
  119. float_round_nearest_even = FP_RN,
  120. float_round_to_zero = FP_RZ,
  121. float_round_down = FP_RM,
  122. float_round_up = FP_RP
  123. };
  124. /*
  125. -------------------------------------------------------------------------------
  126. Software IEC/IEEE floating-point exception flags.
  127. -------------------------------------------------------------------------------
  128. */
  129. enum {
  130. float_flag_inexact = FP_X_IMP,
  131. float_flag_underflow = FP_X_UFL,
  132. float_flag_overflow = FP_X_OFL,
  133. float_flag_divbyzero = FP_X_DZ,
  134. float_flag_invalid = FP_X_INV
  135. };
  136. /*
  137. -------------------------------------------------------------------------------
  138. Software IEC/IEEE integer-to-floating-point conversion routines.
  139. -------------------------------------------------------------------------------
  140. */
  141. float32 int32_to_float32( int );
  142. float64 int32_to_float64( int );
  143. #ifdef FLOATX80
  144. floatx80 int32_to_floatx80( int );
  145. #endif
  146. #ifdef FLOAT128
  147. float128 int32_to_float128( int );
  148. #endif
  149. #ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */
  150. float32 int64_to_float32( int64_t );
  151. float64 int64_to_float64( int64_t );
  152. #ifdef FLOATX80
  153. floatx80 int64_to_floatx80( int64_t );
  154. #endif
  155. #ifdef FLOAT128
  156. float128 int64_to_float128( int64_t );
  157. #endif
  158. #endif
  159. /*
  160. -------------------------------------------------------------------------------
  161. Software IEC/IEEE single-precision conversion routines.
  162. -------------------------------------------------------------------------------
  163. */
  164. int float32_to_int32( float32 );
  165. int float32_to_int32_round_to_zero( float32 );
  166. #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
  167. int64_t float32_to_int64( float32 );
  168. int64_t float32_to_int64_round_to_zero( float32 );
  169. #endif
  170. float64 float32_to_float64( float32 );
  171. #ifdef FLOATX80
  172. floatx80 float32_to_floatx80( float32 );
  173. #endif
  174. #ifdef FLOAT128
  175. float128 float32_to_float128( float32 );
  176. #endif
  177. /*
  178. -------------------------------------------------------------------------------
  179. Software IEC/IEEE single-precision operations.
  180. -------------------------------------------------------------------------------
  181. */
  182. float32 float32_round_to_int( float32 );
  183. float32 float32_add( float32, float32 );
  184. float32 float32_sub( float32, float32 );
  185. float32 float32_mul( float32, float32 );
  186. float32 float32_div( float32, float32 );
  187. float32 float32_rem( float32, float32 );
  188. float32 float32_sqrt( float32 );
  189. int float32_eq( float32, float32 );
  190. int float32_le( float32, float32 );
  191. int float32_lt( float32, float32 );
  192. int float32_eq_signaling( float32, float32 );
  193. int float32_le_quiet( float32, float32 );
  194. int float32_lt_quiet( float32, float32 );
  195. #ifndef SOFTFLOAT_FOR_GCC
  196. int float32_is_signaling_nan( float32 );
  197. #endif
  198. /*
  199. -------------------------------------------------------------------------------
  200. Software IEC/IEEE double-precision conversion routines.
  201. -------------------------------------------------------------------------------
  202. */
  203. int float64_to_int32( float64 );
  204. int float64_to_int32_round_to_zero( float64 );
  205. #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */
  206. int64_t float64_to_int64( float64 );
  207. #ifdef __alpha__
  208. int64_t float64_to_int64_no_overflow( float64 );
  209. #endif /* __alpha__ */
  210. int64_t float64_to_int64_round_to_zero( float64 );
  211. #endif
  212. float32 float64_to_float32( float64 );
  213. #ifdef FLOATX80
  214. floatx80 float64_to_floatx80( float64 );
  215. #endif
  216. #ifdef FLOAT128
  217. float128 float64_to_float128( float64 );
  218. #endif
  219. /*
  220. -------------------------------------------------------------------------------
  221. Software IEC/IEEE double-precision operations.
  222. -------------------------------------------------------------------------------
  223. */
  224. #define float64_default_nan 0xFFF8000000000000LL
  225. static __inline int
  226. float64_is_nan(float64 a)
  227. {
  228. return 0xFFE0000000000000LL < a << 1;
  229. }
  230. static __inline int
  231. float64_is_signaling_nan(float64 a)
  232. {
  233. return (a >> 51 & 0xFFF) == 0xFFE && (a & 0x0007FFFFFFFFFFFFLL);
  234. }
  235. float64 float64_round_to_int( float64 );
  236. float64 float64_add( float64, float64 );
  237. float64 float64_sub( float64, float64 );
  238. float64 float64_mul( float64, float64 );
  239. float64 float64_div( float64, float64 );
  240. float64 float64_rem( float64, float64 );
  241. float64 float64_sqrt( float64 );
  242. int float64_eq( float64, float64 );
  243. int float64_le( float64, float64 );
  244. int float64_lt( float64, float64 );
  245. int float64_eq_signaling( float64, float64 );
  246. int float64_le_quiet( float64, float64 );
  247. int float64_lt_quiet( float64, float64 );
  248. #ifndef SOFTFLOAT_FOR_GCC
  249. int float64_is_signaling_nan( float64 );
  250. #endif
  251. #ifdef FLOATX80
  252. /*
  253. -------------------------------------------------------------------------------
  254. Software IEC/IEEE extended double-precision conversion routines.
  255. -------------------------------------------------------------------------------
  256. */
  257. int floatx80_to_int32( floatx80 );
  258. int floatx80_to_int32_round_to_zero( floatx80 );
  259. int64_t floatx80_to_int64( floatx80 );
  260. int64_t floatx80_to_int64_round_to_zero( floatx80 );
  261. float32 floatx80_to_float32( floatx80 );
  262. float64 floatx80_to_float64( floatx80 );
  263. #ifdef FLOAT128
  264. float128 floatx80_to_float128( floatx80 );
  265. #endif
  266. /*
  267. -------------------------------------------------------------------------------
  268. Software IEC/IEEE extended double-precision rounding precision. Valid
  269. values are 32, 64, and 80.
  270. -------------------------------------------------------------------------------
  271. */
  272. extern int floatx80_rounding_precision;
  273. /*
  274. -------------------------------------------------------------------------------
  275. Software IEC/IEEE extended double-precision operations.
  276. -------------------------------------------------------------------------------
  277. */
  278. floatx80 floatx80_round_to_int( floatx80 );
  279. floatx80 floatx80_add( floatx80, floatx80 );
  280. floatx80 floatx80_sub( floatx80, floatx80 );
  281. floatx80 floatx80_mul( floatx80, floatx80 );
  282. floatx80 floatx80_div( floatx80, floatx80 );
  283. floatx80 floatx80_rem( floatx80, floatx80 );
  284. floatx80 floatx80_sqrt( floatx80 );
  285. int floatx80_eq( floatx80, floatx80 );
  286. int floatx80_le( floatx80, floatx80 );
  287. int floatx80_lt( floatx80, floatx80 );
  288. int floatx80_eq_signaling( floatx80, floatx80 );
  289. int floatx80_le_quiet( floatx80, floatx80 );
  290. int floatx80_lt_quiet( floatx80, floatx80 );
  291. int floatx80_is_signaling_nan( floatx80 );
  292. #endif
  293. #ifdef FLOAT128
  294. /*
  295. -------------------------------------------------------------------------------
  296. Software IEC/IEEE quadruple-precision conversion routines.
  297. -------------------------------------------------------------------------------
  298. */
  299. int float128_to_int32( float128 );
  300. int float128_to_int32_round_to_zero( float128 );
  301. int64_t float128_to_int64( float128 );
  302. int64_t float128_to_int64_round_to_zero( float128 );
  303. float32 float128_to_float32( float128 );
  304. float64 float128_to_float64( float128 );
  305. #ifdef FLOATX80
  306. floatx80 float128_to_floatx80( float128 );
  307. #endif
  308. /*
  309. -------------------------------------------------------------------------------
  310. Software IEC/IEEE quadruple-precision operations.
  311. -------------------------------------------------------------------------------
  312. */
  313. float128 float128_round_to_int( float128 );
  314. float128 float128_add( float128, float128 );
  315. float128 float128_sub( float128, float128 );
  316. float128 float128_mul( float128, float128 );
  317. float128 float128_div( float128, float128 );
  318. float128 float128_rem( float128, float128 );
  319. float128 float128_sqrt( float128 );
  320. int float128_eq( float128, float128 );
  321. int float128_le( float128, float128 );
  322. int float128_lt( float128, float128 );
  323. int float128_eq_signaling( float128, float128 );
  324. int float128_le_quiet( float128, float128 );
  325. int float128_lt_quiet( float128, float128 );
  326. int float128_is_signaling_nan( float128 );
  327. #endif