Add an internal-representation no-op function.
[u/mdw/catacomb] / mpint.c
1 /* -*-c-*-
2 *
3 * $Id: mpint.c,v 1.3 2000/10/08 12:11:22 mdw Exp $
4 *
5 * Conversion between MPs and standard C integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpint.c,v $
33 * Revision 1.3 2000/10/08 12:11:22 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
35 *
36 * Revision 1.2 1999/12/10 23:22:53 mdw
37 * Support for uint32.
38 *
39 * Revision 1.1 1999/11/25 11:38:31 mdw
40 * Support for conversions between MPs and C integers.
41 *
42 */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include "mpint.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* --- Conversion from C integers --- */
51
52 #define FROM(name, type) \
53 mp *mp_from##name(mp *d, type i) { \
54 MP_FROMINT(d, type, i); \
55 return (d); \
56 }
57
58 FROM(short, short)
59 FROM(ushort, unsigned short)
60 FROM(int, int)
61 FROM(uint, unsigned)
62 FROM(uint32, uint32)
63 FROM(long, long)
64 FROM(ulong, unsigned long)
65
66 #undef FROM
67
68 /* --- Conversion to C integers --- */
69
70 #define TO(name, type, max) \
71 type mp_to##name(const mp *m) \
72 { \
73 type i; \
74 MP_TOINT(m, type, max, i); \
75 return (i); \
76 }
77
78 TO(short, short, SHRT_MAX)
79 TO(ushort, unsigned short, USHRT_MAX)
80 TO(int, int, INT_MAX)
81 TO(uint, unsigned, UINT_MAX)
82 TO(uint32, uint32, 0xffffffff)
83 TO(long, long, LONG_MAX)
84 TO(ulong, unsigned long, ULONG_MAX)
85
86 #undef TO
87
88 /*----- Test rig ----------------------------------------------------------*/
89
90 #ifdef TEST_RIG
91
92 #include <mLib/testrig.h>
93
94 static int fromuint(dstr *v)
95 {
96 unsigned long i = *(unsigned long *)v[0].buf;
97 mp *m = *(mp **)v[1].buf;
98 mp *d = mp_fromuint(MP_NEW, i);
99 int ok = 1;
100
101 if (!MP_EQ(d, m)) {
102 fputs("\n*** fromint failed.\n", stderr);
103 fprintf(stderr, "i = %lu", i);
104 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
105 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
106 fputc('\n', stderr);
107 ok = 0;
108 }
109
110 mp_drop(m);
111 mp_drop(d);
112 assert(mparena_count(MPARENA_GLOBAL) == 0);
113 return (ok);
114 }
115
116 static int fromint(dstr *v)
117 {
118 long i = *(long *)v[0].buf;
119 mp *m = *(mp **)v[1].buf;
120 mp *d = mp_fromint(MP_NEW, i);
121 int ok = 1;
122
123 if (!MP_EQ(d, m)) {
124 fputs("\n*** fromint failed.\n", stderr);
125 fprintf(stderr, "i = %li", i);
126 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
127 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
128 fputc('\n', stderr);
129 ok = 0;
130 }
131
132 mp_drop(m);
133 mp_drop(d);
134 assert(mparena_count(MPARENA_GLOBAL) == 0);
135 return (ok);
136 }
137
138 static int touint(dstr *v)
139 {
140 mp *m = *(mp **)v[0].buf;
141 unsigned long i = *(unsigned long *)v[1].buf;
142 unsigned j = mp_touint(m);
143 int ok = 1;
144
145 if (i != j) {
146 fputs("\n*** touint failed.\n", stderr);
147 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
148 fprintf(stderr, "\nexpect = %lu; result = %u\n", i, j);
149 ok = 0;
150 }
151
152 mp_drop(m);
153 assert(mparena_count(MPARENA_GLOBAL) == 0);
154 return (ok);
155 }
156
157 static int toint(dstr *v)
158 {
159 mp *m = *(mp **)v[0].buf;
160 long i = *(long *)v[1].buf;
161 int j = mp_toint(m);
162 int ok = 1;
163
164 if (i != j) {
165 fputs("\n*** toint failed.\n", stderr);
166 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
167 fprintf(stderr, "\nexpect = %li; result = %i\n", i, j);
168 ok = 0;
169 }
170
171 mp_drop(m);
172 assert(mparena_count(MPARENA_GLOBAL) == 0);
173 return (ok);
174 }
175
176 static test_chunk tests[] = {
177 { "fromuint", fromuint, { &type_ulong, &type_mp, 0 } },
178 { "fromint", fromint, { &type_long, &type_mp, 0 } },
179 { "touint", touint, { &type_mp, &type_ulong, 0 } },
180 { "toint", toint, { &type_mp, &type_long, 0 } },
181 { 0, 0, { 0 } }
182 };
183
184 int main(int argc, char *argv[])
185 {
186 sub_init();
187 test_run(argc, argv, tests, SRCDIR "/tests/mpint");
188 return (0);
189 }
190
191 #endif
192
193 /*----- That's all, folks -------------------------------------------------*/