comparison src/number-mp.c @ 1993:e567417c2e5d

[xemacs-hg @ 2004-04-07 02:41:29 by james] Read negative bignums correctly with BSD MP.
author james
date Wed, 07 Apr 2004 02:41:30 +0000
parents 4f5e0297b73f
children 2fc0e2f18322
comparison
equal deleted inserted replaced
1992:4529ff71e646 1993:e567417c2e5d
191 int 191 int
192 bignum_set_string (bignum b, const char *s, int base) 192 bignum_set_string (bignum b, const char *s, int base)
193 { 193 {
194 MINT *mbase; 194 MINT *mbase;
195 short digit; 195 short digit;
196 int neg = 0;
196 197
197 if (base == 0) 198 if (base == 0)
198 { 199 {
199 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) 200 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
200 { 201 {
210 base = 10; 211 base = 10;
211 } 212 }
212 213
213 /* FIXME: signal something if base is < 2 or doesn't fit into a short. */ 214 /* FIXME: signal something if base is < 2 or doesn't fit into a short. */
214 215
216 if (*s == '-')
217 {
218 s++;
219 neg = 1;
220 }
221
215 mbase = MP_ITOM ((short) base); 222 mbase = MP_ITOM ((short) base);
216 MP_MOVE (bignum_zero, b); 223 MP_MOVE (bignum_zero, b);
217 224
218 for (digit = char_to_number (*s); digit >= 0 && digit < base; 225 for (digit = char_to_number (*s); digit >= 0 && digit < base;
219 digit = char_to_number (*++s)) 226 digit = char_to_number (*++s))
223 MP_MULT (b, mbase, b); 230 MP_MULT (b, mbase, b);
224 temp = MP_ITOM (digit); 231 temp = MP_ITOM (digit);
225 MP_MADD (b, temp, b); 232 MP_MADD (b, temp, b);
226 MP_MFREE (temp); 233 MP_MFREE (temp);
227 } 234 }
235
236 if (neg)
237 MP_MSUB (bignum_zero, b, b);
228 238
229 return (digit >= 0) ? -1 : 0; 239 return (digit >= 0) ? -1 : 0;
230 } 240 }
231 241
232 void 242 void