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

Annotation of OpenXM_contrib/gmp/mpz/sqrtrem.c, Revision 1.1.1.2

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.2 ! maekawa     4: Copyright (C) 1991, 1993, 1994, 1996, 2000 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: #ifndef BERKELEY_MP
                     31: void
                     32: #if __STDC__
                     33: mpz_sqrtrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr op)
                     34: #else
                     35: mpz_sqrtrem (root, rem, op)
                     36:      mpz_ptr root;
                     37:      mpz_ptr rem;
                     38:      mpz_srcptr op;
                     39: #endif
                     40: #else /* BERKELEY_MP */
                     41: void
                     42: #if __STDC__
                     43: msqrt (mpz_srcptr op, mpz_ptr root, mpz_ptr rem)
                     44: #else
                     45: msqrt (op, root, rem)
                     46:      mpz_srcptr op;
                     47:      mpz_ptr root;
                     48:      mpz_ptr rem;
                     49: #endif
                     50: #endif /* BERKELEY_MP */
                     51: {
                     52:   mp_size_t op_size, root_size, rem_size;
                     53:   mp_ptr root_ptr, op_ptr;
                     54:   mp_ptr free_me = NULL;
                     55:   mp_size_t free_me_size;
                     56:   TMP_DECL (marker);
                     57:
                     58:   TMP_MARK (marker);
                     59:   op_size = op->_mp_size;
                     60:   if (op_size < 0)
1.1.1.2 ! maekawa    61:     SQRT_OF_NEGATIVE;
1.1       maekawa    62:
                     63:   if (rem->_mp_alloc < op_size)
                     64:     _mpz_realloc (rem, op_size);
                     65:
                     66:   /* The size of the root is accurate after this simple calculation.  */
                     67:   root_size = (op_size + 1) / 2;
                     68:
                     69:   root_ptr = root->_mp_d;
                     70:   op_ptr = op->_mp_d;
                     71:
                     72:   if (root->_mp_alloc < root_size)
                     73:     {
                     74:       if (root_ptr == op_ptr)
                     75:        {
                     76:          free_me = root_ptr;
                     77:          free_me_size = root->_mp_alloc;
                     78:        }
                     79:       else
                     80:        (*_mp_free_func) (root_ptr, root->_mp_alloc * BYTES_PER_MP_LIMB);
                     81:
                     82:       root->_mp_alloc = root_size;
                     83:       root_ptr = (mp_ptr) (*_mp_allocate_func) (root_size * BYTES_PER_MP_LIMB);
                     84:       root->_mp_d = root_ptr;
                     85:     }
                     86:   else
                     87:     {
                     88:       /* Make OP not overlap with ROOT.  */
                     89:       if (root_ptr == op_ptr)
                     90:        {
                     91:          /* ROOT and OP are identical.  Allocate temporary space for OP.  */
                     92:          op_ptr = (mp_ptr) TMP_ALLOC (op_size * BYTES_PER_MP_LIMB);
                     93:          /* Copy to the temporary space.  Hack: Avoid temporary variable
                     94:             by using ROOT_PTR.  */
                     95:          MPN_COPY (op_ptr, root_ptr, op_size);
                     96:        }
                     97:     }
                     98:
                     99:   rem_size = mpn_sqrtrem (root_ptr, rem->_mp_d, op_ptr, op_size);
                    100:
                    101:   root->_mp_size = root_size;
                    102:
                    103:   /* Write remainder size last, to enable us to define this function to
                    104:      give only the square root remainder, if the user calls if with
                    105:      ROOT == REM.  */
                    106:   rem->_mp_size = rem_size;
                    107:
                    108:   if (free_me != NULL)
                    109:     (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
                    110:   TMP_FREE (marker);
                    111: }

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