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 <gmp.h>
13 #include "flint.h"
14 #include "ulong_extras.h"
15 #include "fmpz.h"
16
fmpz_add(fmpz_t f,const fmpz_t g,const fmpz_t h)17 void fmpz_add(fmpz_t f, const fmpz_t g, const fmpz_t h)
18 {
19 fmpz c1 = *g;
20 fmpz c2 = *h;
21
22 if (!COEFF_IS_MPZ(c1)) /* g is small */
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_struct * mpz3 = _fmpz_promote(f); /* g is saved and h is large */
30 __mpz_struct * mpz2 = COEFF_TO_PTR(c2);
31 if (c1 < WORD(0)) flint_mpz_sub_ui(mpz3, mpz2, -c1);
32 else flint_mpz_add_ui(mpz3, mpz2, c1);
33 _fmpz_demote_val(f); /* may have cancelled */
34 }
35 }
36 else
37 {
38 if (!COEFF_IS_MPZ(c2)) /* g is large, h is small */
39 {
40 __mpz_struct * mpz3 = _fmpz_promote(f); /* h is saved and g is large */
41 __mpz_struct * mpz1 = COEFF_TO_PTR(c1);
42 if (c2 < WORD(0)) flint_mpz_sub_ui(mpz3, mpz1, -c2);
43 else flint_mpz_add_ui(mpz3, mpz1, c2);
44 _fmpz_demote_val(f); /* may have cancelled */
45 }
46 else /* g and h are large */
47 {
48 __mpz_struct * mpz3 = _fmpz_promote(f); /* aliasing means f is already large */
49 __mpz_struct * mpz1 = COEFF_TO_PTR(c1);
50 __mpz_struct * mpz2 = COEFF_TO_PTR(c2);
51 mpz_add(mpz3, mpz1, mpz2);
52 _fmpz_demote_val(f); /* may have cancelled */
53 }
54 }
55 }
56