aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/base/digitHandling.c
blob: 79e9dfbf371aebc6dd8e299a3f8ba302b4dffaf4 (plain)
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
/*
 * Copyright (C) 2000, Imperial College
 *
 * This file is part of the Imperial College Exact Real Arithmetic Library.
 * See the copyright notice included in the distribution for conditions
 * of use.
 */

#include <stdio.h>
#include "real.h"
#include "real-impl.h"

/*
 * For a real that has been forced to a stream (by force_R_Digs)
 * this function retrieves the sign and the most significant digit information.
 */
void
retrieveInfo(Real x, Sign *sign, int *count, mpz_t digits)
{
	Real derefToStrm(Real);

	x = derefToStrm(x);

	if (x == NULL)
		Error(FATAL, E_INT, "retrieveInfo", "argument not forced");

	if (x->gen.tag.type == SIGNX) {
		*sign = x->signX.tag.value;
		x = x->signX.x;
	}
	else
		*sign = SPOS;

	if ((x->gen.tag.type = DIGSX)) {
		*count = x->digsX.count;
#ifdef PACK_DIGITS
		if (*count <= DIGITS_PER_WORD)
        	mpz_set_si(digits, x->digsX.word.small);
    	else
#endif
        	mpz_set(digits, x->digsX.word.big);
	}
	else
		Error(FATAL, E_INT, "retrieveInfo",
				"no information has been forced from the argument");
}

/*
 * Given a characteristic pair (c,n) where n is the number
 * of digits recorded in c, this function returns the most significant
 * digit yielding also (c', n-1)
 */
Digit
takeDigit(int *count, mpz_t digits)
{
	Digit digit;

	if (*count > 0)
	  mpz_tdiv_q_2exp(tmpa_z, digits, *count - 1);
	if (mpz_cmp_ui(tmpa_z, 0) == 0)
	  digit = DZERO;
	else
	  if (mpz_cmp_ui(tmpa_z, 1) == 0)
	    digit = DPOS;
	  else
	    if (mpz_cmp_si(tmpa_z, -1) == 0)
	      digit = DNEG;
	    else {
	      Error(FATAL, E_INT, "takeDigit",
		    "characteristic pair is not well formed");
	      digit = DZERO;
	    }

	mpz_mul_2exp(tmpa_z, tmpa_z, *count - 1);
	mpz_sub(digits, digits, tmpa_z);
	*count = *count - 1;
	return digit;
}