/src/libgmp/mpz/sqrtrem.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_sqrtrem(root,rem,x) -- Set ROOT to floor(sqrt(X)) and REM |
2 | | to the remainder, i.e. X - ROOT**2. |
3 | | |
4 | | Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2005, 2011, 2012, 2015 Free |
5 | | Software Foundation, Inc. |
6 | | |
7 | | This file is part of the GNU MP Library. |
8 | | |
9 | | The GNU MP Library is free software; you can redistribute it and/or modify |
10 | | it under the terms of either: |
11 | | |
12 | | * the GNU Lesser General Public License as published by the Free |
13 | | Software Foundation; either version 3 of the License, or (at your |
14 | | option) any later version. |
15 | | |
16 | | or |
17 | | |
18 | | * the GNU General Public License as published by the Free Software |
19 | | Foundation; either version 2 of the License, or (at your option) any |
20 | | later version. |
21 | | |
22 | | or both in parallel, as here. |
23 | | |
24 | | The GNU MP Library is distributed in the hope that it will be useful, but |
25 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
26 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
27 | | for more details. |
28 | | |
29 | | You should have received copies of the GNU General Public License and the |
30 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
31 | | see https://www.gnu.org/licenses/. */ |
32 | | |
33 | | #include "gmp-impl.h" |
34 | | |
35 | | void |
36 | | mpz_sqrtrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr op) |
37 | 82 | { |
38 | 82 | mp_size_t op_size, root_size, rem_size; |
39 | 82 | mp_ptr root_ptr, op_ptr, rem_ptr; |
40 | | |
41 | 82 | op_size = SIZ (op); |
42 | 82 | if (UNLIKELY (op_size <= 0)) |
43 | 3 | { |
44 | 3 | if (UNLIKELY (op_size < 0)) |
45 | 0 | SQRT_OF_NEGATIVE; |
46 | 3 | SIZ(root) = 0; |
47 | 3 | SIZ(rem) = 0; |
48 | 3 | return; |
49 | 3 | } |
50 | | |
51 | | /* No-op if rem == op */ |
52 | 79 | rem_ptr = MPZ_NEWALLOC (rem, op_size); |
53 | | |
54 | | /* The size of the root is accurate after this simple calculation. */ |
55 | 79 | root_size = (op_size + 1) / 2; |
56 | 79 | SIZ (root) = root_size; |
57 | | |
58 | 79 | op_ptr = PTR (op); |
59 | | |
60 | 79 | if (root == op) |
61 | 1 | { |
62 | | /* Allocate temp space for the root, which we then copy to the |
63 | | shared OP/ROOT variable. */ |
64 | 1 | TMP_DECL; |
65 | 1 | TMP_MARK; |
66 | | |
67 | 1 | root_ptr = TMP_ALLOC_LIMBS (root_size); |
68 | 1 | rem_size = mpn_sqrtrem (root_ptr, rem_ptr, op_ptr, op_size); |
69 | | |
70 | 1 | if (rem != root) /* Don't overwrite remainder */ |
71 | 1 | MPN_COPY (op_ptr, root_ptr, root_size); |
72 | | |
73 | 1 | TMP_FREE; |
74 | 1 | } |
75 | 78 | else |
76 | 78 | { |
77 | 78 | root_ptr = MPZ_NEWALLOC (root, root_size); |
78 | | |
79 | 78 | rem_size = mpn_sqrtrem (root_ptr, rem_ptr, op_ptr, op_size); |
80 | 78 | } |
81 | | |
82 | | /* Write remainder size last, to make this function give only the square root |
83 | | remainder, when passed ROOT == REM. */ |
84 | 79 | SIZ (rem) = rem_size; |
85 | 79 | } |