File: convert.c

package info (click to toggle)
numerix 0.22-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,380 kB
  • ctags: 4,165
  • sloc: asm: 26,210; ansic: 12,168; ml: 4,912; sh: 3,899; pascal: 414; makefile: 179
file content (208 lines) | stat: -rw-r--r-- 5,396 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// file kernel/x/c/convert.c: extensible integer <-> long conversion
/*-----------------------------------------------------------------------+
 |  Copyright 2005-2006, Michel Quercia (michel.quercia@prepas.org)      |
 |                                                                       |
 |  This file is part of Numerix. Numerix is free software; you can      |
 |  redistribute it and/or modify it under the terms of the GNU Lesser   |
 |  General Public License as published by the Free Software Foundation; |
 |  either version 2.1 of the License, or (at your option) any later     |
 |  version.                                                             |
 |                                                                       |
 |  The Numerix Library 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  |
 |  Lesser General Public License for more details.                      |
 |                                                                       |
 |  You should have received a copy of the GNU Lesser General Public     |
 |  License along with the GNU MP Library; see the file COPYING. If not, |
 |  write to the Free Software Foundation, Inc., 59 Temple Place -       |
 |  Suite 330, Boston, MA 02111-1307, USA.                               |
 +-----------------------------------------------------------------------+
 |                                                                       |
 |                             Conversion                                |
 |                                                                       |
 +-----------------------------------------------------------------------*/

/* ------------------------------------------------------------ int -> xint
   entre :
   a = long ou Caml/Ocaml int
   _b = NULL ou pointeur sur un entier extensible

   sortie :
   b <- a
   si _b != NULL, *_b <- b
   retourne b
*/
xint xx(copy_int)(xint *_b, long a) {
    long sa = a & SIGN_m;
    xint b;
    xx_push_roots_1(_b);
#ifdef caml_api
#define _b __lr._b
#endif

    /* a <- |a| */
#ifdef c_api
    if (sa) a = -a;
#else
    a = (sa) ? -Long_val(a) : Long_val(a);
#endif

    /* dcompose en 0, 1 ou 2 chiffres */
#if chiffres_per_long == 2
    if (a >= 2*BASE_2) {
        b = xx(enlarge)(_b,2);
        b->val[0] = a;
        b->val[1] = a >> HW;
        b->hd = 2|sa;
    } else
#endif
        if (a) {
            b = xx(enlarge)(_b,1);
            b->val[0] = a;
            b->hd = 1|sa;
        }
        else {
            b = xx(enlarge)(_b,0);
            b->hd = 0;
        }

    xx_update_and_return(_b,b);

#undef _b
}

#if defined(caml_api) || defined(ocaml_api)

xint xx(of_int)(value a) {return xx(copy_int)(xx_null,a);}

#endif

/* ------------------------------------------------------------ xint -> int
   entre :
   a = entier extensible

   sortie :
   retourne le long ou Caml/Ocaml int de mme valeur que a

   erreur :
   INTEGER_OVERFLOW si |a| >= 2^30
*/
long xx(int_of)(xint a) {
    long la = xx_lg(a), sa = xx_sgn(a);
    unsigned long b;

    if (la == 0) return(Val_long(0));

    if (la <= chiffres_per_long) {
        b = a->val[0];
#if chiffres_per_long == 2
        if (la > 1) b += (unsigned long)a->val[1] << HW;
#endif
        if (!(b >> 30)) return((sa) ? Val_long(-b) : Val_long(b));
    }
    xx(failwith)(INTEGER_OVERFLOW);
}


/* -------------------------------------------------------- Structure binaire
   entre :
   a = entier extensible

   sortie :
   si a <> 0 -> 1 + ceil(log_2(|a|))
   si a =  0 -> 0
*/
long xx(nbits)(xint a) {
    long la = xx_lg(a),n;
    chiffre u;

    if (la == 0) return(Val_long(0));
    for (n = (la-1)*HW, u = a->val[la-1]; u; u >>= 1, n++);
    return(Val_long(n));
}

/*
  entre :
  a = entier extensible

  sortie :
  bits 0..30 de |a|
*/
long xx(lowbits)(xint a) {
    unsigned long la = xx_lg(a), u;

    if (la == 0) return(Val_long(0));
    u = a->val[0];
#if HW == 16
    if (la > 1) u += a->val[1] << HW;
#endif
    return(Val_long(u & 0x7fffffff));
}

/*
  entre :
  a = entier extensible

  sortie :
  bits l-31..l-1 de |a|, l = nbits(a)
*/
long xx(highbits)(xint a) {
    unsigned long la = xx_lg(a), u,v;

    if (la == 0) return(Val_long(0));

    u = a->val[la-1];
    if (u >> 30) {while (u>>31) u >>= 1;}
    else {
        v = (la > 1) ? a->val[la-2] : 0;
#if HW == 16
        v <<= HW;
        if (la > 2) v += a->val[la-3];
#endif
        do {u <<= 1; if ((signed long)v < 0) u++; v <<= 1;} while (!(u>>30));
    }
    return(Val_long(u));
}

/*
  entre :
  a = entier extensible
  n = indice

  sortie :
  bits 16*(n-1)..16*n-1 de |a|
*/
long xx(nth_word)(xint a, long n) {
    long la = xx_lg(a), p,u;

#if defined(caml_api) || defined(ocaml_api)
    n = Long_val(n);
#endif
    if (n < 0) return(Val_long(0));
    p = 16*(n%(HW/16));
    n = n/(HW/16);
    u = (n < la) ? a->val[n]>>p : 0;
    return(Val_long(u & 0xffff));
}

/*
  entre :
  a = entier extensible
  n = indice

  sortie :
  bit n de |a|
*/
long xx(nth_bit)(xint a, long n) {
    long la = xx_lg(a), p,u;

#if defined(caml_api) || defined(ocaml_api)
    n = Long_val(n);
#endif
    if (n < 0) return(Val_bool(0));
    p = n%HW;
    n = n/HW;
    u = (n < la) ? a->val[n]>>p : 0;
    return(Val_bool(u & 1));
}