1 /*
2     Copyright (C) 2010 Sebastian Pancratz
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <gmp.h>
15 #include "flint.h"
16 #include "ulong_extras.h"
17 #include "fmpz.h"
18 
fmpz_sqrtrem(fmpz_t f,fmpz_t r,const fmpz_t g)19 void fmpz_sqrtrem(fmpz_t f, fmpz_t r, const fmpz_t g)
20 {
21     if (fmpz_sgn(g) < 0)
22     {
23         flint_printf("Exception (fmpz_sqrtrem). g is negative.\n");
24         flint_abort();
25     }
26 
27     if (!COEFF_IS_MPZ(*g))
28     {
29         if (COEFF_IS_MPZ(*r))
30             _fmpz_clear_mpz(*r);
31         fmpz_set_ui(f, n_sqrtrem((mp_limb_t *) r, *g));
32     }
33     else
34     {
35         __mpz_struct * r_mpz_ptr, * f_mpz_ptr;
36         _fmpz_promote(f); /* must not hang on to pointer whilst promoting */
37         r_mpz_ptr = _fmpz_promote(r);
38 		f_mpz_ptr = COEFF_TO_PTR(*f);
39 
40         mpz_sqrtrem(f_mpz_ptr, r_mpz_ptr, COEFF_TO_PTR(*g));
41         _fmpz_demote_val(f);
42         _fmpz_demote_val(r);
43     }
44 }
45