fcnvfu.c 11 KB

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