Annotation of OpenXM_contrib/gmp/mpf/integer.c, Revision 1.1
1.1 ! maekawa 1: /* mpf_trunc, mpf_floor, mpf_ceil -- Assign a float from another float while
! 2: rounding it to an integer.
! 3:
! 4: Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
! 5:
! 6: This file is part of the GNU MP Library.
! 7:
! 8: The GNU MP Library is free software; you can redistribute it and/or modify
! 9: it under the terms of the GNU Lesser General Public License as published by
! 10: the Free Software Foundation; either version 2.1 of the License, or (at your
! 11: option) any later version.
! 12:
! 13: The GNU MP Library is distributed in the hope that it will be useful, but
! 14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 15: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 16: License for more details.
! 17:
! 18: You should have received a copy of the GNU Lesser General Public License
! 19: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
! 20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 21: MA 02111-1307, USA. */
! 22:
! 23: #include "gmp.h"
! 24: #include "gmp-impl.h"
! 25:
! 26:
! 27: #if defined (OPERATION_floor)
! 28: #define _MPF_FLOOR_OR_CEIL
! 29: #define FUNC_NAME mpf_floor
! 30: #define MPF_FLOOR 1
! 31: #define MPF_CEIL 0
! 32:
! 33: #elif defined (OPERATION_ceil)
! 34: #define _MPF_FLOOR_OR_CEIL
! 35: #define FUNC_NAME mpf_ceil
! 36: #define MPF_CEIL 1
! 37: #define MPF_FLOOR 0
! 38:
! 39: #elif defined (OPERATION_trunc)
! 40: #define FUNC_NAME mpf_trunc
! 41:
! 42: #else
! 43: Error, error, unrecognised OPERATION
! 44: #endif
! 45:
! 46:
! 47: #ifdef _MPF_FLOOR_OR_CEIL
! 48: static int
! 49: #if __STDC__
! 50: mpn_zero_p (mp_ptr p, mp_size_t n)
! 51: #else
! 52: mpn_zero_p (p, n)
! 53: mp_ptr p;
! 54: mp_size_t n;
! 55: #endif
! 56: {
! 57: mp_size_t i;
! 58:
! 59: for (i = 0; i < n; i++)
! 60: {
! 61: if (p[i] != 0)
! 62: return 0;
! 63: }
! 64:
! 65: return 1;
! 66: }
! 67: #endif
! 68:
! 69: void
! 70: #if __STDC__
! 71: FUNC_NAME (mpf_ptr r, mpf_srcptr u)
! 72: #else
! 73: FUNC_NAME (r, u)
! 74: mpf_ptr r;
! 75: mpf_srcptr u;
! 76: #endif
! 77: {
! 78: mp_ptr rp, up;
! 79: mp_size_t size, asize;
! 80: mp_size_t prec;
! 81: mp_size_t ignored_n;
! 82: mp_exp_t exp;
! 83:
! 84: size = u->_mp_size;
! 85: rp = r->_mp_d;
! 86: exp = u->_mp_exp;
! 87:
! 88: /* Single out the case where |u| < 1. */
! 89: if (exp <= 0)
! 90: {
! 91: #ifdef _MPF_FLOOR_OR_CEIL
! 92: if ((MPF_FLOOR && size < 0) || (MPF_CEIL && size >= 0))
! 93: {
! 94: rp[0] = 1;
! 95: r->_mp_size = MPF_FLOOR ? -1 : 1;
! 96: r->_mp_exp = 1;
! 97: return;
! 98: }
! 99: #endif
! 100: r->_mp_size = 0;
! 101: return;
! 102: }
! 103:
! 104: prec = r->_mp_prec /* + 1 */;
! 105: asize = ABS (size);
! 106:
! 107: #ifdef _MPF_FLOOR_OR_CEIL
! 108: ignored_n = 0;
! 109: #endif
! 110: up = u->_mp_d;
! 111:
! 112: if (asize > prec)
! 113: {
! 114: #ifdef _MPF_FLOOR_OR_CEIL
! 115: ignored_n = asize - prec;
! 116: #endif
! 117: up += asize - prec;
! 118: asize = prec;
! 119: }
! 120:
! 121: if (asize > exp)
! 122: {
! 123: long diff = asize - exp;
! 124: #ifdef _MPF_FLOOR_OR_CEIL
! 125: ignored_n += diff;
! 126: #endif
! 127: up += diff;
! 128: asize = exp;
! 129: }
! 130:
! 131: #ifdef _MPF_FLOOR_OR_CEIL
! 132: if (((MPF_FLOOR && size < 0) || (MPF_CEIL && size >= 0))
! 133: && ! mpn_zero_p (up - ignored_n, ignored_n))
! 134: {
! 135: mp_limb_t cy;
! 136: cy = mpn_add_1 (rp, up, asize, (mp_limb_t) 1);
! 137: if (cy != 0)
! 138: {
! 139: rp[asize++] = cy;
! 140: exp++;
! 141: }
! 142: }
! 143: else
! 144: #endif
! 145: MPN_COPY_INCR (rp, up, asize);
! 146:
! 147: r->_mp_exp = exp;
! 148: r->_mp_size = size >= 0 ? asize : -asize;
! 149: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>