forked from numpy/numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastumath_unsigned.inc
3201 lines (3084 loc) · 152 KB
/
fastumath_unsigned.inc
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
/* -*- c -*- */
#include "Python.h"
#include "Numeric/arrayobject.h"
#include "Numeric/ufuncobject.h"
#include "abstract.h"
#include <math.h>
#include "mconf_lite.h"
/* Fast umath module whose functions do not check for range and domain errors.
Replacement for umath + additions for isnan, isfinite, and isinf
Also allows comparison operations on complex numbers (just compares the
real part) and logical operations.
All logical operations return UBYTE arrays.
This version supports unsigned types.
*/
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#ifndef LONG_BIT
#define LONG_BIT (CHAR_BIT * sizeof(long))
#endif
#ifndef INT_BIT
#define INT_BIT (CHAR_BIT * sizeof(int))
#endif
#ifndef SHORT_BIT
#define SHORT_BIT (CHAR_BIT * sizeof(short))
#endif
#ifndef UINT_BIT
#define UINT_BIT (CHAR_BIT * sizeof(unsigned int))
#endif
#ifndef USHORT_BIT
#define USHORT_BIT (CHAR_BIT * sizeof(unsigned short))
#endif
/* A whole slew of basic math functions are provided by Konrad Hinsen. */
#if !defined(__STDC__) && !defined(_MSC_VER)
extern double fmod (double, double);
extern double frexp (double, int *);
extern double ldexp (double, int);
extern double modf (double, double *);
#endif
#ifndef M_PI
#define M_PI 3.1415926535897931
#endif
#define ABS(x) ((x) < 0 ? -(x) : (x))
/* isnan and isinf and isfinite functions */
static void FLOAT_isnan(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) ABS(isnan((double)(*((float *)i1))));
}
}
static void DOUBLE_isnan(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) ABS(isnan((double)(*((double *)i1))));
}
}
static void CFLOAT_isnan(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isnan((double)((float *)i1)[0]) || isnan((double)((float *)i1)[1]);
}
}
static void CDOUBLE_isnan(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isnan((double)((double *)i1)[0]) || isnan((double)((double *)i1)[1]);
}
}
static void FLOAT_isinf(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) !(isfinite((double)(*((float *)i1))) || isnan((double)(*((float *)i1))));
}
}
static void DOUBLE_isinf(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op)= (unsigned char) !(isfinite((double)(*((double *)i1))) || isnan((double)(*((double *)i1))));
}
}
static void CFLOAT_isinf(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op)= (unsigned char) !((isfinite((double)(((float *)i1)[0])) && isfinite((double)(((float *)i1)[1]))) || isnan((double)(((float *)i1)[0])) || isnan((double)(((float *)i1)[1])));
}
}
static void CDOUBLE_isinf(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op)= (unsigned char) !((isfinite((double)(((double *)i1)[0])) && isfinite((double)(((double *)i1)[1]))) || isnan((double)(((double *)i1)[0])) || isnan((double)(((double *)i1)[1])));
}
}
static void FLOAT_isfinite(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isfinite((double)(*((float *)i1)));
}
}
static void DOUBLE_isfinite(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isfinite((double)(*((double *)i1)));
}
}
static void CFLOAT_isfinite(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isfinite((double)((float *)i1)[0]) && isfinite((double)((float *)i1)[1]);
}
}
static void CDOUBLE_isfinite(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0], os=steps[1], n=dimensions[0];
char *i1=args[0], *op=args[1];
for (i=0; i < n; i++, i1+=is1, op+=os) {
*((unsigned char *)op) = (unsigned char) isfinite((double)((double *)i1)[0]) && isfinite((double)((double *)i1)[1]);
}
}
static PyUFuncGenericFunction isnan_functions[] = {FLOAT_isnan, DOUBLE_isnan, CFLOAT_isnan, CDOUBLE_isnan, NULL};
static PyUFuncGenericFunction isinf_functions[] = {FLOAT_isinf, DOUBLE_isinf, CFLOAT_isinf, CDOUBLE_isinf, NULL};
static PyUFuncGenericFunction isfinite_functions[] = {FLOAT_isfinite, DOUBLE_isfinite, CFLOAT_isfinite, CDOUBLE_isfinite, NULL};
static char isinf_signatures[] = { PyArray_FLOAT, PyArray_UBYTE, PyArray_DOUBLE, PyArray_UBYTE, PyArray_CFLOAT, PyArray_UBYTE, PyArray_CDOUBLE, PyArray_UBYTE, };
static void * isnan_data[] = {(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL};
static void * isinf_data[] = {(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL};
static void * isfinite_data[] = {(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL};
/* Some functions needed from ufunc object, so that Py_complex's aren't being returned
between code possibly compiled with different compilers.
*/
typedef Py_complex ComplexBinaryFunc(Py_complex x, Py_complex y);
typedef Py_complex ComplexUnaryFunc(Py_complex x);
static void fastumath_F_F_As_D_D(char **args, int *dimensions, int *steps, void *func) {
int i; Py_complex x;
char *ip1=args[0], *op=args[1];
for(i=0; i<*dimensions; i++, ip1+=steps[0], op+=steps[1]) {
x.real = ((float *)ip1)[0]; x.imag = ((float *)ip1)[1];
x = ((ComplexUnaryFunc *)func)(x);
((float *)op)[0] = (float)x.real;
((float *)op)[1] = (float)x.imag;
}
}
static void fastumath_D_D(char **args, int *dimensions, int *steps, void *func) {
int i; Py_complex x;
char *ip1=args[0], *op=args[1];
for(i=0; i<*dimensions; i++, ip1+=steps[0], op+=steps[1]) {
x.real = ((double *)ip1)[0]; x.imag = ((double *)ip1)[1];
x = ((ComplexUnaryFunc *)func)(x);
((double *)op)[0] = x.real;
((double *)op)[1] = x.imag;
}
}
static void fastumath_FF_F_As_DD_D(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2];
char *ip1=args[0], *ip2=args[1], *op=args[2];
int n=dimensions[0];
Py_complex x, y;
for(i=0; i<n; i++, ip1+=is1, ip2+=is2, op+=os) {
x.real = ((float *)ip1)[0]; x.imag = ((float *)ip1)[1];
y.real = ((float *)ip2)[0]; y.imag = ((float *)ip2)[1];
x = ((ComplexBinaryFunc *)func)(x, y);
((float *)op)[0] = (float)x.real;
((float *)op)[1] = (float)x.imag;
}
}
static void fastumath_DD_D(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2];
char *ip1=args[0], *ip2=args[1], *op=args[2];
int n=dimensions[0];
Py_complex x,y;
for(i=0; i<n; i++, ip1+=is1, ip2+=is2, op+=os) {
x.real = ((double *)ip1)[0]; x.imag = ((double *)ip1)[1];
y.real = ((double *)ip2)[0]; y.imag = ((double *)ip2)[1];
x = ((ComplexBinaryFunc *)func)(x, y);
((double *)op)[0] = x.real;
((double *)op)[1] = x.imag;
}
}
#if !defined(HAVE_INVERSE_HYPERBOLIC)
static double acosh(double x)
{
return log(x + sqrt((x-1.0)*(x+1.0)));
}
static double asinh(double xx)
{
double x;
int sign;
if (xx < 0.0) {
sign = -1;
x = -xx;
}
else {
sign = 1;
x = xx;
}
return sign*log(x + sqrt(x*x+1.0));
}
static double atanh(double x)
{
return 0.5*log((1.0+x)/(1.0-x));
}
#endif
#if defined(HAVE_HYPOT)
#if !defined(NeXT) && !defined(_MSC_VER)
extern double hypot(double, double);
#endif
#else
double hypot(double x, double y)
{
double yx;
x = fabs(x);
y = fabs(y);
if (x < y) {
double temp = x;
x = y;
y = temp;
}
if (x == 0.)
return 0.;
else {
yx = y/x;
return x*sqrt(1.+yx*yx);
}
}
#endif
#ifdef i860
/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
#undef HUGE_VAL
#endif
#ifdef HUGE_VAL
#define CHECK(x) if (errno != 0) ; else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; else errno = ERANGE
#else
#define CHECK(x) /* Don't know how to check */
#endif
/* First, the C functions that do the real work */
/* constants */
static Py_complex c_1 = {1., 0.};
static Py_complex c_half = {0.5, 0.};
static Py_complex c_i = {0., 1.};
static Py_complex c_i2 = {0., 0.5};
/*
static Py_complex c_mi = {0., -1.};
static Py_complex c_pi2 = {M_PI/2., 0.};
*/
static Py_complex c_quot_fast(Py_complex a, Py_complex b)
{
/******************************************************************/
/* This algorithm is better, and is pretty obvious: first divide the
* numerators and denominator by whichever of {b.real, b.imag} has
* larger magnitude. The earliest reference I found was to CACM
* Algorithm 116 (Complex Division, Robert L. Smith, Stanford
* University). As usual, though, we're still ignoring all IEEE
* endcases.
*/
Py_complex r; /* the result */
const double abs_breal = b.real < 0 ? -b.real : b.real;
const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
if ((b.real == 0.0) && (b.imag == 0.0)) {
r.real = a.real / b.real;
r.imag = a.imag / b.imag;
/* if (a.real == 0.0) {r.real = a.real/b.real;} */
/* else if (a.real < 0.0) {r.real = -1.0/0.0;} */
/* else if (a.real > 0.0) {r.real = 1.0/0.0;} */
/* if (a.imag == 0.0) {r.imag = a.imag/b.imag;} */
/* else if (a.imag < 0.0) {r.imag = -1.0/0.0;} */
/* else if (a.imag > 0.0) {r.imag = 1.0/0.0;} */
return r;
}
if (abs_breal >= abs_bimag) {
/* divide tops and bottom by b.real */
const double ratio = b.imag / b.real;
const double denom = b.real + b.imag * ratio;
r.real = (a.real + a.imag * ratio) / denom;
r.imag = (a.imag - a.real * ratio) / denom;
}
else {
/* divide tops and bottom by b.imag */
const double ratio = b.real / b.imag;
const double denom = b.real * ratio + b.imag;
r.real = (a.real * ratio + a.imag) / denom;
r.imag = (a.imag * ratio - a.real) / denom;
}
return r;
}
#if PY_VERSION_HEX >= 0x02020000
static Py_complex c_quot_floor_fast(Py_complex a, Py_complex b)
{
/* Not really sure what to do here, but it looks like Python takes the
floor of the real part and returns that as the answer. So, we will do the same.
*/
Py_complex r;
r = c_quot_fast(a, b);
r.imag = 0.0;
r.real = floor(r.real);
return r;
}
#endif
static Py_complex c_sqrt(Py_complex x)
{
Py_complex r;
double s,d;
if (x.real == 0. && x.imag == 0.)
r = x;
else {
s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
d = 0.5*x.imag/s;
if (x.real > 0.) {
r.real = s;
r.imag = d;
}
else if (x.imag >= 0.) {
r.real = d;
r.imag = s;
}
else {
r.real = -d;
r.imag = -s;
}
}
return r;
}
static Py_complex c_log(Py_complex x)
{
Py_complex r;
double l = hypot(x.real,x.imag);
r.imag = atan2(x.imag, x.real);
r.real = log(l);
return r;
}
static Py_complex c_prodi(Py_complex x)
{
Py_complex r;
r.real = -x.imag;
r.imag = x.real;
return r;
}
static Py_complex c_acos(Py_complex x)
{
return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
c_sqrt(c_diff(c_1,c_prod(x,x))))))));
}
static Py_complex c_acosh(Py_complex x)
{
return c_log(c_sum(x,c_prod(c_i,
c_sqrt(c_diff(c_1,c_prod(x,x))))));
}
static Py_complex c_asin(Py_complex x)
{
return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
c_sqrt(c_diff(c_1,c_prod(x,x)))))));
}
static Py_complex c_asinh(Py_complex x)
{
return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
}
static Py_complex c_atan(Py_complex x)
{
return c_prod(c_i2,c_log(c_quot_fast(c_sum(c_i,x),c_diff(c_i,x))));
}
static Py_complex c_atanh(Py_complex x)
{
return c_prod(c_half,c_log(c_quot_fast(c_sum(c_1,x),c_diff(c_1,x))));
}
static Py_complex c_cos(Py_complex x)
{
Py_complex r;
r.real = cos(x.real)*cosh(x.imag);
r.imag = -sin(x.real)*sinh(x.imag);
return r;
}
static Py_complex c_cosh(Py_complex x)
{
Py_complex r;
r.real = cos(x.imag)*cosh(x.real);
r.imag = sin(x.imag)*sinh(x.real);
return r;
}
static Py_complex c_exp(Py_complex x)
{
Py_complex r;
double l = exp(x.real);
r.real = l*cos(x.imag);
r.imag = l*sin(x.imag);
return r;
}
static Py_complex c_log10(Py_complex x)
{
Py_complex r;
double l = hypot(x.real,x.imag);
r.imag = atan2(x.imag, x.real)/log(10.);
r.real = log10(l);
return r;
}
static Py_complex c_sin(Py_complex x)
{
Py_complex r;
r.real = sin(x.real)*cosh(x.imag);
r.imag = cos(x.real)*sinh(x.imag);
return r;
}
static Py_complex c_sinh(Py_complex x)
{
Py_complex r;
r.real = cos(x.imag)*sinh(x.real);
r.imag = sin(x.imag)*cosh(x.real);
return r;
}
static Py_complex c_tan(Py_complex x)
{
Py_complex r;
double sr,cr,shi,chi;
double rs,is,rc,ic;
double d;
sr = sin(x.real);
cr = cos(x.real);
shi = sinh(x.imag);
chi = cosh(x.imag);
rs = sr*chi;
is = cr*shi;
rc = cr*chi;
ic = -sr*shi;
d = rc*rc + ic*ic;
r.real = (rs*rc+is*ic)/d;
r.imag = (is*rc-rs*ic)/d;
return r;
}
static Py_complex c_tanh(Py_complex x)
{
Py_complex r;
double si,ci,shr,chr;
double rs,is,rc,ic;
double d;
si = sin(x.imag);
ci = cos(x.imag);
shr = sinh(x.real);
chr = cosh(x.real);
rs = ci*shr;
is = si*chr;
rc = ci*chr;
ic = si*shr;
d = rc*rc + ic*ic;
r.real = (rs*rc+is*ic)/d;
r.imag = (is*rc-rs*ic)/d;
return r;
}
static long powll(long x, long n, int nbits)
/* Overflow check: overflow will occur if log2(abs(x)) * n > nbits. */
{
long r = 1;
long p = x;
double logtwox;
long mask = 1;
if (n < 0) PyErr_SetString(PyExc_ValueError, "Integer to a negative power");
if (x != 0) {
logtwox = log10 (fabs ( (double) x))/log10 ( (double) 2.0);
if (logtwox * (double) n > (double) nbits)
PyErr_SetString(PyExc_ArithmeticError, "Integer overflow in power.");
}
while (mask > 0 && n >= mask) {
if (n & mask)
r *= p;
mask <<= 1;
p *= p;
}
return r;
}
static void UBYTE_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned char *)op)=*((unsigned char *)i1) + *((unsigned char *)i2);
}
}
static void SBYTE_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((signed char *)op)=*((signed char *)i1) + *((signed char *)i2);
}
}
static void SHORT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((short *)op)=*((short *)i1) + *((short *)i2);
}
}
static void USHORT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned short *)op)=*((unsigned short *)i1) + *((unsigned short *)i2);
}
}
static void INT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((int *)op)=*((int *)i1) + *((int *)i2);
}
}
static void UINT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned int *)op)=*((unsigned int *)i1) + *((unsigned int *)i2);
}
}
static void LONG_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((long *)op)=*((long *)i1) + *((long *)i2);
}
}
static void FLOAT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((float *)op)=*((float *)i1) + *((float *)i2);
}
}
static void DOUBLE_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((double *)op)=*((double *)i1) + *((double *)i2);
}
}
static void CFLOAT_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
((float *)op)[0]=((float *)i1)[0] + ((float *)i2)[0]; ((float *)op)[1]=((float *)i1)[1] + ((float *)i2)[1];
}
}
static void CDOUBLE_add(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
((double *)op)[0]=((double *)i1)[0] + ((double *)i2)[0]; ((double *)op)[1]=((double *)i1)[1] + ((double *)i2)[1];
}
}
static void UBYTE_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned char *)op)=*((unsigned char *)i1) - *((unsigned char *)i2);
}
}
static void SBYTE_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((signed char *)op)=*((signed char *)i1) - *((signed char *)i2);
}
}
static void SHORT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((short *)op)=*((short *)i1) - *((short *)i2);
}
}
static void USHORT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned short *)op)=*((unsigned short *)i1) - *((unsigned short *)i2);
}
}
static void INT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((int *)op)=*((int *)i1) - *((int *)i2);
}
}
static void UINT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((unsigned int *)op)=*((unsigned int *)i1) - *((unsigned int *)i2);
}
}
static void LONG_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((long *)op)=*((long *)i1) - *((long *)i2);
}
}
static void FLOAT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((float *)op)=*((float *)i1) - *((float *)i2);
}
}
static void DOUBLE_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
*((double *)op)=*((double *)i1) - *((double *)i2);
}
}
static void CFLOAT_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
((float *)op)[0]=((float *)i1)[0] - ((float *)i2)[0]; ((float *)op)[1]=((float *)i1)[1] - ((float *)i2)[1];
}
}
static void CDOUBLE_subtract(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
((double *)op)[0]=((double *)i1)[0] - ((double *)i2)[0]; ((double *)op)[1]=((double *)i1)[1] - ((double *)i2)[1];
}
}
static void UBYTE_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
unsigned int x;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
x = (unsigned int) (*((unsigned char *)i1)) * (unsigned int) (*((unsigned char *)i2));
if (x > 255) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
*((unsigned char *)op)=(unsigned char) x;
}
}
static void SBYTE_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
int x;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
x = (int) (*((signed char *)i1)) * (int) (*((signed char *)i2));
if (x > 127 || x < -128) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
*((signed char *)op)=(signed char) x;
}
}
static void SHORT_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
short a, b, ah, bh, x, y;
int s;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
s = 1;
a = *((short *)i1);
b = *((short *)i2);
ah = a >> (SHORT_BIT/2);
bh = b >> (SHORT_BIT/2);
/* Quick test for common case: two small positive shorts */
if (ah == 0 && bh == 0) {
if ((x=a*b) < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
else {
*((short *)op)=x;
continue;
}
}
/* Arrange that a >= b >= 0 */
if (a < 0) {
a = -a;
if (a < 0) {
/* Largest negative */
if (b == 0 || b == 1) {
*((short *)op)=a*b;
continue;
}
else {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
}
s = -s;
ah = a >> (SHORT_BIT/2);
}
if (b < 0) {
b = -b;
if (b < 0) {
/* Largest negative */
if (a == 0 || a == 1) {
*((short *)op)=a*b;
continue;
}
else {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
}
s = -s;
bh = b >> (SHORT_BIT/2);
}
/* 1) both ah and bh > 0 : then report overflow */
if (ah != 0 && bh != 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
/* 2) both ah and bh = 0 : then compute a*b and report
overflow if it comes out negative */
if (ah == 0 && bh == 0) {
if ((x=a*b) < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
else {
*((short *)op)=s * x;
continue;
}
}
if (a < b) {
/* Swap */
x = a;
a = b;
b = x;
ah = bh;
/* bh not used beyond this point */
}
/* 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;
if (y >= (1 << (SHORT_BIT/2 - 1))) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
a &= (1 << (SHORT_BIT/2)) - 1;
x = a*b;
if (x < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
x += y << (SHORT_BIT/2);
if (x < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
*((short *)op)=s*x;
}
}
static void USHORT_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
unsigned int x;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
x = (unsigned int) (*((unsigned short *)i1)) * (unsigned int) (*((unsigned short *)i2));
if (x > 65535) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
*((unsigned short *)op)=(unsigned short) x;
}
}
static void INT_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
int a, b, ah, bh, x, y;
int s;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
s = 1;
a = *((int *)i1);
b = *((int *)i2);
ah = a >> (INT_BIT/2);
bh = b >> (INT_BIT/2);
/* Quick test for common case: two small positive ints */
if (ah == 0 && bh == 0) {
if ((x=a*b) < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
else {
*((int *)op)=x;
continue;
}
}
/* Arrange that a >= b >= 0 */
if (a < 0) {
a = -a;
if (a < 0) {
/* Largest negative */
if (b == 0 || b == 1) {
*((int *)op)=a*b;
continue;
}
else {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
}
s = -s;
ah = a >> (INT_BIT/2);
}
if (b < 0) {
b = -b;
if (b < 0) {
/* Largest negative */
if (a == 0 || a == 1) {
*((int *)op)=a*b;
continue;
}
else {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
}
s = -s;
bh = b >> (INT_BIT/2);
}
/* 1) both ah and bh > 0 : then report overflow */
if (ah != 0 && bh != 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
/* 2) both ah and bh = 0 : then compute a*b and report
overflow if it comes out negative */
if (ah == 0 && bh == 0) {
if ((x=a*b) < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
else {
*((int *)op)=s * x;
continue;
}
}
if (a < b) {
/* Swap */
x = a;
a = b;
b = x;
ah = bh;
/* bh not used beyond this point */
}
/* 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;
if (y >= (1 << (INT_BIT/2 - 1))) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
a &= (1 << (INT_BIT/2)) - 1;
x = a*b;
if (x < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
x += y << (INT_BIT/2);
if (x < 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
*((int *)op)=s*x;
}
}
static void UINT_multiply(char **args, int *dimensions, int *steps, void *func) {
int i, is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0];
char *i1=args[0], *i2=args[1], *op=args[2];
unsigned int a, b, ah, bh, x, y;
for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
a = *((unsigned int *)i1);
b = *((unsigned int *)i2);
ah = a >> (INT_BIT/2);
bh = b >> (INT_BIT/2);
/* Quick test for common case: two small positive ints */
if (ah == 0 && bh == 0) { /* result should fit into bits available. */
x = a*b;
*((unsigned int *)op)=x;
continue;
}
/* 1) both ah and bh > 0 : then report overflow */
if (ah != 0 && bh != 0) {
PyErr_SetString (PyExc_ArithmeticError, "Integer overflow in multiply.");
return;
}
/* Otherwise one and only one of ah or bh is non-zero. Make it so a > b (ah >0 and bh=0) */
if (a < b) {
/* Swap */
x = a;
a = b;
b = x;
ah = bh;
/* bh not used beyond this point */
}
/* Now a = ah */
/* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
it's >= 2^(INT_BIT/2) -- shifted_version won't fit in unsigned int.
Then compute al*bl (this should fit in the allotated space)
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;