frnd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Purpose:
  12. * Single Floating-point Round to Integer
  13. * Double Floating-point Round to Integer
  14. * Quad Floating-point Round to Integer (returns unimplemented)
  15. *
  16. * External Interfaces:
  17. * dbl_frnd(srcptr,nullptr,dstptr,status)
  18. * sgl_frnd(srcptr,nullptr,dstptr,status)
  19. *
  20. * END_DESC
  21. */
  22. #include "float.h"
  23. #include "sgl_float.h"
  24. #include "dbl_float.h"
  25. #include "cnv_float.h"
  26. /*
  27. * Single Floating-point Round to Integer
  28. */
  29. /*ARGSUSED*/
  30. int
  31. sgl_frnd(sgl_floating_point *srcptr,
  32. unsigned int *nullptr,
  33. sgl_floating_point *dstptr,
  34. unsigned int *status)
  35. {
  36. register unsigned int src, result;
  37. register int src_exponent;
  38. register boolean inexact = FALSE;
  39. src = *srcptr;
  40. /*
  41. * check source operand for NaN or infinity
  42. */
  43. if ((src_exponent = Sgl_exponent(src)) == SGL_INFINITY_EXPONENT) {
  44. /*
  45. * is signaling NaN?
  46. */
  47. if (Sgl_isone_signaling(src)) {
  48. /* trap if INVALIDTRAP enabled */
  49. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  50. /* make NaN quiet */
  51. Set_invalidflag();
  52. Sgl_set_quiet(src);
  53. }
  54. /*
  55. * return quiet NaN or infinity
  56. */
  57. *dstptr = src;
  58. return(NOEXCEPTION);
  59. }
  60. /*
  61. * Need to round?
  62. */
  63. if ((src_exponent -= SGL_BIAS) >= SGL_P - 1) {
  64. *dstptr = src;
  65. return(NOEXCEPTION);
  66. }
  67. /*
  68. * Generate result
  69. */
  70. if (src_exponent >= 0) {
  71. Sgl_clear_exponent_set_hidden(src);
  72. result = src;
  73. Sgl_rightshift(result,(SGL_P-1) - (src_exponent));
  74. /* check for inexact */
  75. if (Sgl_isinexact_to_fix(src,src_exponent)) {
  76. inexact = TRUE;
  77. /* round result */
  78. switch (Rounding_mode()) {
  79. case ROUNDPLUS:
  80. if (Sgl_iszero_sign(src)) Sgl_increment(result);
  81. break;
  82. case ROUNDMINUS:
  83. if (Sgl_isone_sign(src)) Sgl_increment(result);
  84. break;
  85. case ROUNDNEAREST:
  86. if (Sgl_isone_roundbit(src,src_exponent))
  87. if (Sgl_isone_stickybit(src,src_exponent)
  88. || (Sgl_isone_lowmantissa(result)))
  89. Sgl_increment(result);
  90. }
  91. }
  92. Sgl_leftshift(result,(SGL_P-1) - (src_exponent));
  93. if (Sgl_isone_hiddenoverflow(result))
  94. Sgl_set_exponent(result,src_exponent + (SGL_BIAS+1));
  95. else Sgl_set_exponent(result,src_exponent + SGL_BIAS);
  96. }
  97. else {
  98. result = src; /* set sign */
  99. Sgl_setzero_exponentmantissa(result);
  100. /* check for inexact */
  101. if (Sgl_isnotzero_exponentmantissa(src)) {
  102. inexact = TRUE;
  103. /* round result */
  104. switch (Rounding_mode()) {
  105. case ROUNDPLUS:
  106. if (Sgl_iszero_sign(src))
  107. Sgl_set_exponent(result,SGL_BIAS);
  108. break;
  109. case ROUNDMINUS:
  110. if (Sgl_isone_sign(src))
  111. Sgl_set_exponent(result,SGL_BIAS);
  112. break;
  113. case ROUNDNEAREST:
  114. if (src_exponent == -1)
  115. if (Sgl_isnotzero_mantissa(src))
  116. Sgl_set_exponent(result,SGL_BIAS);
  117. }
  118. }
  119. }
  120. *dstptr = result;
  121. if (inexact) {
  122. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  123. else Set_inexactflag();
  124. }
  125. return(NOEXCEPTION);
  126. }
  127. /*
  128. * Double Floating-point Round to Integer
  129. */
  130. /*ARGSUSED*/
  131. int
  132. dbl_frnd(
  133. dbl_floating_point *srcptr,
  134. unsigned int *nullptr,
  135. dbl_floating_point *dstptr,
  136. unsigned int *status)
  137. {
  138. register unsigned int srcp1, srcp2, resultp1, resultp2;
  139. register int src_exponent;
  140. register boolean inexact = FALSE;
  141. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  142. /*
  143. * check source operand for NaN or infinity
  144. */
  145. if ((src_exponent = Dbl_exponent(srcp1)) == DBL_INFINITY_EXPONENT) {
  146. /*
  147. * is signaling NaN?
  148. */
  149. if (Dbl_isone_signaling(srcp1)) {
  150. /* trap if INVALIDTRAP enabled */
  151. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  152. /* make NaN quiet */
  153. Set_invalidflag();
  154. Dbl_set_quiet(srcp1);
  155. }
  156. /*
  157. * return quiet NaN or infinity
  158. */
  159. Dbl_copytoptr(srcp1,srcp2,dstptr);
  160. return(NOEXCEPTION);
  161. }
  162. /*
  163. * Need to round?
  164. */
  165. if ((src_exponent -= DBL_BIAS) >= DBL_P - 1) {
  166. Dbl_copytoptr(srcp1,srcp2,dstptr);
  167. return(NOEXCEPTION);
  168. }
  169. /*
  170. * Generate result
  171. */
  172. if (src_exponent >= 0) {
  173. Dbl_clear_exponent_set_hidden(srcp1);
  174. resultp1 = srcp1;
  175. resultp2 = srcp2;
  176. Dbl_rightshift(resultp1,resultp2,(DBL_P-1) - (src_exponent));
  177. /* check for inexact */
  178. if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
  179. inexact = TRUE;
  180. /* round result */
  181. switch (Rounding_mode()) {
  182. case ROUNDPLUS:
  183. if (Dbl_iszero_sign(srcp1))
  184. Dbl_increment(resultp1,resultp2);
  185. break;
  186. case ROUNDMINUS:
  187. if (Dbl_isone_sign(srcp1))
  188. Dbl_increment(resultp1,resultp2);
  189. break;
  190. case ROUNDNEAREST:
  191. if (Dbl_isone_roundbit(srcp1,srcp2,src_exponent))
  192. if (Dbl_isone_stickybit(srcp1,srcp2,src_exponent)
  193. || (Dbl_isone_lowmantissap2(resultp2)))
  194. Dbl_increment(resultp1,resultp2);
  195. }
  196. }
  197. Dbl_leftshift(resultp1,resultp2,(DBL_P-1) - (src_exponent));
  198. if (Dbl_isone_hiddenoverflow(resultp1))
  199. Dbl_set_exponent(resultp1,src_exponent + (DBL_BIAS+1));
  200. else Dbl_set_exponent(resultp1,src_exponent + DBL_BIAS);
  201. }
  202. else {
  203. resultp1 = srcp1; /* set sign */
  204. Dbl_setzero_exponentmantissa(resultp1,resultp2);
  205. /* check for inexact */
  206. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  207. inexact = TRUE;
  208. /* round result */
  209. switch (Rounding_mode()) {
  210. case ROUNDPLUS:
  211. if (Dbl_iszero_sign(srcp1))
  212. Dbl_set_exponent(resultp1,DBL_BIAS);
  213. break;
  214. case ROUNDMINUS:
  215. if (Dbl_isone_sign(srcp1))
  216. Dbl_set_exponent(resultp1,DBL_BIAS);
  217. break;
  218. case ROUNDNEAREST:
  219. if (src_exponent == -1)
  220. if (Dbl_isnotzero_mantissa(srcp1,srcp2))
  221. Dbl_set_exponent(resultp1,DBL_BIAS);
  222. }
  223. }
  224. }
  225. Dbl_copytoptr(resultp1,resultp2,dstptr);
  226. if (inexact) {
  227. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  228. else Set_inexactflag();
  229. }
  230. return(NOEXCEPTION);
  231. }