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

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

1.1       maekawa     1: /* mpz_sqrt(root, u) --  Set ROOT to floor(sqrt(U)).
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1993, 1994, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa     4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2 ! maekawa     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
1.1       maekawa    10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! maekawa    14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! maekawa    17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    18: along with the GNU MP 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:
1.1.1.2 ! maekawa    22: #include <stdio.h> /* for NULL */
1.1       maekawa    23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25:
                     26: void
                     27: #if __STDC__
                     28: mpz_sqrt (mpz_ptr root, mpz_srcptr op)
                     29: #else
                     30: mpz_sqrt (root, op)
                     31:      mpz_ptr root;
                     32:      mpz_srcptr op;
                     33: #endif
                     34: {
                     35:   mp_size_t op_size, root_size;
                     36:   mp_ptr root_ptr, op_ptr;
                     37:   mp_ptr free_me = NULL;
                     38:   mp_size_t free_me_size;
                     39:   TMP_DECL (marker);
                     40:
                     41:   TMP_MARK (marker);
                     42:   op_size = op->_mp_size;
                     43:   if (op_size < 0)
1.1.1.2 ! maekawa    44:     SQRT_OF_NEGATIVE;
1.1       maekawa    45:
                     46:   /* The size of the root is accurate after this simple calculation.  */
                     47:   root_size = (op_size + 1) / 2;
                     48:
                     49:   root_ptr = root->_mp_d;
                     50:   op_ptr = op->_mp_d;
                     51:
                     52:   if (root->_mp_alloc < root_size)
                     53:     {
                     54:       if (root_ptr == op_ptr)
                     55:        {
                     56:          free_me = root_ptr;
                     57:          free_me_size = root->_mp_alloc;
                     58:        }
                     59:       else
                     60:        (*_mp_free_func) (root_ptr, root->_mp_alloc * BYTES_PER_MP_LIMB);
                     61:
                     62:       root->_mp_alloc = root_size;
                     63:       root_ptr = (mp_ptr) (*_mp_allocate_func) (root_size * BYTES_PER_MP_LIMB);
                     64:       root->_mp_d = root_ptr;
                     65:     }
                     66:   else
                     67:     {
                     68:       /* Make OP not overlap with ROOT.  */
                     69:       if (root_ptr == op_ptr)
                     70:        {
                     71:          /* ROOT and OP are identical.  Allocate temporary space for OP.  */
                     72:          op_ptr = (mp_ptr) TMP_ALLOC (op_size * BYTES_PER_MP_LIMB);
                     73:          /* Copy to the temporary space.  Hack: Avoid temporary variable
                     74:             by using ROOT_PTR.  */
                     75:          MPN_COPY (op_ptr, root_ptr, op_size);
                     76:        }
                     77:     }
                     78:
                     79:   mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size);
                     80:
                     81:   root->_mp_size = root_size;
                     82:
                     83:   if (free_me != NULL)
                     84:     (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
                     85:   TMP_FREE (marker);
                     86: }

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