1 /*
2     Copyright (C) 2009 William Hart
3     Copyright (C) 2010 Sebastian Pancratz
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <gmp.h>
16 #include "flint.h"
17 #include "fmpz.h"
18 
19 void
fmpz_cdiv_q_ui(fmpz_t f,const fmpz_t g,ulong h)20 fmpz_cdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)
21 {
22     fmpz c1 = *g;
23     ulong c2 = h;
24 
25     if (h == 0)
26     {
27         flint_printf("Exception: division by zero in fmpz_cdiv_q_ui\n");
28         flint_abort();
29     }
30 
31     if (!COEFF_IS_MPZ(c1))      /* g is small */
32     {
33         if (c1 > 0)
34         {
35             ulong q = c1 / c2;
36             ulong r = c1 - c2 * q;
37 
38             if (r)
39                 ++q;
40 
41             fmpz_set_ui(f, q);
42         }
43         else
44         {
45             fmpz_set_si(f, - (((ulong) -c1) / c2));
46         }
47     }
48     else                        /* g is large */
49     {
50         __mpz_struct *mpz_ptr = _fmpz_promote(f);
51 
52         flint_mpz_cdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), c2);
53         _fmpz_demote_val(f);    /* division by h may result in small value */
54     }
55 }
56