Annotation of OpenXM_contrib/gmp/mpz/sqrtrem.c, Revision 1.1.1.3
1.1 maekawa 1: /* mpz_sqrtrem(root,rem,x) -- Set ROOT to floor(sqrt(X)) and REM
2: to the remainder, i.e. X - ROOT**2.
3:
1.1.1.3 ! ohara 4: Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
1.1 maekawa 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
1.1.1.2 maekawa 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
1.1 maekawa 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
1.1.1.2 maekawa 15: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 16: License for more details.
17:
1.1.1.2 maekawa 18: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 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:
1.1.1.2 maekawa 23: #include <stdio.h> /* for NULL */
1.1 maekawa 24: #include "gmp.h"
25: #include "gmp-impl.h"
1.1.1.2 maekawa 26: #ifdef BERKELEY_MP
27: #include "mp.h"
28: #endif
1.1 maekawa 29:
30: void
1.1.1.3 ! ohara 31: #ifndef BERKELEY_MP
1.1 maekawa 32: mpz_sqrtrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr op)
33: #else /* BERKELEY_MP */
34: msqrt (mpz_srcptr op, mpz_ptr root, mpz_ptr rem)
35: #endif /* BERKELEY_MP */
36: {
37: mp_size_t op_size, root_size, rem_size;
38: mp_ptr root_ptr, op_ptr;
39: mp_ptr free_me = NULL;
40: mp_size_t free_me_size;
41: TMP_DECL (marker);
42:
43: TMP_MARK (marker);
44: op_size = op->_mp_size;
1.1.1.3 ! ohara 45: if (op_size <= 0)
! 46: {
! 47: if (op_size < 0)
! 48: SQRT_OF_NEGATIVE;
! 49: SIZ(root) = 0;
! 50: SIZ(rem) = 0;
! 51: return;
! 52: }
1.1 maekawa 53:
54: if (rem->_mp_alloc < op_size)
55: _mpz_realloc (rem, op_size);
56:
57: /* The size of the root is accurate after this simple calculation. */
58: root_size = (op_size + 1) / 2;
59:
60: root_ptr = root->_mp_d;
61: op_ptr = op->_mp_d;
62:
63: if (root->_mp_alloc < root_size)
64: {
65: if (root_ptr == op_ptr)
66: {
67: free_me = root_ptr;
68: free_me_size = root->_mp_alloc;
69: }
70: else
1.1.1.3 ! ohara 71: (*__gmp_free_func) (root_ptr, root->_mp_alloc * BYTES_PER_MP_LIMB);
1.1 maekawa 72:
73: root->_mp_alloc = root_size;
1.1.1.3 ! ohara 74: root_ptr = (mp_ptr) (*__gmp_allocate_func) (root_size * BYTES_PER_MP_LIMB);
1.1 maekawa 75: root->_mp_d = root_ptr;
76: }
77: else
78: {
79: /* Make OP not overlap with ROOT. */
80: if (root_ptr == op_ptr)
81: {
82: /* ROOT and OP are identical. Allocate temporary space for OP. */
83: op_ptr = (mp_ptr) TMP_ALLOC (op_size * BYTES_PER_MP_LIMB);
84: /* Copy to the temporary space. Hack: Avoid temporary variable
85: by using ROOT_PTR. */
86: MPN_COPY (op_ptr, root_ptr, op_size);
87: }
88: }
89:
90: rem_size = mpn_sqrtrem (root_ptr, rem->_mp_d, op_ptr, op_size);
91:
92: root->_mp_size = root_size;
93:
94: /* Write remainder size last, to enable us to define this function to
95: give only the square root remainder, if the user calls if with
96: ROOT == REM. */
97: rem->_mp_size = rem_size;
98:
99: if (free_me != NULL)
1.1.1.3 ! ohara 100: (*__gmp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
1.1 maekawa 101: TMP_FREE (marker);
102: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>