Back to index

glibc  2.9
op-1.h
Go to the documentation of this file.
00001 /* Software floating-point emulation.
00002    Basic one-word fraction declaration and manipulation.
00003    Copyright (C) 1997,1998,1999,2006 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Richard Henderson (rth@cygnus.com),
00006                 Jakub Jelinek (jj@ultra.linux.cz),
00007                 David S. Miller (davem@redhat.com) and
00008                 Peter Maydell (pmaydell@chiark.greenend.org.uk).
00009 
00010    The GNU C Library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Lesser General Public
00012    License as published by the Free Software Foundation; either
00013    version 2.1 of the License, or (at your option) any later version.
00014 
00015    In addition to the permissions in the GNU Lesser General Public
00016    License, the Free Software Foundation gives you unlimited
00017    permission to link the compiled version of this file into
00018    combinations with other programs, and to distribute those
00019    combinations without any restriction coming from the use of this
00020    file.  (The Lesser General Public License restrictions do apply in
00021    other respects; for example, they cover modification of the file,
00022    and distribution when not linked into a combine executable.)
00023 
00024    The GNU C Library is distributed in the hope that it will be useful,
00025    but WITHOUT ANY WARRANTY; without even the implied warranty of
00026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027    Lesser General Public License for more details.
00028 
00029    You should have received a copy of the GNU Lesser General Public
00030    License along with the GNU C Library; if not, write to the Free
00031    Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
00032    MA 02110-1301, USA.  */
00033 
00034 #define _FP_FRAC_DECL_1(X)  _FP_W_TYPE X##_f
00035 #define _FP_FRAC_COPY_1(D,S)       (D##_f = S##_f)
00036 #define _FP_FRAC_SET_1(X,I) (X##_f = I)
00037 #define _FP_FRAC_HIGH_1(X)  (X##_f)
00038 #define _FP_FRAC_LOW_1(X)   (X##_f)
00039 #define _FP_FRAC_WORD_1(X,w)       (X##_f)
00040 
00041 #define _FP_FRAC_ADDI_1(X,I)       (X##_f += I)
00042 #define _FP_FRAC_SLL_1(X,N)               \
00043   do {                                    \
00044     if (__builtin_constant_p(N) && (N) == 1)     \
00045       X##_f += X##_f;                            \
00046     else                                  \
00047       X##_f <<= (N);                      \
00048   } while (0)
00049 #define _FP_FRAC_SRL_1(X,N) (X##_f >>= N)
00050 
00051 /* Right shift with sticky-lsb.  */
00052 #define _FP_FRAC_SRST_1(X,S,N,sz)  __FP_FRAC_SRST_1(X##_f, S, N, sz)
00053 #define _FP_FRAC_SRS_1(X,N,sz)     __FP_FRAC_SRS_1(X##_f, N, sz)
00054 
00055 #define __FP_FRAC_SRST_1(X,S,N,sz)               \
00056 do {                                             \
00057   S = (__builtin_constant_p(N) && (N) == 1              \
00058        ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0);  \
00059   X = X >> (N);                                         \
00060 } while (0)
00061 
00062 #define __FP_FRAC_SRS_1(X,N,sz)                                       \
00063    (X = (X >> (N) | (__builtin_constant_p(N) && (N) == 1              \
00064                    ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
00065 
00066 #define _FP_FRAC_ADD_1(R,X,Y)      (R##_f = X##_f + Y##_f)
00067 #define _FP_FRAC_SUB_1(R,X,Y)      (R##_f = X##_f - Y##_f)
00068 #define _FP_FRAC_DEC_1(X,Y) (X##_f -= Y##_f)
00069 #define _FP_FRAC_CLZ_1(z, X)       __FP_CLZ(z, X##_f)
00070 
00071 /* Predicates */
00072 #define _FP_FRAC_NEGP_1(X)  ((_FP_WS_TYPE)X##_f < 0)
00073 #define _FP_FRAC_ZEROP_1(X) (X##_f == 0)
00074 #define _FP_FRAC_OVERP_1(fs,X)     (X##_f & _FP_OVERFLOW_##fs)
00075 #define _FP_FRAC_CLEAR_OVERP_1(fs,X)      (X##_f &= ~_FP_OVERFLOW_##fs)
00076 #define _FP_FRAC_EQ_1(X, Y) (X##_f == Y##_f)
00077 #define _FP_FRAC_GE_1(X, Y) (X##_f >= Y##_f)
00078 #define _FP_FRAC_GT_1(X, Y) (X##_f > Y##_f)
00079 
00080 #define _FP_ZEROFRAC_1             0
00081 #define _FP_MINFRAC_1              1
00082 #define _FP_MAXFRAC_1              (~(_FP_WS_TYPE)0)
00083 
00084 /*
00085  * Unpack the raw bits of a native fp value.  Do not classify or
00086  * normalize the data.
00087  */
00088 
00089 #define _FP_UNPACK_RAW_1(fs, X, val)                           \
00090   do {                                                  \
00091     union _FP_UNION_##fs _flo; _flo.flt = (val);        \
00092                                                         \
00093     X##_f = _flo.bits.frac;                             \
00094     X##_e = _flo.bits.exp;                              \
00095     X##_s = _flo.bits.sign;                             \
00096   } while (0)
00097 
00098 #define _FP_UNPACK_RAW_1_P(fs, X, val)                         \
00099   do {                                                  \
00100     union _FP_UNION_##fs *_flo =                        \
00101       (union _FP_UNION_##fs *)(val);                           \
00102                                                         \
00103     X##_f = _flo->bits.frac;                                   \
00104     X##_e = _flo->bits.exp;                             \
00105     X##_s = _flo->bits.sign;                                   \
00106   } while (0)
00107 
00108 /*
00109  * Repack the raw bits of a native fp value.
00110  */
00111 
00112 #define _FP_PACK_RAW_1(fs, val, X)                      \
00113   do {                                                  \
00114     union _FP_UNION_##fs _flo;                                 \
00115                                                         \
00116     _flo.bits.frac = X##_f;                             \
00117     _flo.bits.exp  = X##_e;                             \
00118     _flo.bits.sign = X##_s;                             \
00119                                                         \
00120     (val) = _flo.flt;                                          \
00121   } while (0)
00122 
00123 #define _FP_PACK_RAW_1_P(fs, val, X)                           \
00124   do {                                                  \
00125     union _FP_UNION_##fs *_flo =                        \
00126       (union _FP_UNION_##fs *)(val);                           \
00127                                                         \
00128     _flo->bits.frac = X##_f;                                   \
00129     _flo->bits.exp  = X##_e;                                   \
00130     _flo->bits.sign = X##_s;                                   \
00131   } while (0)
00132 
00133 
00134 /*
00135  * Multiplication algorithms:
00136  */
00137 
00138 /* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
00139    multiplication immediately.  */
00140 
00141 #define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y)                        \
00142   do {                                                         \
00143     R##_f = X##_f * Y##_f;                                     \
00144     /* Normalize since we know where the msb of the multiplicands     \
00145        were (bit B), we know that the msb of the of the product is    \
00146        at either 2B or 2B-1.  */                               \
00147     _FP_FRAC_SRS_1(R, wfracbits-1, 2*wfracbits);               \
00148   } while (0)
00149 
00150 /* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
00151 
00152 #define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit)                 \
00153   do {                                                         \
00154     _FP_W_TYPE _Z_f0, _Z_f1;                                          \
00155     doit(_Z_f1, _Z_f0, X##_f, Y##_f);                                 \
00156     /* Normalize since we know where the msb of the multiplicands     \
00157        were (bit B), we know that the msb of the of the product is    \
00158        at either 2B or 2B-1.  */                               \
00159     _FP_FRAC_SRS_2(_Z, wfracbits-1, 2*wfracbits);                     \
00160     R##_f = _Z_f0;                                             \
00161   } while (0)
00162 
00163 /* Finally, a simple widening multiply algorithm.  What fun!  */
00164 
00165 #define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y)                       \
00166   do {                                                         \
00167     _FP_W_TYPE _xh, _xl, _yh, _yl, _z_f0, _z_f1, _a_f0, _a_f1;        \
00168                                                                \
00169     /* split the words in half */                              \
00170     _xh = X##_f >> (_FP_W_TYPE_SIZE/2);                               \
00171     _xl = X##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);              \
00172     _yh = Y##_f >> (_FP_W_TYPE_SIZE/2);                               \
00173     _yl = Y##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);              \
00174                                                                \
00175     /* multiply the pieces */                                         \
00176     _z_f0 = _xl * _yl;                                                \
00177     _a_f0 = _xh * _yl;                                                \
00178     _a_f1 = _xl * _yh;                                                \
00179     _z_f1 = _xh * _yh;                                                \
00180                                                                \
00181     /* reassemble into two full words */                       \
00182     if ((_a_f0 += _a_f1) < _a_f1)                              \
00183       _z_f1 += (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2);                  \
00184     _a_f1 = _a_f0 >> (_FP_W_TYPE_SIZE/2);                      \
00185     _a_f0 = _a_f0 << (_FP_W_TYPE_SIZE/2);                      \
00186     _FP_FRAC_ADD_2(_z, _z, _a);                                       \
00187                                                                \
00188     /* normalize */                                            \
00189     _FP_FRAC_SRS_2(_z, wfracbits - 1, 2*wfracbits);                   \
00190     R##_f = _z_f0;                                             \
00191   } while (0)
00192 
00193 
00194 /*
00195  * Division algorithms:
00196  */
00197 
00198 /* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
00199    division immediately.  Give this macro either _FP_DIV_HELP_imm for
00200    C primitives or _FP_DIV_HELP_ldiv for the ISO function.  Which you
00201    choose will depend on what the compiler does with divrem4.  */
00202 
00203 #define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit)           \
00204   do {                                           \
00205     _FP_W_TYPE _q, _r;                                  \
00206     X##_f <<= (X##_f < Y##_f                            \
00207               ? R##_e--, _FP_WFRACBITS_##fs             \
00208               : _FP_WFRACBITS_##fs - 1);         \
00209     doit(_q, _r, X##_f, Y##_f);                         \
00210     R##_f = _q | (_r != 0);                      \
00211   } while (0)
00212 
00213 /* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
00214    that may be useful in this situation.  This first is for a primitive
00215    that requires normalization, the second for one that does not.  Look
00216    for UDIV_NEEDS_NORMALIZATION to tell which your machine needs.  */
00217 
00218 #define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y)                         \
00219   do {                                                         \
00220     _FP_W_TYPE _nh, _nl, _q, _r, _y;                                  \
00221                                                                \
00222     /* Normalize Y -- i.e. make the most significant bit set.  */     \
00223     _y = Y##_f << _FP_WFRACXBITS_##fs;                                \
00224                                                                \
00225     /* Shift X op correspondingly high, that is, up one full word.  */       \
00226     if (X##_f < Y##_f)                                                \
00227       {                                                               \
00228        R##_e--;                                                \
00229        _nl = 0;                                                \
00230        _nh = X##_f;                                            \
00231       }                                                               \
00232     else                                                       \
00233       {                                                               \
00234        _nl = X##_f << (_FP_W_TYPE_SIZE - 1);                          \
00235        _nh = X##_f >> 1;                                       \
00236       }                                                               \
00237                                                                \
00238     udiv_qrnnd(_q, _r, _nh, _nl, _y);                                 \
00239     R##_f = _q | (_r != 0);                                    \
00240   } while (0)
00241 
00242 #define _FP_DIV_MEAT_1_udiv(fs, R, X, Y)         \
00243   do {                                           \
00244     _FP_W_TYPE _nh, _nl, _q, _r;                 \
00245     if (X##_f < Y##_f)                                  \
00246       {                                                 \
00247        R##_e--;                                  \
00248        _nl = X##_f << _FP_WFRACBITS_##fs;        \
00249        _nh = X##_f >> _FP_WFRACXBITS_##fs;              \
00250       }                                                 \
00251     else                                         \
00252       {                                                 \
00253        _nl = X##_f << (_FP_WFRACBITS_##fs - 1);  \
00254        _nh = X##_f >> (_FP_WFRACXBITS_##fs + 1); \
00255       }                                                 \
00256     udiv_qrnnd(_q, _r, _nh, _nl, Y##_f);         \
00257     R##_f = _q | (_r != 0);                      \
00258   } while (0)
00259   
00260   
00261 /*
00262  * Square root algorithms:
00263  * We have just one right now, maybe Newton approximation
00264  * should be added for those machines where division is fast.
00265  */
00266  
00267 #define _FP_SQRT_MEAT_1(R, S, T, X, q)                  \
00268   do {                                           \
00269     while (q != _FP_WORK_ROUND)                         \
00270       {                                                 \
00271         T##_f = S##_f + q;                       \
00272         if (T##_f <= X##_f)                      \
00273           {                                      \
00274             S##_f = T##_f + q;                          \
00275             X##_f -= T##_f;                      \
00276             R##_f += q;                                 \
00277           }                                      \
00278         _FP_FRAC_SLL_1(X, 1);                           \
00279         q >>= 1;                                 \
00280       }                                                 \
00281     if (X##_f)                                          \
00282       {                                                 \
00283        if (S##_f < X##_f)                        \
00284          R##_f |= _FP_WORK_ROUND;                \
00285        R##_f |= _FP_WORK_STICKY;                 \
00286       }                                                 \
00287   } while (0)
00288 
00289 /*
00290  * Assembly/disassembly for converting to/from integral types.  
00291  * No shifting or overflow handled here.
00292  */
00293 
00294 #define _FP_FRAC_ASSEMBLE_1(r, X, rsize)  (r = X##_f)
00295 #define _FP_FRAC_DISASSEMBLE_1(X, r, rsize)      (X##_f = r)
00296 
00297 
00298 /*
00299  * Convert FP values between word sizes
00300  */
00301 
00302 #define _FP_FRAC_COPY_1_1(D, S)           (D##_f = S##_f)