summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
blob: e9c35d7273de470fb86fd8fddab1b14e1994a231 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/***********************************************************
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* Integer object implementation */

#include "allobjects.h"
#include "modsupport.h"

#ifdef __STDC__
#include <limits.h>
#endif

#ifndef LONG_MAX
#define LONG_MAX 0X7FFFFFFFL
#endif

#ifndef LONG_MIN
#define LONG_MIN (-LONG_MAX-1)
#endif

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

#ifndef LONG_BIT
#define LONG_BIT (CHAR_BIT * sizeof(long))
#endif

/* Standard Booleans */

intobject FalseObject = {
	OB_HEAD_INIT(&Inttype)
	0
};

intobject TrueObject = {
	OB_HEAD_INIT(&Inttype)
	1
};

static object *
err_ovf(msg)
	char *msg;
{
	err_setstr(OverflowError, msg);
	return NULL;
}

/* Integers are quite normal objects, to make object handling uniform.
   (Using odd pointers to represent integers would save much space
   but require extra checks for this special case throughout the code.)
   Since, a typical Python program spends much of its time allocating
   and deallocating integers, these operations should be very fast.
   Therefore we use a dedicated allocation scheme with a much lower
   overhead (in space and time) than straight malloc(): a simple
   dedicated free list, filled when necessary with memory from malloc().
*/

#define BLOCK_SIZE	1000	/* 1K less typical malloc overhead */
#define N_INTOBJECTS	(BLOCK_SIZE / sizeof(intobject))

static intobject *
fill_free_list()
{
	intobject *p, *q;
	p = NEW(intobject, N_INTOBJECTS);
	if (p == NULL)
		return (intobject *)err_nomem();
	q = p + N_INTOBJECTS;
	while (--q > p)
		*(intobject **)q = q-1;
	*(intobject **)q = NULL;
	return p + N_INTOBJECTS - 1;
}

static intobject *free_list = NULL;
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS		100
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS		1
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
   can be shared.
   The integers that are saved are those in the range
   -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
#ifdef COUNT_ALLOCS
int quick_int_allocs, quick_neg_int_allocs;
#endif

object *
newintobject(ival)
	long ival;
{
	register intobject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
	if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
	    (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
		INCREF(v);
#ifdef COUNT_ALLOCS
		if (ival >= 0)
			quick_int_allocs++;
		else
			quick_neg_int_allocs++;
#endif
		return (object *) v;
	}
#endif
	if (free_list == NULL) {
		if ((free_list = fill_free_list()) == NULL)
			return NULL;
	}
	v = free_list;
	free_list = *(intobject **)free_list;
	v->ob_type = &Inttype;
	v->ob_ival = ival;
	NEWREF(v);
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
	if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
		/* save this one for a following allocation */
		INCREF(v);
		small_ints[ival + NSMALLNEGINTS] = v;
	}
#endif
	return (object *) v;
}

static void
int_dealloc(v)
	intobject *v;
{
	*(intobject **)v = free_list;
	free_list = v;
}

long
getintvalue(op)
	register object *op;
{
	if (!is_intobject(op)) {
		err_badcall();
		return -1;
	}
	else
		return ((intobject *)op) -> ob_ival;
}

/* Methods */

/* ARGSUSED */
static int
int_print(v, fp, flags)
	intobject *v;
	FILE *fp;
	int flags; /* Not used but required by interface */
{
	fprintf(fp, "%ld", v->ob_ival);
	return 0;
}

static object *
int_repr(v)
	intobject *v;
{
	char buf[20];
	sprintf(buf, "%ld", v->ob_ival);
	return newstringobject(buf);
}

static int
int_compare(v, w)
	intobject *v, *w;
{
	register long i = v->ob_ival;
	register long j = w->ob_ival;
	return (i < j) ? -1 : (i > j) ? 1 : 0;
}

static long
int_hash(v)
	intobject *v;
{
	long x = v -> ob_ival;
	if (x == -1)
		x = -2;
	return x;
}

static object *
int_add(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b, x;
	a = v->ob_ival;
	b = w->ob_ival;
	x = a + b;
	if ((x^a) < 0 && (x^b) < 0)
		return err_ovf("integer addition");
	return newintobject(x);
}

static object *
int_sub(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b, x;
	a = v->ob_ival;
	b = w->ob_ival;
	x = a - b;
	if ((x^a) < 0 && (x^~b) < 0)
		return err_ovf("integer subtraction");
	return newintobject(x);
}

static object *
int_mul(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	double x;
	a = v->ob_ival;
	b = w->ob_ival;
	x = (double)a * (double)b;
	if (x > LONG_MAX || x < (double) (long) (LONG_MIN))
		return err_ovf("integer multiplication");
	return newintobject(a * b);
}

static int
i_divmod(x, y, p_xdivy, p_xmody)
	register intobject *x, *y;
	long *p_xdivy, *p_xmody;
{
	long xi = x->ob_ival;
	long yi = y->ob_ival;
	long xdivy, xmody;
	
	if (yi == 0) {
		err_setstr(ZeroDivisionError, "integer division or modulo");
		return -1;
	}
	if (yi < 0) {
		if (xi < 0)
			xdivy = -xi / -yi;
		else
			xdivy = - (xi / -yi);
	}
	else {
		if (xi < 0)
			xdivy = - (-xi / yi);
		else
			xdivy = xi / yi;
	}
	xmody = xi - xdivy*yi;
	if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
		xmody += yi;
		xdivy -= 1;
	}
	*p_xdivy = xdivy;
	*p_xmody = xmody;
	return 0;
}

static object *
int_div(x, y)
	intobject *x;
	intobject *y;
{
	long d, m;
	if (i_divmod(x, y, &d, &m) < 0)
		return NULL;
	return newintobject(d);
}

static object *
int_mod(x, y)
	intobject *x;
	intobject *y;
{
	long d, m;
	if (i_divmod(x, y, &d, &m) < 0)
		return NULL;
	return newintobject(m);
}

static object *
int_divmod(x, y)
	intobject *x;
	intobject *y;
{
	object *v, *v0, *v1;
	long d, m;
	if (i_divmod(x, y, &d, &m) < 0)
		return NULL;
	return mkvalue("(ll)", d, m);
}

static object *
int_pow(v, w)
	intobject *v;
	intobject *w;
{
	register long iv, iw, ix;
	iv = v->ob_ival;
	iw = w->ob_ival;
	if (iw < 0) {
		err_setstr(ValueError, "integer to the negative power");
		return NULL;
	}
	ix = 1;
	while (--iw >= 0) {
		long prev = ix;
		ix = ix * iv;
		if (iv == 0)
			break; /* 0 to some power -- avoid ix / 0 */
		if (ix / iv != prev)
			return err_ovf("integer pow()");
	}
	return newintobject(ix);
}

static object *
int_neg(v)
	intobject *v;
{
	register long a, x;
	a = v->ob_ival;
	x = -a;
	if (a < 0 && x < 0)
		return err_ovf("integer negation");
	return newintobject(x);
}

static object *
int_pos(v)
	intobject *v;
{
	INCREF(v);
	return (object *)v;
}

static object *
int_abs(v)
	intobject *v;
{
	if (v->ob_ival >= 0)
		return int_pos(v);
	else
		return int_neg(v);
}

static int
int_nonzero(v)
	intobject *v;
{
	return v->ob_ival != 0;
}

static object *
int_invert(v)
	intobject *v;
{
	return newintobject(~v->ob_ival);
}

static object *
int_lshift(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	if (b < 0) {
		err_setstr(ValueError, "negative shift count");
		return NULL;
	}
	if (a == 0 || b == 0) {
		INCREF(v);
		return (object *) v;
	}
	if (b >= LONG_BIT) {
		return newintobject(0L);
	}
	a = (unsigned long)a << b;
	return newintobject(a);
}

static object *
int_rshift(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	if (b < 0) {
		err_setstr(ValueError, "negative shift count");
		return NULL;
	}
	if (a == 0 || b == 0) {
		INCREF(v);
		return (object *) v;
	}
	if (b >= LONG_BIT) {
		if (a < 0)
			a = -1;
		else
			a = 0;
	}
	else {
		if (a < 0)
			a = ~( ~(unsigned long)a >> b );
		else
			a = (unsigned long)a >> b;
	}
	return newintobject(a);
}

static object *
int_and(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	return newintobject(a & b);
}

static object *
int_xor(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	return newintobject(a ^ b);
}

static object *
int_or(v, w)
	intobject *v;
	intobject *w;
{
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	return newintobject(a | b);
}

static object *
int_int(v)
	intobject *v;
{
	INCREF(v);
	return (object *)v;
}

static object *
int_long(v)
	intobject *v;
{
	return newlongobject((v -> ob_ival));
}

static object *
int_float(v)
	intobject *v;
{
	return newfloatobject((double)(v -> ob_ival));
}

static object *
int_oct(v)
	intobject *v;
{
	char buf[20];
	long x = v -> ob_ival;
	if (x == 0)
		strcpy(buf, "0");
	else if (x > 0)
		sprintf(buf, "0%lo", x);
	else
		sprintf(buf, "-0%lo", -x);
	return newstringobject(buf);
}

static object *
int_hex(v)
	intobject *v;
{
	char buf[20];
	long x = v -> ob_ival;
	if (x >= 0)
		sprintf(buf, "0x%lx", x);
	else
		sprintf(buf, "-0x%lx", -x);
	return newstringobject(buf);
}

static number_methods int_as_number = {
	int_add,	/*nb_add*/
	int_sub,	/*nb_subtract*/
	int_mul,	/*nb_multiply*/
	int_div,	/*nb_divide*/
	int_mod,	/*nb_remainder*/
	int_divmod,	/*nb_divmod*/
	int_pow,	/*nb_power*/
	int_neg,	/*nb_negative*/
	int_pos,	/*nb_positive*/
	int_abs,	/*nb_absolute*/
	int_nonzero,	/*nb_nonzero*/
	int_invert,	/*nb_invert*/
	int_lshift,	/*nb_lshift*/
	int_rshift,	/*nb_rshift*/
	int_and,	/*nb_and*/
	int_xor,	/*nb_xor*/
	int_or,		/*nb_or*/
	0,		/*nb_coerce*/
	int_int,	/*nb_int*/
	int_long,	/*nb_long*/
	int_float,	/*nb_float*/
	int_oct,	/*nb_oct*/
	int_hex,	/*nb_hex*/
};

typeobject Inttype = {
	OB_HEAD_INIT(&Typetype)
	0,
	"int",
	sizeof(intobject),
	0,
	int_dealloc,	/*tp_dealloc*/
	int_print,	/*tp_print*/
	0,		/*tp_getattr*/
	0,		/*tp_setattr*/
	int_compare,	/*tp_compare*/
	int_repr,	/*tp_repr*/
	&int_as_number,	/*tp_as_number*/
	0,		/*tp_as_sequence*/
	0,		/*tp_as_mapping*/
	int_hash,	/*tp_hash*/
};