1 /* mpz_sqrt(root, u) -- Set ROOT to floor(sqrt(U)). 2 3 Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2005, 2012 Free Software 4 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 3 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. If not, see http://www.gnu.org/licenses/. */ 20 21 #include <stdio.h> /* for NULL */ 22 #include "gmp.h" 23 #include "gmp-impl.h" 24 25 void 26 mpz_sqrt (mpz_ptr root, mpz_srcptr op) 27 { 28 mp_size_t op_size, root_size; 29 mp_ptr root_ptr, op_ptr; 30 31 op_size = SIZ (op); 32 if (UNLIKELY (op_size <= 0)) 33 { 34 if (op_size < 0) 35 SQRT_OF_NEGATIVE; 36 SIZ(root) = 0; 37 return; 38 } 39 40 /* The size of the root is accurate after this simple calculation. */ 41 root_size = (op_size + 1) / 2; 42 SIZ (root) = root_size; 43 44 op_ptr = PTR (op); 45 46 if (root == op) 47 { 48 /* Allocate temp space for the root, which we then copy to the 49 shared OP/ROOT variable. */ 50 TMP_DECL; 51 TMP_MARK; 52 53 root_ptr = TMP_ALLOC_LIMBS (root_size); 54 mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size); 55 56 MPN_COPY (op_ptr, root_ptr, root_size); 57 58 TMP_FREE; 59 } 60 else 61 { 62 root_ptr = MPZ_REALLOC (root, root_size); 63 64 mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size); 65 } 66 } 67