frnd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  3. *
  4. * Floating-point emulation code
  5. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * BEGIN_DESC
  23. *
  24. * Purpose:
  25. * Single Floating-point Round to Integer
  26. * Double Floating-point Round to Integer
  27. * Quad Floating-point Round to Integer (returns unimplemented)
  28. *
  29. * External Interfaces:
  30. * dbl_frnd(srcptr,nullptr,dstptr,status)
  31. * sgl_frnd(srcptr,nullptr,dstptr,status)
  32. *
  33. * END_DESC
  34. */
  35. #include "float.h"
  36. #include "sgl_float.h"
  37. #include "dbl_float.h"
  38. #include "cnv_float.h"
  39. /*
  40. * Single Floating-point Round to Integer
  41. */
  42. /*ARGSUSED*/
  43. int
  44. sgl_frnd(sgl_floating_point *srcptr,
  45. unsigned int *nullptr,
  46. sgl_floating_point *dstptr,
  47. unsigned int *status)
  48. {
  49. register unsigned int src, result;
  50. register int src_exponent;
  51. register boolean inexact = FALSE;
  52. src = *srcptr;
  53. /*
  54. * check source operand for NaN or infinity
  55. */
  56. if ((src_exponent = Sgl_exponent(src)) == SGL_INFINITY_EXPONENT) {
  57. /*
  58. * is signaling NaN?
  59. */
  60. if (Sgl_isone_signaling(src)) {
  61. /* trap if INVALIDTRAP enabled */
  62. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  63. /* make NaN quiet */
  64. Set_invalidflag();
  65. Sgl_set_quiet(src);
  66. }
  67. /*
  68. * return quiet NaN or infinity
  69. */
  70. *dstptr = src;
  71. return(NOEXCEPTION);
  72. }
  73. /*
  74. * Need to round?
  75. */
  76. if ((src_exponent -= SGL_BIAS) >= SGL_P - 1) {
  77. *dstptr = src;
  78. return(NOEXCEPTION);
  79. }
  80. /*
  81. * Generate result
  82. */
  83. if (src_exponent >= 0) {
  84. Sgl_clear_exponent_set_hidden(src);
  85. result = src;
  86. Sgl_rightshift(result,(SGL_P-1) - (src_exponent));
  87. /* check for inexact */
  88. if (Sgl_isinexact_to_fix(src,src_exponent)) {
  89. inexact = TRUE;
  90. /* round result */
  91. switch (Rounding_mode()) {
  92. case ROUNDPLUS:
  93. if (Sgl_iszero_sign(src)) Sgl_increment(result);
  94. break;
  95. case ROUNDMINUS:
  96. if (Sgl_isone_sign(src)) Sgl_increment(result);
  97. break;
  98. case ROUNDNEAREST:
  99. if (Sgl_isone_roundbit(src,src_exponent))
  100. if (Sgl_isone_stickybit(src,src_exponent)
  101. || (Sgl_isone_lowmantissa(result)))
  102. Sgl_increment(result);
  103. }
  104. }
  105. Sgl_leftshift(result,(SGL_P-1) - (src_exponent));
  106. if (Sgl_isone_hiddenoverflow(result))
  107. Sgl_set_exponent(result,src_exponent + (SGL_BIAS+1));
  108. else Sgl_set_exponent(result,src_exponent + SGL_BIAS);
  109. }
  110. else {
  111. result = src; /* set sign */
  112. Sgl_setzero_exponentmantissa(result);
  113. /* check for inexact */
  114. if (Sgl_isnotzero_exponentmantissa(src)) {
  115. inexact = TRUE;
  116. /* round result */
  117. switch (Rounding_mode()) {
  118. case ROUNDPLUS:
  119. if (Sgl_iszero_sign(src))
  120. Sgl_set_exponent(result,SGL_BIAS);
  121. break;
  122. case ROUNDMINUS:
  123. if (Sgl_isone_sign(src))
  124. Sgl_set_exponent(result,SGL_BIAS);
  125. break;
  126. case ROUNDNEAREST:
  127. if (src_exponent == -1)
  128. if (Sgl_isnotzero_mantissa(src))
  129. Sgl_set_exponent(result,SGL_BIAS);
  130. }
  131. }
  132. }
  133. *dstptr = result;
  134. if (inexact) {
  135. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  136. else Set_inexactflag();
  137. }
  138. return(NOEXCEPTION);
  139. }
  140. /*
  141. * Double Floating-point Round to Integer
  142. */
  143. /*ARGSUSED*/
  144. int
  145. dbl_frnd(
  146. dbl_floating_point *srcptr,
  147. unsigned int *nullptr,
  148. dbl_floating_point *dstptr,
  149. unsigned int *status)
  150. {
  151. register unsigned int srcp1, srcp2, resultp1, resultp2;
  152. register int src_exponent;
  153. register boolean inexact = FALSE;
  154. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  155. /*
  156. * check source operand for NaN or infinity
  157. */
  158. if ((src_exponent = Dbl_exponent(srcp1)) == DBL_INFINITY_EXPONENT) {
  159. /*
  160. * is signaling NaN?
  161. */
  162. if (Dbl_isone_signaling(srcp1)) {
  163. /* trap if INVALIDTRAP enabled */
  164. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  165. /* make NaN quiet */
  166. Set_invalidflag();
  167. Dbl_set_quiet(srcp1);
  168. }
  169. /*
  170. * return quiet NaN or infinity
  171. */
  172. Dbl_copytoptr(srcp1,srcp2,dstptr);
  173. return(NOEXCEPTION);
  174. }
  175. /*
  176. * Need to round?
  177. */
  178. if ((src_exponent -= DBL_BIAS) >= DBL_P - 1) {
  179. Dbl_copytoptr(srcp1,srcp2,dstptr);
  180. return(NOEXCEPTION);
  181. }
  182. /*
  183. * Generate result
  184. */
  185. if (src_exponent >= 0) {
  186. Dbl_clear_exponent_set_hidden(srcp1);
  187. resultp1 = srcp1;
  188. resultp2 = srcp2;
  189. Dbl_rightshift(resultp1,resultp2,(DBL_P-1) - (src_exponent));
  190. /* check for inexact */
  191. if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
  192. inexact = TRUE;
  193. /* round result */
  194. switch (Rounding_mode()) {
  195. case ROUNDPLUS:
  196. if (Dbl_iszero_sign(srcp1))
  197. Dbl_increment(resultp1,resultp2);
  198. break;
  199. case ROUNDMINUS:
  200. if (Dbl_isone_sign(srcp1))
  201. Dbl_increment(resultp1,resultp2);
  202. break;
  203. case ROUNDNEAREST:
  204. if (Dbl_isone_roundbit(srcp1,srcp2,src_exponent))
  205. if (Dbl_isone_stickybit(srcp1,srcp2,src_exponent)
  206. || (Dbl_isone_lowmantissap2(resultp2)))
  207. Dbl_increment(resultp1,resultp2);
  208. }
  209. }
  210. Dbl_leftshift(resultp1,resultp2,(DBL_P-1) - (src_exponent));
  211. if (Dbl_isone_hiddenoverflow(resultp1))
  212. Dbl_set_exponent(resultp1,src_exponent + (DBL_BIAS+1));
  213. else Dbl_set_exponent(resultp1,src_exponent + DBL_BIAS);
  214. }
  215. else {
  216. resultp1 = srcp1; /* set sign */
  217. Dbl_setzero_exponentmantissa(resultp1,resultp2);
  218. /* check for inexact */
  219. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  220. inexact = TRUE;
  221. /* round result */
  222. switch (Rounding_mode()) {
  223. case ROUNDPLUS:
  224. if (Dbl_iszero_sign(srcp1))
  225. Dbl_set_exponent(resultp1,DBL_BIAS);
  226. break;
  227. case ROUNDMINUS:
  228. if (Dbl_isone_sign(srcp1))
  229. Dbl_set_exponent(resultp1,DBL_BIAS);
  230. break;
  231. case ROUNDNEAREST:
  232. if (src_exponent == -1)
  233. if (Dbl_isnotzero_mantissa(srcp1,srcp2))
  234. Dbl_set_exponent(resultp1,DBL_BIAS);
  235. }
  236. }
  237. }
  238. Dbl_copytoptr(resultp1,resultp2,dstptr);
  239. if (inexact) {
  240. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  241. else Set_inexactflag();
  242. }
  243. return(NOEXCEPTION);
  244. }