fcnvuf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/fcnvuf.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * Fixed point to Floating-point Converts
  16. *
  17. * External Interfaces:
  18. * dbl_to_dbl_fcnvuf(srcptr,nullptr,dstptr,status)
  19. * dbl_to_sgl_fcnvuf(srcptr,nullptr,dstptr,status)
  20. * sgl_to_dbl_fcnvuf(srcptr,nullptr,dstptr,status)
  21. * sgl_to_sgl_fcnvuf(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. * Fixed point to Floating-point Converts *
  36. ************************************************************************/
  37. /*
  38. * Convert Single Unsigned Fixed to Single Floating-point format
  39. */
  40. int
  41. sgl_to_sgl_fcnvuf(
  42. unsigned int *srcptr,
  43. unsigned int *nullptr,
  44. sgl_floating_point *dstptr,
  45. unsigned int *status)
  46. {
  47. register unsigned int src, result = 0;
  48. register int dst_exponent;
  49. src = *srcptr;
  50. /* Check for zero */
  51. if (src == 0) {
  52. Sgl_setzero(result);
  53. *dstptr = result;
  54. return(NOEXCEPTION);
  55. }
  56. /*
  57. * Generate exponent and normalized mantissa
  58. */
  59. dst_exponent = 16; /* initialize for normalization */
  60. /*
  61. * Check word for most significant bit set. Returns
  62. * a value in dst_exponent indicating the bit position,
  63. * between -1 and 30.
  64. */
  65. Find_ms_one_bit(src,dst_exponent);
  66. /* left justify source, with msb at bit position 0 */
  67. src <<= dst_exponent+1;
  68. Sgl_set_mantissa(result, src >> SGL_EXP_LENGTH);
  69. Sgl_set_exponent(result, 30+SGL_BIAS - dst_exponent);
  70. /* check for inexact */
  71. if (Suint_isinexact_to_sgl(src)) {
  72. switch (Rounding_mode()) {
  73. case ROUNDPLUS:
  74. Sgl_increment(result);
  75. break;
  76. case ROUNDMINUS: /* never negative */
  77. break;
  78. case ROUNDNEAREST:
  79. Sgl_roundnearest_from_suint(src,result);
  80. break;
  81. }
  82. if (Is_inexacttrap_enabled()) {
  83. *dstptr = result;
  84. return(INEXACTEXCEPTION);
  85. }
  86. else Set_inexactflag();
  87. }
  88. *dstptr = result;
  89. return(NOEXCEPTION);
  90. }
  91. /*
  92. * Single Unsigned Fixed to Double Floating-point
  93. */
  94. int
  95. sgl_to_dbl_fcnvuf(
  96. unsigned int *srcptr,
  97. unsigned int *nullptr,
  98. dbl_floating_point *dstptr,
  99. unsigned int *status)
  100. {
  101. register int dst_exponent;
  102. register unsigned int src, resultp1 = 0, resultp2 = 0;
  103. src = *srcptr;
  104. /* Check for zero */
  105. if (src == 0) {
  106. Dbl_setzero(resultp1,resultp2);
  107. Dbl_copytoptr(resultp1,resultp2,dstptr);
  108. return(NOEXCEPTION);
  109. }
  110. /*
  111. * Generate exponent and normalized mantissa
  112. */
  113. dst_exponent = 16; /* initialize for normalization */
  114. /*
  115. * Check word for most significant bit set. Returns
  116. * a value in dst_exponent indicating the bit position,
  117. * between -1 and 30.
  118. */
  119. Find_ms_one_bit(src,dst_exponent);
  120. /* left justify source, with msb at bit position 0 */
  121. src <<= dst_exponent+1;
  122. Dbl_set_mantissap1(resultp1, src >> DBL_EXP_LENGTH);
  123. Dbl_set_mantissap2(resultp2, src << (32-DBL_EXP_LENGTH));
  124. Dbl_set_exponent(resultp1, (30+DBL_BIAS) - dst_exponent);
  125. Dbl_copytoptr(resultp1,resultp2,dstptr);
  126. return(NOEXCEPTION);
  127. }
  128. /*
  129. * Double Unsigned Fixed to Single Floating-point
  130. */
  131. int
  132. dbl_to_sgl_fcnvuf(
  133. dbl_unsigned *srcptr,
  134. unsigned int *nullptr,
  135. sgl_floating_point *dstptr,
  136. unsigned int *status)
  137. {
  138. int dst_exponent;
  139. unsigned int srcp1, srcp2, result = 0;
  140. Duint_copyfromptr(srcptr,srcp1,srcp2);
  141. /* Check for zero */
  142. if (srcp1 == 0 && srcp2 == 0) {
  143. Sgl_setzero(result);
  144. *dstptr = result;
  145. return(NOEXCEPTION);
  146. }
  147. /*
  148. * Generate exponent and normalized mantissa
  149. */
  150. dst_exponent = 16; /* initialize for normalization */
  151. if (srcp1 == 0) {
  152. /*
  153. * Check word for most significant bit set. Returns
  154. * a value in dst_exponent indicating the bit position,
  155. * between -1 and 30.
  156. */
  157. Find_ms_one_bit(srcp2,dst_exponent);
  158. /* left justify source, with msb at bit position 0 */
  159. srcp1 = srcp2 << dst_exponent+1;
  160. srcp2 = 0;
  161. /*
  162. * since msb set is in second word, need to
  163. * adjust bit position count
  164. */
  165. dst_exponent += 32;
  166. }
  167. else {
  168. /*
  169. * Check word for most significant bit set. Returns
  170. * a value in dst_exponent indicating the bit position,
  171. * between -1 and 30.
  172. *
  173. */
  174. Find_ms_one_bit(srcp1,dst_exponent);
  175. /* left justify source, with msb at bit position 0 */
  176. if (dst_exponent >= 0) {
  177. Variable_shift_double(srcp1,srcp2,(31-dst_exponent),
  178. srcp1);
  179. srcp2 <<= dst_exponent+1;
  180. }
  181. }
  182. Sgl_set_mantissa(result, srcp1 >> SGL_EXP_LENGTH);
  183. Sgl_set_exponent(result, (62+SGL_BIAS) - dst_exponent);
  184. /* check for inexact */
  185. if (Duint_isinexact_to_sgl(srcp1,srcp2)) {
  186. switch (Rounding_mode()) {
  187. case ROUNDPLUS:
  188. Sgl_increment(result);
  189. break;
  190. case ROUNDMINUS: /* never negative */
  191. break;
  192. case ROUNDNEAREST:
  193. Sgl_roundnearest_from_duint(srcp1,srcp2,result);
  194. break;
  195. }
  196. if (Is_inexacttrap_enabled()) {
  197. *dstptr = result;
  198. return(INEXACTEXCEPTION);
  199. }
  200. else Set_inexactflag();
  201. }
  202. *dstptr = result;
  203. return(NOEXCEPTION);
  204. }
  205. /*
  206. * Double Unsigned Fixed to Double Floating-point
  207. */
  208. int
  209. dbl_to_dbl_fcnvuf(
  210. dbl_unsigned *srcptr,
  211. unsigned int *nullptr,
  212. dbl_floating_point *dstptr,
  213. unsigned int *status)
  214. {
  215. register int dst_exponent;
  216. register unsigned int srcp1, srcp2, resultp1 = 0, resultp2 = 0;
  217. Duint_copyfromptr(srcptr,srcp1,srcp2);
  218. /* Check for zero */
  219. if (srcp1 == 0 && srcp2 ==0) {
  220. Dbl_setzero(resultp1,resultp2);
  221. Dbl_copytoptr(resultp1,resultp2,dstptr);
  222. return(NOEXCEPTION);
  223. }
  224. /*
  225. * Generate exponent and normalized mantissa
  226. */
  227. dst_exponent = 16; /* initialize for normalization */
  228. if (srcp1 == 0) {
  229. /*
  230. * Check word for most significant bit set. Returns
  231. * a value in dst_exponent indicating the bit position,
  232. * between -1 and 30.
  233. */
  234. Find_ms_one_bit(srcp2,dst_exponent);
  235. /* left justify source, with msb at bit position 0 */
  236. srcp1 = srcp2 << dst_exponent+1;
  237. srcp2 = 0;
  238. /*
  239. * since msb set is in second word, need to
  240. * adjust bit position count
  241. */
  242. dst_exponent += 32;
  243. }
  244. else {
  245. /*
  246. * Check word for most significant bit set. Returns
  247. * a value in dst_exponent indicating the bit position,
  248. * between -1 and 30.
  249. */
  250. Find_ms_one_bit(srcp1,dst_exponent);
  251. /* left justify source, with msb at bit position 0 */
  252. if (dst_exponent >= 0) {
  253. Variable_shift_double(srcp1,srcp2,(31-dst_exponent),
  254. srcp1);
  255. srcp2 <<= dst_exponent+1;
  256. }
  257. }
  258. Dbl_set_mantissap1(resultp1, srcp1 >> DBL_EXP_LENGTH);
  259. Shiftdouble(srcp1,srcp2,DBL_EXP_LENGTH,resultp2);
  260. Dbl_set_exponent(resultp1, (62+DBL_BIAS) - dst_exponent);
  261. /* check for inexact */
  262. if (Duint_isinexact_to_dbl(srcp2)) {
  263. switch (Rounding_mode()) {
  264. case ROUNDPLUS:
  265. Dbl_increment(resultp1,resultp2);
  266. break;
  267. case ROUNDMINUS: /* never negative */
  268. break;
  269. case ROUNDNEAREST:
  270. Dbl_roundnearest_from_duint(srcp2,resultp1,
  271. resultp2);
  272. break;
  273. }
  274. if (Is_inexacttrap_enabled()) {
  275. Dbl_copytoptr(resultp1,resultp2,dstptr);
  276. return(INEXACTEXCEPTION);
  277. }
  278. else Set_inexactflag();
  279. }
  280. Dbl_copytoptr(resultp1,resultp2,dstptr);
  281. return(NOEXCEPTION);
  282. }