fcnvfxt.c 7.8 KB

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