1 /*
2     Copyright (C) 2009 William Hart
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 <http://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 
19 ulong
fmpz_fdiv_ui(const fmpz_t g,ulong h)20 fmpz_fdiv_ui(const fmpz_t g, ulong h)
21 {
22     fmpz c1 = *g;
23     ulong r;
24 
25     if (h == UWORD(0))
26     {
27         flint_printf("Exception (fmpz_fdiv_ui). Division by 0.\n");
28         flint_abort();
29     }
30 
31     if (!COEFF_IS_MPZ(c1))      /* g is small */
32     {
33         if (c1 < WORD(0))
34         {
35             r = h - (-c1 % h);  /* C doesn't correctly handle negative mods */
36             if (r == h)
37                 r = 0;
38         }
39         else
40             r = c1 % h;
41 
42         return r;
43     }
44     else                        /* g is large */
45     {
46         return flint_mpz_fdiv_ui(COEFF_TO_PTR(c1), h);
47     }
48 }
49