1#include <stdio.h>
2/*
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 *
5 * Use of this software is governed by the MIT license
6 *
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 */
10
11#include <isl_int.h>
12
13uint32_t isl_gmp_hash(mpz_t v, uint32_t hash)
14{
15	int sa = v[0]._mp_size;
16	int abs_sa = sa < 0 ? -sa : sa;
17	unsigned char *data = (unsigned char *)v[0]._mp_d;
18	unsigned char *end = data + abs_sa * sizeof(v[0]._mp_d[0]);
19
20	if (sa < 0)
21		isl_hash_byte(hash, 0xFF);
22	for (; data < end; ++data)
23		isl_hash_byte(hash, *data);
24	return hash;
25}
26
27/* This function tries to produce outputs that do not depend on
28 * the version of GMP that is being used.
29 *
30 * In particular, when computing the extended gcd of -1 and 9,
31 * some versions will produce
32 *
33 *	1 = -1 * -1 + 0 * 9
34 *
35 * while other versions will produce
36 *
37 *	1 = 8 * -1 + 1 * 9
38 *
39 * If configure detects that we are in the former case, then
40 * mpz_gcdext will be called directly.  Otherwise, this function
41 * is called and then we try to mimic the behavior of the other versions.
42 */
43void isl_gmp_gcdext(mpz_t G, mpz_t S, mpz_t T, mpz_t A, mpz_t B)
44{
45	if (mpz_divisible_p(B, A)) {
46		mpz_set_si(S, mpz_sgn(A));
47		mpz_set_si(T, 0);
48		mpz_abs(G, A);
49		return;
50	}
51	if (mpz_divisible_p(A, B)) {
52		mpz_set_si(S, 0);
53		mpz_set_si(T, mpz_sgn(B));
54		mpz_abs(G, B);
55		return;
56	}
57	mpz_gcdext(G, S, T, A, B);
58}
59