fcnvfx.c 14 KB

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