1 /*
2     Copyright (C) 2009 William Hart
3     Copyright (C) 2014 Abhinav Baid
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 <http://www.gnu.org/licenses/>.
11 */
12 
13 #include "fmpz.h"
14 
15 void
fmpz_smod(fmpz_t f,const fmpz_t g,const fmpz_t h)16 fmpz_smod(fmpz_t f, const fmpz_t g, const fmpz_t h)
17 {
18     fmpz c2 = *h;
19 
20     if (!COEFF_IS_MPZ(c2))      /* h is small */
21     {
22         ulong tmp;
23 
24         tmp = FLINT_ABS(c2);
25 
26         fmpz_mod(f, g, h);
27         if (fmpz_cmp_ui(f, tmp / 2) > 0)
28         {
29             fmpz_sub_ui(f, f, tmp);
30         }
31     }
32     else                        /* h is large */
33     {
34         fmpz_t tmp, rtmp;
35 
36         fmpz_init(tmp);
37         fmpz_init(rtmp);
38         fmpz_abs(tmp, h);
39         fmpz_fdiv_q_2exp(rtmp, tmp, 1);
40 
41         fmpz_mod(f, g, h);
42         if (fmpz_cmp(f, rtmp) > 0)
43         {
44             fmpz_sub(f, f, tmp);
45         }
46         fmpz_clear(tmp);
47         fmpz_clear(rtmp);
48     }
49 }
50