[e90b2f]: src / c / big_ll.d Maximize Restore History

Download this file

big_ll.d    170 lines (149 with data), 4.1 kB

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