3 * $Id: mpint.c,v 1.1 1999/11/25 11:38:31 mdw Exp $
5 * Conversion between MPs and standard C integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/11/25 11:38:31 mdw
34 * Support for conversions between MPs and C integers.
38 /*----- Header files ------------------------------------------------------*/
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- Conversion from C integers --- */
46 #define FROM(name, type) \
47 mp *mp_from##name(mp *d, type i) { \
48 MP_FROMINT(d, type, i); \
53 FROM(ushort, unsigned short)
57 FROM(ulong, unsigned long)
61 /* --- Conversion to C integers --- */
63 #define TO(name, type, max) \
64 type mp_to##name(const mp *m) \
67 MP_TOINT(m, type, max, i); \
71 TO(short, short, SHRT_MAX)
72 TO(ushort, unsigned short, USHRT_MAX)
74 TO(uint, unsigned, UINT_MAX)
75 TO(long, long, LONG_MAX)
76 TO(ulong, unsigned long, ULONG_MAX)
80 /*----- Test rig ----------------------------------------------------------*/
84 #include <mLib/testrig.h>
86 static int fromuint(dstr *v)
88 unsigned i = *(unsigned *)v[0].buf;
89 mp *m = *(mp **)v[1].buf;
90 mp *d = mp_fromuint(MP_NEW, i);
93 if (MP_CMP(d, !=, m)) {
94 fputs("\n*** fromint failed.\n", stderr);
95 fprintf(stderr, "i = %u", i);
96 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
97 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
107 static int fromint(dstr *v)
109 int i = *(int *)v[0].buf;
110 mp *m = *(mp **)v[1].buf;
111 mp *d = mp_fromint(MP_NEW, i);
114 if (MP_CMP(d, !=, m)) {
115 fputs("\n*** fromint failed.\n", stderr);
116 fprintf(stderr, "i = %i", i);
117 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
118 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
128 static int touint(dstr *v)
130 mp *m = *(mp **)v[0].buf;
131 unsigned i = *(unsigned *)v[1].buf;
132 unsigned j = mp_touint(m);
136 fputs("\n*** touint failed.\n", stderr);
137 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
138 fprintf(stderr, "\nexpect = %u; result = %u\n", i, j);
146 static int toint(dstr *v)
148 mp *m = *(mp **)v[0].buf;
149 int i = *(int *)v[1].buf;
154 fputs("\n*** toint failed.\n", stderr);
155 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
156 fprintf(stderr, "\nexpect = %i; result = %i\n", i, j);
164 static test_chunk tests[] = {
165 { "fromuint", fromuint, { &type_int, &type_mp, 0 } },
166 { "fromint", fromint, { &type_int, &type_mp, 0 } },
167 { "touint", touint, { &type_mp, &type_int, 0 } },
168 { "toint", toint, { &type_mp, &type_int, 0 } },
172 int main(int argc, char *argv[])
175 test_run(argc, argv, tests, SRCDIR "/tests/mpint");
181 /*----- That's all, folks -------------------------------------------------*/