dp_tlong.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:4k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* IEEE754 floating point arithmetic
  2.  * double precision: common utilities
  3.  */
  4. /*
  5.  * MIPS floating point support
  6.  * Copyright (C) 1994-2000 Algorithmics Ltd.  All rights reserved.
  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. long long ieee754dp_tlong(ieee754dp x)
  28. {
  29. COMPXDP;
  30. CLEARCX;
  31. EXPLODEXDP;
  32. switch (xc) {
  33. case IEEE754_CLASS_SNAN:
  34. case IEEE754_CLASS_QNAN:
  35. SETCX(IEEE754_INVALID_OPERATION);
  36. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  37. case IEEE754_CLASS_INF:
  38. SETCX(IEEE754_OVERFLOW);
  39. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  40. case IEEE754_CLASS_ZERO:
  41. return 0;
  42. case IEEE754_CLASS_DNORM: /* much too small */
  43. SETCX(IEEE754_UNDERFLOW);
  44. return ieee754di_xcpt(0, "dp_tlong", x);
  45. case IEEE754_CLASS_NORM:
  46. break;
  47. }
  48. if (xe >= 63) {
  49. SETCX(IEEE754_OVERFLOW);
  50. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  51. }
  52. if (xe < 0) {
  53. if (ieee754_csr.rm == IEEE754_RU) {
  54. if (xs) { /* Negative  */
  55. return 0x0000000000000000LL;
  56. } else { /* Positive */
  57. return 0x0000000000000001LL;
  58. }
  59. } else if (ieee754_csr.rm == IEEE754_RD) {
  60. if (xs) { /* Negative , return -1 */
  61. return 0xffffffffffffffffLL;
  62. } else { /* Positive */
  63. return 0x0000000000000000LL;
  64. }
  65. } else {
  66. SETCX(IEEE754_UNDERFLOW);
  67. return ieee754di_xcpt(0, "dp_tlong", x);
  68. }
  69. }
  70. /* oh gawd */
  71. if (xe > DP_MBITS) {
  72. xm <<= xe - DP_MBITS;
  73. } else if (xe < DP_MBITS) {
  74. unsigned long long residue;
  75. unsigned long long mask = 0;
  76. int i;
  77. int round;
  78. int sticky;
  79. int odd;
  80. /* compute mask */
  81. for (i = 0; i < DP_MBITS - xe; i++) {
  82. mask = mask << 1;
  83. mask = mask | 0x1;
  84. }
  85. residue = (xm & mask) << (64 - (DP_MBITS - xe));
  86. round =
  87.     ((0x8000000000000000LL & residue) !=
  88.      0x0000000000000000LL);
  89. sticky =
  90.     ((0x7fffffffffffffffLL & residue) !=
  91.      0x0000000000000000LL);
  92. xm >>= DP_MBITS - xe;
  93. odd = ((xm & 0x1) != 0x0000000000000000LL);
  94. /* Do the rounding */
  95. if (!round && sticky) {
  96. if ((ieee754_csr.rm == IEEE754_RU && !xs)
  97.     || (ieee754_csr.rm == IEEE754_RD && xs)) {
  98. xm++;
  99. }
  100. } else if (round && !sticky) {
  101. if ((ieee754_csr.rm == IEEE754_RU && !xs)
  102.     || (ieee754_csr.rm == IEEE754_RD && xs)
  103.     || (ieee754_csr.rm == IEEE754_RN && odd)) {
  104. xm++;
  105. }
  106. } else if (round && sticky) {
  107. if ((ieee754_csr.rm == IEEE754_RU && !xs)
  108.     || (ieee754_csr.rm == IEEE754_RD && xs)
  109.     || (ieee754_csr.rm == IEEE754_RN)) {
  110. xm++;
  111. }
  112. }
  113. }
  114. if (xs)
  115. return -xm;
  116. else
  117. return xm;
  118. }
  119. unsigned long long ieee754dp_tulong(ieee754dp x)
  120. {
  121. ieee754dp hb = ieee754dp_1e63();
  122. /* what if x < 0 ?? */
  123. if (ieee754dp_lt(x, hb))
  124. return (unsigned long long) ieee754dp_tlong(x);
  125. return (unsigned long long) ieee754dp_tlong(ieee754dp_sub(x, hb)) |
  126.     (1ULL << 63);
  127. }