Debian Codegraph @

src/c/big_ll.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * big_ll.c - bignum emulation with long long
6 *
7 * Copyright (c) 2005 Maciek Pasternacki
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 #include <ecl/ecl.h>
14 #include <ecl/internal.h>
15
16 void
17 _ecl_big_register_free(cl_object x) {}
18
19 cl_object
20 _ecl_big_register_copy(cl_object old)
21 {
22 cl_object new = ecl_alloc_object(t_bignum);
23 ecl_bignum(new) = ecl_bignum(old);
24 return new;
25 }
26
27 static cl_object
28 big_normalize(cl_object x)
29 {
30 if (ecl_bignum(x) == 0ll)
31 return(ecl_make_fixnum(0));
32 if (ECL_NEGATIVE_FIXNUM <= ecl_bignum(x) && ecl_bignum(x) <= MOST_POSITIVE_FIXNUM)
33 return(ecl_make_fixnum(ecl_bignum(x)));
34 return x;
35 }
36
37 cl_object
38 _ecl_big_register_normalize(cl_object x)
39 {
40 if (ecl_bignum(x) == 0ll)
41 return(ecl_make_fixnum(0));
42 if (ECL_NEGATIVE_FIXNUM <= ecl_bignum(x) && ecl_bignum(x) <= MOST_POSITIVE_FIXNUM)
43 return(ecl_make_fixnum(ecl_bignum(x)));
44 return _ecl_big_register_copy(x);
45 }
46
47 static cl_object
48 big_alloc(int size)
49 {
50 volatile cl_object x = ecl_alloc_object(t_bignum);
51 if (size <= 0)
52 ecl_internal_error("negative or zero size for bignum in big_alloc");
53 ecl_bignum(x) = 0ll;
54 return x;
55 }
56
57 static cl_object
58 _ecl_big_copy(cl_object x)
59 {
60 volatile cl_object y = ecl_alloc_object(t_bignum);
61 ecl_bignum(y) = ecl_bignum(x);
62 return y;
63 }
64
65 cl_object
66 _ecl_big_gcd(cl_object x, cl_object y)
67 {
68 big_num_t i = ecl_bignum(x), j = ecl_bignum(y);
69 cl_object gcd = ecl_alloc_object(t_bignum);
70 while ( 1 ) {
71 big_num_t k;
72 if ( i<j ) {
73 k = i;
74 i = j;
75 j = k;
76 }
77 if ( j == 0 ) {
78 ecl_bignum(gcd) = k;
79 return gcd;
80 }
81 k = i % j;
82 i = j;
83 j = k;
84 }
85 }
86
87 int
88 _ecl_big_num_t_sgn(big_num_t x)
89 {
90 return ( x == (big_num_t)0 ) ? 0 : (x < (big_num_t)0) ? -1 : 1;
91 }
92
93 cl_object
94 _ecl_big_times_big(cl_object x, cl_object y)
95 {
96 cl_object z = ecl_alloc_object(t_bignum);
97 ecl_bignum(z) = ecl_bignum(x) * ecl_bignum(y);
98 return z;
99 }
100
101 cl_object
102 _ecl_big_times_fix(cl_object x, cl_fixnum y)
103 {
104 cl_object z = ecl_alloc_object(t_bignum);
105 ecl_bignum(z) = ecl_bignum(x) * y;
106 return big_normalize(z);
107 }
108
109 cl_object
110 _ecl_big_plus_big(cl_object x, cl_object y)
111 {
112 cl_object z = ecl_alloc_object(t_bignum);
113 ecl_bignum(z) = ecl_bignum(x) + ecl_bignum(y);
114 return z;
115 }
116
117 cl_object
118 _ecl_big_plus_fix(cl_object x, cl_fixnum y)
119 {
120 cl_object z = ecl_alloc_object(t_bignum);
121 ecl_bignum(z) = ecl_bignum(x) + y;
122 return big_normalize(z);
123 }
124
125 cl_object
126 _ecl_fix_times_fix(cl_fixnum x, cl_fixnum y)
127 {
128 cl_object z = ecl_alloc_object(t_bignum);
129 ecl_bignum(z) = x * y;
130 return big_normalize(z);
131 }
132
133 cl_object
134 _ecl_big_ceiling(cl_object a, cl_object b, cl_object *pr)
135 {
136 cl_object q = ecl_alloc_object(t_bignum);
137 cl_object r = ecl_alloc_object(t_bignum);
138 ecl_bignum(q) = ecl_bignum(x) / ecl_bignum(y);
139 ecl_bignum(r) = ecl_bignum(x) % ecl_bignum(y);
140 *pr = big_normalize(r);
141 return big_normalize(q);
142 }
143
144 cl_object
145 _ecl_big_floor(cl_object a, cl_object b, cl_object *pr)
146 {
147 cl_object q = ecl_alloc_object(t_bignum);
148 cl_object r = ecl_alloc_object(t_bignum);
149 ecl_bignum(q) = ecl_bignum(x) / ecl_bignum(y);
150 ecl_bignum(r) = ecl_bignum(x) % ecl_bignum(y);
151 *pr = big_normalize(r);
152 return big_normalize(q);
153 }
154
155 cl_object
156 _ecl_big_negate(cl_object x)
157 {
158 cl_object z = ecl_alloc_object(t_bignum);
159 ecl_bignum(z) = - ecl_bignum(x);
160 return z;
161 }
162
163 void
164 init_big(void)
165 {
166 }