[BACK]Return to exceptions.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr

Annotation of OpenXM_contrib/gmp/mpfr/exceptions.c, Revision 1.1.1.1

1.1       ohara       1: /* Exception flags and utilities.
                      2:
                      3: Copyright 2001 Free Software Foundation.
                      4:
                      5: This file is part of the MPFR Library.
                      6:
                      7: The MPFR Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The MPFR Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include "gmp.h"
                     23: #include "gmp-impl.h"
                     24: #include "mpfr.h"
                     25: #include "mpfr-impl.h"
                     26:
                     27: unsigned int __mpfr_flags = 0;
                     28:
                     29: mp_exp_t __mpfr_emin = MPFR_EMIN_DEFAULT;
                     30: mp_exp_t __mpfr_emax = MPFR_EMAX_DEFAULT;
                     31:
                     32: #undef mpfr_get_emin
                     33:
                     34: mp_exp_t
                     35: mpfr_get_emin (void)
                     36: {
                     37:   return __mpfr_emin;
                     38: }
                     39:
                     40: #undef mpfr_set_emin
                     41:
                     42: int
                     43: mpfr_set_emin (mp_exp_t exponent)
                     44: {
                     45:   if (exponent >= MPFR_EMIN_MIN && exponent <= MPFR_EMIN_MAX)
                     46:     {
                     47:       __mpfr_emin = exponent;
                     48:       return 0;
                     49:     }
                     50:   else
                     51:     {
                     52:       return 1;
                     53:     }
                     54: }
                     55:
                     56: #undef mpfr_get_emax
                     57:
                     58: mp_exp_t
                     59: mpfr_get_emax (void)
                     60: {
                     61:   return __mpfr_emax;
                     62: }
                     63:
                     64: #undef mpfr_set_emax
                     65:
                     66: int
                     67: mpfr_set_emax (mp_exp_t exponent)
                     68: {
                     69:   if (exponent >= MPFR_EMAX_MIN && exponent <= MPFR_EMAX_MAX)
                     70:     {
                     71:       __mpfr_emax = exponent;
                     72:       return 0;
                     73:     }
                     74:   else
                     75:     {
                     76:       return 1;
                     77:     }
                     78: }
                     79:
                     80: #undef mpfr_clear_flags
                     81:
                     82: void
                     83: mpfr_clear_flags (void)
                     84: {
                     85:   __mpfr_flags = 0;
                     86: }
                     87:
                     88: #undef mpfr_clear_underflow
                     89:
                     90: void
                     91: mpfr_clear_underflow (void)
                     92: {
                     93:   __mpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_UNDERFLOW;
                     94: }
                     95:
                     96: #undef mpfr_clear_overflow
                     97:
                     98: void
                     99: mpfr_clear_overflow (void)
                    100: {
                    101:   __mpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_OVERFLOW;
                    102: }
                    103:
                    104: #undef mpfr_clear_nanflag
                    105:
                    106: void
                    107: mpfr_clear_nanflag (void)
                    108: {
                    109:   __mpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_NAN;
                    110: }
                    111:
                    112: #undef mpfr_clear_inexflag
                    113:
                    114: void
                    115: mpfr_clear_inexflag (void)
                    116: {
                    117:   __mpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_INEXACT;
                    118: }
                    119:
                    120: #undef mpfr_check_range
                    121:
                    122: int
                    123: mpfr_check_range (mpfr_ptr x, mp_rnd_t rnd_mode)
                    124: {
                    125:   if (MPFR_IS_FP(x) && MPFR_NOTZERO(x))
                    126:     { /* x is a non-zero FP */
                    127:       mp_exp_t exp = MPFR_EXP(x);
                    128:       if (exp < __mpfr_emin)
                    129:         return mpfr_set_underflow(x, rnd_mode, MPFR_SIGN(x));
                    130:       if (exp > __mpfr_emax)
                    131:         return mpfr_set_overflow(x, rnd_mode, MPFR_SIGN(x));
                    132:     }
                    133:   return 0;
                    134: }
                    135:
                    136: #undef mpfr_underflow_p
                    137:
                    138: int
                    139: mpfr_underflow_p (void)
                    140: {
                    141:   return __mpfr_flags & MPFR_FLAGS_UNDERFLOW;
                    142: }
                    143:
                    144: #undef mpfr_overflow_p
                    145:
                    146: int
                    147: mpfr_overflow_p (void)
                    148: {
                    149:   return __mpfr_flags & MPFR_FLAGS_OVERFLOW;
                    150: }
                    151:
                    152: #undef mpfr_nanflag_p
                    153:
                    154: int
                    155: mpfr_nanflag_p (void)
                    156: {
                    157:   return __mpfr_flags & MPFR_FLAGS_NAN;
                    158: }
                    159:
                    160: #undef mpfr_inexflag_p
                    161:
                    162: int
                    163: mpfr_inexflag_p (void)
                    164: {
                    165:   return __mpfr_flags & MPFR_FLAGS_INEXACT;
                    166: }
                    167:
                    168: #undef mpfr_set_underflow
                    169:
                    170: int
                    171: mpfr_set_underflow (mpfr_ptr x, mp_rnd_t rnd_mode, int sign)
                    172: {
                    173:   int inex;
                    174:
                    175:   MPFR_CLEAR_FLAGS(x);
                    176:   if ((rnd_mode == GMP_RNDU && sign > 0)
                    177:    || (rnd_mode == GMP_RNDD && sign < 0))
                    178:     {
                    179:       mp_size_t xn;
                    180:       mp_limb_t *xp;
                    181:
                    182:       MPFR_EXP(x) = __mpfr_emin;
                    183:       xn = (MPFR_PREC(x)-1)/BITS_PER_MP_LIMB;
                    184:       xp = MPFR_MANT(x);
                    185:       xp[xn] = GMP_LIMB_HIGHBIT;
                    186:       MPN_ZERO(xp, xn);
                    187:       inex = 1;
                    188:     }
                    189:   else
                    190:     {
                    191:       MPFR_SET_ZERO(x);
                    192:       inex = -1;
                    193:     }
                    194:   if (MPFR_SIGN(x) != sign)
                    195:     MPFR_CHANGE_SIGN(x);
                    196:   __mpfr_flags |= MPFR_FLAGS_INEXACT | MPFR_FLAGS_UNDERFLOW;
                    197:   return sign > 0 ? inex : -inex;
                    198: }
                    199:
                    200: #undef mpfr_set_overflow
                    201:
                    202: int
                    203: mpfr_set_overflow (mpfr_ptr x, mp_rnd_t rnd_mode, int sign)
                    204: {
                    205:   int inex;
                    206:
                    207:   MPFR_CLEAR_FLAGS(x);
                    208:   if ((rnd_mode == GMP_RNDU && sign < 0)
                    209:    || (rnd_mode == GMP_RNDD && sign > 0))
                    210:     {
                    211:       mp_size_t xn, i;
                    212:       int sh;
                    213:       mp_limb_t *xp;
                    214:
                    215:       MPFR_EXP(x) = __mpfr_emax;
                    216:       xn = 1 + (MPFR_PREC(x) - 1) / BITS_PER_MP_LIMB;
                    217:       sh = xn * BITS_PER_MP_LIMB - MPFR_PREC(x);
                    218:       xp = MPFR_MANT(x);
                    219:       xp[0] = MP_LIMB_T_MAX << sh;
                    220:       for (i = 1; i < xn; i++)
                    221:         xp[i] = MP_LIMB_T_MAX;
                    222:       inex = -1;
                    223:     }
                    224:   else
                    225:     {
                    226:       MPFR_SET_INF(x);
                    227:       inex = 1;
                    228:     }
                    229:   if (MPFR_SIGN(x) != sign)
                    230:     MPFR_CHANGE_SIGN(x);
                    231:   __mpfr_flags |= MPFR_FLAGS_INEXACT | MPFR_FLAGS_OVERFLOW;
                    232:   return sign > 0 ? inex : -inex;
                    233: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>