annotate src/number-mp.c @ 5795:d2c0ff38ad5c

Report lstream errors when encoding/decoding. See <CAHCOHQ=FAieD-2nP303fMvwkii8HK2z+X7gRZ2+4PH1CA5_-NA@mail.gmail.com> in xemacs-patches.
author Jerry James <james@xemacs.org>
date Wed, 14 May 2014 14:16:24 -0600
parents a2912073be85
children 574f0cded429
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
1 /* Numeric types for XEmacs using the MP library.
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
2 Copyright (C) 2004 Jerry James.
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
3
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
4 This file is part of XEmacs.
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
5
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 5231
diff changeset
6 XEmacs is free software: you can redistribute it and/or modify it
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 5231
diff changeset
8 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 5231
diff changeset
9 option) any later version.
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
10
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
14 for more details.
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
15
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 5231
diff changeset
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
18
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
19 /* Synched up with: Not in FSF. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
20
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
21 #include <config.h>
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
22 #include <limits.h>
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
23 #include <math.h>
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
24 #include <stdlib.h>
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
25 #include "lisp.h"
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
26
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
27 static MINT *bignum_bytesize, *bignum_one, *bignum_two;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
28 MINT *bignum_zero, *intern_bignum;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
29 MINT *bignum_min_int, *bignum_max_int, *bignum_max_uint;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
30 MINT *bignum_min_long, *bignum_max_long, *bignum_max_ulong;
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
31 MINT *bignum_min_llong, *bignum_max_llong, *bignum_max_ullong;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
32 short div_rem;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
33
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
34 char *
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
35 bignum_to_string (bignum b, int base)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
36 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
37 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
38 unsigned int bufsize = 128U, index = 0U;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
39 int sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
40 char *buffer = xnew_array (char, 128), *retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
41 MINT *quo = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
42 short rem;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
43
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
44 /* FIXME: signal something if base is < 2 or doesn't fit into a short. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
45
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
46 /* Save the sign for later */
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
47 sign = bignum_sign (b);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
48
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
49 if (sign == 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
50 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
51 XREALLOC_ARRAY (buffer, char, 2);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
52 buffer[0] = '0';
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
53 buffer[1] = '\0';
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
54 return buffer;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
55 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
56 /* Copy abs(b) into quo for destructive modification */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
57 else if (sign < 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
58 MP_MSUB (bignum_zero, b, quo);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
59 else
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
60 MP_MOVE (b, quo);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
61
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
62 /* Loop over the digits of b (in BASE) and place each one into buffer */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
63 for (i = 0U; MP_MCMP(quo, bignum_zero) > 0; i++)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
64 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
65 MP_SDIV (quo, base, quo, &rem);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
66 if (index == bufsize)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
67 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
68 bufsize <<= 1;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
69 XREALLOC_ARRAY (buffer, char, bufsize);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
70 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
71 buffer[index++] = rem < 10 ? rem + '0' : rem - 10 + 'a';
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
72 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
73 MP_MFREE (quo);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
74
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
75 /* Reverse the digits, maybe add a minus sign, and add a null terminator */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
76 bufsize = index + (sign < 0 ? 1 : 0) + 1;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
77 retval = xnew_array (char, bufsize);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
78 if (sign < 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
79 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
80 retval[0] = '-';
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
81 i = 1;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
82 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
83 else
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
84 i = 0;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
85 for (; i < bufsize - 1; i++)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
86 retval[i] = buffer[--index];
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
87 retval[bufsize - 1] = '\0';
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4802
diff changeset
88 xfree (buffer);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
89 return retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
90 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
91
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
92 #define BIGNUM_TO_TYPE(type,accumtype) do { \
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
93 if (0 == sign) \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
94 { \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
95 return (type)0; \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
96 } \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
97 \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
98 bignum_init (quo); \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
99 \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
100 if (sign < 0) \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
101 { \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
102 MP_MSUB (bignum_zero, b, quo); \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
103 } \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
104 else \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
105 { \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
106 MP_MOVE (b, quo); \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
107 } \
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
108 \
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
109 for (i = 0U; i < sizeof(type); i++) \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
110 { \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
111 MP_SDIV (quo, 256, quo, &rem); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
112 retval |= ((accumtype) rem) << (8 * i); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
113 } \
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
114 bignum_fini (quo); \
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
115 } while (0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
116
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
117 int
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
118 bignum_to_int (bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
119 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
120 short rem, sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
121 unsigned int retval = 0;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
122 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
123 MINT *quo;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
124
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
125 sign = bignum_sign (b);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
126 BIGNUM_TO_TYPE (int, unsigned int);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
127 return ((int) retval) * sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
128 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
129
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
130 unsigned int
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
131 bignum_to_uint (bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
132 {
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
133 short rem, sign;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
134 unsigned int retval = 0U;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
135 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
136 MINT *quo;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
137
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
138 sign = bignum_sign (b);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
139 BIGNUM_TO_TYPE (unsigned int, unsigned int);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
140 return retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
141 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
142
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
143 long
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
144 bignum_to_long (bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
145 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
146 short rem, sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
147 unsigned long retval = 0L;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
148 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
149 MINT *quo;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
150
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
151 sign = bignum_sign (b);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
152 BIGNUM_TO_TYPE (long, unsigned long);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
153 return ((long) retval) * sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
154 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
155
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
156 unsigned long
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
157 bignum_to_ulong (bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
158 {
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
159 short rem, sign;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
160 unsigned long retval = 0UL;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
161 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
162 MINT *quo;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
163
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
164 sign = bignum_sign (b);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
165 BIGNUM_TO_TYPE (unsigned long, unsigned long);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
166 return retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
167 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
168
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
169 long long
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
170 bignum_to_llong (bignum b)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
171 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
172 short rem, sign;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
173 unsigned long long retval = 0LL;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
174 REGISTER unsigned int i;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
175 MINT *quo;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
176
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
177 sign = bignum_sign (b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
178 BIGNUM_TO_TYPE (long long, unsigned long long);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
179 return ((long long) retval) * sign;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
180 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
181
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
182 unsigned long long
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
183 bignum_to_ullong (bignum b)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
184 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
185 short rem, sign;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
186 unsigned long long retval = 0UL;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
187 REGISTER unsigned int i;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
188 MINT *quo;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
189
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
190 sign = bignum_sign (b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
191 BIGNUM_TO_TYPE (unsigned long long, unsigned long long);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
192 return retval;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
193 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
194
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
195 double
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
196 bignum_to_double (bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
197 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
198 short rem, sign;
1990
4f5e0297b73f [xemacs-hg @ 2004-04-06 20:52:08 by james]
james
parents: 1983
diff changeset
199 double retval = 0.0, factor = 1.0;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
200 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
201 MINT *quo;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
202
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
203 sign = bignum_sign (b);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
204 bignum_init (quo);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
205 if (sign < 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
206 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
207 MP_MSUB (bignum_zero, b, quo);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
208 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
209 else
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
210 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
211 MP_MOVE (b, quo);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
212 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
213
1990
4f5e0297b73f [xemacs-hg @ 2004-04-06 20:52:08 by james]
james
parents: 1983
diff changeset
214 for (i = 0U; MP_MCMP (quo, bignum_zero) > 0; i++)
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
215 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
216 MP_SDIV (quo, 256, quo, &rem);
1990
4f5e0297b73f [xemacs-hg @ 2004-04-06 20:52:08 by james]
james
parents: 1983
diff changeset
217 retval += rem * factor;
4f5e0297b73f [xemacs-hg @ 2004-04-06 20:52:08 by james]
james
parents: 1983
diff changeset
218 factor *= 256.0;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
219 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
220 MP_MFREE (quo);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
221 return retval * sign;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
222 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
223
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
224 static short
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
225 char_to_number (char c)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
226 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
227 if (c >= '0' && c <= '9')
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
228 return c - '0';
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
229 if (c >= 'a' && c <= 'z')
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
230 return c - 'a' + 10;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
231 if (c >= 'A' && c <= 'Z')
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
232 return c - 'A' + 10;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
233 return -1;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
234 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
235
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
236 int
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
237 bignum_set_string (bignum b, const char *s, int base)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
238 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
239 MINT *mbase;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
240 short digit;
1993
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
241 int neg = 0;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
242
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
243 if (base == 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
244 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
245 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
246 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
247 base = 16;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
248 s += 2;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
249 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
250 else if (*s == '0')
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
251 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
252 base = 8;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
253 s++;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
254 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
255 else
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
256 base = 10;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
257 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
258
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
259 /* FIXME: signal something if base is < 2 or doesn't fit into a short. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
260
1993
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
261 if (*s == '-')
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
262 {
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
263 s++;
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
264 neg = 1;
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
265 }
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
266
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
267 mbase = MP_ITOM ((short) base);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
268 MP_MOVE (bignum_zero, b);
4802
2fc0e2f18322 Don't create any bignums before pdumping. Add bignum, ratio, and bigfloat
Jerry James <james@xemacs.org>
parents: 1993
diff changeset
269
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
270 for (digit = char_to_number (*s); digit >= 0 && digit < base;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
271 digit = char_to_number (*++s))
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
272 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
273 MINT *temp;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
274
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
275 MP_MULT (b, mbase, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
276 temp = MP_ITOM (digit);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
277 MP_MADD (b, temp, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
278 MP_MFREE (temp);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
279 }
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
280 MP_MFREE (mbase);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
281
1993
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
282 if (neg)
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
283 MP_MSUB (bignum_zero, b, b);
e567417c2e5d [xemacs-hg @ 2004-04-07 02:41:29 by james]
james
parents: 1990
diff changeset
284
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
285 return (digit >= 0) ? -1 : 0;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
286 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
287
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
288 void
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
289 bignum_set_long (bignum b, long l)
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
290 {
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
291 char hex[SIZEOF_LONG * 2U + 2U];
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
292 MINT *temp;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
293 int neg = l < 0L;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
294
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
295 snprintf (hex, SIZEOF_LONG * 2U + 2U, "%lx",
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
296 neg ? (unsigned long) -l : (unsigned long) l);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
297 temp = MP_XTOM (hex);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
298 if (neg)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
299 MP_MSUB (bignum_zero, temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
300 else
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
301 MP_MOVE (temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
302 MP_MFREE (temp);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
303 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
304
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
305 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
306 bignum_set_ulong (bignum b, unsigned long l)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
307 {
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
308 char hex[SIZEOF_LONG * 2U + 2U];
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
309 MINT *temp;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
310
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
311 snprintf (hex, SIZEOF_LONG * 2U + 2U, "%lx", l);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
312 temp = MP_XTOM (hex);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
313 MP_MOVE (temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
314 MP_MFREE (temp);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
315 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
316
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
317 void
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
318 bignum_set_llong (bignum b, long long l)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
319 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
320 char hex[SIZEOF_LONG_LONG * 2U + 2U];
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
321 MINT *temp;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
322 int neg = l < 0LL;
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
323
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
324 snprintf (hex, SIZEOF_LONG_LONG * 2U + 2U, "%llx",
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
325 neg ? (unsigned long long) -l : (unsigned long long) l);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
326 temp = MP_XTOM (hex);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
327 if (neg)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
328 MP_MSUB (bignum_zero, temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
329 else
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
330 MP_MOVE (temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
331 MP_MFREE (temp);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
332 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
333
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
334 void
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
335 bignum_set_ullong (bignum b, unsigned long long l)
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
336 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
337 char hex[SIZEOF_LONG_LONG * 2U + 2U];
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
338 MINT *temp;
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
339
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
340 snprintf (hex, SIZEOF_LONG_LONG * 2U + 2U, "%llx", l);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
341 temp = MP_XTOM (hex);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
342 MP_MOVE (temp, b);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
343 MP_MFREE (temp);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
344 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
345
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
346 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
347 bignum_set_double (bignum b, double d)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
348 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
349 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
350 int negative = (d < 0) ? 1 : 0;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
351 MINT *multiplier = MP_ITOM (1);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
352
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
353 MP_MOVE (bignum_zero, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
354 if (negative)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
355 d = -d;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
356 for (i = 0UL; d > 0.0; d /= 256, i++)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
357 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
358 MINT *temp = MP_ITOM ((short) fmod (d, 256.0));
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
359 MP_MULT (multiplier, temp, temp);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
360 MP_MADD (b, temp, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
361 MP_MULT (multiplier, bignum_bytesize, multiplier);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
362 MP_MFREE (temp);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
363 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
364 MP_MFREE (multiplier);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
365 if (negative)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
366 MP_MSUB (bignum_zero, b, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
367 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
368
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
369 /* Return nonzero if b1 is exactly divisible by b2 */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
370 int
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
371 bignum_divisible_p (bignum b1, bignum b2)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
372 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
373 int retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
374 MINT *rem = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
375 MP_MDIV (b1, b2, intern_bignum, rem);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
376 retval = (MP_MCMP (rem, bignum_zero) == 0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
377 MP_MFREE (rem);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
378 return retval;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
379 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
380
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
381 void bignum_ceil (bignum quotient, bignum N, bignum D)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
382 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
383 MP_MDIV (N, D, quotient, intern_bignum);
5646
7aa144d1404b Remove a redundant double division, number-mp.c:bignum_ceil().
Aidan Kehoe <kehoea@parhasard.net>
parents: 5602
diff changeset
384
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
385 if (MP_MCMP (intern_bignum, bignum_zero) != 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
386 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
387 short signN = MP_MCMP (N, bignum_zero);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
388 short signD = MP_MCMP (D, bignum_zero);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
389
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
390 /* If the quotient is positive, add one, since we're rounding to
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
391 positive infinity. */
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
392 if (signD < 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
393 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
394 if (signN <= 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
395 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
396 MP_MADD (quotient, bignum_one, quotient);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
397 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
398 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
399 else if (signN >= 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
400 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
401 MP_MADD (quotient, bignum_one, quotient);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
402 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
403 }
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
404 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
405
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
406 void bignum_floor (bignum quotient, bignum N, bignum D)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
407 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
408 MP_MDIV (N, D, quotient, intern_bignum);
5602
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
409
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
410 if (MP_MCMP (intern_bignum, bignum_zero) != 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
411 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
412 short signN = MP_MCMP (N, bignum_zero);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
413 short signD = MP_MCMP (D, bignum_zero);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
414
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
415 /* If the quotient is negative, subtract one, we're rounding to minus
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
416 infinity. */
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
417 if (signD < 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
418 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
419 if (signN >= 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
420 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
421 MP_MSUB (quotient, bignum_one, quotient);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
422 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
423 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
424 else if (signN < 0)
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
425 {
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
426 MP_MSUB (quotient, bignum_one, quotient);
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
427 }
c9e5612f5424 Support the MP library on recent FreeBSD, have it pass relevant tests.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5405
diff changeset
428 }
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
429 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
430
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
431 /* RESULT = N to the POWth power */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
432 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
433 bignum_pow (bignum result, bignum n, unsigned long pow)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
434 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
435 MP_MOVE (bignum_one, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
436 for ( ; pow > 0UL; pow--)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
437 MP_MULT (result, n, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
438 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
439
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
440 /* lcm(b1,b2) = b1 * b2 / gcd(b1, b2) */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
441 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
442 bignum_lcm (bignum result, bignum b1, bignum b2)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
443 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
444 MP_MULT (b1, b2, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
445 MP_GCD (b1, b2, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
446 MP_MDIV (result, intern_bignum, result, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
447 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
448
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
449 /* FIXME: We can't handle negative args, so right now we just make them
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
450 positive before doing anything else. How should we really handle negative
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
451 args? */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
452 #define bignum_bit_op(result, b1, b2, op) \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
453 REGISTER unsigned int i; \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
454 MINT *multiplier = MP_ITOM (1), *n1 = MP_ITOM (0), *n2 = MP_ITOM (0); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
455 \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
456 if (MP_MCMP (bignum_zero, b1) > 0) \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
457 MP_MSUB (bignum_zero, b1, n1); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
458 else \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
459 MP_MOVE (b1, n1); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
460 if (MP_MCMP (bignum_zero, b2) > 0) \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
461 MP_MSUB (bignum_zero, b2, n2); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
462 else \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
463 MP_MOVE (b2, n2); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
464 \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
465 MP_MOVE (bignum_zero, result); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
466 \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
467 for (i = 0UL; MP_MCMP (bignum_zero, n1) < 0 && \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
468 MP_MCMP (bignum_zero, n2) < 0; i++) \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
469 { \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
470 short byte1, byte2; \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
471 MINT *temp; \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
472 \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
473 MP_SDIV (n1, 256, n1, &byte1); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
474 MP_SDIV (n2, 256, n2, &byte2); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
475 temp = MP_ITOM (byte1 op byte2); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
476 MP_MULT (multiplier, temp, temp); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
477 MP_MADD (result, temp, result); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
478 MP_MULT (multiplier, bignum_bytesize, multiplier); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
479 MP_MFREE (temp); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
480 } \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
481 MP_MFREE (n2); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
482 MP_MFREE (n1); \
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
483 MP_MFREE (multiplier)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
484
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
485 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
486 bignum_and (bignum result, bignum b1, bignum b2)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
487 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
488 bignum_bit_op (result, b1, b2, &);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
489 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
490
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
491 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
492 bignum_ior (bignum result, bignum b1, bignum b2)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
493 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
494 bignum_bit_op (result, b1, b2, |);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
495 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
496
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
497 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
498 bignum_xor (bignum result, bignum b1, bignum b2)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
499 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
500 bignum_bit_op (result, b1, b2, ^);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
501 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
502
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
503 /* NOT is not well-defined for bignums ... where do you stop flipping bits?
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
504 We just flip until we see the last one. This is probably a bad idea. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
505 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
506 bignum_not (bignum result, bignum b)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
507 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
508 REGISTER unsigned int i;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
509 MINT *multiplier = MP_ITOM (1), *n = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
510
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
511 if (MP_MCMP (bignum_zero, b) > 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
512 MP_MSUB (bignum_zero, b, n);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
513 else
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
514 MP_MOVE (b, n);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
515
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
516 MP_MOVE (bignum_zero, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
517
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
518 for (i = 0UL; MP_MCMP (bignum_zero, n) < 0; i++)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
519 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
520 short byte;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
521 MINT *temp;
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
522
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
523 MP_SDIV (n, 256, n, &byte);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
524 temp = MP_ITOM (~byte);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
525 MP_MULT (multiplier, temp, temp);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
526 MP_MADD (result, temp, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
527 MP_MULT (multiplier, bignum_bytesize, multiplier);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
528 MP_MFREE (temp);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
529 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
530 MP_MFREE (n);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
531 MP_MFREE (multiplier);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
532 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
533
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
534 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
535 bignum_setbit (bignum b, unsigned long bit)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
536 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
537 bignum_pow (intern_bignum, bignum_two, bit);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
538 bignum_ior (b, b, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
539 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
540
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
541 /* This is so evil, even I feel queasy. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
542 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
543 bignum_clrbit (bignum b, unsigned long bit)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
544 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
545 MINT *num = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
546
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
547 /* See if the bit is set, and subtract it off if so */
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
548 MP_MOVE (b, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
549 bignum_pow (num, bignum_two, bit);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
550 bignum_ior (intern_bignum, intern_bignum, num);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
551 if (MP_MCMP (b, intern_bignum) == 0)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
552 MP_MSUB (b, num, b);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
553 MP_MFREE (num);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
554 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
555
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
556 int
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
557 bignum_testbit (bignum b, unsigned long bit)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
558 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
559 bignum_pow (intern_bignum, bignum_two, bit);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
560 bignum_and (intern_bignum, b, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
561 return MP_MCMP (intern_bignum, bignum_zero);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
562 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
563
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
564 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
565 bignum_lshift (bignum result, bignum b, unsigned long bits)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
566 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
567 bignum_pow (intern_bignum, bignum_two, bits);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
568 MP_MULT (b, intern_bignum, result);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
569 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
570
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
571 void
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
572 bignum_rshift (bignum result, bignum b, unsigned long bits)
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
573 {
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
574 bignum_pow (intern_bignum, bignum_two, bits);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
575 MP_MDIV (b, intern_bignum, result, intern_bignum);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
576 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
577
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
578 void
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
579 bignum_random (bignum result, bignum limit)
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
580 {
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
581 MINT *denominator = MP_ITOM (0), *divisor = MP_ITOM (0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
582 bignum_set_long (denominator, RAND_MAX);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
583 MP_MADD (denominator, bignum_one, denominator);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
584 MP_MADD (limit, bignum_one, divisor);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
585 MP_MDIV (denominator, divisor, denominator, intern_bignum);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
586 MP_MFREE (divisor);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
587
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
588 do
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
589 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
590 MINT *limitcmp = MP_ITOM (1);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
591
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
592 /* Accumulate at least as many random bits as in LIMIT */
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
593 MP_MOVE (bignum_zero, result);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
594 do
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
595 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
596 bignum_lshift (limitcmp, limitcmp, FIXNUM_VALBITS);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
597 bignum_lshift (result, result, FIXNUM_VALBITS);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
598 bignum_set_long (intern_bignum, get_random ());
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
599 MP_MADD (intern_bignum, result, result);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
600 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
601 while (MP_MCMP (limitcmp, limit) <= 0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
602 MP_MDIV (result, denominator, result, intern_bignum);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
603 MP_MFREE (limitcmp);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
604 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
605 while (MP_MCMP (limit, result) <= 0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
606
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
607 MP_MFREE (denominator);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
608 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
609
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
610 #ifdef HAVE_MP_SET_MEMORY_FUNCTIONS
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
611 /* We need the next two functions due to the extra parameter. */
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
612 static void *
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
613 mp_realloc (void *ptr, size_t UNUSED (old_size), size_t new_size)
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
614 {
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
615 return xrealloc (ptr, new_size);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
616 }
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
617
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
618 static void
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
619 mp_free (void *ptr, size_t UNUSED (size))
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
620 {
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
621 xfree (ptr);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
622 }
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
623 #endif
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
624
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
625 void
5739
a2912073be85 Support bignums with MPIR. Add documentation on the bignum, ratio,
Jerry James <james@xemacs.org>
parents: 5736
diff changeset
626 init_number_mp (void)
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
627 {
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
628 #ifdef HAVE_MP_SET_MEMORY_FUNCTIONS
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
629 mp_set_memory_functions ((void *(*) (size_t)) xmalloc, mp_realloc, mp_free);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
630 #endif
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
631
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
632 bignum_zero = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
633 bignum_one = MP_ITOM (1);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
634 bignum_two = MP_ITOM (2);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
635
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
636 /* intern_bignum holds throwaway values from macro expansions in
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
637 number-mp.h. Its value is immaterial. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
638 intern_bignum = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
639
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
640 /* The multiplier used to shift a number left by one byte's worth of bits */
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
641 bignum_bytesize = MP_ITOM (256);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
642
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
643 /* The MP interface only supports turning short ints into MINTs, so we have
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
644 to set these the hard way. */
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
645
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
646 bignum_min_int = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
647 bignum_set_long (bignum_min_int, INT_MIN);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
648
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
649 bignum_max_int = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
650 bignum_set_long (bignum_max_int, INT_MAX);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
651
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
652 bignum_max_uint = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
653 bignum_set_ulong (bignum_max_uint, UINT_MAX);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
654
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
655 bignum_min_long = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
656 bignum_set_long (bignum_min_long, LONG_MIN);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
657
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
658 bignum_max_long = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
659 bignum_set_long (bignum_max_long, LONG_MAX);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
660
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
661 bignum_max_ulong = MP_ITOM (0);
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
662 bignum_set_ulong (bignum_max_ulong, ULONG_MAX);
5736
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
663
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
664 bignum_min_llong = MP_ITOM (0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
665 bignum_set_llong (bignum_min_llong, LLONG_MIN);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
666
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
667 bignum_max_llong = MP_ITOM (0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
668 bignum_set_llong (bignum_max_llong, LLONG_MAX);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
669
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
670 bignum_max_ullong = MP_ITOM (0);
3192994c49ca Convert C (un)signed long long values to bignums properly.
Jerry James <james@xemacs.org>
parents: 5646
diff changeset
671 bignum_set_ullong (bignum_max_ullong, ULLONG_MAX);
1983
9c872f33ecbe [xemacs-hg @ 2004-04-05 22:49:31 by james]
james
parents:
diff changeset
672 }