1 /*
2     Copyright (C) 2012 Thomas M. DuBuisson
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 <gmp.h>
13 #include "flint.h"
14 #include "ulong_extras.h"
15 #include "fmpz.h"
16 
fmpz_and(fmpz_t f,const fmpz_t g,const fmpz_t h)17 void fmpz_and(fmpz_t f, const fmpz_t g, const fmpz_t h)
18 {
19         fmpz c1,c2;
20         c1 = *g;
21         c2 = *h;
22         if(!COEFF_IS_MPZ(c1))
23         {
24             if(!COEFF_IS_MPZ(c2)) /* both inputs are small */
25             {
26                 fmpz_set_si(f, c1 & c2);
27             } else /* g is small, h is large */
28             {
29                 mpz_t tmp;
30                 __mpz_struct *mpz3 = _fmpz_promote(f);
31                 flint_mpz_init_set_si(tmp, c1);
32                 mpz_and(mpz3, COEFF_TO_PTR(c2), tmp);
33                 _fmpz_demote_val(f);
34                 mpz_clear(tmp);
35             }
36         } else
37         {
38             if(!COEFF_IS_MPZ(c2)) /* g is large, h is small */
39             {
40                 mpz_t tmp;
41                 __mpz_struct *mpz3 = _fmpz_promote(f);
42                 flint_mpz_init_set_si(tmp, c2);
43                 mpz_and(mpz3, COEFF_TO_PTR(c1), tmp);
44                 _fmpz_demote_val(f);
45                 mpz_clear(tmp);
46             } else /* g and h are large */
47             {
48                 __mpz_struct * mpz3 = _fmpz_promote(f);
49                 __mpz_struct * mpz1 = COEFF_TO_PTR(c1);
50                 __mpz_struct * mpz2 = COEFF_TO_PTR(c2);
51                 mpz_and(mpz3, mpz1, mpz2);
52                 _fmpz_demote_val(f);
53             }
54         }
55 }
56