4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "st-large-integer.h"
26 #include "st-universe.h"
30 #define VALUE(oop) (&(ST_LARGE_INTEGER(oop)->value))
33 st_large_integer_new_from_string (const char *string
, st_uint radix
)
38 st_assert (string
!= NULL
);
40 result
= mp_init (&value
);
41 if (result
!= MP_OKAY
)
44 result
= mp_read_radix (&value
, string
, radix
);
45 if (result
!= MP_OKAY
)
48 return st_large_integer_new (&value
);
52 fprintf (stderr
, mp_error_to_string (result
));
57 st_large_integer_to_string (st_oop integer
, st_uint radix
)
62 result
= mp_radix_size (VALUE (integer
), radix
, &size
);
63 if (result
!= MP_OKAY
)
66 char *str
= st_malloc (size
);
68 mp_toradix (VALUE (integer
), str
, radix
);
69 if (result
!= MP_OKAY
)
75 fprintf (stderr
, mp_error_to_string (result
));
80 allocate_with_value (st_space
*space
, st_oop
class, mp_int
* value
)
84 object
= st_space_allocate_object (space
, class, sizeof (struct st_large_integer
) / sizeof (st_oop
));
87 *VALUE (object
) = *value
;
89 mp_init (VALUE (object
));
95 st_large_integer_new (mp_int
* value
)
97 return allocate_with_value (memory
->moving_space
, st_large_integer_class
, value
);
102 allocate (st_space
*space
, st_oop
class)
104 return allocate_with_value (space
, class, NULL
);
108 large_integer_copy (st_oop object
)
113 result
= mp_init_copy (&value
, VALUE (object
));
114 if (result
!= MP_OKAY
)
115 st_assert_not_reached ();
117 return st_large_integer_new (&value
);
121 large_integer_size (st_oop object
)
123 return (sizeof (struct st_large_integer
) / sizeof (st_oop
));
127 large_integer_contents (st_oop object
, struct contents
*contents
)
129 contents
->oops
= NULL
;
134 st_large_integer_descriptor (void)
136 static st_descriptor __descriptor
=
137 { .allocate
= allocate
,
138 .allocate_arrayed
= NULL
,
139 .copy
= large_integer_copy
,
140 .size
= large_integer_size
,
141 .contents
= large_integer_contents
,
144 return & __descriptor
;