summaryrefslogtreecommitdiff
path: root/MLKInteger.m
blob: b2ce0a812380a9718269cba8374e977059a4ee3f (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* -*- mode: objc; coding: utf-8 -*- */
/* Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime.
 * Copyright (C) 2008  Matthias Andreas Benkard.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "MLKDynamicContext.h"
#import "MLKInteger.h"
#import "MLKPackage.h"
#import "runtime-compatibility.h"
#import "util.h"

#import <Foundation/NSString.h>

@implementation MLKInteger
-(MLKInteger *) initWithMPZ:(mpz_t)mpz
{
  self = [super init];
  mpz_init_set (value, mpz);
  return self;
}

-(MLKInteger *) initWithInt:(int)intValue
{
  self = [super init];
  mpz_init_set_si (value, intValue);
  return self;
}

-(MLKInteger *) initWithIntptr_t:(intptr_t)intptr_t_value
{
  self = [super init];
  mpz_init_set_si (value, intptr_t_value);
  return self;
}

-(MLKInteger *) initWithFixnum:(id)fixnum
{
  self = [self initWithIntptr_t:MLKIntWithFixnum(fixnum)];
  return self;
}

-(MLKInteger *) initWithString:(NSString *)string
                      negative:(BOOL)negative
                          base:(unsigned int)base
{
  self = [super init];
  mpz_init_set_str (value, [string UTF8String], base);
  if (negative) mpz_neg (value, value);    
  return self;
}

+(MLKInteger *) integerWithMPZ:(mpz_t)mpz
{
  return AUTORELEASE ([[MLKInteger alloc] initWithMPZ:mpz]);
}

+(MLKInteger *) integerWithString:(NSString *)string
                         negative:(BOOL)negative
                             base:(unsigned int)base
{
  return AUTORELEASE ([[MLKInteger alloc] initWithString:string
                                          negative:negative
                                          base:base]);
}

+(MLKInteger *) integerWithInt:(int)intValue
{
  return AUTORELEASE ([[MLKInteger alloc] initWithInt:intValue]);
}

+(MLKInteger *) integerWithIntptr_t:(intptr_t)intptr_t_value
{
  return AUTORELEASE ([[MLKInteger alloc] initWithIntptr_t:intptr_t_value]);
}

+(MLKInteger *) integerWithFixnum:(id)fixnum
{
  return AUTORELEASE ([[MLKInteger alloc] initWithFixnum:fixnum]);  
}


#define DEFINE_MPZ_TWOARG_OPERATION(SELECTOR, GMPFUN)                           \
  DEFINE_GMP_OPERATION (SELECTOR (MLKNumber *)arg,                              \
                        mpz,                                                    \
                        GMPFUN (mpval, self->value, ((MLKInteger*)arg)->value), \
                        MLKNumber,                                              \
                        MLKInteger,                                             \
                        integerWithMPZ:)

DEFINE_MPZ_TWOARG_OPERATION (add:, mpz_add)
DEFINE_MPZ_TWOARG_OPERATION (subtract:, mpz_sub)
DEFINE_MPZ_TWOARG_OPERATION (multiplyWith:, mpz_mul)
DEFINE_MPZ_TWOARG_OPERATION (divideBy:, mpz_div)


#define DEFINE_MPZ_TWOARG_INTONLY_OPERATION(SELECTOR, GMPFUN)                   \
  DEFINE_GMP_OPERATION (SELECTOR (MLKInteger *)arg,                             \
                        mpz,                                                    \
                        GMPFUN (mpval, self->value, ((MLKInteger*)arg)->value), \
                        MLKInteger,                                             \
                        MLKInteger,                                             \
                        integerWithMPZ:)

DEFINE_MPZ_TWOARG_INTONLY_OPERATION (mod:, mpz_mod)
DEFINE_MPZ_TWOARG_INTONLY_OPERATION (exactlyDivideBy:, mpz_divexact)
DEFINE_MPZ_TWOARG_INTONLY_OPERATION (gcd:, mpz_gcd)
DEFINE_MPZ_TWOARG_INTONLY_OPERATION (lcm:, mpz_lcm)


-(MLKInteger *) pow:(MLKInteger *)exponent
{
  mpz_t mpz;
  mpz_t i;

  mpz_init_set_ui (mpz, 1);
  mpz_init_set (i, exponent->value);

  while (mpz_sgn (i) > 0)
    {
      mpz_mul (mpz, mpz, self->value);
      mpz_sub_ui (i, i, 1);
    }

  MLKInteger *obj = [MLKInteger integerWithMPZ:mpz];
  mpz_clear (mpz);
  mpz_clear (i);

  return obj;
}

-(BOOL) evenp
{
  return mpz_even_p (self->value);
}

-(BOOL) oddp
{
  return mpz_odd_p (self->value);
}

-(MLKInteger *) isqrt
{
  mpz_t mpz;

  mpz_init (mpz);
  mpz_sqrt (mpz, self->value);
  MLKInteger *obj = [MLKInteger integerWithMPZ:mpz];
  mpz_clear (mpz);

  return obj;
}

-(BOOL) fitsIntoFixnum
{
#ifdef NO_FIXNUMS
  return NO;
#else
  return (mpz_sizeinbase (self->value, 2) <= (sizeof (id)) * 8 - 2);
#endif
}

-(id) fixnumValue
{
#ifdef NO_FIXNUMS
  return self;
#else
  return MLKFixnumWithInt ([self intValue]);
#endif
}

-(int) intValue
{
  return mpz_get_si (value);
}

-(double) doubleValue
{
  return mpz_get_d (value);
}

-(NSComparisonResult) compare:(MLKInteger *)arg
{
  int cmp = mpz_cmp (self->value, arg->value);

  if (cmp == 0)
    return NSOrderedSame;
  else if (cmp < 0)
    return NSOrderedAscending;
  else
    return NSOrderedDescending;
}

-(BOOL) isEqual:(id)arg
{
  return ([arg isKindOfClass:[MLKInteger class]]
          && [self compare:arg] == 0);
}

-(NSString *) description
{
  return [self descriptionWithBase:10];
}

-(NSString *) descriptionWithBase:(int)base
{
  NSString *str;
  char cstr[mpz_sizeinbase (self->value, base) + 2];

  mpz_get_str (cstr, base, self->value);
  str = [NSString stringWithUTF8String:cstr];
  
  return str;
}

-(NSString *) descriptionForLisp
{
  MLKInteger *base = [[MLKDynamicContext currentContext]
                       valueForSymbol:[[MLKPackage
                                          findPackage:@"COMMON-LISP"]
                                         intern:@"*PRINT-BASE*"]];
  return [self descriptionWithBase:[base intValue]];
}

-(void) dealloc
{
  mpz_clear (value);
  [super dealloc];
}
@end