File: pgint8object.c

package info (click to toggle)
python-pgsql 2.4.0-5sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 1,291
  • sloc: ansic: 5,913; python: 4,334; sh: 73; makefile: 71
file content (1188 lines) | stat: -rw-r--r-- 29,538 bytes parent folder | download
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
#ident "@(#) $Id: pgint8object.c,v 1.14 2002/10/02 11:32:07 ghaering Exp $"
/* vi:set sw=4 ts=8 showmode ai: */
/**(H+)*****************************************************************\
| Name:		pgint8object.c						|
|									|
| Description:	This file implements the PostgreSQL int8 type for	|
|		Pyhon.  This Python object is part of the libpq module.	|
|									|
| Note:		This type object is based on ideas and concepts in the	|
|		Python integer and Python long type object.		|
|=======================================================================|
| Copyright 2001 by Billy G. Allie.					|
| All rights reserved.							|
|									|
| Permission to use, copy, modify, and distribute this software and its	|
| documentation for any purpose and without fee is hereby granted, pro-	|
| vided that the above copyright notice appear in all copies and that	|
| both that copyright notice and this permission notice appear in sup-	|
| porting documentation, and that the copyright owner's name not be	|
| used in advertising or publicity pertaining to distribution of the	|
| software without specific, written prior permission.			|
|									|
| THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,	|
| INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN	|
| NO EVENT SHALL THE AUTHOR(S) 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.					|
|=======================================================================|
| People who have worked on this code.					|
|									|
| Ini Name								|
| --- ----------------------------------------------------------------- |
|  gh Gerhard Haering <gerhard.haering@gmx.de>				|
| bga Billy G. Allie <bill.allie@mug.org>				|
|=======================================================================|
| Revision History:							|
|									|
| Date      Ini Description						|
| --------- --- ------------------------------------------------------- |
| 02OCT2002 gh  LONG_LONG_MAX => LLONG_MAX				|
| 01OCT2002 gh  HAVE_LONG_LONG => HAVE_LONG_LONG_SUPPORT		|
| 16SEP2002 gh  Reflect the change from windows/ to port/.		|
| 01OCT2001 bga Changed all new style comments to original style.	|
| 26SEP2001 bga Change the constructors so that they return PyObject *	|
|		instead of PgInt8Object *.				|
| 03SEP2001 gh	Modified code for 64bit (long long) support in the MS	|
|		Windows environment with MS Visual C++.			|
|	    bga Made changes to avoid use of long long constants.  This	|
|		was done to assist in the use of MS Visual C++, which	|
|		uses something other than LL to specify long long con-	|
|		tants. (It's ugly, I know.  Thanks MS.)			|
| 30AUG2001 bga Use PyObject_Del() instead of PyMem_Free() in dealloc()	|
|		to delete the object.					|
| 11AUG2001 bga Change code to use Py_BuildValue() for creating Python	|
|		objects from C values (where possible).			|
|	    --- General code clean up.  Removed some definitions that	|
|		are in the new libpqmodule.h header file.		|
| 03AUG2001 bga Changed 'PyMem_DEL()' to 'PyMem_Free()'.		|
| 14JUN2001 bga Change #if defined(LONG_LONG) to reference the macro	|
|		HAVE_LONG_LONG.						|
| 09JUN2001 bga This file did not build under Python 2.0.  It does now.	|
| 06JUN2001 bga To the extent possible, I picked the "lint" from the	|
|		code.							|
| 04JUN2001 bga Changed PyObject_HEAD_INIT(&PyType_Type) to		|
|		PyObject_HEAD_INIT(NULL) to silence error produced by	|
|		some compilers.						|
| 31MAY2001 bga Initial release by Billy G. Allie.			|
\*(H-)******************************************************************/

#include <ctype.h>
#include "Python.h"

#include "libpqmodule.h"

#if defined(HAVE_LONG_LONG_SUPPORT)

static PyObject *err_ovf(char *msg)
{
    PyErr_SetString(PyExc_OverflowError, msg);
    return NULL;
}

static void int8_dealloc(PgInt8Object *v)
{
    PyObject_Del(v);
}

PY_LONG_LONG PgInt8_GetMax(void)
{
    return LLONG_MAX;
}

PY_LONG_LONG PgInt8_AsLongLong(register PgInt8Object *op)
{
    if (op && PgInt8_Check(op))
	return PgInt8_AS_LONGLONG((PgInt8Object *)op);
	
    PyErr_SetString(PyExc_TypeError, "a PgInt8 is required");
    return (PY_LONG_LONG)-1;
}

long PgInt8_AsLong(register PgInt8Object *op)
{
    PY_LONG_LONG val;
    long ival;
	
    if (op && PgInt8_Check(op))
    {
        val = PgInt8_AS_LONGLONG(op);
        ival = (long)val;
        if ((PY_LONG_LONG)ival != val)
        {
            PyErr_SetString(PyExc_OverflowError,
                            "PgInt8 too large to convert");
            return -1L;
        }
        return ival;
    }
	
    PyErr_SetString(PyExc_TypeError, "a PgInt8 is required");
    return -1L;
}

PyObject *PgInt8_FromLongLong(PY_LONG_LONG ival)
{
    register PyObject *v;

    if ((v = (PyObject *)PyObject_New(PgInt8Object, &PgInt8_Type)) ==
	(PyObject *)NULL)
    {
	return v;
    }
    ((PgInt8Object *)v)->ob_ival = ival;

    return v;
}

PyObject *PgInt8_FromLong(long ival)
{
    register PyObject *v;

    if ((v = (PyObject *)PyObject_New(PgInt8Object, &PgInt8_Type)) ==
	(PyObject *)NULL)
    {
	return v;
    }
    ((PgInt8Object *)v)->ob_ival = (PY_LONG_LONG)ival;

    return v;
}

PyObject *PgInt8_FromString(char *s, char **pend, int base)
{
    char *end;
    PY_LONG_LONG x;
    char buffer[256]; /* For errors */

    if ((base != 0 && base < 2) || base > 36)
    {
	PyErr_SetString(PyExc_ValueError,
			"PgInt8() base must be >= 2 and <= 36");
	return NULL;
    }

    while (*s && isspace(Py_CHARMASK((unsigned int)(*s))))
	s++;
    errno = 0;
    if (base == 0 && s[0] == '0')
	x = pg_strtoull(s, &end, base);
    else
	x = pg_strtoll(s, &end, base);
    if (end == s || !isalnum(Py_CHARMASK((unsigned int)(end[-1]))))
	goto bad;
    while (*end && isspace(Py_CHARMASK((unsigned int)(*end))))
	end++;
    if (*end != '\0') {
bad:
	sprintf(buffer, "invalid literal for PgInt8(): %.200s", s);
	PyErr_SetString(PyExc_ValueError, buffer);
	return (PyObject *)NULL;
    }
    else if (errno != 0) {
	sprintf(buffer, "PgInt8() literal too large: %.200s", s);
	PyErr_SetString(PyExc_ValueError, buffer);
	return (PyObject *)NULL;
    }
    if (pend)
	*pend = end;
    return PgInt8_FromLongLong(x);
}

PyObject *PgInt8_FromUnicode(Py_UNICODE *s, int length, int base)
{
    char buffer[256];
    
    if (length >= sizeof(buffer)) {
	PyErr_SetString(PyExc_ValueError,
			"int() literal too large to convert");
	return (PyObject *)NULL;
    }
    if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
	return (PyObject *)NULL;

    return PgInt8_FromString(buffer, NULL, base);
}

static int convert_binop(PyObject *v, PyObject *w, PY_LONG_LONG *a, PY_LONG_LONG *b)
 {
     if (PgInt8_Check(v))
     { 
         *a = PgInt8_AS_LONGLONG(v);
     }
     else if (PyLong_Check(v))
     { 
         *a = PyLong_AsLongLong(v);

         if (*a == -1 && PyErr_Occurred())
             return 0;
     }
     else if (PyInt_Check(v)) {
         *a = (PY_LONG_LONG)(PyInt_AS_LONG(v));
     }
     else
     {
         return 0;
     }

     if (w == Py_None)
         return 1;
     else if (PgInt8_Check(w))
     { 
         *b = PgInt8_AS_LONGLONG(w);
     }
     else if (PyLong_Check(w))
     {
         *b = PyLong_AsLongLong(w);

         if (*b == -1 && PyErr_Occurred())
             return 0;
     }
     else if (PyInt_Check(w))
     {
         *b = (PY_LONG_LONG)(PyInt_AS_LONG(w));
     }
     else
     {
         return 0;
     }

     return 1;
}

#if (PY_VERSION_HEX >= 0x02010000)
#define CONVERT_BINOP(v, w, a, b) \
	if (!convert_binop(v, w, a, b)) { \
		Py_INCREF(Py_NotImplemented); \
		return Py_NotImplemented; \
	}

#define PY_NOT_IMPLEMENTED \
	Py_INCREF(Py_NotImplemented); \
	return Py_NotImplemented;
#else
#define CONVERT_BINOP(v, w, a, b) \
	if (!convert_binop(v, w, a, b)) { \
	    PyErr_SetString(PyExc_NotImplementedError, \
			    "operation not implemented for PgInt8 type"); \
	    return NULL; \
	}

#define PY_NOT_IMPLEMENTED \
	PyErr_SetString(PyExc_NotImplementedError, \
			"operation not implemented for PgInt8 type"); \
	return NULL; \

#define Py_TPFLAGS_CHECKTYPES 0
#endif

#define CONVERT_TO_LONGLONG(v, a) \
		CONVERT_BINOP(v, Py_None, a, (PY_LONG_LONG *)NULL)

/* Methods */

/* ARGSUSED */
static int int8_print(PgInt8Object *v, FILE *fp, int flags)
     /* flags -- not used but required by interface */
{
    fprintf(fp, "%lld", PgInt8_AS_LONGLONG(v));
    return 0;
}

static PyObject *int8_repr(PgInt8Object *v)
{
    char buf[32];
    sprintf(buf, "%lld", PgInt8_AS_LONGLONG(v));
    return Py_BuildValue("s", buf);
}

static int int8_compare(PgInt8Object *v, PgInt8Object *w)
{
    register PY_LONG_LONG i = PgInt8_AS_LONGLONG(v);
    register PY_LONG_LONG j = PgInt8_AS_LONGLONG(w);
    
    return (i < j) ? -1 : (i > j) ? 1 : 0;
}

static long int8_hash(PgInt8Object *v)
{
    PY_LONG_LONG x = PgInt8_AS_LONGLONG(v);

    /* Have PgInt8s has to the same value as Ints for the same value */
    if (!(((PY_LONG_LONG)LONG_MIN < x) && (x <= (PY_LONG_LONG)LONG_MAX)))
    {
	x = (((unsigned PY_LONG_LONG)x >> 32U) + (unsigned PY_LONG_LONG)x) &
	     0x7fffffffU;
    }

    if (x == (PY_LONG_LONG)-1)
	x = (PY_LONG_LONG)-2;

    return (long)x;
}

static int int8_coerce(PyObject **pv, PyObject **pw)
{
    if (PgInt8_Check(*pv))
    {
	if (PyInt_Check(*pw))
	{
	    *pw = (PyObject *)PgInt8_FromLong(PyInt_AS_LONG(*pw));
	    Py_INCREF(*pv);
	}
	else if (PyLong_Check(*pw))
	{
	    *pv = (PyObject *)PyLong_FromLongLong(PgInt8_AS_LONGLONG(*pv));
	    Py_INCREF(*pw);
	}
	else if (PyFloat_Check(*pw))
	{
	  *pv = (PyObject *)PyFloat_FromDouble((double)PgInt8_AS_LONGLONG(*pv));
	    Py_INCREF(*pw);
	}
	else if (PyComplex_Check(*pw))
	{
	    *pv = (PyObject *)PyComplex_FromDoubles(
				    (double)PgInt8_AS_LONGLONG(*pv), (double)0);
	    Py_INCREF(*pw);
	}
	else
	    return 1;

	return 0;
    }
    else if (PgInt8_Check(*pw)) {
	if (PyInt_Check(*pv))
	{
	    *pv = (PyObject *)PgInt8_FromLong(PyInt_AS_LONG(*pv));
	    Py_INCREF(*pv);
	}
	else if (PyLong_Check(*pv))
	{
	    *pw = (PyObject *)PyLong_FromLongLong(PgInt8_AS_LONGLONG(*pw));
	    Py_INCREF(*pw);
	}
	else if (PyFloat_Check(*pv))
	{
	  *pw = Py_BuildValue("d", (double)PgInt8_AS_LONGLONG(*pw));
	    Py_INCREF(*pw);
	}
	else if (PyComplex_Check(*pv))
	{
	    *pw = (PyObject *)PyComplex_FromDoubles(
				    (double)PgInt8_AS_LONGLONG(*pw), (double)0);
	    Py_INCREF(*pv);
	}
	else
	    return 1;

	return 0;
    }

    return 1; /* Can't do it */
}

static PyObject *int8_add(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b, x;
    PyObject *v1 = v, *w1 =w;

    if (!(PgInt8_Check(v) && PgInt8_Check(w)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the add method of the	*/
	/* type that they were coerced into.				*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_add)
		return (PyObject *)(*nb->nb_add)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    /* We are adding two PgInt8s. */

    CONVERT_BINOP(v1, w1, &a, &b);

    x = a + b;

    if ((((unsigned PY_LONG_LONG)x ^ (unsigned PY_LONG_LONG)a) < 0) &&
	(((unsigned PY_LONG_LONG)x ^ (unsigned PY_LONG_LONG)b) < 0))
        return err_ovf("PgInt8 addition");

    return (PyObject *)PgInt8_FromLongLong(x);
}

static PyObject *int8_sub(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b, x;
    PyObject *v1 = v, *w1 = w;

    if (!(PgInt8_Check(v) && PgInt8_Check(w)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the subtract method of	*/
	/* the type that they were coerced into.			*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_subtract)
		return (PyObject *)(*nb->nb_subtract)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    /* We are subtracting two PgInt8s. */
    
    CONVERT_BINOP(v1, w1, &a, &b);
    
    x = a - b;

    if ((((unsigned PY_LONG_LONG)x ^ (unsigned PY_LONG_LONG)a) < 0) &&
	(((unsigned PY_LONG_LONG)x ^ (unsigned PY_LONG_LONG)b) < 0))
	return err_ovf("PgInt8 subtraction");

    return (PyObject *)PgInt8_FromLongLong(x);
}

static PyObject *PgInt8_repeat(PyObject *v, PyObject *w)
{
    /* sequence * long */
    long n = PgInt8_AsLong((PgInt8Object *)w);

    if (n == -1 && PyErr_Occurred())
        return NULL;
    else
        return (PyObject *)(*v->ob_type->tp_as_sequence->sq_repeat)(v, n);
}

/*
Integer overflow checking used to be done using a double, but on 64
bit machines (where both long and double are 64 bit) this fails
because the double doesn't have enough precision.  John Tromp suggests
the following algorithm:

Suppose again we normalize a and b to be nonnegative.
Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
Now we test ah and bh against zero and get essentially 3 possible outcomes.

1) both ah and bh > 0 : then report overflow

2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
                        negative

3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
                        compute al*bl and report overflow if it's negative
                        add (ah*bl) << 32 to al*bl and report overflow if
                        it's negative

In case of no overflow the result is then negated if necessary.

The majority of cases will be 2), in which case this method is the same as
what I suggested before. If multiplication is expensive enough, then the
other method is faster on case 3), but also more work to program, so I
guess the above is the preferred solution.

*/

static PyObject *int8_mul(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b, ah, bh, x, y;
    int s = 1;
    PyObject *v1 = v, *w1 = w;

    if (v->ob_type->tp_as_sequence &&
        v->ob_type->tp_as_sequence->sq_repeat) {
        return PgInt8_repeat(v, w);
    }
    else if (w->ob_type->tp_as_sequence &&
             w->ob_type->tp_as_sequence->sq_repeat) {
        return PgInt8_repeat(w, v);
    }

    if (!(PgInt8_Check(v) && PgInt8_Check(w)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the multiply method of	*/
	/* the type that they were coerced into.			*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_subtract)
		return (PyObject *)(*nb->nb_multiply)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    CONVERT_BINOP(v1, w1, &a, &b);

    ah = (unsigned PY_LONG_LONG)a >> (unsigned int)(LONG_LONG_BIT / 2);
    bh = (unsigned PY_LONG_LONG)b >> (unsigned int)(LONG_LONG_BIT / 2);

    /* Quick test for common case: two small positive ints */

    if (ah == 0 && bh == 0) {
	x = a * b;
	if (x < 0)
	    goto bad;
	return (PyObject *)PgInt8_FromLongLong(x);
    }

    /* Arrange that a >= b >= 0 */

    if (a < 0) {
	a = -a;
	if (a < 0) {
	    /* Largest negative */
	    if (b == 0 || b == 1) {
		x = a * b;
		goto ok;
	    }
	    else
		goto bad;
	}
	s = -s;
	ah = (unsigned PY_LONG_LONG)a >> (unsigned int)(LONG_LONG_BIT / 2);
    }
    if (b < 0) {
	b = -b;
	if (b < 0) {
	    /* Largest negative */
	    if (a == 0 || (a == 1 && s == 1)) {
		x = a * b;
		goto ok;
	    }
	    else
		goto bad;
	}
	s = -s;
	bh = (unsigned PY_LONG_LONG)b >> (unsigned int)(LONG_LONG_BIT / 2);
    }

    /* 1) both ah and bh > 0 : then report overflow */

    if (ah != 0 && bh != 0)
	goto bad;

    /* 2) both ah and bh = 0 : then compute a*b and report
			       overflow if it comes out negative */

    if (ah == 0 && bh == 0) {
	x = a * b;
	if (x < 0)
	    goto bad;
	return (PyObject *)PgInt8_FromLongLong(x * s);
    }

    /* Note: At this point one of ah or bh is equal to zero (but not both)
	     From step 1: !(ah != 0 && bh != 0) => (ah == 0 || bh == 0)
	     From step 2: !(ah == 0 && bh == 0) => (ah != 0 || bh != 0)
	     Taken together (ah == 0 || bh == 0) && (ah != 0 || bh != 0),
	     which can only be satisfied by only one of ah or bh being 0. */

    if (a < b) {
	/* Swap */
	x = a;
	a = b;
	b = x;
	ah = bh;
	/* bh is not needed anymore */
    }

    /* 3) ah > 0 and bh = 0: compute ah*bl and report overflow if it's >= 2^31
			     compute al*bl and report overflow if it's negative
			     add (ah*bl)<<32 to al*bl and report overflow if
			     it's negative (NB b == bl in this case, and we
			     make a = al) */

    y = ah * b; /* since bh = 0, b := bl */

    if (y >= ((unsigned PY_LONG_LONG)1 << (unsigned)((LONG_LONG_BIT / 2) - 1)))
	goto bad;

    a = (unsigned PY_LONG_LONG)a &
	(((PY_LONG_LONG)1 << (unsigned)(LONG_LONG_BIT / 2)) - 1);
    x = a * b;
    if (x < 0)
	goto bad;
    x += (unsigned PY_LONG_LONG)y << (unsigned int)(LONG_LONG_BIT / 2);
    if (x < 0)
	goto bad;
ok:
    return (PyObject *)PgInt8_FromLongLong(x * s);

bad:
    return err_ovf("PgInt8 multiplication");
}

static int i_divmod(register PY_LONG_LONG xi, register PY_LONG_LONG yi,
		    PY_LONG_LONG *p_xdivy, PY_LONG_LONG *p_xmody)
{
    PY_LONG_LONG xdivy, xmody;
    
    if (yi == 0) {
	PyErr_SetString(PyExc_ZeroDivisionError,
			"PgInt8 division or modulo by zero");
	return -1;
    }
    if (yi < 0) {
	if (xi < 0) {
	    if (yi == -1 && -xi < 0) {
		/* most negative / -1 */
		err_ovf("PgInt8 division");
		return -1;
	    }
	    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 PyObject *int8_div(PyObject *x, PyObject *y)
{
    PY_LONG_LONG xi, yi;
    PY_LONG_LONG d, m;
    PyObject *v1 = x, *w1 = y;

    if (!(PgInt8_Check(x) && PgInt8_Check(y)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the divide method of the	*/
	/* type that they were coerced into.				*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_subtract)
		return (PyObject *)(*nb->nb_divide)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    CONVERT_BINOP(v1, w1, &xi, &yi);

    if (i_divmod(xi, yi, &d, &m) < 0)
	return NULL;

    return (PyObject *)PgInt8_FromLongLong(d);
}

static PyObject *int8_mod(PyObject *x, PyObject *y)
{
    PY_LONG_LONG xi, yi;
    PY_LONG_LONG d, m;
    PyObject *v1 = x, *w1 = y;

    if (!(PgInt8_Check(x) && PgInt8_Check(y)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the remainder method of	*/
	/* the type that they were coerced into.			*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_subtract)
		return (PyObject *)(*nb->nb_remainder)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    CONVERT_BINOP(v1, w1, &xi, &yi);

    if (i_divmod(xi, yi, &d, &m) < 0)
	return NULL;

    return (PyObject *)PgInt8_FromLongLong(m);
}

static PyObject *int8_divmod(PyObject *x, PyObject *y)
{
    PY_LONG_LONG xi, yi;
    PY_LONG_LONG d, m;
    PyObject *v1 = x, *w1 = y;

    if (!(PgInt8_Check(x) && PgInt8_Check(y)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the divmod method of the	*/
	/* type that they were coerced into.				*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_subtract)
		return (PyObject *)(*nb->nb_divmod)(v1, w1);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    CONVERT_BINOP(v1, w1, &xi, &yi);

    if (i_divmod(xi, yi, &d, &m) < 0)
	return NULL;

    return Py_BuildValue("(oo)", PgInt8_FromLongLong(d),
                         	 PgInt8_FromLongLong(m));
}

static PyObject *int8_pow(PyObject *v, PyObject *w, PyObject *z)
{
#if 1
    PY_LONG_LONG iv, iw, iz=0, ix, temp, prev;
    PyObject *v1 = v, *w1 =w;

    if (!(PgInt8_Check(v) && PgInt8_Check(w)))
    {
	/* Ok, we are subtracting mixed mode numbers, first we coerce	*/
	/* the numbers to the same type.				*/

	if (int8_coerce(&v1, &w1))
	{
	    PY_NOT_IMPLEMENTED
	}

	/* Now that the numbers are the same type, we check to see if	*/
	/* they are PgInt8s.  If not, we call the power method of the	*/
	/* type that they were coerced into.				*/

	if (!PgInt8_Check(v1))
	{
	    PyNumberMethods *nb;

	    nb = v1->ob_type->tp_as_number;
	    if (nb && nb->nb_add)
		return (PyObject *)(*nb->nb_power)(v1, w1, z);
	    else
	    {
		PY_NOT_IMPLEMENTED
	    }
	}
    }

    CONVERT_BINOP(v, w, &iv, &iw);

    if (iw < 0)
    {
	if (iv)
	    PyErr_SetString(PyExc_ValueError,
			    "cannot raise PgInt8 to a negative power");
	else
	    PyErr_SetString(PyExc_ZeroDivisionError,
			    "cannot raise 0 to a negative power");
	return (PyObject *)NULL;
    }

    if ((PyObject *)z != Py_None)
    {
	CONVERT_TO_LONGLONG(z, &iz);
	if (iz == 0)
	{
	    PyErr_SetString(PyExc_ValueError,
			    "pow() arg 3 cannot be 0");
	    return (PyObject *)NULL;
	}
    }
    /*
     * XXX: The original exponentiation code stopped looping
     * when temp hit zero; this code will continue onwards
     * unnecessarily, but at least it won't cause any errors.
     * Hopefully the speed improvement from the fast exponentiation
     * will compensate for the slight inefficiency.
     * XXX: Better handling of overflows is desperately needed.
     */
    temp = iv;
    ix = 1;
    while (iw > 0)
    {
	prev = ix;	/* Save value for overflow check */
	if ((unsigned PY_LONG_LONG)iw & 1U)
	{
	    ix = ix*temp;
	    if (temp == 0)
		break; /* Avoid ix / 0 */
	    if (ix / temp != prev)
		return err_ovf("PgInt8 exponentiation");
	}
	iw = (unsigned PY_LONG_LONG)iw >> 1U; /* Shift exponent down by 1 bit */
	if (iw == 0) break;
	prev = temp;
	temp *= temp;	/* Square the value of temp */
	if (prev != 0 && (temp / prev) != prev)
	    return err_ovf("PgInt8 exponentiation");
	if (iz)
	{
	    /* If we did a multiplication, perform a modulo */
	    ix = ix % iz;
	    temp = temp % iz;
	}
    }
    if (iz)
    {
	PY_LONG_LONG div, mod;
	if (i_divmod(ix, iz, &div, &mod) < 0)
	    return (PyObject *)NULL;
	ix=mod;
    }
    return (PyObject *)PgInt8_FromLongLong(ix);
#else
    PY_LONG_LONG iv, iw, ix;

    CONVERT_BINOP(v, w, &iv, &iw);

    if (iw < 0) {
	PyErr_SetString(PyExc_ValueError,
			"PgInt8 to the negative power");
	return (PyObject *)NULL;
    }
    if ((PyObject *)z != Py_None) {
	PyErr_SetString(PyExc_TypeError,
			"pow(PgInt8, PgInt8, PgInt8) not yet supported");
	return (PyObject *)NULL;
    }
    ix = 1;
    while (--iw >= 0) {
	PY_LONG_LONG prev = ix;
	ix = ix * iv;
	if (iv == 0)
	    break; /* 0 to some power -- avoid ix / 0 */
	if (ix / iv != prev)
	    return err_ovf("PgInt8 exponentiation");
    }
    return (PyObject *)PgInt8_FromLongLong(ix);
#endif
}				

static PyObject *int8_neg(PgInt8Object *v)
{
    PY_LONG_LONG a, x;

    a = PgInt8_AS_LONGLONG(v);
    x = -a;

    if (a < 0 && x < 0)
	return err_ovf("PgInt8 negation");

    return (PyObject *)PgInt8_FromLongLong(x);
}

static PyObject *int8_pos(PgInt8Object *v)
{
    Py_INCREF(v);
    return (PyObject *)v;
}

static PyObject *int8_abs(PyObject *v)
{
    PY_LONG_LONG a, x;

    CONVERT_TO_LONGLONG(v, &a);

    if (a >= 0)
	return (PyObject *)PgInt8_FromLongLong(a);
    else
    {
        x = -a;

        if (a < 0 && x < 0)
            return err_ovf("PgInt8 negation");

	return (PyObject *)PgInt8_FromLongLong(x);
    }
}

static int int8_nonzero(PgInt8Object *v)
{
    return (PgInt8_AS_LONGLONG(v) != 0);
}

static PyObject *int8_invert(PgInt8Object *v)
{
    unsigned PY_LONG_LONG a = (unsigned PY_LONG_LONG)PgInt8_AS_LONGLONG(v);

    return (PyObject *)PgInt8_FromLongLong(~a);
}

static PyObject *int8_lshift(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b;

    CONVERT_BINOP(v, w, &a, &b);

    if (b < 0) {
	PyErr_SetString(PyExc_ValueError, "negative shift count");
	return (PyObject *)NULL;
    }
    if (a == 0 || b == 0) {
	Py_INCREF(v);
	return v;
    }
    if (b >= LONG_LONG_BIT) {
	return (PyObject *)PgInt8_FromLong(0L);
    }
    a = (unsigned PY_LONG_LONG)a << b;
    return (PyObject *)PgInt8_FromLongLong(a);
}

static PyObject *int8_rshift(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b;

    CONVERT_BINOP(v, w, &a, &b);

    if (b < 0) {
	PyErr_SetString(PyExc_ValueError, "negative shift count");
	return (PyObject *)NULL;
    }
    if (a == 0 || b == 0) {
	Py_INCREF(v);
	return (PyObject *)v;
    }
    if (b >= LONG_LONG_BIT) {
	if (a < 0)
	    a = (PY_LONG_LONG)-1;
	else
	    a = (PY_LONG_LONG)0;
    }
    else {
	a = Py_ARITHMETIC_RIGHT_SHIFT(PY_LONG_LONG, a, b);
    }
    return (PyObject *)PgInt8_FromLongLong(a);
}

static PyObject *int8_and(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b;

    CONVERT_BINOP(v, w, &a, &b);

    return (PyObject *)PgInt8_FromLongLong((unsigned PY_LONG_LONG)a &
					   (unsigned PY_LONG_LONG)b);
}

static PyObject *int8_xor(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b;

    CONVERT_BINOP(v, w, &a, &b);

    return (PyObject *)PgInt8_FromLongLong((unsigned PY_LONG_LONG)a ^
					   (unsigned PY_LONG_LONG)b);
}

static PyObject *int8_or(PyObject *v, PyObject *w)
{
    PY_LONG_LONG a, b;

    CONVERT_BINOP(v, w, &a, &b);

    return (PyObject *)PgInt8_FromLongLong((unsigned PY_LONG_LONG)a |
					   (unsigned PY_LONG_LONG)b);
}

static PyObject *int8_int(PgInt8Object *v)
{
    long x;

    x = PgInt8_AsLong(v);

    if (PyErr_Occurred())
        return NULL;

    return Py_BuildValue("l", x);
}

static PyObject *int8_long(PgInt8Object *v)
{
    return PyLong_FromLongLong(PgInt8_AS_LONGLONG(v));
}

static PyObject *int8_float(PgInt8Object *v)
{
    return Py_BuildValue("d", (double)(PgInt8_AS_LONGLONG(v)));
}

static PyObject *int8_oct(PgInt8Object *v)
{
    char buf[100];
    PY_LONG_LONG x = PgInt8_AS_LONGLONG(v);
    if (x == 0)
	strcpy(buf, "0");
    else
	sprintf(buf, "0%llo", x);
    return Py_BuildValue("s", buf);
}

static PyObject *int8_hex(PgInt8Object *v)
{
    char buf[100];
    PY_LONG_LONG x = PgInt8_AS_LONGLONG(v);
    sprintf(buf, "0x%llx", x);
    return Py_BuildValue("s", buf);
}

static PyNumberMethods int8_as_number = {
	(binaryfunc)    int8_add,	/*nb_add*/
	(binaryfunc)    int8_sub,	/*nb_subtract*/
	(binaryfunc)    int8_mul,	/*nb_multiply*/
	(binaryfunc)    int8_div,	/*nb_divide*/
	(binaryfunc)    int8_mod,	/*nb_remainder*/
	(binaryfunc)    int8_divmod,	/*nb_divmod*/
	(ternaryfunc)   int8_pow,	/*nb_power*/
	(unaryfunc)     int8_neg,	/*nb_negative*/
	(unaryfunc)     int8_pos,	/*nb_positive*/
	(unaryfunc)     int8_abs,	/*nb_absolute*/
	(inquiry)       int8_nonzero,	/*nb_nonzero*/
	(unaryfunc)     int8_invert,	/*nb_invert*/
	(binaryfunc)    int8_lshift,	/*nb_lshift*/
	(binaryfunc)    int8_rshift,	/*nb_rshift*/
	(binaryfunc)    int8_and,	/*nb_and*/
	(binaryfunc)    int8_xor,	/*nb_xor*/
	(binaryfunc)    int8_or,	/*nb_or*/
	(coercion)      int8_coerce,	/*nb_coerce*/
	(unaryfunc)     int8_int,	/*nb_int*/
	(unaryfunc)     int8_long,	/*nb_long*/
	(unaryfunc)     int8_float,	/*nb_float*/
	(unaryfunc)     int8_oct,	/*nb_oct*/
	(unaryfunc)     int8_hex, 	/*nb_hex*/
	0,				/*nb_inplace_add*/
	0,				/*nb_inplace_subtract*/
	0,				/*nb_inplace_multiply*/
	0,				/*nb_inplace_divide*/
	0,				/*nb_inplace_remainder*/
	0,				/*nb_inplace_power*/
	0,				/*nb_inplace_lshift*/
	0,				/*nb_inplace_rshift*/
	0,				/*nb_inplace_and*/
	0,				/*nb_inplace_xor*/
	0,				/*nb_inplace_or*/
};

PyTypeObject PgInt8_Type = {
	PyObject_HEAD_INIT(NULL)
	0,
	"PgInt8",
	sizeof(PgInt8Object),
	0,
	(destructor)int8_dealloc, 	/*tp_dealloc*/
	(printfunc)int8_print,		/*tp_print*/
	0,				/*tp_getattr*/
	0,				/*tp_setattr*/
	(cmpfunc)int8_compare,		/*tp_compare*/
	(reprfunc)int8_repr,		/*tp_repr*/
	&int8_as_number,		/*tp_as_number*/
	0,				/*tp_as_sequence*/
	0,				/*tp_as_mapping*/
	(hashfunc)int8_hash,		/*tp_hash*/
        0,				/*tp_call*/
        0,				/*tp_str*/
	0,				/*tp_getattro*/
	0,				/*tp_setattro*/
	0,				/*tp_as_buffer*/
	Py_TPFLAGS_CHECKTYPES		/*tp_flags*/
};

void initpgint8(void)
{
    PgInt8_Type.ob_type = &PyType_Type;
}
#endif