op-common.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:25k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Software floating-point emulation. Common operations.
  2.    Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.    Contributed by Richard Henderson (rth@cygnus.com),
  5.   Jakub Jelinek (jj@ultra.linux.cz),
  6.   David S. Miller (davem@redhat.com) and
  7.   Peter Maydell (pmaydell@chiark.greenend.org.uk).
  8.    The GNU C Library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.    The GNU C Library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.    Library General Public License for more details.
  16.    You should have received a copy of the GNU Library General Public
  17.    License along with the GNU C Library; see the file COPYING.LIB.  If
  18.    not, write to the Free Software Foundation, Inc.,
  19.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20. #ifndef __MATH_EMU_OP_COMMON_H__
  21. #define __MATH_EMU_OP_COMMON_H__
  22. #define _FP_DECL(wc, X)
  23.   _FP_I_TYPE X##_c=0, X##_s=0, X##_e=0;
  24.   _FP_FRAC_DECL_##wc(X)
  25. /*
  26.  * Finish truely unpacking a native fp value by classifying the kind
  27.  * of fp value and normalizing both the exponent and the fraction.
  28.  */
  29. #define _FP_UNPACK_CANONICAL(fs, wc, X)
  30. do {
  31.   switch (X##_e)
  32.   {
  33.   default:
  34.     _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_##fs;
  35.     _FP_FRAC_SLL_##wc(X, _FP_WORKBITS);
  36.     X##_e -= _FP_EXPBIAS_##fs;
  37.     X##_c = FP_CLS_NORMAL;
  38.     break;
  39.   case 0:
  40.     if (_FP_FRAC_ZEROP_##wc(X))
  41.       X##_c = FP_CLS_ZERO;
  42.     else
  43.       {
  44. /* a denormalized number */
  45. _FP_I_TYPE _shift;
  46. _FP_FRAC_CLZ_##wc(_shift, X);
  47. _shift -= _FP_FRACXBITS_##fs;
  48. _FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKBITS));
  49. X##_e -= _FP_EXPBIAS_##fs - 1 + _shift;
  50. X##_c = FP_CLS_NORMAL;
  51. FP_SET_EXCEPTION(FP_EX_DENORM);
  52. if (FP_DENORM_ZERO)
  53.   {
  54.     FP_SET_EXCEPTION(FP_EX_INEXACT);
  55.     X##_c = FP_CLS_ZERO;
  56.   }
  57.       }
  58.     break;
  59.   case _FP_EXPMAX_##fs:
  60.     if (_FP_FRAC_ZEROP_##wc(X))
  61.       X##_c = FP_CLS_INF;
  62.     else
  63.       {
  64. X##_c = FP_CLS_NAN;
  65. /* Check for signaling NaN */
  66. if (!(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs))
  67.   FP_SET_EXCEPTION(FP_EX_INVALID);
  68.       }
  69.     break;
  70.   }
  71. } while (0)
  72. /*
  73.  * Before packing the bits back into the native fp result, take care
  74.  * of such mundane things as rounding and overflow.  Also, for some
  75.  * kinds of fp values, the original parts may not have been fully
  76.  * extracted -- but that is ok, we can regenerate them now.
  77.  */
  78. #define _FP_PACK_CANONICAL(fs, wc, X)
  79. do {
  80.   switch (X##_c)
  81.   {
  82.   case FP_CLS_NORMAL:
  83.     X##_e += _FP_EXPBIAS_##fs;
  84.     if (X##_e > 0)
  85.       {
  86. _FP_ROUND(wc, X);
  87. if (_FP_FRAC_OVERP_##wc(fs, X))
  88.   {
  89.     _FP_FRAC_CLEAR_OVERP_##wc(fs, X);
  90.     X##_e++;
  91.   }
  92. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS);
  93. if (X##_e >= _FP_EXPMAX_##fs)
  94.   {
  95.     /* overflow */
  96.     switch (FP_ROUNDMODE)
  97.       {
  98.       case FP_RND_NEAREST:
  99. X##_c = FP_CLS_INF;
  100. break;
  101.       case FP_RND_PINF:
  102. if (!X##_s) X##_c = FP_CLS_INF;
  103. break;
  104.       case FP_RND_MINF:
  105. if (X##_s) X##_c = FP_CLS_INF;
  106. break;
  107.       }
  108.     if (X##_c == FP_CLS_INF)
  109.       {
  110. /* Overflow to infinity */
  111. X##_e = _FP_EXPMAX_##fs;
  112. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);
  113.       }
  114.     else
  115.       {
  116. /* Overflow to maximum normal */
  117. X##_e = _FP_EXPMAX_##fs - 1;
  118. _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc);
  119.       }
  120.     FP_SET_EXCEPTION(FP_EX_OVERFLOW);
  121.             FP_SET_EXCEPTION(FP_EX_INEXACT);
  122.   }
  123.       }
  124.     else
  125.       {
  126. /* we've got a denormalized number */
  127. X##_e = -X##_e + 1;
  128. if (X##_e <= _FP_WFRACBITS_##fs)
  129.   {
  130.     _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs);
  131.     _FP_ROUND(wc, X);
  132.     if (_FP_FRAC_HIGH_##fs(X)
  133. & (_FP_OVERFLOW_##fs >> 1))
  134.       {
  135.         X##_e = 1;
  136.         _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);
  137.       }
  138.     else
  139.       {
  140. X##_e = 0;
  141. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS);
  142. FP_SET_EXCEPTION(FP_EX_UNDERFLOW);
  143.       }
  144.   }
  145. else
  146.   {
  147.     /* underflow to zero */
  148.     X##_e = 0;
  149.     if (!_FP_FRAC_ZEROP_##wc(X))
  150.       {
  151.         _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc);
  152.         _FP_ROUND(wc, X);
  153.         _FP_FRAC_LOW_##wc(X) >>= (_FP_WORKBITS);
  154.       }
  155.     FP_SET_EXCEPTION(FP_EX_UNDERFLOW);
  156.   }
  157.       }
  158.     break;
  159.   case FP_CLS_ZERO:
  160.     X##_e = 0;
  161.     _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);
  162.     break;
  163.   case FP_CLS_INF:
  164.     X##_e = _FP_EXPMAX_##fs;
  165.     _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);
  166.     break;
  167.   case FP_CLS_NAN:
  168.     X##_e = _FP_EXPMAX_##fs;
  169.     if (!_FP_KEEPNANFRACP)
  170.       {
  171. _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs);
  172. X##_s = _FP_NANSIGN_##fs;
  173.       }
  174.     else
  175.       _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs;
  176.     break;
  177.   }
  178. } while (0)
  179. /* This one accepts raw argument and not cooked,  returns
  180.  * 1 if X is a signaling NaN.
  181.  */
  182. #define _FP_ISSIGNAN(fs, wc, X)
  183. ({
  184.   int __ret = 0;
  185.   if (X##_e == _FP_EXPMAX_##fs)
  186.     {
  187.       if (!_FP_FRAC_ZEROP_##wc(X)
  188.   && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs))
  189. __ret = 1;
  190.     }
  191.   __ret;
  192. })
  193. /*
  194.  * Main addition routine.  The input values should be cooked.
  195.  */
  196. #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP)      
  197. do {      
  198.   switch (_FP_CLS_COMBINE(X##_c, Y##_c))      
  199.   {      
  200.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):      
  201.     {      
  202.       /* shift the smaller number so that its exponent matches the larger */ 
  203.       _FP_I_TYPE diff = X##_e - Y##_e;      
  204.      
  205.       if (diff < 0)      
  206. {      
  207.   diff = -diff;      
  208.   if (diff <= _FP_WFRACBITS_##fs)      
  209.     _FP_FRAC_SRS_##wc(X, diff, _FP_WFRACBITS_##fs);      
  210.   else if (!_FP_FRAC_ZEROP_##wc(X))      
  211.     _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc);      
  212.   R##_e = Y##_e;      
  213. }      
  214.       else      
  215. {      
  216.   if (diff > 0)      
  217.     {      
  218.       if (diff <= _FP_WFRACBITS_##fs)      
  219.         _FP_FRAC_SRS_##wc(Y, diff, _FP_WFRACBITS_##fs);      
  220.       else if (!_FP_FRAC_ZEROP_##wc(Y))      
  221.         _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc);      
  222.     }      
  223.   R##_e = X##_e;      
  224. }      
  225.      
  226.       R##_c = FP_CLS_NORMAL;      
  227.      
  228.       if (X##_s == Y##_s)      
  229. {      
  230.   R##_s = X##_s;      
  231.   _FP_FRAC_ADD_##wc(R, X, Y);      
  232.   if (_FP_FRAC_OVERP_##wc(fs, R))      
  233.     {      
  234.       _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs);      
  235.       R##_e++;      
  236.     }      
  237. }      
  238.       else      
  239. {      
  240.   R##_s = X##_s;      
  241.   _FP_FRAC_SUB_##wc(R, X, Y);      
  242.   if (_FP_FRAC_ZEROP_##wc(R))      
  243.     {      
  244.       /* return an exact zero */      
  245.       if (FP_ROUNDMODE == FP_RND_MINF)      
  246. R##_s |= Y##_s;      
  247.       else      
  248. R##_s &= Y##_s;      
  249.       R##_c = FP_CLS_ZERO;      
  250.     }      
  251.   else      
  252.     {      
  253.       if (_FP_FRAC_NEGP_##wc(R))      
  254. {      
  255.   _FP_FRAC_SUB_##wc(R, Y, X);      
  256.   R##_s = Y##_s;      
  257. }      
  258.      
  259.       /* renormalize after subtraction */      
  260.       _FP_FRAC_CLZ_##wc(diff, R);      
  261.       diff -= _FP_WFRACXBITS_##fs;      
  262.       if (diff)      
  263. {      
  264.   R##_e -= diff;      
  265.   _FP_FRAC_SLL_##wc(R, diff);      
  266. }      
  267.     }      
  268. }      
  269.       break;      
  270.     }      
  271.      
  272.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):      
  273.     _FP_CHOOSENAN(fs, wc, R, X, Y, OP);      
  274.     break;      
  275.      
  276.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):      
  277.     R##_e = X##_e;      
  278.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):      
  279.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):      
  280.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):      
  281.     _FP_FRAC_COPY_##wc(R, X);      
  282.     R##_s = X##_s;      
  283.     R##_c = X##_c;      
  284.     break;      
  285.      
  286.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):      
  287.     R##_e = Y##_e;      
  288.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):      
  289.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):      
  290.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):      
  291.     _FP_FRAC_COPY_##wc(R, Y);      
  292.     R##_s = Y##_s;      
  293.     R##_c = Y##_c;      
  294.     break;      
  295.      
  296.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):      
  297.     if (X##_s != Y##_s)      
  298.       {      
  299. /* +INF + -INF => NAN */      
  300. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);      
  301. R##_s = _FP_NANSIGN_##fs;      
  302. R##_c = FP_CLS_NAN;      
  303. FP_SET_EXCEPTION(FP_EX_INVALID);      
  304. break;      
  305.       }      
  306.     /* FALLTHRU */      
  307.      
  308.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):      
  309.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):      
  310.     R##_s = X##_s;      
  311.     R##_c = FP_CLS_INF;      
  312.     break;      
  313.      
  314.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):      
  315.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):      
  316.     R##_s = Y##_s;      
  317.     R##_c = FP_CLS_INF;      
  318.     break;      
  319.      
  320.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):      
  321.     /* make sure the sign is correct */      
  322.     if (FP_ROUNDMODE == FP_RND_MINF)      
  323.       R##_s = X##_s | Y##_s;      
  324.     else      
  325.       R##_s = X##_s & Y##_s;      
  326.     R##_c = FP_CLS_ZERO;      
  327.     break;      
  328.      
  329.   default:      
  330.     abort();      
  331.   }      
  332. } while (0)
  333. #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL(fs, wc, R, X, Y, '+')
  334. #define _FP_SUB(fs, wc, R, X, Y)      
  335.   do {      
  336.     if (Y##_c != FP_CLS_NAN) Y##_s ^= 1;      
  337.     _FP_ADD_INTERNAL(fs, wc, R, X, Y, '-');      
  338.   } while (0)
  339. /*
  340.  * Main negation routine.  FIXME -- when we care about setting exception
  341.  * bits reliably, this will not do.  We should examine all of the fp classes.
  342.  */
  343. #define _FP_NEG(fs, wc, R, X)
  344.   do {
  345.     _FP_FRAC_COPY_##wc(R, X);
  346.     R##_c = X##_c;
  347.     R##_e = X##_e;
  348.     R##_s = 1 ^ X##_s;
  349.   } while (0)
  350. /*
  351.  * Main multiplication routine.  The input values should be cooked.
  352.  */
  353. #define _FP_MUL(fs, wc, R, X, Y)
  354. do {
  355.   R##_s = X##_s ^ Y##_s;
  356.   switch (_FP_CLS_COMBINE(X##_c, Y##_c))
  357.   {
  358.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):
  359.     R##_c = FP_CLS_NORMAL;
  360.     R##_e = X##_e + Y##_e + 1;
  361.     _FP_MUL_MEAT_##fs(R,X,Y);
  362.     if (_FP_FRAC_OVERP_##wc(fs, R))
  363.       _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs);
  364.     else
  365.       R##_e--;
  366.     break;
  367.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):
  368.     _FP_CHOOSENAN(fs, wc, R, X, Y, '*');
  369.     break;
  370.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):
  371.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):
  372.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):
  373.     R##_s = X##_s;
  374.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):
  375.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):
  376.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):
  377.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):
  378.     _FP_FRAC_COPY_##wc(R, X);
  379.     R##_c = X##_c;
  380.     break;
  381.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):
  382.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):
  383.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):
  384.     R##_s = Y##_s;
  385.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):
  386.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):
  387.     _FP_FRAC_COPY_##wc(R, Y);
  388.     R##_c = Y##_c;
  389.     break;
  390.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):
  391.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):
  392.     R##_s = _FP_NANSIGN_##fs;
  393.     R##_c = FP_CLS_NAN;
  394.     _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);
  395.     FP_SET_EXCEPTION(FP_EX_INVALID);
  396.     break;
  397.   default:
  398.     abort();
  399.   }
  400. } while (0)
  401. /*
  402.  * Main division routine.  The input values should be cooked.
  403.  */
  404. #define _FP_DIV(fs, wc, R, X, Y)
  405. do {
  406.   R##_s = X##_s ^ Y##_s;
  407.   switch (_FP_CLS_COMBINE(X##_c, Y##_c))
  408.   {
  409.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):
  410.     R##_c = FP_CLS_NORMAL;
  411.     R##_e = X##_e - Y##_e;
  412.     _FP_DIV_MEAT_##fs(R,X,Y);
  413.     break;
  414.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):
  415.     _FP_CHOOSENAN(fs, wc, R, X, Y, '/');
  416.     break;
  417.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):
  418.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):
  419.   case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):
  420.     R##_s = X##_s;
  421.     _FP_FRAC_COPY_##wc(R, X);
  422.     R##_c = X##_c;
  423.     break;
  424.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):
  425.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):
  426.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):
  427.     R##_s = Y##_s;
  428.     _FP_FRAC_COPY_##wc(R, Y);
  429.     R##_c = Y##_c;
  430.     break;
  431.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):
  432.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):
  433.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):
  434.     R##_c = FP_CLS_ZERO;
  435.     break;
  436.   case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):
  437.     FP_SET_EXCEPTION(FP_EX_DIVZERO);
  438.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):
  439.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):
  440.     R##_c = FP_CLS_INF;
  441.     break;
  442.   case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):
  443.   case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):
  444.     R##_s = _FP_NANSIGN_##fs;
  445.     R##_c = FP_CLS_NAN;
  446.     _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);
  447.     FP_SET_EXCEPTION(FP_EX_INVALID);
  448.     break;
  449.   default:
  450.     abort();
  451.   }
  452. } while (0)
  453. /*
  454.  * Main differential comparison routine.  The inputs should be raw not
  455.  * cooked.  The return is -1,0,1 for normal values, 2 otherwise.
  456.  */
  457. #define _FP_CMP(fs, wc, ret, X, Y, un)
  458.   do {
  459.     /* NANs are unordered */
  460.     if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X))
  461. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y)))
  462.       {
  463. ret = un;
  464.       }
  465.     else
  466.       {
  467. int __is_zero_x;
  468. int __is_zero_y;
  469. __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0;
  470. __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0;
  471. if (__is_zero_x && __is_zero_y)
  472. ret = 0;
  473. else if (__is_zero_x)
  474. ret = Y##_s ? 1 : -1;
  475. else if (__is_zero_y)
  476. ret = X##_s ? -1 : 1;
  477. else if (X##_s != Y##_s)
  478.   ret = X##_s ? -1 : 1;
  479. else if (X##_e > Y##_e)
  480.   ret = X##_s ? -1 : 1;
  481. else if (X##_e < Y##_e)
  482.   ret = X##_s ? 1 : -1;
  483. else if (_FP_FRAC_GT_##wc(X, Y))
  484.   ret = X##_s ? -1 : 1;
  485. else if (_FP_FRAC_GT_##wc(Y, X))
  486.   ret = X##_s ? 1 : -1;
  487. else
  488.   ret = 0;
  489.       }
  490.   } while (0)
  491. /* Simplification for strict equality.  */
  492. #define _FP_CMP_EQ(fs, wc, ret, X, Y)   
  493.   do {   
  494.     /* NANs are unordered */   
  495.     if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X))   
  496. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y)))   
  497.       {   
  498. ret = 1;   
  499.       }   
  500.     else   
  501.       {   
  502. ret = !(X##_e == Y##_e   
  503. && _FP_FRAC_EQ_##wc(X, Y)   
  504. && (X##_s == Y##_s || !X##_e && _FP_FRAC_ZEROP_##wc(X))); 
  505.       }   
  506.   } while (0)
  507. /*
  508.  * Main square root routine.  The input value should be cooked.
  509.  */
  510. #define _FP_SQRT(fs, wc, R, X)
  511. do {
  512.     _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S);
  513.     _FP_W_TYPE q;
  514.     switch (X##_c)
  515.     {
  516.     case FP_CLS_NAN:
  517. _FP_FRAC_COPY_##wc(R, X);
  518. R##_s = X##_s;
  519.      R##_c = FP_CLS_NAN;
  520.      break;
  521.     case FP_CLS_INF:
  522.      if (X##_s)
  523.        {
  524.          R##_s = _FP_NANSIGN_##fs;
  525.     R##_c = FP_CLS_NAN; /* NAN */
  526.     _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);
  527.     FP_SET_EXCEPTION(FP_EX_INVALID);
  528.        }
  529.      else
  530.        {
  531.          R##_s = 0;
  532.          R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */
  533.        }
  534.      break;
  535.     case FP_CLS_ZERO:
  536. R##_s = X##_s;
  537. R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */
  538. break;
  539.     case FP_CLS_NORMAL:
  540.      R##_s = 0;
  541.         if (X##_s)
  542.           {
  543.     R##_c = FP_CLS_NAN; /* sNAN */
  544.     R##_s = _FP_NANSIGN_##fs;
  545.     _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);
  546.     FP_SET_EXCEPTION(FP_EX_INVALID);
  547.     break;
  548.           }
  549.      R##_c = FP_CLS_NORMAL;
  550.         if (X##_e & 1)
  551.           _FP_FRAC_SLL_##wc(X, 1);
  552.         R##_e = X##_e >> 1;
  553.         _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc);
  554.         _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc);
  555.         q = _FP_OVERFLOW_##fs >> 1;
  556.         _FP_SQRT_MEAT_##wc(R, S, T, X, q);
  557.     }
  558.   } while (0)
  559. /*
  560.  * Convert from FP to integer
  561.  */
  562. /* RSIGNED can have following values:
  563.  * 0:  the number is required to be 0..(2^rsize)-1, if not, NV is set plus
  564.  *     the result is either 0 or (2^rsize)-1 depending on the sign in such case.
  565.  * 1:  the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is
  566.  *     set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
  567.  *     on the sign in such case.
  568.  * 2:  the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is
  569.  *     set plus the result is truncated to fit into destination.
  570.  * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
  571.  *     set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
  572.  *     on the sign in such case.
  573.  */
  574. #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned)
  575.   do {
  576.     switch (X##_c)
  577.       {
  578.       case FP_CLS_NORMAL:
  579. if (X##_e < 0)
  580.   {
  581.     FP_SET_EXCEPTION(FP_EX_INEXACT);
  582.   case FP_CLS_ZERO:
  583.     r = 0;
  584.   }
  585. else if (X##_e >= rsize - (rsigned > 0 || X##_s)
  586.  || (!rsigned && X##_s))
  587.   { /* overflow */
  588.   case FP_CLS_NAN:                                                      
  589.   case FP_CLS_INF:
  590.     if (rsigned == 2)
  591.       {
  592. if (X##_c != FP_CLS_NORMAL
  593.     || X##_e >= rsize - 1 + _FP_WFRACBITS_##fs)
  594.   r = 0;
  595. else
  596.   {
  597.     _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1));
  598.     _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  599.   }
  600.       }
  601.     else if (rsigned)
  602.       {
  603. r = 1;
  604. r <<= rsize - 1;
  605. r -= 1 - X##_s;
  606.       }
  607.     else
  608.       {
  609. r = 0;
  610. if (X##_s)
  611.   r = ~r;
  612.       }
  613.     FP_SET_EXCEPTION(FP_EX_INVALID);
  614.   }
  615. else
  616.   {
  617.     if (_FP_W_TYPE_SIZE*wc < rsize)
  618.       {
  619. _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  620. r <<= X##_e - _FP_WFRACBITS_##fs;
  621.       }
  622.     else
  623.       {
  624. if (X##_e >= _FP_WFRACBITS_##fs)
  625.   _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1));
  626. else if (X##_e < _FP_WFRACBITS_##fs - 1)
  627.   {
  628.     _FP_FRAC_SRS_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 2),
  629.       _FP_WFRACBITS_##fs);
  630.     if (_FP_FRAC_LOW_##wc(X) & 1)
  631.       FP_SET_EXCEPTION(FP_EX_INEXACT);
  632.     _FP_FRAC_SRL_##wc(X, 1);
  633.   }
  634. _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  635.       }
  636.     if (rsigned && X##_s)
  637.       r = -r;
  638.   }
  639. break;
  640.       }
  641.   } while (0)
  642. #define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, rsigned)
  643.   do {
  644.     r = 0;
  645.     switch (X##_c)
  646.       {
  647.       case FP_CLS_NORMAL:
  648. if (X##_e >= _FP_FRACBITS_##fs - 1)
  649.   {
  650.     if (X##_e < rsize - 1 + _FP_WFRACBITS_##fs)
  651.       {
  652. if (X##_e >= _FP_WFRACBITS_##fs - 1)
  653.   {
  654.     _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  655.     r <<= X##_e - _FP_WFRACBITS_##fs + 1;
  656.   }
  657. else
  658.   {
  659.     _FP_FRAC_SRL_##wc(X, _FP_WORKBITS - X##_e
  660.       + _FP_FRACBITS_##fs - 1);
  661.     _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  662.   }
  663.       }
  664.   }
  665. else
  666.   {
  667.     if (X##_e <= -_FP_WORKBITS - 1)
  668.       _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc);
  669.     else
  670.       _FP_FRAC_SRS_##wc(X, _FP_FRACBITS_##fs - 1 - X##_e,
  671. _FP_WFRACBITS_##fs);
  672.     _FP_ROUND(wc, X);
  673.     _FP_FRAC_SRL_##wc(X, _FP_WORKBITS);
  674.     _FP_FRAC_ASSEMBLE_##wc(r, X, rsize);
  675.   }
  676. if (rsigned && X##_s)
  677.   r = -r;
  678. if (X##_e >= rsize - (rsigned > 0 || X##_s)
  679.     || (!rsigned && X##_s))
  680.   { /* overflow */
  681.   case FP_CLS_NAN:                                                      
  682.   case FP_CLS_INF:
  683.     if (!rsigned)
  684.       {
  685. r = 0;
  686. if (X##_s)
  687.   r = ~r;
  688.       }
  689.     else if (rsigned != 2)
  690.       {
  691. r = 1;
  692. r <<= rsize - 1;
  693. r -= 1 - X##_s;
  694.       }
  695.     FP_SET_EXCEPTION(FP_EX_INVALID);
  696.   }
  697. break;
  698.       case FP_CLS_ZERO:
  699.         break;
  700.       }
  701.   } while (0)
  702. #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype)
  703.   do {
  704.     if (r)
  705.       {
  706.         unsigned rtype ur_;
  707. X##_c = FP_CLS_NORMAL;
  708. if ((X##_s = (r < 0)))
  709.   r = -r;
  710. ur_ = (unsigned rtype) r;
  711. if (rsize <= _FP_W_TYPE_SIZE)
  712.   __FP_CLZ(X##_e, ur_);
  713. else
  714.   __FP_CLZ_2(X##_e, (_FP_W_TYPE)(ur_ >> _FP_W_TYPE_SIZE), 
  715.      (_FP_W_TYPE)ur_);
  716. if (rsize < _FP_W_TYPE_SIZE)
  717. X##_e -= (_FP_W_TYPE_SIZE - rsize);
  718. X##_e = rsize - X##_e - 1;
  719. if (_FP_FRACBITS_##fs < rsize && _FP_WFRACBITS_##fs < X##_e)
  720.   __FP_FRAC_SRS_1(ur_, (X##_e - _FP_WFRACBITS_##fs + 1), rsize);
  721. _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsize);
  722. if ((_FP_WFRACBITS_##fs - X##_e - 1) > 0)
  723.   _FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1));
  724.       }
  725.     else
  726.       {
  727. X##_c = FP_CLS_ZERO, X##_s = 0;
  728.       }
  729.   } while (0)
  730. #define FP_CONV(dfs,sfs,dwc,swc,D,S)
  731.   do {
  732.     _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S);
  733.     D##_e = S##_e;
  734.     D##_c = S##_c;
  735.     D##_s = S##_s;
  736.   } while (0)
  737. /*
  738.  * Helper primitives.
  739.  */
  740. /* Count leading zeros in a word.  */
  741. #ifndef __FP_CLZ
  742. #if _FP_W_TYPE_SIZE < 64
  743. /* this is just to shut the compiler up about shifts > word length -- PMM 02/1998 */
  744. #define __FP_CLZ(r, x)
  745.   do {
  746.     _FP_W_TYPE _t = (x);
  747.     r = _FP_W_TYPE_SIZE - 1;
  748.     if (_t > 0xffff) r -= 16;
  749.     if (_t > 0xffff) _t >>= 16;
  750.     if (_t > 0xff) r -= 8;
  751.     if (_t > 0xff) _t >>= 8;
  752.     if (_t & 0xf0) r -= 4;
  753.     if (_t & 0xf0) _t >>= 4;
  754.     if (_t & 0xc) r -= 2;
  755.     if (_t & 0xc) _t >>= 2;
  756.     if (_t & 0x2) r -= 1;
  757.   } while (0)
  758. #else /* not _FP_W_TYPE_SIZE < 64 */
  759. #define __FP_CLZ(r, x)
  760.   do {
  761.     _FP_W_TYPE _t = (x);
  762.     r = _FP_W_TYPE_SIZE - 1;
  763.     if (_t > 0xffffffff) r -= 32;
  764.     if (_t > 0xffffffff) _t >>= 32;
  765.     if (_t > 0xffff) r -= 16;
  766.     if (_t > 0xffff) _t >>= 16;
  767.     if (_t > 0xff) r -= 8;
  768.     if (_t > 0xff) _t >>= 8;
  769.     if (_t & 0xf0) r -= 4;
  770.     if (_t & 0xf0) _t >>= 4;
  771.     if (_t & 0xc) r -= 2;
  772.     if (_t & 0xc) _t >>= 2;
  773.     if (_t & 0x2) r -= 1;
  774.   } while (0)
  775. #endif /* not _FP_W_TYPE_SIZE < 64 */
  776. #endif /* ndef __FP_CLZ */
  777. #define _FP_DIV_HELP_imm(q, r, n, d)
  778.   do {
  779.     q = n / d, r = n % d;
  780.   } while (0)
  781. #endif /* __MATH_EMU_OP_COMMON_H__ */