forked from numpy/numpy
/
scalarmath.c.src
1886 lines (1682 loc) · 53.9 KB
/
scalarmath.c.src
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 -*- */
/* The purpose of this module is to add faster math for array scalars
that does not go through the ufunc machinery
but still supports error-modes.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define _UMATHMODULE
#define _MULTIARRAYMODULE
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include "npy_config.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/arrayscalars.h"
#include "npy_import.h"
#include "npy_pycompat.h"
#include "numpy/halffloat.h"
#include "templ_common.h"
#include "binop_override.h"
#include "npy_longdouble.h"
#include "array_coercion.h"
#include "common.h"
#include "can_cast_table.h"
/* TODO: Used for some functions, should possibly move these to npy_math.h */
#include "loops.h"
/* Basic operations:
*
* BINARY:
*
* add, subtract, multiply, divide, remainder, divmod, power,
* floor_divide, true_divide
*
* lshift, rshift, and, or, xor (integers only)
*
* UNARY:
*
* negative, positive, absolute, nonzero, invert, int, long, float, oct, hex
*
*/
/**begin repeat
* #name = byte, short, int, long, longlong#
* #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong#
*/
static NPY_INLINE int
@name@_ctype_add(@type@ a, @type@ b, @type@ *out) {
*out = a + b;
if ((*out^a) >= 0 || (*out^b) >= 0) {
return 0;
}
return NPY_FPE_OVERFLOW;
}
static NPY_INLINE int
@name@_ctype_subtract(@type@ a, @type@ b, @type@ *out) {
*out = a - b;
if ((*out^a) >= 0 || (*out^~b) >= 0) {
return 0;
}
return NPY_FPE_OVERFLOW;
}
/**end repeat**/
/**begin repeat
* #name = ubyte, ushort, uint, ulong, ulonglong#
* #type = npy_ubyte, npy_ushort, npy_uint, npy_ulong, npy_ulonglong#
*/
static NPY_INLINE int
@name@_ctype_add(@type@ a, @type@ b, @type@ *out) {
*out = a + b;
if (*out >= a && *out >= b) {
return 0;
}
return NPY_FPE_OVERFLOW;
}
static NPY_INLINE int
@name@_ctype_subtract(@type@ a, @type@ b, @type@ *out) {
*out = a - b;
if (a >= b) {
return 0;
}
return NPY_FPE_OVERFLOW;
}
/**end repeat**/
#ifndef NPY_SIZEOF_BYTE
#define NPY_SIZEOF_BYTE 1
#endif
/**begin repeat
*
* #name = byte, ubyte, short, ushort,
* int, uint, long, ulong#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort,
* npy_int, npy_uint, npy_long, npy_ulong#
* #big = npy_int, npy_uint, npy_int, npy_uint,
* npy_longlong, npy_ulonglong, npy_longlong, npy_ulonglong#
* #NAME = BYTE, UBYTE, SHORT, USHORT,
* INT, UINT, LONG, ULONG#
* #SIZENAME = BYTE*2, SHORT*2, INT*2, LONG*2#
* #SIZE = INT*4,LONGLONG*4#
* #neg = (1,0)*4#
*/
#if NPY_SIZEOF_@SIZE@ > NPY_SIZEOF_@SIZENAME@
static NPY_INLINE int
@name@_ctype_multiply(@type@ a, @type@ b, @type@ *out) {
@big@ temp;
temp = ((@big@) a) * ((@big@) b);
*out = (@type@) temp;
#if @neg@
if (temp > NPY_MAX_@NAME@ || temp < NPY_MIN_@NAME@) {
#else
if (temp > NPY_MAX_@NAME@) {
#endif
return NPY_FPE_OVERFLOW;
}
return 0;
}
#endif
/**end repeat**/
/**begin repeat
*
* #name = int, uint, long, ulong,
* longlong, ulonglong#
* #type = npy_int, npy_uint, npy_long, npy_ulong,
* npy_longlong, npy_ulonglong#
* #SIZE = INT*2, LONG*2, LONGLONG*2#
*/
#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_@SIZE@
static NPY_INLINE int
@name@_ctype_multiply(@type@ a, @type@ b, @type@ *out) {
if (npy_mul_with_overflow_@name@(out, a, b)) {
return NPY_FPE_OVERFLOW;
}
return 0;
}
#endif
/**end repeat**/
/**begin repeat
*
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
* #neg = (1,0)*5#
* #NAME = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG#
*/
#if @neg@
#define DIVIDEBYZERO_CHECK (b == 0 || (a == NPY_MIN_@NAME@ && b == -1))
#else
#define DIVIDEBYZERO_CHECK (b == 0)
#endif
static NPY_INLINE int
@name@_ctype_divide(@type@ a, @type@ b, @type@ *out) {
if (b == 0) {
*out = 0;
return NPY_FPE_DIVIDEBYZERO;
}
#if @neg@
else if (b == -1 && a == NPY_MIN_@NAME@) {
*out = NPY_MIN_@NAME@;
return NPY_FPE_OVERFLOW;
}
#endif
else {
#if @neg@
@type@ tmp;
tmp = a / b;
if (((a > 0) != (b > 0)) && (a % b != 0)) {
tmp--;
}
*out = tmp;
#else
*out = a / b;
#endif
return 0;
}
}
#define @name@_ctype_floor_divide @name@_ctype_divide
static NPY_INLINE int
@name@_ctype_remainder(@type@ a, @type@ b, @type@ *out) {
if (DIVIDEBYZERO_CHECK) {
*out = 0;
if (b == 0) {
return NPY_FPE_DIVIDEBYZERO;
}
return 0;
}
#if @neg@
else if ((a > 0) == (b > 0)) {
*out = a % b;
}
else {
/* handled like Python does */
*out = a % b;
if (*out) *out += b;
}
#else
*out = a % b;
#endif
return 0;
}
#undef DIVIDEBYZERO_CHECK
/**end repeat**/
/**begin repeat
*
* #name = byte, ubyte, short, ushort, int, uint, long,
* ulong, longlong, ulonglong#
*/
static NPY_INLINE int
@name@_ctype_true_divide(npy_@name@ a, npy_@name@ b, npy_double *out)
{
*out = (npy_double)a / (npy_double)b;
return 0;
}
/**end repeat**/
/* b will always be positive in this call */
/**begin repeat
*
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
* #upc = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG#
*/
static NPY_INLINE int
@name@_ctype_power(@type@ a, @type@ b, @type@ *out) {
@type@ tmp;
if (b == 0) {
*out = 1;
return 0;
}
if (a == 1) {
*out = 1;
return 0;
}
tmp = b & 1 ? a : 1;
b >>= 1;
while (b > 0) {
a *= a;
if (b & 1) {
tmp *= a;
}
b >>= 1;
}
*out = tmp;
return 0;
}
/**end repeat**/
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
* #suffix = hh,uhh,h,uh,,u,l,ul,ll,ull#
*/
/**begin repeat1
* #oper = and, xor, or#
* #op = &, ^, |#
*/
static NPY_INLINE int
@name@_ctype_@oper@(@type@ arg1, @type@ arg2, @type@ *out)
{
*out = arg1 @op@ arg2;
return 0;
}
/**end repeat1**/
static NPY_INLINE int
@name@_ctype_lshift(@type@ arg1, @type@ arg2, @type@ *out)
{
*out = npy_lshift@suffix@(arg1, arg2);
return 0;
}
static NPY_INLINE int
@name@_ctype_rshift(@type@ arg1, @type@ arg2, @type@ *out)
{
*out = npy_rshift@suffix@(arg1, arg2);
return 0;
}
/**end repeat**/
/**begin repeat
* #name = float, double, longdouble#
* #type = npy_float, npy_double, npy_longdouble#
* #c = f, , l#
*/
/**begin repeat1
* #OP = +, -, *, /#
* #oper = add, subtract, multiply, divide#
*/
static NPY_INLINE int
@name@_ctype_@oper@(@type@ a, @type@ b, @type@ *out)
{
*out = a @OP@ b;
return 0;
}
/**end repeat1**/
#define @name@_ctype_true_divide @name@_ctype_divide
static NPY_INLINE int
@name@_ctype_floor_divide(@type@ a, @type@ b, @type@ *out) {
*out = npy_floor_divide@c@(a, b);
return 0;
}
static NPY_INLINE int
@name@_ctype_remainder(@type@ a, @type@ b, @type@ *out) {
*out = npy_remainder@c@(a, b);
return 0;
}
static NPY_INLINE int
@name@_ctype_divmod(@type@ a, @type@ b, @type@ *out1, @type@ *out2) {
*out1 = npy_divmod@c@(a, b, out2);
return 0;
}
/**end repeat**/
/**begin repeat
* #OP = +, -, *, /#
* #oper = add, subtract, multiply, divide#
*/
static NPY_INLINE int
half_ctype_@oper@(npy_half a, npy_half b, npy_half *out)
{
float res = npy_half_to_float(a) @OP@ npy_half_to_float(b);
*out = npy_float_to_half(res);
return 0;
}
/**end repeat**/
#define half_ctype_true_divide half_ctype_divide
static NPY_INLINE int
half_ctype_floor_divide(npy_half a, npy_half b, npy_half *out)
{
npy_half mod;
if (!b) {
float res = npy_half_to_float(a) / npy_half_to_float(b);
*out = npy_float_to_half(res);
}
else {
*out = npy_half_divmod(a, b, &mod);
}
return 0;
}
static NPY_INLINE int
half_ctype_remainder(npy_half a, npy_half b, npy_half *out)
{
npy_half_divmod(a, b, out);
return 0;
}
static NPY_INLINE int
half_ctype_divmod(npy_half a, npy_half b, npy_half *out1, npy_half *out2)
{
*out1 = npy_half_divmod(a, b, out2);
return 0;
}
/**begin repeat
* #name = cfloat, cdouble, clongdouble#
* #type = npy_cfloat, npy_cdouble, npy_clongdouble#
* #TYPE = CFLOAT, CDOUBLE, CLONGDOUBLE#
* #rname = float, double, longdouble#
* #rtype = npy_float, npy_double, npy_longdouble#
* #c = f,,l#
*/
static NPY_INLINE int
@name@_ctype_add(@type@ a, @type@ b, @type@ *out)
{
out->real = a.real + b.real;
out->imag = a.imag + b.imag;
return 0;
}
static NPY_INLINE int
@name@_ctype_subtract(@type@ a, @type@ b, @type@ *out)
{
out->real = a.real - b.real;
out->imag = a.imag - b.imag;
return 0;
}
/*
* TODO: Mark as to work around FPEs not being issues on clang 12.
* This should be removed when possible.
*/
static NPY_INLINE int
@name@_ctype_multiply( @type@ a, @type@ b, @type@ *out)
{
out->real = a.real * b.real - a.imag * b.imag;
out->imag = a.real * b.imag + a.imag * b.real;
return 0;
}
/* Use the ufunc loop directly to avoid duplicating the complicated logic */
static NPY_INLINE int
@name@_ctype_divide(@type@ a, @type@ b, @type@ *out)
{
char *args[3] = {(char *)&a, (char *)&b, (char *)out};
npy_intp steps[3];
npy_intp size = 1;
@TYPE@_divide(args, &size, steps, NULL);
return 0;
}
#define @name@_ctype_true_divide @name@_ctype_divide
/**end repeat**/
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint, long, ulong,
* longlong, ulonglong#
*/
static NPY_INLINE int
@name@_ctype_divmod(npy_@name@ a, npy_@name@ b, npy_@name@ *out, npy_@name@ *out2)
{
int res = @name@_ctype_floor_divide(a, b, out);
res |= @name@_ctype_remainder(a, b, out2);
return res;
}
/**end repeat**/
/**begin repeat
* #name = float, double, longdouble#
* #type = npy_float, npy_double, npy_longdouble#
* #c = f,,l#
*/
static NPY_INLINE int
@name@_ctype_power(@type@ a, @type@ b, @type@ *out)
{
*out = npy_pow@c@(a, b);
return 0;
}
/**end repeat**/
static NPY_INLINE int
half_ctype_power(npy_half a, npy_half b, npy_half *out)
{
const npy_float af = npy_half_to_float(a);
const npy_float bf = npy_half_to_float(b);
const npy_float outf = npy_powf(af,bf);
*out = npy_float_to_half(outf);
return 0;
}
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* float, double, longdouble#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_float, npy_double, npy_longdouble#
* #uns = (0,1)*5,0*3#
*/
static NPY_INLINE int
@name@_ctype_negative(@type@ a, @type@ *out)
{
*out = -a;
#if @uns@
return NPY_FPE_OVERFLOW;
#else
return 0;
#endif
}
/**end repeat**/
static NPY_INLINE int
half_ctype_negative(npy_half a, npy_half *out)
{
*out = a^0x8000u;
return 0;
}
/**begin repeat
* #name = cfloat, cdouble, clongdouble#
* #type = npy_cfloat, npy_cdouble, npy_clongdouble#
*/
static NPY_INLINE int
@name@_ctype_negative(@type@ a, @type@ *out)
{
out->real = -a.real;
out->imag = -a.imag;
return 0;
}
/**end repeat**/
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* half, float, double, longdouble#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_half, npy_float, npy_double, npy_longdouble#
*/
static NPY_INLINE int
@name@_ctype_positive(@type@ a, @type@ *out)
{
*out = a;
return 0;
}
/**end repeat**/
/**begin repeat
* #name = cfloat, cdouble, clongdouble#
* #type = npy_cfloat, npy_cdouble, npy_clongdouble#
* #c = f,,l#
*/
static NPY_INLINE int
@name@_ctype_positive(@type@ a, @type@ *out)
{
out->real = a.real;
out->imag = a.imag;
return 0;
}
static NPY_INLINE int
@name@_ctype_power(@type@ a, @type@ b, @type@ *out)
{
*out = npy_cpow@c@(a, b);
return 0;
}
/**end repeat**/
/**begin repeat
* #name = ubyte, ushort, uint, ulong, ulonglong#
*/
#define @name@_ctype_absolute @name@_ctype_positive
/**end repeat**/
/**begin repeat
* #name = byte, short, int, long, longlong#
* #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong#
*/
static NPY_INLINE int
@name@_ctype_absolute(@type@ a, @type@ *out)
{
*out = (a < 0 ? -a : a);
return 0;
}
/**end repeat**/
/**begin repeat
* #name = float, double, longdouble#
* #type = npy_float, npy_double, npy_longdouble#
* #c = f,,l#
*/
static NPY_INLINE int
@name@_ctype_absolute(@type@ a, @type@ *out)
{
*out = npy_fabs@c@(a);
return 0;
}
/**end repeat**/
static NPY_INLINE int
half_ctype_absolute(npy_half a, npy_half *out)
{
*out = a&0x7fffu;
return 0;
}
/**begin repeat
* #name = cfloat, cdouble, clongdouble#
* #type = npy_cfloat, npy_cdouble, npy_clongdouble#
* #rtype = npy_float, npy_double, npy_longdouble#
* #c = f,,l#
*/
static NPY_INLINE int
@name@_ctype_absolute(@type@ a, @rtype@ *out)
{
*out = npy_cabs@c@(a);
return 0;
}
/**end repeat**/
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint, long,
* ulong, longlong, ulonglong#
*/
static NPY_INLINE int
@name@_ctype_invert(npy_@name@ a, npy_@name@ *out)
{
*out = ~a;
return 0;
}
/**end repeat**/
/*** END OF BASIC CODE **/
/*
* How binary operators work
* -------------------------
*
* All binary (numeric) operators use the larger of the two types, with the
* exception of unsigned int and signed int mixed cases which must promote
* to a larger type.
*
* The strategy employed for all binary operation is that we coerce the other
* scalar if it is safe to do. E.g. `float64 + float32` the `float64` can
* convert `float32` and do the operation as `float64 + float64`.
* OTOH, for `float32 + float64` it is safe, and we should defer to `float64`.
*
* So we have multiple possible paths:
* - The other scalar is a subclass. In principle *both* inputs could be
* different subclasses. In this case it would make sense to defer, but
* Python's `int` does not try this as well, so we do not here:
*
* class A(int): pass
* class B(int):
* def __add__(self, other): return "b"
* __radd__ = __add__
*
* A(1) + B(1) # return 2
* B(1) + A(1) # return "b"
*
* - The other scalar can be converted: All is good, we do the operation
* - The other scalar cannot be converted, there are two possibilities:
* - The reverse should work, so we return NotImplemented to defer.
* (If self is a subclass, this will end up in the "unknown" path.)
* - Neither works (e.g. `uint8 + int8`): We currently use the array path.
* - The other object is a unknown. It could be either a scalar, an array,
* or an array-like (including a list!). Because NumPy scalars pretend to be
* arrays we fall into the array fallback path here _normally_ (through
* the generic scalar path).
* First we check if we should defer, though.
*
* The last possibility is awkward and leads to very confusing situations.
* The problem is that usually we should defer (return NotImplemented)
* in that path.
* If the other object is a NumPy array (or array-like) it will know what to
* do. If NumPy knows that it is a scalar (not generic `object`), then it
* would make sense to try and use the "array path" (i.e. deal with it
* using the ufunc machinery).
*
* But this overlooks two things that currently work:
*
* 1. `np.float64(3) * [1, 2, 3]` happily returns an array result.
* 2. `np.int32(3) * decimal.Decimal(3)` works! (see below)
*
* The first must work, because scalars pretend to be arrays. Which means
* they inherit the greedy "convert the other object to an array" logic.
* This may be a questionable choice, but is fine.
* (As of now, it is not negotiable, since NumPy often converts 0-D arrays
* to scalars.)
*
* The second one is more confusing. This works also by using the ufunc
* machinery (array path), but it works because:
*
* np.add(np.int32(3), decimal.Decimal(3))
*
* Will convert the `int32` to an int32 array, and the decimal to an object
* array. It then *casts* the `int32` array to an object array.
* The casting step CONVERTS the integer to a Python integer. The ufunc object
* loop will then call back into Python scalar logic.
*
* The above would be recursive, if it was not for the conversion of the int32
* to a Python integer!
* This leads us to the EXCEEDINGLY IMPORTANT special case:
*
* WARNING: longdouble and clongdouble do NOT convert to a Python scalar
* when cast to object. Thus they MUST NEVER take the array-path.
* However, they STILL should defer at least for
* `np.longdouble(3) + array`.
*
*
* As a general note, in the above we defer exactly when we know that deferring
* will work. `longdouble` uses the "simple" logic of generally deferring
* though, because it would otherwise easily run into an infinite recursion.
*
*
* The future?!
* ------------
*
* This is very tricky and it would be nice to formalize away that "recursive"
* path we currently use. I (seberg) have currently no great idea on this,
* this is more brainstorming!
*
* If both are scalars (known to NumPy), they have a DType and we may be able
* to do the ufunc promotion to make sure there is no risk of recursion.
*
* In principle always deferring would probably be clean. But we likely cannot
* do that? There is also an issue that it is nice that we allow adding a
* DType for an existing Python scalar (which will not know about NumPy
* scalars).
* The DType/ufunc machinery teaches NumPy how arrays will work with that
* Python scalar, but the DType may need to help us decide whether we should
* defer (return NotImplemented) or try using the ufunc machinery (or a
* simplified ufunc-like machinery limited to scalars).
*/
/*
* Enum used to describe the space of possibilities when converting the second
* argument to a binary operation.
* Any of these flags may be combined with the return flag of
* `may_need_deferring` indicating that the other is any type of object which
* may e.g. define an `__array_priority__`.
*/
typedef enum {
/* An error occurred (should not really happen/be possible) */
CONVERSION_ERROR = -1,
/* A known NumPy scalar, but of higher precision: we defer */
DEFER_TO_OTHER_KNOWN_SCALAR,
/*
* Conversion was successful (known scalar of less precision). Note that
* the other value may still be a subclass of such a scalar so even here
* we may have to check for deferring.
* More specialized subclass handling, which defers based on whether the
* subclass has an implementation, plausible but complicated.
* We do not do it, as even CPython does not do it for the builtin `int`.
*/
CONVERSION_SUCCESS,
/*
* Other object is an unkown scalar or array-like, we (typically) use
* the generic path, which normally ends up in the ufunc machinery.
*/
OTHER_IS_UNKNOWN_OBJECT,
/*
* Promotion necessary
*/
PROMOTION_REQUIRED,
} conversion_result;
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* half, float, double, longdouble,
* cfloat, cdouble, clongdouble#
* #Name = Byte, UByte, Short, UShort, Int, UInt,
* Long, ULong, LongLong, ULongLong,
* Half, Float, Double, LongDouble,
* CFloat, CDouble, CLongDouble#
* #TYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG,
* HALF, FLOAT, DOUBLE, LONGDOUBLE,
* CFLOAT, CDOUBLE, CLONGDOUBLE#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_half, npy_float, npy_double, npy_longdouble,
* npy_cfloat, npy_cdouble, npy_clongdouble#
*/
#define IS_@TYPE@ 1
#define IS_SAFE(FROM, TO) _npy_can_cast_safely_table[FROM][TO]
/*
* TODO: This whole thing is awkward, and we should create a helper header to
* define inline functions that convert single elements for all numeric
* types. That could then also be used to define all cast loops.
* (Even if that may get more complex for SIMD at some point.)
* For now, half casts could be optimized because of that.
*/
#if defined(IS_HALF)
#define CONVERT_TO_RESULT(value) \
*result = npy_float_to_half((float)(value))
#elif defined(IS_CFLOAT) || defined(IS_CDOUBLE) || defined(IS_CLONGDOUBLE)
#define CONVERT_TO_RESULT(value) \
result->real = value; \
result->imag = 0
#else
#define CONVERT_TO_RESULT(value) *result = value
#endif
#define GET_VALUE_OR_DEFER(OTHER, Other, value) \
case NPY_##OTHER: \
if (IS_SAFE(NPY_##OTHER, NPY_@TYPE@)) { \
CONVERT_TO_RESULT(PyArrayScalar_VAL(value, Other)); \
ret = CONVERSION_SUCCESS; \
} \
else if (IS_SAFE(NPY_@TYPE@, NPY_##OTHER)) { \
/*
* If self can cast safely to other, this is clear:
* we should definitely defer.
*/ \
ret = DEFER_TO_OTHER_KNOWN_SCALAR; \
} \
else { \
/* Otherwise, we must promote */ \
ret = PROMOTION_REQUIRED; \
} \
break;
/*
* Complex to complex (and rejecting complex to real) is a bit different:
*/
#if defined(IS_CFLOAT) || defined(IS_CDOUBLE) || defined(IS_CLONGDOUBLE)
#define GET_CVALUE_OR_DEFER(OTHER, Other, value) \
case NPY_##OTHER: \
if (IS_SAFE(NPY_##OTHER, NPY_@TYPE@)) { \
assert(Py_TYPE(value) == &Py##Other##ArrType_Type); \
result->real = PyArrayScalar_VAL(value, Other).real; \
result->imag = PyArrayScalar_VAL(value, Other).imag; \
ret = 1; \
} \
else if (IS_SAFE(NPY_@TYPE@, NPY_##OTHER)) { \
ret = DEFER_TO_OTHER_KNOWN_SCALAR; \
} \
else { \
ret = PROMOTION_REQUIRED; \
} \
break;
#else
/* Getting a complex value to real is never safe: */
#define GET_CVALUE_OR_DEFER(OTHER, Other, value) \
case NPY_##OTHER: \
if (IS_SAFE(NPY_@TYPE@, NPY_##OTHER)) { \
ret = DEFER_TO_OTHER_KNOWN_SCALAR; \
} \
else { \
ret = PROMOTION_REQUIRED; \
} \
break;
#endif
/**
* Convert the value to the own type and and store the result.
*
* @param value The value to convert (if compatible)
* @param result The result value (output)
* @param may_need_deferring Set to `NPY_TRUE` when the caller must check
* `BINOP_GIVE_UP_IF_NEEDED` (or similar) due to possible implementation
* of `__array_priority__` (or similar).
* This is set for unknown objects and all subclasses even when they
* can be handled.
* @result The result value indicating what we did with `value` or what type
* of object it is (see `conversion_result`).
*/
static NPY_INLINE conversion_result
convert_to_@name@(PyObject *value, @type@ *result, npy_bool *may_need_deferring)
{
PyArray_Descr *descr;
*may_need_deferring = NPY_FALSE;
if (Py_TYPE(value) == &Py@Name@ArrType_Type) {
*result = PyArrayScalar_VAL(value, @Name@);
return CONVERSION_SUCCESS;
}
/* Optimize the identical scalar specifically. */
if (PyArray_IsScalar(value, @Name@)) {
*result = PyArrayScalar_VAL(value, @Name@);
/*
* In principle special, assyemetric, handling could be possible for
* explicit subclasses.
* In practice, we just check the normal deferring logic.
*/
*may_need_deferring = NPY_TRUE;
return CONVERSION_SUCCESS;
}
/*
* Then we check for the basic Python types float, int, and complex.
* (this is a bit tedious to do right for complex).
*/
if (PyBool_Check(value)) {
CONVERT_TO_RESULT(value == Py_True);
return CONVERSION_SUCCESS;
}
if (PyFloat_Check(value)) {
if (!PyFloat_CheckExact(value)) {
/* A NumPy double is a float subclass, but special. */
if (PyArray_IsScalar(value, Double)) {
descr = PyArray_DescrFromType(NPY_DOUBLE);
goto numpy_scalar;
}
*may_need_deferring = NPY_TRUE;
}
if (!IS_SAFE(NPY_DOUBLE, NPY_@TYPE@)) {
return PROMOTION_REQUIRED;
}
CONVERT_TO_RESULT(PyFloat_AS_DOUBLE(value));
return CONVERSION_SUCCESS;
}
if (PyLong_Check(value)) {
if (!PyLong_CheckExact(value)) {
*may_need_deferring = NPY_TRUE;
}
if (!IS_SAFE(NPY_LONG, NPY_@TYPE@)) {
/*
* long -> (c)longdouble is safe, so `THER_IS_UNKNOWN_OBJECT` will
* be returned below for huge integers.
*/
return PROMOTION_REQUIRED;
}
int overflow;
long val = PyLong_AsLongAndOverflow(value, &overflow);
if (overflow) {
return OTHER_IS_UNKNOWN_OBJECT; /* handle as if arbitrary object */
}
if (error_converting(val)) {
return CONVERSION_ERROR; /* should not be possible */
}
CONVERT_TO_RESULT(val);
return CONVERSION_SUCCESS;
}
if (PyComplex_Check(value)) {
if (!PyComplex_CheckExact(value)) {
/* A NumPy complex double is a float subclass, but special. */
if (PyArray_IsScalar(value, CDouble)) {
descr = PyArray_DescrFromType(NPY_CDOUBLE);
goto numpy_scalar;
}
*may_need_deferring = NPY_TRUE;
}
if (!IS_SAFE(NPY_CDOUBLE, NPY_@TYPE@)) {
return PROMOTION_REQUIRED;
}
#if defined(IS_CFLOAT) || defined(IS_CDOUBLE) || defined(IS_CLONGDOUBLE)
Py_complex val = PyComplex_AsCComplex(value);
if (error_converting(val.real)) {
return CONVERSION_ERROR; /* should not be possible */
}
result->real = val.real;
result->imag = val.imag;
return CONVERSION_SUCCESS;
#else
/* unreachable, always unsafe cast above; return to avoid warning */
assert(0);
return OTHER_IS_UNKNOWN_OBJECT;
#endif /* defined(IS_CFLOAT) || ... */
}
/*
* (seberg) It would be nice to use `PyArray_DiscoverDTypeFromScalarType`