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