fcnvff.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  4. *
  5. * Floating-point emulation code
  6. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>
  7. */
  8. /*
  9. * BEGIN_DESC
  10. *
  11. * File:
  12. * @(#) pa/spmath/fcnvff.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * Single Floating-point to Double Floating-point
  16. * Double Floating-point to Single Floating-point
  17. *
  18. * External Interfaces:
  19. * dbl_to_sgl_fcnvff(srcptr,nullptr,dstptr,status)
  20. * sgl_to_dbl_fcnvff(srcptr,nullptr,dstptr,status)
  21. *
  22. * Internal Interfaces:
  23. *
  24. * Theory:
  25. * <<please update with a overview of the operation of this file>>
  26. *
  27. * END_DESC
  28. */
  29. #include "float.h"
  30. #include "sgl_float.h"
  31. #include "dbl_float.h"
  32. #include "cnv_float.h"
  33. /*
  34. * Single Floating-point to Double Floating-point
  35. */
  36. /*ARGSUSED*/
  37. int
  38. sgl_to_dbl_fcnvff(
  39. sgl_floating_point *srcptr,
  40. unsigned int *nullptr,
  41. dbl_floating_point *dstptr,
  42. unsigned int *status)
  43. {
  44. register unsigned int src, resultp1, resultp2;
  45. register int src_exponent;
  46. src = *srcptr;
  47. src_exponent = Sgl_exponent(src);
  48. Dbl_allp1(resultp1) = Sgl_all(src); /* set sign of result */
  49. /*
  50. * Test for NaN or infinity
  51. */
  52. if (src_exponent == SGL_INFINITY_EXPONENT) {
  53. /*
  54. * determine if NaN or infinity
  55. */
  56. if (Sgl_iszero_mantissa(src)) {
  57. /*
  58. * is infinity; want to return double infinity
  59. */
  60. Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
  61. Dbl_copytoptr(resultp1,resultp2,dstptr);
  62. return(NOEXCEPTION);
  63. }
  64. else {
  65. /*
  66. * is NaN; signaling or quiet?
  67. */
  68. if (Sgl_isone_signaling(src)) {
  69. /* trap if INVALIDTRAP enabled */
  70. if (Is_invalidtrap_enabled())
  71. return(INVALIDEXCEPTION);
  72. /* make NaN quiet */
  73. else {
  74. Set_invalidflag();
  75. Sgl_set_quiet(src);
  76. }
  77. }
  78. /*
  79. * NaN is quiet, return as double NaN
  80. */
  81. Dbl_setinfinity_exponent(resultp1);
  82. Sgl_to_dbl_mantissa(src,resultp1,resultp2);
  83. Dbl_copytoptr(resultp1,resultp2,dstptr);
  84. return(NOEXCEPTION);
  85. }
  86. }
  87. /*
  88. * Test for zero or denormalized
  89. */
  90. if (src_exponent == 0) {
  91. /*
  92. * determine if zero or denormalized
  93. */
  94. if (Sgl_isnotzero_mantissa(src)) {
  95. /*
  96. * is denormalized; want to normalize
  97. */
  98. Sgl_clear_signexponent(src);
  99. Sgl_leftshiftby1(src);
  100. Sgl_normalize(src,src_exponent);
  101. Sgl_to_dbl_exponent(src_exponent,resultp1);
  102. Sgl_to_dbl_mantissa(src,resultp1,resultp2);
  103. }
  104. else {
  105. Dbl_setzero_exponentmantissa(resultp1,resultp2);
  106. }
  107. Dbl_copytoptr(resultp1,resultp2,dstptr);
  108. return(NOEXCEPTION);
  109. }
  110. /*
  111. * No special cases, just complete the conversion
  112. */
  113. Sgl_to_dbl_exponent(src_exponent, resultp1);
  114. Sgl_to_dbl_mantissa(Sgl_mantissa(src), resultp1,resultp2);
  115. Dbl_copytoptr(resultp1,resultp2,dstptr);
  116. return(NOEXCEPTION);
  117. }
  118. /*
  119. * Double Floating-point to Single Floating-point
  120. */
  121. /*ARGSUSED*/
  122. int
  123. dbl_to_sgl_fcnvff(
  124. dbl_floating_point *srcptr,
  125. unsigned int *nullptr,
  126. sgl_floating_point *dstptr,
  127. unsigned int *status)
  128. {
  129. register unsigned int srcp1, srcp2, result;
  130. register int src_exponent, dest_exponent, dest_mantissa;
  131. register boolean inexact = FALSE, guardbit = FALSE, stickybit = FALSE;
  132. register boolean lsb_odd = FALSE;
  133. boolean is_tiny = FALSE;
  134. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  135. src_exponent = Dbl_exponent(srcp1);
  136. Sgl_all(result) = Dbl_allp1(srcp1); /* set sign of result */
  137. /*
  138. * Test for NaN or infinity
  139. */
  140. if (src_exponent == DBL_INFINITY_EXPONENT) {
  141. /*
  142. * determine if NaN or infinity
  143. */
  144. if (Dbl_iszero_mantissa(srcp1,srcp2)) {
  145. /*
  146. * is infinity; want to return single infinity
  147. */
  148. Sgl_setinfinity_exponentmantissa(result);
  149. *dstptr = result;
  150. return(NOEXCEPTION);
  151. }
  152. /*
  153. * is NaN; signaling or quiet?
  154. */
  155. if (Dbl_isone_signaling(srcp1)) {
  156. /* trap if INVALIDTRAP enabled */
  157. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  158. else {
  159. Set_invalidflag();
  160. /* make NaN quiet */
  161. Dbl_set_quiet(srcp1);
  162. }
  163. }
  164. /*
  165. * NaN is quiet, return as single NaN
  166. */
  167. Sgl_setinfinity_exponent(result);
  168. Sgl_set_mantissa(result,Dallp1(srcp1)<<3 | Dallp2(srcp2)>>29);
  169. if (Sgl_iszero_mantissa(result)) Sgl_set_quiet(result);
  170. *dstptr = result;
  171. return(NOEXCEPTION);
  172. }
  173. /*
  174. * Generate result
  175. */
  176. Dbl_to_sgl_exponent(src_exponent,dest_exponent);
  177. if (dest_exponent > 0) {
  178. Dbl_to_sgl_mantissa(srcp1,srcp2,dest_mantissa,inexact,guardbit,
  179. stickybit,lsb_odd);
  180. }
  181. else {
  182. if (Dbl_iszero_exponentmantissa(srcp1,srcp2)){
  183. Sgl_setzero_exponentmantissa(result);
  184. *dstptr = result;
  185. return(NOEXCEPTION);
  186. }
  187. if (Is_underflowtrap_enabled()) {
  188. Dbl_to_sgl_mantissa(srcp1,srcp2,dest_mantissa,inexact,
  189. guardbit,stickybit,lsb_odd);
  190. }
  191. else {
  192. /* compute result, determine inexact info,
  193. * and set Underflowflag if appropriate
  194. */
  195. Dbl_to_sgl_denormalized(srcp1,srcp2,dest_exponent,
  196. dest_mantissa,inexact,guardbit,stickybit,lsb_odd,
  197. is_tiny);
  198. }
  199. }
  200. /*
  201. * Now round result if not exact
  202. */
  203. if (inexact) {
  204. switch (Rounding_mode()) {
  205. case ROUNDPLUS:
  206. if (Sgl_iszero_sign(result)) dest_mantissa++;
  207. break;
  208. case ROUNDMINUS:
  209. if (Sgl_isone_sign(result)) dest_mantissa++;
  210. break;
  211. case ROUNDNEAREST:
  212. if (guardbit) {
  213. if (stickybit || lsb_odd) dest_mantissa++;
  214. }
  215. }
  216. }
  217. Sgl_set_exponentmantissa(result,dest_mantissa);
  218. /*
  219. * check for mantissa overflow after rounding
  220. */
  221. if ((dest_exponent>0 || Is_underflowtrap_enabled()) &&
  222. Sgl_isone_hidden(result)) dest_exponent++;
  223. /*
  224. * Test for overflow
  225. */
  226. if (dest_exponent >= SGL_INFINITY_EXPONENT) {
  227. /* trap if OVERFLOWTRAP enabled */
  228. if (Is_overflowtrap_enabled()) {
  229. /*
  230. * Check for gross overflow
  231. */
  232. if (dest_exponent >= SGL_INFINITY_EXPONENT+SGL_WRAP)
  233. return(UNIMPLEMENTEDEXCEPTION);
  234. /*
  235. * Adjust bias of result
  236. */
  237. Sgl_setwrapped_exponent(result,dest_exponent,ovfl);
  238. *dstptr = result;
  239. if (inexact)
  240. if (Is_inexacttrap_enabled())
  241. return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
  242. else Set_inexactflag();
  243. return(OVERFLOWEXCEPTION);
  244. }
  245. Set_overflowflag();
  246. inexact = TRUE;
  247. /* set result to infinity or largest number */
  248. Sgl_setoverflow(result);
  249. }
  250. /*
  251. * Test for underflow
  252. */
  253. else if (dest_exponent <= 0) {
  254. /* trap if UNDERFLOWTRAP enabled */
  255. if (Is_underflowtrap_enabled()) {
  256. /*
  257. * Check for gross underflow
  258. */
  259. if (dest_exponent <= -(SGL_WRAP))
  260. return(UNIMPLEMENTEDEXCEPTION);
  261. /*
  262. * Adjust bias of result
  263. */
  264. Sgl_setwrapped_exponent(result,dest_exponent,unfl);
  265. *dstptr = result;
  266. if (inexact)
  267. if (Is_inexacttrap_enabled())
  268. return(UNDERFLOWEXCEPTION|INEXACTEXCEPTION);
  269. else Set_inexactflag();
  270. return(UNDERFLOWEXCEPTION);
  271. }
  272. /*
  273. * result is denormalized or signed zero
  274. */
  275. if (inexact && is_tiny) Set_underflowflag();
  276. }
  277. else Sgl_set_exponent(result,dest_exponent);
  278. *dstptr = result;
  279. /*
  280. * Trap if inexact trap is enabled
  281. */
  282. if (inexact)
  283. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  284. else Set_inexactflag();
  285. return(NOEXCEPTION);
  286. }