dp_modf.c 2.0 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. /* modf function is always exact for a finite number
  28. */
  29. ieee754dp ieee754dp_modf(ieee754dp x, ieee754dp * ip)
  30. {
  31. COMPXDP;
  32. CLEARCX;
  33. EXPLODEXDP;
  34. switch (xc) {
  35. case IEEE754_CLASS_SNAN:
  36. case IEEE754_CLASS_QNAN:
  37. case IEEE754_CLASS_INF:
  38. case IEEE754_CLASS_ZERO:
  39. *ip = x;
  40. return x;
  41. case IEEE754_CLASS_DNORM:
  42. /* far to small */
  43. *ip = ieee754dp_zero(xs);
  44. return x;
  45. case IEEE754_CLASS_NORM:
  46. break;
  47. }
  48. if (xe < 0) {
  49. *ip = ieee754dp_zero(xs);
  50. return x;
  51. }
  52. if (xe >= DP_MBITS) {
  53. *ip = x;
  54. return ieee754dp_zero(xs);
  55. }
  56. /* generate ipart mantissa by clearing bottom bits
  57. */
  58. *ip = builddp(xs, xe + DP_EBIAS,
  59. ((xm >> (DP_MBITS - xe)) << (DP_MBITS - xe)) &
  60. ~DP_HIDDEN_BIT);
  61. /* generate fpart mantissa by clearing top bits
  62. * and normalizing (must be able to normalize)
  63. */
  64. xm = (xm << (64 - (DP_MBITS - xe))) >> (64 - (DP_MBITS - xe));
  65. if (xm == 0)
  66. return ieee754dp_zero(xs);
  67. while ((xm >> DP_MBITS) == 0) {
  68. xm <<= 1;
  69. xe--;
  70. }
  71. return builddp(xs, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  72. }