dp_fint.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* IEEE754 floating point arithmetic
  2. * double precision: common utilities
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. * http://www.algor.co.uk
  8. *
  9. * ########################################################################
  10. *
  11. * This program is free software; you can distribute it and/or modify it
  12. * under the terms of the GNU General Public License (Version 2) as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  23. *
  24. * ########################################################################
  25. */
  26. #include "ieee754dp.h"
  27. ieee754dp ieee754dp_fint(int x)
  28. {
  29. u64 xm;
  30. int xe;
  31. int xs;
  32. CLEARCX;
  33. if (x == 0)
  34. return ieee754dp_zero(0);
  35. if (x == 1 || x == -1)
  36. return ieee754dp_one(x < 0);
  37. if (x == 10 || x == -10)
  38. return ieee754dp_ten(x < 0);
  39. xs = (x < 0);
  40. if (xs) {
  41. if (x == (1 << 31))
  42. xm = ((unsigned) 1 << 31); /* max neg can't be safely negated */
  43. else
  44. xm = -x;
  45. } else {
  46. xm = x;
  47. }
  48. #if 1
  49. /* normalize - result can never be inexact or overflow */
  50. xe = DP_MBITS;
  51. while ((xm >> DP_MBITS) == 0) {
  52. xm <<= 1;
  53. xe--;
  54. }
  55. return builddp(xs, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  56. #else
  57. /* normalize */
  58. xe = DP_MBITS + 3;
  59. while ((xm >> (DP_MBITS + 3)) == 0) {
  60. xm <<= 1;
  61. xe--;
  62. }
  63. DPNORMRET1(xs, xe, xm, "fint", x);
  64. #endif
  65. }
  66. ieee754dp ieee754dp_funs(unsigned int u)
  67. {
  68. if ((int) u < 0)
  69. return ieee754dp_add(ieee754dp_1e31(),
  70. ieee754dp_fint(u & ~(1 << 31)));
  71. return ieee754dp_fint(u);
  72. }