fcnvfu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. * File:
  25. * @(#) pa/spmath/fcnvfu.c $Revision: 1.1 $
  26. *
  27. * Purpose:
  28. * Floating-point to Unsigned Fixed-point Converts
  29. *
  30. * External Interfaces:
  31. * dbl_to_dbl_fcnvfu(srcptr,nullptr,dstptr,status)
  32. * dbl_to_sgl_fcnvfu(srcptr,nullptr,dstptr,status)
  33. * sgl_to_dbl_fcnvfu(srcptr,nullptr,dstptr,status)
  34. * sgl_to_sgl_fcnvfu(srcptr,nullptr,dstptr,status)
  35. *
  36. * Internal Interfaces:
  37. *
  38. * Theory:
  39. * <<please update with a overview of the operation of this file>>
  40. *
  41. * END_DESC
  42. */
  43. #include "float.h"
  44. #include "sgl_float.h"
  45. #include "dbl_float.h"
  46. #include "cnv_float.h"
  47. /************************************************************************
  48. * Floating-point to Unsigned Fixed-point Converts *
  49. ************************************************************************/
  50. /*
  51. * Single Floating-point to Single Unsigned Fixed
  52. */
  53. /*ARGSUSED*/
  54. int
  55. sgl_to_sgl_fcnvfu(
  56. sgl_floating_point *srcptr,
  57. unsigned int *nullptr,
  58. unsigned int *dstptr,
  59. unsigned int *status)
  60. {
  61. register unsigned int src, result;
  62. register int src_exponent;
  63. register boolean inexact = FALSE;
  64. src = *srcptr;
  65. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  66. /*
  67. * Test for overflow
  68. */
  69. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  70. if (Sgl_isone_sign(src)) {
  71. result = 0;
  72. } else {
  73. result = 0xffffffff;
  74. }
  75. if (Is_invalidtrap_enabled()) {
  76. return(INVALIDEXCEPTION);
  77. }
  78. Set_invalidflag();
  79. *dstptr = result;
  80. return(NOEXCEPTION);
  81. }
  82. /*
  83. * Generate result
  84. */
  85. if (src_exponent >= 0) {
  86. /*
  87. * Check sign.
  88. * If negative, trap unimplemented.
  89. */
  90. if (Sgl_isone_sign(src)) {
  91. result = 0;
  92. if (Is_invalidtrap_enabled()) {
  93. return(INVALIDEXCEPTION);
  94. }
  95. Set_invalidflag();
  96. *dstptr = result;
  97. return(NOEXCEPTION);
  98. }
  99. Sgl_clear_signexponent_set_hidden(src);
  100. Suint_from_sgl_mantissa(src,src_exponent,result);
  101. /* check for inexact */
  102. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  103. inexact = TRUE;
  104. /* round result */
  105. switch (Rounding_mode()) {
  106. case ROUNDPLUS:
  107. result++;
  108. break;
  109. case ROUNDMINUS: /* never negative */
  110. break;
  111. case ROUNDNEAREST:
  112. if (Sgl_isone_roundbit(src,src_exponent) &&
  113. (Sgl_isone_stickybit(src,src_exponent) ||
  114. (result & 1))) {
  115. result++;
  116. }
  117. break;
  118. }
  119. }
  120. } else {
  121. result = 0;
  122. /* check for inexact */
  123. if (Sgl_isnotzero_exponentmantissa(src)) {
  124. inexact = TRUE;
  125. /* round result */
  126. switch (Rounding_mode()) {
  127. case ROUNDPLUS:
  128. if (Sgl_iszero_sign(src)) {
  129. result++;
  130. }
  131. break;
  132. case ROUNDMINUS:
  133. if (Sgl_isone_sign(src)) {
  134. result = 0;
  135. if (Is_invalidtrap_enabled()) {
  136. return(INVALIDEXCEPTION);
  137. }
  138. Set_invalidflag();
  139. inexact = FALSE;
  140. }
  141. break;
  142. case ROUNDNEAREST:
  143. if (src_exponent == -1 &&
  144. Sgl_isnotzero_mantissa(src)) {
  145. if (Sgl_isone_sign(src)) {
  146. result = 0;
  147. if (Is_invalidtrap_enabled()) {
  148. return(INVALIDEXCEPTION);
  149. }
  150. Set_invalidflag();
  151. inexact = FALSE;
  152. }
  153. else result++;
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. *dstptr = result;
  160. if (inexact) {
  161. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  162. else Set_inexactflag();
  163. }
  164. return(NOEXCEPTION);
  165. }
  166. /*
  167. * Single Floating-point to Double Unsigned Fixed
  168. */
  169. /*ARGSUSED*/
  170. int
  171. sgl_to_dbl_fcnvfu(
  172. sgl_floating_point *srcptr,
  173. unsigned int *nullptr,
  174. dbl_unsigned *dstptr,
  175. unsigned int *status)
  176. {
  177. register int src_exponent;
  178. register unsigned int src, resultp1, resultp2;
  179. register boolean inexact = FALSE;
  180. src = *srcptr;
  181. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  182. /*
  183. * Test for overflow
  184. */
  185. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  186. if (Sgl_isone_sign(src)) {
  187. resultp1 = resultp2 = 0;
  188. } else {
  189. resultp1 = resultp2 = 0xffffffff;
  190. }
  191. if (Is_invalidtrap_enabled()) {
  192. return(INVALIDEXCEPTION);
  193. }
  194. Set_invalidflag();
  195. Duint_copytoptr(resultp1,resultp2,dstptr);
  196. return(NOEXCEPTION);
  197. }
  198. /*
  199. * Generate result
  200. */
  201. if (src_exponent >= 0) {
  202. /*
  203. * Check sign.
  204. * If negative, trap unimplemented.
  205. */
  206. if (Sgl_isone_sign(src)) {
  207. resultp1 = resultp2 = 0;
  208. if (Is_invalidtrap_enabled()) {
  209. return(INVALIDEXCEPTION);
  210. }
  211. Set_invalidflag();
  212. Duint_copytoptr(resultp1,resultp2,dstptr);
  213. return(NOEXCEPTION);
  214. }
  215. Sgl_clear_signexponent_set_hidden(src);
  216. Duint_from_sgl_mantissa(src,src_exponent,resultp1,resultp2);
  217. /* check for inexact */
  218. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  219. inexact = TRUE;
  220. /* round result */
  221. switch (Rounding_mode()) {
  222. case ROUNDPLUS:
  223. Duint_increment(resultp1,resultp2);
  224. break;
  225. case ROUNDMINUS: /* never negative */
  226. break;
  227. case ROUNDNEAREST:
  228. if (Sgl_isone_roundbit(src,src_exponent) &&
  229. (Sgl_isone_stickybit(src,src_exponent) ||
  230. Duint_isone_lowp2(resultp2))) {
  231. Duint_increment(resultp1,resultp2);
  232. }
  233. break;
  234. }
  235. }
  236. } else {
  237. Duint_setzero(resultp1,resultp2);
  238. /* check for inexact */
  239. if (Sgl_isnotzero_exponentmantissa(src)) {
  240. inexact = TRUE;
  241. /* round result */
  242. switch (Rounding_mode()) {
  243. case ROUNDPLUS:
  244. if (Sgl_iszero_sign(src)) {
  245. Duint_increment(resultp1,resultp2);
  246. }
  247. break;
  248. case ROUNDMINUS:
  249. if (Sgl_isone_sign(src)) {
  250. resultp1 = resultp2 = 0;
  251. if (Is_invalidtrap_enabled()) {
  252. return(INVALIDEXCEPTION);
  253. }
  254. Set_invalidflag();
  255. inexact = FALSE;
  256. }
  257. break;
  258. case ROUNDNEAREST:
  259. if (src_exponent == -1 &&
  260. Sgl_isnotzero_mantissa(src)) {
  261. if (Sgl_isone_sign(src)) {
  262. resultp1 = 0;
  263. resultp2 = 0;
  264. if (Is_invalidtrap_enabled()) {
  265. return(INVALIDEXCEPTION);
  266. }
  267. Set_invalidflag();
  268. inexact = FALSE;
  269. }
  270. else Duint_increment(resultp1,resultp2);
  271. }
  272. }
  273. }
  274. }
  275. Duint_copytoptr(resultp1,resultp2,dstptr);
  276. if (inexact) {
  277. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  278. else Set_inexactflag();
  279. }
  280. return(NOEXCEPTION);
  281. }
  282. /*
  283. * Double Floating-point to Single Unsigned Fixed
  284. */
  285. /*ARGSUSED*/
  286. int
  287. dbl_to_sgl_fcnvfu (dbl_floating_point * srcptr, unsigned int *nullptr,
  288. unsigned int *dstptr, unsigned int *status)
  289. {
  290. register unsigned int srcp1, srcp2, result;
  291. register int src_exponent;
  292. register boolean inexact = FALSE;
  293. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  294. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  295. /*
  296. * Test for overflow
  297. */
  298. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  299. if (Dbl_isone_sign(srcp1)) {
  300. result = 0;
  301. } else {
  302. result = 0xffffffff;
  303. }
  304. if (Is_invalidtrap_enabled()) {
  305. return(INVALIDEXCEPTION);
  306. }
  307. Set_invalidflag();
  308. *dstptr = result;
  309. return(NOEXCEPTION);
  310. }
  311. /*
  312. * Generate result
  313. */
  314. if (src_exponent >= 0) {
  315. /*
  316. * Check sign.
  317. * If negative, trap unimplemented.
  318. */
  319. if (Dbl_isone_sign(srcp1)) {
  320. result = 0;
  321. if (Is_invalidtrap_enabled()) {
  322. return(INVALIDEXCEPTION);
  323. }
  324. Set_invalidflag();
  325. *dstptr = result;
  326. return(NOEXCEPTION);
  327. }
  328. Dbl_clear_signexponent_set_hidden(srcp1);
  329. Suint_from_dbl_mantissa(srcp1,srcp2,src_exponent,result);
  330. /* check for inexact */
  331. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  332. inexact = TRUE;
  333. /* round result */
  334. switch (Rounding_mode()) {
  335. case ROUNDPLUS:
  336. result++;
  337. break;
  338. case ROUNDMINUS: /* never negative */
  339. break;
  340. case ROUNDNEAREST:
  341. if(Dbl_isone_roundbit(srcp1,srcp2,src_exponent) &&
  342. (Dbl_isone_stickybit(srcp1,srcp2,src_exponent)||
  343. result&1))
  344. result++;
  345. break;
  346. }
  347. /* check for overflow */
  348. if (result == 0) {
  349. result = 0xffffffff;
  350. if (Is_invalidtrap_enabled()) {
  351. return(INVALIDEXCEPTION);
  352. }
  353. Set_invalidflag();
  354. *dstptr = result;
  355. return(NOEXCEPTION);
  356. }
  357. }
  358. } else {
  359. result = 0;
  360. /* check for inexact */
  361. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  362. inexact = TRUE;
  363. /* round result */
  364. switch (Rounding_mode()) {
  365. case ROUNDPLUS:
  366. if (Dbl_iszero_sign(srcp1)) result++;
  367. break;
  368. case ROUNDMINUS:
  369. if (Dbl_isone_sign(srcp1)) {
  370. result = 0;
  371. if (Is_invalidtrap_enabled()) {
  372. return(INVALIDEXCEPTION);
  373. }
  374. Set_invalidflag();
  375. inexact = FALSE;
  376. }
  377. break;
  378. case ROUNDNEAREST:
  379. if (src_exponent == -1 &&
  380. Dbl_isnotzero_mantissa(srcp1,srcp2))
  381. if (Dbl_isone_sign(srcp1)) {
  382. result = 0;
  383. if (Is_invalidtrap_enabled()) {
  384. return(INVALIDEXCEPTION);
  385. }
  386. Set_invalidflag();
  387. inexact = FALSE;
  388. }
  389. else result++;
  390. }
  391. }
  392. }
  393. *dstptr = result;
  394. if (inexact) {
  395. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  396. else Set_inexactflag();
  397. }
  398. return(NOEXCEPTION);
  399. }
  400. /*
  401. * Double Floating-point to Double Unsigned Fixed
  402. */
  403. /*ARGSUSED*/
  404. int
  405. dbl_to_dbl_fcnvfu (dbl_floating_point * srcptr, unsigned int *nullptr,
  406. dbl_unsigned * dstptr, unsigned int *status)
  407. {
  408. register int src_exponent;
  409. register unsigned int srcp1, srcp2, resultp1, resultp2;
  410. register boolean inexact = FALSE;
  411. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  412. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  413. /*
  414. * Test for overflow
  415. */
  416. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  417. if (Dbl_isone_sign(srcp1)) {
  418. resultp1 = resultp2 = 0;
  419. } else {
  420. resultp1 = resultp2 = 0xffffffff;
  421. }
  422. if (Is_invalidtrap_enabled()) {
  423. return(INVALIDEXCEPTION);
  424. }
  425. Set_invalidflag();
  426. Duint_copytoptr(resultp1,resultp2,dstptr);
  427. return(NOEXCEPTION);
  428. }
  429. /*
  430. * Generate result
  431. */
  432. if (src_exponent >= 0) {
  433. /*
  434. * Check sign.
  435. * If negative, trap unimplemented.
  436. */
  437. if (Dbl_isone_sign(srcp1)) {
  438. resultp1 = resultp2 = 0;
  439. if (Is_invalidtrap_enabled()) {
  440. return(INVALIDEXCEPTION);
  441. }
  442. Set_invalidflag();
  443. Duint_copytoptr(resultp1,resultp2,dstptr);
  444. return(NOEXCEPTION);
  445. }
  446. Dbl_clear_signexponent_set_hidden(srcp1);
  447. Duint_from_dbl_mantissa(srcp1,srcp2,src_exponent,resultp1,
  448. resultp2);
  449. /* check for inexact */
  450. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  451. inexact = TRUE;
  452. /* round result */
  453. switch (Rounding_mode()) {
  454. case ROUNDPLUS:
  455. Duint_increment(resultp1,resultp2);
  456. break;
  457. case ROUNDMINUS: /* never negative */
  458. break;
  459. case ROUNDNEAREST:
  460. if(Dbl_isone_roundbit(srcp1,srcp2,src_exponent))
  461. if(Dbl_isone_stickybit(srcp1,srcp2,src_exponent) ||
  462. Duint_isone_lowp2(resultp2))
  463. Duint_increment(resultp1,resultp2);
  464. }
  465. }
  466. } else {
  467. Duint_setzero(resultp1,resultp2);
  468. /* check for inexact */
  469. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  470. inexact = TRUE;
  471. /* round result */
  472. switch (Rounding_mode()) {
  473. case ROUNDPLUS:
  474. if (Dbl_iszero_sign(srcp1)) {
  475. Duint_increment(resultp1,resultp2);
  476. }
  477. break;
  478. case ROUNDMINUS:
  479. if (Dbl_isone_sign(srcp1)) {
  480. resultp1 = resultp2 = 0;
  481. if (Is_invalidtrap_enabled()) {
  482. return(INVALIDEXCEPTION);
  483. }
  484. Set_invalidflag();
  485. inexact = FALSE;
  486. }
  487. break;
  488. case ROUNDNEAREST:
  489. if (src_exponent == -1 &&
  490. Dbl_isnotzero_mantissa(srcp1,srcp2))
  491. if (Dbl_iszero_sign(srcp1)) {
  492. Duint_increment(resultp1,resultp2);
  493. } else {
  494. resultp1 = 0;
  495. resultp2 = 0;
  496. if (Is_invalidtrap_enabled()) {
  497. return(INVALIDEXCEPTION);
  498. }
  499. Set_invalidflag();
  500. inexact = FALSE;
  501. }
  502. }
  503. }
  504. }
  505. Duint_copytoptr(resultp1,resultp2,dstptr);
  506. if (inexact) {
  507. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  508. else Set_inexactflag();
  509. }
  510. return(NOEXCEPTION);
  511. }