fcnvxf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/fcnvxf.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * Single Fixed-point to Single Floating-point
  16. * Single Fixed-point to Double Floating-point
  17. * Double Fixed-point to Single Floating-point
  18. * Double Fixed-point to Double Floating-point
  19. *
  20. * External Interfaces:
  21. * dbl_to_dbl_fcnvxf(srcptr,nullptr,dstptr,status)
  22. * dbl_to_sgl_fcnvxf(srcptr,nullptr,dstptr,status)
  23. * sgl_to_dbl_fcnvxf(srcptr,nullptr,dstptr,status)
  24. * sgl_to_sgl_fcnvxf(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 fixed-point to single floating-point format
  39. */
  40. int
  41. sgl_to_sgl_fcnvxf(
  42. int *srcptr,
  43. unsigned int *nullptr,
  44. sgl_floating_point *dstptr,
  45. unsigned int *status)
  46. {
  47. register int src, dst_exponent;
  48. register unsigned int result = 0;
  49. src = *srcptr;
  50. /*
  51. * set sign bit of result and get magnitude of source
  52. */
  53. if (src < 0) {
  54. Sgl_setone_sign(result);
  55. Int_negate(src);
  56. }
  57. else {
  58. Sgl_setzero_sign(result);
  59. /* Check for zero */
  60. if (src == 0) {
  61. Sgl_setzero(result);
  62. *dstptr = result;
  63. return(NOEXCEPTION);
  64. }
  65. }
  66. /*
  67. * Generate exponent and normalized mantissa
  68. */
  69. dst_exponent = 16; /* initialize for normalization */
  70. /*
  71. * Check word for most significant bit set. Returns
  72. * a value in dst_exponent indicating the bit position,
  73. * between -1 and 30.
  74. */
  75. Find_ms_one_bit(src,dst_exponent);
  76. /* left justify source, with msb at bit position 1 */
  77. if (dst_exponent >= 0) src <<= dst_exponent;
  78. else src = 1 << 30;
  79. Sgl_set_mantissa(result, src >> (SGL_EXP_LENGTH-1));
  80. Sgl_set_exponent(result, 30+SGL_BIAS - dst_exponent);
  81. /* check for inexact */
  82. if (Int_isinexact_to_sgl(src)) {
  83. switch (Rounding_mode()) {
  84. case ROUNDPLUS:
  85. if (Sgl_iszero_sign(result))
  86. Sgl_increment(result);
  87. break;
  88. case ROUNDMINUS:
  89. if (Sgl_isone_sign(result))
  90. Sgl_increment(result);
  91. break;
  92. case ROUNDNEAREST:
  93. Sgl_roundnearest_from_int(src,result);
  94. }
  95. if (Is_inexacttrap_enabled()) {
  96. *dstptr = result;
  97. return(INEXACTEXCEPTION);
  98. }
  99. else Set_inexactflag();
  100. }
  101. *dstptr = result;
  102. return(NOEXCEPTION);
  103. }
  104. /*
  105. * Single Fixed-point to Double Floating-point
  106. */
  107. int
  108. sgl_to_dbl_fcnvxf(
  109. int *srcptr,
  110. unsigned int *nullptr,
  111. dbl_floating_point *dstptr,
  112. unsigned int *status)
  113. {
  114. register int src, dst_exponent;
  115. register unsigned int resultp1 = 0, resultp2 = 0;
  116. src = *srcptr;
  117. /*
  118. * set sign bit of result and get magnitude of source
  119. */
  120. if (src < 0) {
  121. Dbl_setone_sign(resultp1);
  122. Int_negate(src);
  123. }
  124. else {
  125. Dbl_setzero_sign(resultp1);
  126. /* Check for zero */
  127. if (src == 0) {
  128. Dbl_setzero(resultp1,resultp2);
  129. Dbl_copytoptr(resultp1,resultp2,dstptr);
  130. return(NOEXCEPTION);
  131. }
  132. }
  133. /*
  134. * Generate exponent and normalized mantissa
  135. */
  136. dst_exponent = 16; /* initialize for normalization */
  137. /*
  138. * Check word for most significant bit set. Returns
  139. * a value in dst_exponent indicating the bit position,
  140. * between -1 and 30.
  141. */
  142. Find_ms_one_bit(src,dst_exponent);
  143. /* left justify source, with msb at bit position 1 */
  144. if (dst_exponent >= 0) src <<= dst_exponent;
  145. else src = 1 << 30;
  146. Dbl_set_mantissap1(resultp1, src >> DBL_EXP_LENGTH - 1);
  147. Dbl_set_mantissap2(resultp2, src << (33-DBL_EXP_LENGTH));
  148. Dbl_set_exponent(resultp1, (30+DBL_BIAS) - dst_exponent);
  149. Dbl_copytoptr(resultp1,resultp2,dstptr);
  150. return(NOEXCEPTION);
  151. }
  152. /*
  153. * Double Fixed-point to Single Floating-point
  154. */
  155. int
  156. dbl_to_sgl_fcnvxf(
  157. dbl_integer *srcptr,
  158. unsigned int *nullptr,
  159. sgl_floating_point *dstptr,
  160. unsigned int *status)
  161. {
  162. int dst_exponent, srcp1;
  163. unsigned int result = 0, srcp2;
  164. Dint_copyfromptr(srcptr,srcp1,srcp2);
  165. /*
  166. * set sign bit of result and get magnitude of source
  167. */
  168. if (srcp1 < 0) {
  169. Sgl_setone_sign(result);
  170. Dint_negate(srcp1,srcp2);
  171. }
  172. else {
  173. Sgl_setzero_sign(result);
  174. /* Check for zero */
  175. if (srcp1 == 0 && srcp2 == 0) {
  176. Sgl_setzero(result);
  177. *dstptr = result;
  178. return(NOEXCEPTION);
  179. }
  180. }
  181. /*
  182. * Generate exponent and normalized mantissa
  183. */
  184. dst_exponent = 16; /* initialize for normalization */
  185. if (srcp1 == 0) {
  186. /*
  187. * Check word for most significant bit set. Returns
  188. * a value in dst_exponent indicating the bit position,
  189. * between -1 and 30.
  190. */
  191. Find_ms_one_bit(srcp2,dst_exponent);
  192. /* left justify source, with msb at bit position 1 */
  193. if (dst_exponent >= 0) {
  194. srcp1 = srcp2 << dst_exponent;
  195. srcp2 = 0;
  196. }
  197. else {
  198. srcp1 = srcp2 >> 1;
  199. srcp2 <<= 31;
  200. }
  201. /*
  202. * since msb set is in second word, need to
  203. * adjust bit position count
  204. */
  205. dst_exponent += 32;
  206. }
  207. else {
  208. /*
  209. * Check word for most significant bit set. Returns
  210. * a value in dst_exponent indicating the bit position,
  211. * between -1 and 30.
  212. *
  213. */
  214. Find_ms_one_bit(srcp1,dst_exponent);
  215. /* left justify source, with msb at bit position 1 */
  216. if (dst_exponent > 0) {
  217. Variable_shift_double(srcp1,srcp2,(32-dst_exponent),
  218. srcp1);
  219. srcp2 <<= dst_exponent;
  220. }
  221. /*
  222. * If dst_exponent = 0, we don't need to shift anything.
  223. * If dst_exponent = -1, src = - 2**63 so we won't need to
  224. * shift srcp2.
  225. */
  226. else srcp1 >>= -(dst_exponent);
  227. }
  228. Sgl_set_mantissa(result, srcp1 >> SGL_EXP_LENGTH - 1);
  229. Sgl_set_exponent(result, (62+SGL_BIAS) - dst_exponent);
  230. /* check for inexact */
  231. if (Dint_isinexact_to_sgl(srcp1,srcp2)) {
  232. switch (Rounding_mode()) {
  233. case ROUNDPLUS:
  234. if (Sgl_iszero_sign(result))
  235. Sgl_increment(result);
  236. break;
  237. case ROUNDMINUS:
  238. if (Sgl_isone_sign(result))
  239. Sgl_increment(result);
  240. break;
  241. case ROUNDNEAREST:
  242. Sgl_roundnearest_from_dint(srcp1,srcp2,result);
  243. }
  244. if (Is_inexacttrap_enabled()) {
  245. *dstptr = result;
  246. return(INEXACTEXCEPTION);
  247. }
  248. else Set_inexactflag();
  249. }
  250. *dstptr = result;
  251. return(NOEXCEPTION);
  252. }
  253. /*
  254. * Double Fixed-point to Double Floating-point
  255. */
  256. int
  257. dbl_to_dbl_fcnvxf(
  258. dbl_integer *srcptr,
  259. unsigned int *nullptr,
  260. dbl_floating_point *dstptr,
  261. unsigned int *status)
  262. {
  263. register int srcp1, dst_exponent;
  264. register unsigned int srcp2, resultp1 = 0, resultp2 = 0;
  265. Dint_copyfromptr(srcptr,srcp1,srcp2);
  266. /*
  267. * set sign bit of result and get magnitude of source
  268. */
  269. if (srcp1 < 0) {
  270. Dbl_setone_sign(resultp1);
  271. Dint_negate(srcp1,srcp2);
  272. }
  273. else {
  274. Dbl_setzero_sign(resultp1);
  275. /* Check for zero */
  276. if (srcp1 == 0 && srcp2 ==0) {
  277. Dbl_setzero(resultp1,resultp2);
  278. Dbl_copytoptr(resultp1,resultp2,dstptr);
  279. return(NOEXCEPTION);
  280. }
  281. }
  282. /*
  283. * Generate exponent and normalized mantissa
  284. */
  285. dst_exponent = 16; /* initialize for normalization */
  286. if (srcp1 == 0) {
  287. /*
  288. * Check word for most significant bit set. Returns
  289. * a value in dst_exponent indicating the bit position,
  290. * between -1 and 30.
  291. */
  292. Find_ms_one_bit(srcp2,dst_exponent);
  293. /* left justify source, with msb at bit position 1 */
  294. if (dst_exponent >= 0) {
  295. srcp1 = srcp2 << dst_exponent;
  296. srcp2 = 0;
  297. }
  298. else {
  299. srcp1 = srcp2 >> 1;
  300. srcp2 <<= 31;
  301. }
  302. /*
  303. * since msb set is in second word, need to
  304. * adjust bit position count
  305. */
  306. dst_exponent += 32;
  307. }
  308. else {
  309. /*
  310. * Check word for most significant bit set. Returns
  311. * a value in dst_exponent indicating the bit position,
  312. * between -1 and 30.
  313. */
  314. Find_ms_one_bit(srcp1,dst_exponent);
  315. /* left justify source, with msb at bit position 1 */
  316. if (dst_exponent > 0) {
  317. Variable_shift_double(srcp1,srcp2,(32-dst_exponent),
  318. srcp1);
  319. srcp2 <<= dst_exponent;
  320. }
  321. /*
  322. * If dst_exponent = 0, we don't need to shift anything.
  323. * If dst_exponent = -1, src = - 2**63 so we won't need to
  324. * shift srcp2.
  325. */
  326. else srcp1 >>= -(dst_exponent);
  327. }
  328. Dbl_set_mantissap1(resultp1, srcp1 >> (DBL_EXP_LENGTH-1));
  329. Shiftdouble(srcp1,srcp2,DBL_EXP_LENGTH-1,resultp2);
  330. Dbl_set_exponent(resultp1, (62+DBL_BIAS) - dst_exponent);
  331. /* check for inexact */
  332. if (Dint_isinexact_to_dbl(srcp2)) {
  333. switch (Rounding_mode()) {
  334. case ROUNDPLUS:
  335. if (Dbl_iszero_sign(resultp1)) {
  336. Dbl_increment(resultp1,resultp2);
  337. }
  338. break;
  339. case ROUNDMINUS:
  340. if (Dbl_isone_sign(resultp1)) {
  341. Dbl_increment(resultp1,resultp2);
  342. }
  343. break;
  344. case ROUNDNEAREST:
  345. Dbl_roundnearest_from_dint(srcp2,resultp1,
  346. resultp2);
  347. }
  348. if (Is_inexacttrap_enabled()) {
  349. Dbl_copytoptr(resultp1,resultp2,dstptr);
  350. return(INEXACTEXCEPTION);
  351. }
  352. else Set_inexactflag();
  353. }
  354. Dbl_copytoptr(resultp1,resultp2,dstptr);
  355. return(NOEXCEPTION);
  356. }