fcnvfut.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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) <bame@debian.org>
  7. */
  8. /*
  9. * BEGIN_DESC
  10. *
  11. * File:
  12. * @(#) pa/spmath/fcnvfut.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * Floating-point to Unsigned Fixed-point Converts with Truncation
  16. *
  17. * External Interfaces:
  18. * dbl_to_dbl_fcnvfut(srcptr,nullptr,dstptr,status)
  19. * dbl_to_sgl_fcnvfut(srcptr,nullptr,dstptr,status)
  20. * sgl_to_dbl_fcnvfut(srcptr,nullptr,dstptr,status)
  21. * sgl_to_sgl_fcnvfut(srcptr,nullptr,dstptr,status)
  22. *
  23. * Internal Interfaces:
  24. *
  25. * Theory:
  26. * <<please update with a overview of the operation of this file>>
  27. *
  28. * END_DESC
  29. */
  30. #include "float.h"
  31. #include "sgl_float.h"
  32. #include "dbl_float.h"
  33. #include "cnv_float.h"
  34. /************************************************************************
  35. * Floating-point to Unsigned Fixed-point Converts with Truncation *
  36. ************************************************************************/
  37. /*
  38. * Convert single floating-point to single fixed-point format
  39. * with truncated result
  40. */
  41. /*ARGSUSED*/
  42. int
  43. sgl_to_sgl_fcnvfut (sgl_floating_point * srcptr, unsigned int *nullptr,
  44. unsigned int *dstptr, unsigned int *status)
  45. {
  46. register unsigned int src, result;
  47. register int src_exponent;
  48. src = *srcptr;
  49. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  50. /*
  51. * Test for overflow
  52. */
  53. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  54. if (Sgl_isone_sign(src)) {
  55. result = 0;
  56. } else {
  57. result = 0xffffffff;
  58. }
  59. if (Is_invalidtrap_enabled()) {
  60. return(INVALIDEXCEPTION);
  61. }
  62. Set_invalidflag();
  63. *dstptr = result;
  64. return(NOEXCEPTION);
  65. }
  66. /*
  67. * Generate result
  68. */
  69. if (src_exponent >= 0) {
  70. /*
  71. * Check sign.
  72. * If negative, trap unimplemented.
  73. */
  74. if (Sgl_isone_sign(src)) {
  75. result = 0;
  76. if (Is_invalidtrap_enabled()) {
  77. return(INVALIDEXCEPTION);
  78. }
  79. Set_invalidflag();
  80. *dstptr = result;
  81. return(NOEXCEPTION);
  82. }
  83. Sgl_clear_signexponent_set_hidden(src);
  84. Suint_from_sgl_mantissa(src,src_exponent,result);
  85. *dstptr = result;
  86. /* check for inexact */
  87. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  88. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  89. else Set_inexactflag();
  90. }
  91. }
  92. else {
  93. *dstptr = 0;
  94. /* check for inexact */
  95. if (Sgl_isnotzero_exponentmantissa(src)) {
  96. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  97. else Set_inexactflag();
  98. }
  99. }
  100. return(NOEXCEPTION);
  101. }
  102. /*
  103. * Single Floating-point to Double Unsigned Fixed
  104. */
  105. /*ARGSUSED*/
  106. int
  107. sgl_to_dbl_fcnvfut (sgl_floating_point * srcptr, unsigned int *nullptr,
  108. dbl_unsigned * dstptr, unsigned int *status)
  109. {
  110. register int src_exponent;
  111. register unsigned int src, resultp1, resultp2;
  112. src = *srcptr;
  113. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  114. /*
  115. * Test for overflow
  116. */
  117. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  118. if (Sgl_isone_sign(src)) {
  119. resultp1 = resultp2 = 0;
  120. } else {
  121. resultp1 = resultp2 = 0xffffffff;
  122. }
  123. if (Is_invalidtrap_enabled()) {
  124. return(INVALIDEXCEPTION);
  125. }
  126. Set_invalidflag();
  127. Duint_copytoptr(resultp1,resultp2,dstptr);
  128. return(NOEXCEPTION);
  129. }
  130. /*
  131. * Generate result
  132. */
  133. if (src_exponent >= 0) {
  134. /*
  135. * Check sign.
  136. * If negative, trap unimplemented.
  137. */
  138. if (Sgl_isone_sign(src)) {
  139. resultp1 = resultp2 = 0;
  140. if (Is_invalidtrap_enabled()) {
  141. return(INVALIDEXCEPTION);
  142. }
  143. Set_invalidflag();
  144. Duint_copytoptr(resultp1,resultp2,dstptr);
  145. return(NOEXCEPTION);
  146. }
  147. Sgl_clear_signexponent_set_hidden(src);
  148. Duint_from_sgl_mantissa(src,src_exponent,resultp1,resultp2);
  149. Duint_copytoptr(resultp1,resultp2,dstptr);
  150. /* check for inexact */
  151. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  152. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  153. else Set_inexactflag();
  154. }
  155. }
  156. else {
  157. Duint_setzero(resultp1,resultp2);
  158. Duint_copytoptr(resultp1,resultp2,dstptr);
  159. /* check for inexact */
  160. if (Sgl_isnotzero_exponentmantissa(src)) {
  161. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  162. else Set_inexactflag();
  163. }
  164. }
  165. return(NOEXCEPTION);
  166. }
  167. /*
  168. * Double Floating-point to Single Unsigned Fixed
  169. */
  170. /*ARGSUSED*/
  171. int
  172. dbl_to_sgl_fcnvfut (dbl_floating_point * srcptr, unsigned int *nullptr,
  173. unsigned int *dstptr, unsigned int *status)
  174. {
  175. register unsigned int srcp1, srcp2, result;
  176. register int src_exponent;
  177. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  178. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  179. /*
  180. * Test for overflow
  181. */
  182. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  183. if (Dbl_isone_sign(srcp1)) {
  184. result = 0;
  185. } else {
  186. result = 0xffffffff;
  187. }
  188. if (Is_invalidtrap_enabled()) {
  189. return(INVALIDEXCEPTION);
  190. }
  191. Set_invalidflag();
  192. *dstptr = result;
  193. return(NOEXCEPTION);
  194. }
  195. /*
  196. * Generate result
  197. */
  198. if (src_exponent >= 0) {
  199. /*
  200. * Check sign.
  201. * If negative, trap unimplemented.
  202. */
  203. if (Dbl_isone_sign(srcp1)) {
  204. result = 0;
  205. if (Is_invalidtrap_enabled()) {
  206. return(INVALIDEXCEPTION);
  207. }
  208. Set_invalidflag();
  209. *dstptr = result;
  210. return(NOEXCEPTION);
  211. }
  212. Dbl_clear_signexponent_set_hidden(srcp1);
  213. Suint_from_dbl_mantissa(srcp1,srcp2,src_exponent,result);
  214. *dstptr = result;
  215. /* check for inexact */
  216. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  217. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  218. else Set_inexactflag();
  219. }
  220. }
  221. else {
  222. *dstptr = 0;
  223. /* check for inexact */
  224. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  225. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  226. else Set_inexactflag();
  227. }
  228. }
  229. return(NOEXCEPTION);
  230. }
  231. /*
  232. * Double Floating-point to Double Unsigned Fixed
  233. */
  234. /*ARGSUSED*/
  235. int
  236. dbl_to_dbl_fcnvfut (dbl_floating_point * srcptr, unsigned int *nullptr,
  237. dbl_unsigned * dstptr, unsigned int *status)
  238. {
  239. register int src_exponent;
  240. register unsigned int srcp1, srcp2, resultp1, resultp2;
  241. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  242. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  243. /*
  244. * Test for overflow
  245. */
  246. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  247. if (Dbl_isone_sign(srcp1)) {
  248. resultp1 = resultp2 = 0;
  249. } else {
  250. resultp1 = resultp2 = 0xffffffff;
  251. }
  252. if (Is_invalidtrap_enabled()) {
  253. return(INVALIDEXCEPTION);
  254. }
  255. Set_invalidflag();
  256. Duint_copytoptr(resultp1,resultp2,dstptr);
  257. return(NOEXCEPTION);
  258. }
  259. /*
  260. * Generate result
  261. */
  262. if (src_exponent >= 0) {
  263. /*
  264. * Check sign.
  265. * If negative, trap unimplemented.
  266. */
  267. if (Dbl_isone_sign(srcp1)) {
  268. resultp1 = resultp2 = 0;
  269. if (Is_invalidtrap_enabled()) {
  270. return(INVALIDEXCEPTION);
  271. }
  272. Set_invalidflag();
  273. Duint_copytoptr(resultp1,resultp2,dstptr);
  274. return(NOEXCEPTION);
  275. }
  276. Dbl_clear_signexponent_set_hidden(srcp1);
  277. Duint_from_dbl_mantissa(srcp1,srcp2,src_exponent,
  278. resultp1,resultp2);
  279. Duint_copytoptr(resultp1,resultp2,dstptr);
  280. /* check for inexact */
  281. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  282. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  283. else Set_inexactflag();
  284. }
  285. }
  286. else {
  287. Duint_setzero(resultp1,resultp2);
  288. Duint_copytoptr(resultp1,resultp2,dstptr);
  289. /* check for inexact */
  290. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  291. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  292. else Set_inexactflag();
  293. }
  294. }
  295. return(NOEXCEPTION);
  296. }