chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / mp-io.c
1 /* -*-c-*-
2  *
3  * Loading and storing of multiprecision 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 "mp.h"
31
32 /*----- Main code ---------------------------------------------------------*/
33
34 /* --- @mp_octets@ --- *
35  *
36  * Arguments:   @const mp *m@ = a multiprecision integer
37  *
38  * Returns:     The number of octets required to represent @m@.
39  *
40  * Use:         Calculates the external storage required for a multiprecision
41  *              integer.
42  */
43
44 size_t mp_octets(const mp *m)
45 {
46   size_t sz;
47   MPX_OCTETS(sz, m->v, m->vl);
48   return (sz);
49 }
50
51 /* --- @mp_octets2c@ --- *
52  *
53  * Arguments:   @const mp *m@ = a multiprecision integer
54  *
55  * Returns:     The number of octets required to represent @m@.
56  *
57  * Use:         Calculates the external storage required for a multiprecision
58  *              integer represented as two's complement.
59  */
60
61 size_t mp_octets2c(const mp *m)
62 {
63   size_t sz;
64   MPX_OCTETS2C(sz, m->v, m->vl);
65   return (sz);
66 }
67
68 /* --- @mp_bits@ --- *
69  *
70  * Arguments:   @const mp *m@ = a multiprecision integer
71  *
72  * Returns:     The number of bits required to represent @m@.
73  *
74  * Use:         Calculates the external storage required for a multiprecision
75  *              integer.
76  */
77
78 unsigned long mp_bits(const mp *m)
79 {
80   unsigned long bits;
81   MPX_BITS(bits, m->v, m->vl);
82   return (bits);
83 }
84
85 /* --- @mp_loadl@ --- *
86  *
87  * Arguments:   @mp *d@ = destination
88  *              @const void *pv@ = pointer to source data
89  *              @size_t sz@ = size of the source data
90  *
91  * Returns:     Resulting multiprecision number.
92  *
93  * Use:         Loads a multiprecision number from an array of octets.  The
94  *              first byte in the array is the least significant.  More
95  *              formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
96  *              then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
97  */
98
99 mp *mp_loadl(mp *d, const void *pv, size_t sz)
100 {
101   MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
102   mpx_loadl(d->v, d->vl, pv, sz);
103   d->f &= ~(MP_UNDEF | MP_NEG);
104   mp_shrink(d);
105   return (d);
106 }
107
108 /* --- @mp_storel@ --- *
109  *
110  * Arguments:   @const mp *m@ = source
111  *              @void *pv@ = pointer to output array
112  *              @size_t sz@ = size of the output array
113  *
114  * Returns:     ---
115  *
116  * Use:         Stores a multiprecision number in an array of octets.  The
117  *              first byte in the array is the least significant.  If the
118  *              array is too small to represent the number, high-order bits
119  *              are truncated; if the array is too large, high order bytes
120  *              are filled with zeros.  More formally, if the number is
121  *              %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
122  *              then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
123  */
124
125 void mp_storel(const mp *m, void *pv, size_t sz)
126 {
127   mpx_storel(m->v, m->vl, pv, sz);
128 }
129
130 /* --- @mp_loadb@ --- *
131  *
132  * Arguments:   @mp *d@ = destination
133  *              @const void *pv@ = pointer to source data
134  *              @size_t sz@ = size of the source data
135  *
136  * Returns:     Resulting multiprecision number.
137  *
138  * Use:         Loads a multiprecision number from an array of octets.  The
139  *              last byte in the array is the least significant.  More
140  *              formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
141  *              then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
142  */
143
144 mp *mp_loadb(mp *d, const void *pv, size_t sz)
145 {
146   MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
147   mpx_loadb(d->v, d->vl, pv, sz);
148   d->f &= ~(MP_UNDEF | MP_NEG);
149   mp_shrink(d);
150   return (d);
151 }
152
153 /* --- @mp_storeb@ --- *
154  *
155  * Arguments:   @const mp *m@ = source
156  *              @void *pv@ = pointer to output array
157  *              @size_t sz@ = size of the output array
158  *
159  * Returns:     ---
160  *
161  * Use:         Stores a multiprecision number in an array of octets.  The
162  *              last byte in the array is the least significant.  If the
163  *              array is too small to represent the number, high-order bits
164  *              are truncated; if the array is too large, high order bytes
165  *              are filled with zeros.  More formally, if the number is
166  *              %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
167  *              then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
168  */
169
170 void mp_storeb(const mp *m, void *pv, size_t sz)
171 {
172   mpx_storeb(m->v, m->vl, pv, sz);
173 }
174
175 /* --- @mp_loadl2c@ --- *
176  *
177  * Arguments:   @mp *d@ = destination
178  *              @const void *pv@ = pointer to source data
179  *              @size_t sz@ = size of the source data
180  *
181  * Returns:     Resulting multiprecision number.
182  *
183  * Use:         Loads a multiprecision number from an array of octets as
184  *              two's complement.  The first byte in the array is the least
185  *              significant.
186  */
187
188 mp *mp_loadl2c(mp *d, const void *pv, size_t sz)
189 {
190   const octet *ov = pv;
191   MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
192   if (!sz || !(ov[sz - 1] & 0x80)) {
193     mpx_loadl(d->v, d->vl, pv, sz);
194     d->f &= ~MP_NEG;
195   } else {
196     mpx_loadl2cn(d->v, d->vl, pv, sz);
197     d->f |= MP_NEG;
198   }
199   d->f &= ~MP_UNDEF;
200   mp_shrink(d);
201   return (d);
202 }
203
204 /* --- @mp_storel2c@ --- *
205  *
206  * Arguments:   @const mp *m@ = source
207  *              @void *pv@ = pointer to output array
208  *              @size_t sz@ = size of the output array
209  *
210  * Returns:     ---
211  *
212  * Use:         Stores a multiprecision number in an array of octets as two's
213  *              complement.  The first byte in the array is the least
214  *              significant.  If the array is too small to represent the
215  *              number, high-order bits are truncated; if the array is too
216  *              large, high order bytes are sign-extended.
217  */
218
219 void mp_storel2c(const mp *m, void *pv, size_t sz)
220 {
221   if (MP_NEGP(m))
222     mpx_storel2cn(m->v, m->vl, pv, sz);
223   else
224     mpx_storel(m->v, m->vl, pv, sz);
225 }
226
227 /* --- @mp_loadb2c@ --- *
228  *
229  * Arguments:   @mp *d@ = destination
230  *              @const void *pv@ = pointer to source data
231  *              @size_t sz@ = size of the source data
232  *
233  * Returns:     Resulting multiprecision number.
234  *
235  * Use:         Loads a multiprecision number from an array of octets as
236  *              two's complement.  The last byte in the array is the least
237  *              significant.
238  */
239
240 mp *mp_loadb2c(mp *d, const void *pv, size_t sz)
241 {
242   const octet *ov = pv;
243   MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
244   if (!sz || !(ov[0] & 0x80)) {
245     mpx_loadb(d->v, d->vl, pv, sz);
246     d->f &= ~MP_NEG;
247   } else {
248     mpx_loadb2cn(d->v, d->vl, pv, sz);
249     d->f |= MP_NEG;
250   }
251   d->f &= ~MP_UNDEF;
252   mp_shrink(d);
253   return (d);
254 }
255
256 /* --- @mp_storeb2c@ --- *
257  *
258  * Arguments:   @const mp *m@ = source
259  *              @void *pv@ = pointer to output array
260  *              @size_t sz@ = size of the output array
261  *
262  * Returns:     ---
263  *
264  * Use:         Stores a multiprecision number in an array of octets, as
265  *              two's complement.  The last byte in the array is the least
266  *              significant.  If the array is too small to represent the
267  *              number, high-order bits are truncated; if the array is too
268  *              large, high order bytes are sign-extended.
269  */
270
271 void mp_storeb2c(const mp *m, void *pv, size_t sz)
272 {
273   if (MP_NEGP(m))
274     mpx_storeb2cn(m->v, m->vl, pv, sz);
275   else
276     mpx_storeb(m->v, m->vl, pv, sz);
277 }
278
279 /*----- That's all, folks -------------------------------------------------*/