File: xgcd.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (117 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2012 William Hart

******************************************************************************/

#include <gmp.h>
#include "flint.h"
#include "ulong_extras.h"
#include "fmpz.h"

void
fmpz_xgcd(fmpz_t d, fmpz_t a, fmpz_t b, const fmpz_t f, const fmpz_t g)
{
    fmpz_t t1, t2;
    fmpz *f1, *g1;

    if (fmpz_is_zero(f))
    {
        int sign = fmpz_sgn(g);
        fmpz_abs(d, g);
        fmpz_set_ui(a, 0);
        if (sign == 0)
            fmpz_set_ui(b, 0);
        else if (sign > 0)
            fmpz_set_ui(b, 1);
        else
            fmpz_set_si(b, -1);
    }
    else if (fmpz_cmp(f, g) == 0)
    {
        if (fmpz_sgn(f) > 0)
        {
            fmpz_set(d, f);
            fmpz_set_ui(a, 1);
        }
        else
        {
            fmpz_neg(d, f);
            fmpz_set_si(a, -1);
        }
        fmpz_set_si(b, 0);
    }
    else
    {
        int sign1 = fmpz_sgn(f);
        int sign2 = fmpz_sgn(g);

        fmpz_init(t1);
        fmpz_init(t2);

        /* support aliasing */
        if (d == f || a == f || sign1 < 0)
        {
            f1 = t1;
            if (sign1 < 0)
                fmpz_neg(f1, f);
            else
                fmpz_set(f1, f);
        }
        else
            f1 = (fmpz *) f;

        if (d == g || a == g || sign2 < 0)
        {
            g1 = t2;
            if (sign2 < 0)
                fmpz_neg(g1, g);
            else
                fmpz_set(g1, g);
        }
        else
            g1 = (fmpz *) g;

        if (fmpz_cmp(f1, g1) < 0)
        {
            fmpz_gcdinv(d, a, f1, g1);
            fmpz_mul(t1, a, f1);
            fmpz_sub(t1, d, t1);
            fmpz_divexact(b, t1, g1);
        }
        else                    /* g < f */
        {
            fmpz_gcdinv(d, b, g1, f1);
            fmpz_mul(t2, b, g1);
            fmpz_sub(t2, d, t2);
            fmpz_divexact(a, t2, f1);
        }

        if (sign1 < 0)
            fmpz_neg(a, a);
        if (sign2 < 0)
            fmpz_neg(b, b);

        fmpz_clear(t1);
        fmpz_clear(t2);
    }
}