/src/gmp-6.2.1/mpz/rootrem.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_rootrem(root, rem, u, nth) --  Set ROOT to trunc(U^(1/nth)) and  | 
2  |  |    set REM to the remainder.  | 
3  |  |  | 
4  |  | Copyright 1999-2003, 2005, 2012 Free Software 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 either:  | 
10  |  |  | 
11  |  |   * the GNU Lesser General Public License as published by the Free  | 
12  |  |     Software Foundation; either version 3 of the License, or (at your  | 
13  |  |     option) any later version.  | 
14  |  |  | 
15  |  | or  | 
16  |  |  | 
17  |  |   * the GNU General Public License as published by the Free Software  | 
18  |  |     Foundation; either version 2 of the License, or (at your option) any  | 
19  |  |     later version.  | 
20  |  |  | 
21  |  | or both in parallel, as here.  | 
22  |  |  | 
23  |  | The GNU MP Library is distributed in the hope that it will be useful, but  | 
24  |  | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  | 
25  |  | or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  | 
26  |  | for more details.  | 
27  |  |  | 
28  |  | You should have received copies of the GNU General Public License and the  | 
29  |  | GNU Lesser General Public License along with the GNU MP Library.  If not,  | 
30  |  | see https://www.gnu.org/licenses/.  */  | 
31  |  |  | 
32  |  | #include <stdio.h>    /* for NULL */  | 
33  |  | #include "gmp-impl.h"  | 
34  |  |  | 
35  |  | void  | 
36  |  | mpz_rootrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr u, unsigned long int nth)  | 
37  | 0  | { | 
38  | 0  |   mp_ptr rootp, up, remp;  | 
39  | 0  |   mp_size_t us, un, rootn, remn;  | 
40  | 0  |   TMP_DECL;  | 
41  |  | 
  | 
42  | 0  |   us = SIZ(u);  | 
43  |  |  | 
44  |  |   /* even roots of negatives provoke an exception */  | 
45  | 0  |   if (UNLIKELY (us < 0 && (nth & 1) == 0))  | 
46  | 0  |     SQRT_OF_NEGATIVE;  | 
47  |  |  | 
48  |  |   /* root extraction interpreted as c^(1/nth) means a zeroth root should  | 
49  |  |      provoke a divide by zero, do this even if c==0 */  | 
50  | 0  |   if (UNLIKELY (nth == 0))  | 
51  | 0  |     DIVIDE_BY_ZERO;  | 
52  |  |  | 
53  | 0  |   if (us == 0)  | 
54  | 0  |     { | 
55  | 0  |       if (root != NULL)  | 
56  | 0  |   SIZ(root) = 0;  | 
57  | 0  |       SIZ(rem) = 0;  | 
58  | 0  |       return;  | 
59  | 0  |     }  | 
60  |  |  | 
61  | 0  |   un = ABS (us);  | 
62  | 0  |   rootn = (un - 1) / nth + 1;  | 
63  |  | 
  | 
64  | 0  |   TMP_MARK;  | 
65  |  |  | 
66  |  |   /* FIXME: Perhaps disallow root == NULL */  | 
67  | 0  |   if (root != NULL && u != root)  | 
68  | 0  |     rootp = MPZ_NEWALLOC (root, rootn);  | 
69  | 0  |   else  | 
70  | 0  |     rootp = TMP_ALLOC_LIMBS (rootn);  | 
71  |  | 
  | 
72  | 0  |   if (u != rem)  | 
73  | 0  |     remp = MPZ_NEWALLOC (rem, un);  | 
74  | 0  |   else  | 
75  | 0  |     remp = TMP_ALLOC_LIMBS (un);  | 
76  |  | 
  | 
77  | 0  |   up = PTR(u);  | 
78  |  | 
  | 
79  | 0  |   if (nth == 1)  | 
80  | 0  |     { | 
81  | 0  |       MPN_COPY (rootp, up, un);  | 
82  | 0  |       remn = 0;  | 
83  | 0  |     }  | 
84  | 0  |   else  | 
85  | 0  |     { | 
86  | 0  |       remn = mpn_rootrem (rootp, remp, up, un, (mp_limb_t) nth);  | 
87  | 0  |     }  | 
88  |  |  | 
89  | 0  |   if (root != NULL)  | 
90  | 0  |     { | 
91  | 0  |       SIZ(root) = us >= 0 ? rootn : -rootn;  | 
92  | 0  |       if (u == root)  | 
93  | 0  |   MPN_COPY (up, rootp, rootn);  | 
94  | 0  |     }  | 
95  |  |  | 
96  | 0  |   if (u == rem)  | 
97  | 0  |     MPN_COPY (up, remp, remn);  | 
98  | 0  |   SIZ(rem) = us >= 0 ? remn : -remn;  | 
99  | 0  |   TMP_FREE;  | 
100  | 0  | }  |