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 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
|
#include "Python.h"
#include "structmember.h"
#include "numpy/noprefix.h"
#include "math.h"
#include "complex_functions.inc"
#ifdef _WIN32
#define inline __inline
#endif
#define BLOCK_SIZE1 128
#define BLOCK_SIZE2 8
/* This file and interp_body should really be generated from a description of
the opcodes -- there's too much repetition here for manually editing */
enum OpCodes {
OP_NOOP = 0,
OP_COPY_BB,
OP_INVERT_BB,
OP_AND_BBB,
OP_OR_BBB,
OP_GT_BII,
OP_GE_BII,
OP_EQ_BII,
OP_NE_BII,
OP_GT_BFF,
OP_GE_BFF,
OP_EQ_BFF,
OP_NE_BFF,
OP_CAST_IB,
OP_COPY_II,
OP_ONES_LIKE_II,
OP_NEG_II,
OP_ADD_III,
OP_SUB_III,
OP_MUL_III,
OP_DIV_III,
OP_POW_III,
OP_MOD_III,
OP_WHERE_IFII,
OP_CAST_FB,
OP_CAST_FI,
OP_COPY_FF,
OP_ONES_LIKE_FF,
OP_NEG_FF,
OP_ADD_FFF,
OP_SUB_FFF,
OP_MUL_FFF,
OP_DIV_FFF,
OP_POW_FFF,
OP_MOD_FFF,
OP_SIN_FF,
OP_COS_FF,
OP_TAN_FF,
OP_SQRT_FF,
OP_ARCTAN2_FFF,
OP_WHERE_FFFF,
OP_FUNC_FF,
OP_FUNC_FFF,
OP_EQ_BCC,
OP_NE_BCC,
OP_CAST_CB,
OP_CAST_CI,
OP_CAST_CF,
OP_ONES_LIKE_CC,
OP_COPY_CC,
OP_NEG_CC,
OP_ADD_CCC,
OP_SUB_CCC,
OP_MUL_CCC,
OP_DIV_CCC,
OP_WHERE_CFCC,
OP_FUNC_CC,
OP_FUNC_CCC,
OP_REAL_FC,
OP_IMAG_FC,
OP_COMPLEX_CFF,
OP_REDUCTION,
OP_SUM,
OP_SUM_IIN,
OP_SUM_FFN,
OP_SUM_CCN,
OP_PROD,
OP_PROD_IIN,
OP_PROD_FFN,
OP_PROD_CCN
};
/* returns the sig of the nth op, '\0' if no more ops -1 on failure */
static int
op_signature(int op, int n) {
switch (op) {
case OP_NOOP:
break;
case OP_COPY_BB:
if (n == 0 || n == 1) return 'b';
break;
case OP_INVERT_BB:
if (n == 0 || n == 1) return 'b';
break;
case OP_AND_BBB:
case OP_OR_BBB:
if (n == 0 || n == 1 || n == 2) return 'b';
break;
case OP_GT_BII:
case OP_GE_BII:
case OP_EQ_BII:
case OP_NE_BII:
if (n == 0) return 'b';
if (n == 1 || n == 2) return 'i';
break;
case OP_GT_BFF:
case OP_GE_BFF:
case OP_EQ_BFF:
case OP_NE_BFF:
if (n == 0) return 'b';
if (n == 1 || n == 2) return 'f';
break;
case OP_CAST_IB:
if (n == 0) return 'i';
if (n == 1) return 'b';
break;
case OP_COPY_II:
case OP_ONES_LIKE_II:
case OP_NEG_II:
if (n == 0 || n == 1) return 'i';
break;
case OP_ADD_III:
case OP_SUB_III:
case OP_MUL_III:
case OP_DIV_III:
case OP_MOD_III:
case OP_POW_III:
if (n == 0 || n == 1 || n == 2) return 'i';
break;
case OP_WHERE_IFII:
if (n == 0 || n == 2 || n == 3) return 'i';
if (n == 1) return 'f';
break;
case OP_CAST_FB:
if (n == 0) return 'f';
if (n == 1) return 'b';
break;
case OP_CAST_FI:
if (n == 0) return 'f';
if (n == 1) return 'i';
break;
case OP_COPY_FF:
case OP_ONES_LIKE_FF:
case OP_NEG_FF:
case OP_SIN_FF:
case OP_COS_FF:
case OP_TAN_FF:
case OP_SQRT_FF:
if (n == 0 || n == 1) return 'f';
break;
case OP_ADD_FFF:
case OP_SUB_FFF:
case OP_MUL_FFF:
case OP_DIV_FFF:
case OP_POW_FFF:
case OP_MOD_FFF:
case OP_ARCTAN2_FFF:
if (n == 0 || n == 1 || n == 2) return 'f';
break;
case OP_WHERE_FFFF:
if (n == 0 || n == 1 || n == 2 || n == 3) return 'f';
break;
case OP_FUNC_FF:
if (n == 0 || n == 1) return 'f';
if (n == 2) return 'n';
break;
case OP_FUNC_FFF:
if (n == 0 || n == 1 || n == 2) return 'f';
if (n == 3) return 'n';
break;
case OP_EQ_BCC:
case OP_NE_BCC:
if (n == 0) return 'b';
if (n == 1 || n == 2) return 'c';
break;
case OP_CAST_CB:
if (n == 0) return 'c';
if (n == 1) return 'b';
break;
case OP_CAST_CI:
if (n == 0) return 'c';
if (n == 1) return 'i';
break;
case OP_CAST_CF:
if (n == 0) return 'c';
if (n == 1) return 'f';
break;
case OP_COPY_CC:
case OP_ONES_LIKE_CC:
case OP_NEG_CC:
if (n == 0 || n == 1) return 'c';
break;
case OP_ADD_CCC:
case OP_SUB_CCC:
case OP_MUL_CCC:
case OP_DIV_CCC:
if (n == 0 || n == 1 || n == 2) return 'c';
break;
case OP_WHERE_CFCC:
if (n == 0 || n == 2 || n == 3) return 'c';
if (n == 1) return 'f';
break;
case OP_FUNC_CC:
if (n == 0 || n == 1) return 'c';
if (n == 2) return 'n';
break;
case OP_FUNC_CCC:
if (n == 0 || n == 1 || n == 2) return 'c';
if (n == 3) return 'n';
break;
case OP_REAL_FC:
case OP_IMAG_FC:
if (n == 0) return 'f';
if (n == 1) return 'c';
break;
case OP_COMPLEX_CFF:
if (n == 0) return 'c';
if (n == 1 || n == 2) return 'f';
break;
case OP_PROD_IIN:
case OP_SUM_IIN:
if (n == 0 || n == 1) return 'i';
if (n == 2) return 'n';
break;
case OP_PROD_FFN:
case OP_SUM_FFN:
if (n == 0 || n == 1) return 'f';
if (n == 2) return 'n';
break;
case OP_PROD_CCN:
case OP_SUM_CCN:
if (n == 0 || n == 1) return 'c';
if (n == 2) return 'n';
break;
default:
return -1;
break;
}
return 0;
}
/*
Lots of functions still to be added: exp, ln, log10, etc, etc. Still not
sure which get there own opcodes and which get relegated to loopup table.
Some functions at least (sin and arctan2 for instance) seem to have a large
slowdown when run through lookup table. Not entirely sure why.
To add a function to the lookup table, add to FUNC_CODES (first
group is 1-arg functions, second is 2-arg functions), also to
functions_f or functions_ff as appropriate. Finally, use add_func
down below to add to funccodes. Functions with more arguments
aren't implemented at present, but should be easy; just copy the 1-
or 2-arg case.
To add a function opcode, just copy OP_SIN or OP_ARCTAN2.
Some functions are repeated in this table that are opcodes, but there's
no problem with that as the compiler selects opcodes over functions,
and this makes it easier to compare opcode vs. function speeds.
*/
enum FuncFFCodes {
FUNC_SQRT_FF = 0,
FUNC_SIN_FF,
FUNC_COS_FF,
FUNC_TAN_FF,
FUNC_ARCSIN_FF,
FUNC_ARCCOS_FF,
FUNC_ARCTAN_FF,
FUNC_SINH_FF,
FUNC_COSH_FF,
FUNC_TANH_FF,
FUNC_FF_LAST
};
typedef double (*FuncFFPtr)(double);
/* The order of this array must match the FuncFFCodes enum above */
FuncFFPtr functions_f[] = {
sqrt,
sin,
cos,
tan,
asin,
acos,
atan,
sinh,
cosh,
tanh,
};
enum FuncFFFCodes {
FUNC_FMOD_FFF = 0,
FUNC_FFF_LAST
};
typedef double (*FuncFFFPtr)(double, double);
FuncFFFPtr functions_ff[] = {
fmod,
};
enum FuncCCCodes {
FUNC_SQRT_CC = 0,
FUNC_SIN_CC,
FUNC_COS_CC,
FUNC_TAN_CC,
FUNC_ARCSIN_CC,
FUNC_ARCCOS_CC,
FUNC_ARCTAN_CC,
FUNC_SINH_CC,
FUNC_COSH_CC,
FUNC_TANH_CC,
FUNC_CC_LAST
};
typedef void (*FuncCCPtr)(cdouble*, cdouble*);
/* The order of this array must match the FuncCCCodes enum above */
FuncCCPtr functions_cc[] = {
nc_sqrt,
nc_sin,
nc_cos,
nc_tan,
nc_asin,
nc_acos,
nc_atan,
nc_sinh,
nc_cosh,
nc_tanh,
};
enum FuncCCCCodes {
FUNC_POW_CCC = 0,
FUNC_CCC_LAST
};
typedef void (*FuncCCCPtr)(cdouble*, cdouble*, cdouble*);
FuncCCCPtr functions_ccc[] = {
nc_pow,
};
typedef struct
{
PyObject_HEAD
PyObject *signature; /* a python string */
PyObject *tempsig;
PyObject *constsig;
PyObject *fullsig;
PyObject *program; /* a python string */
PyObject *constants; /* a tuple of int/float/complex */
PyObject *input_names; /* tuple of strings */
char **mem; /* pointers to registers */
char *rawmem; /* a chunks of raw memory for storing registers */
intp *memsteps;
int rawmemsize;
} NumExprObject;
static void
NumExpr_dealloc(NumExprObject *self)
{
Py_XDECREF(self->signature);
Py_XDECREF(self->tempsig);
Py_XDECREF(self->constsig);
Py_XDECREF(self->fullsig);
Py_XDECREF(self->program);
Py_XDECREF(self->constants);
Py_XDECREF(self->input_names);
PyMem_Del(self->mem);
PyMem_Del(self->rawmem);
PyMem_Del(self->memsteps);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject *
NumExpr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
NumExprObject *self = (NumExprObject *)type->tp_alloc(type, 0);
if (self != NULL) {
#define INIT_WITH(name, object) \
self->name = object; \
if (!self->name) { \
Py_DECREF(self); \
return NULL; \
}
INIT_WITH(signature, PyString_FromString(""));
INIT_WITH(tempsig, PyString_FromString(""));
INIT_WITH(constsig, PyString_FromString(""));
INIT_WITH(fullsig, PyString_FromString(""));
INIT_WITH(program, PyString_FromString(""));
INIT_WITH(constants, PyTuple_New(0));
Py_INCREF(Py_None);
self->input_names = Py_None;
self->mem = NULL;
self->rawmem = NULL;
self->memsteps = NULL;
self->rawmemsize = 0;
#undef INIT_WITH
}
return (PyObject *)self;
}
static char
get_return_sig(PyObject* program) {
int sig;
char last_opcode;
int end = PyString_Size(program);
do {
end -= 4;
if (end < 0) return 'X';
}
while ((last_opcode = PyString_AS_STRING(program)[end]) == OP_NOOP);
sig = op_signature(last_opcode, 0);
if (sig <= 0) {
return 'X';
} else {
return (char)sig;
}
}
static int
size_from_char(char c)
{
switch (c) {
case 'b': return sizeof(char);
case 'i': return sizeof(long);
case 'f': return sizeof(double);
case 'c': return 2*sizeof(double);
default:
PyErr_SetString(PyExc_TypeError, "signature value not in 'bifc'");
return -1;
}
}
static int
size_from_sig(PyObject *o)
{
intp size = 0;
char *s = PyString_AsString(o);
if (!s) return -1;
for (; *s != '\0'; s++) {
int x = size_from_char(*s);
if (x == -1) return -1;
size += x;
}
return size;
}
static int
typecode_from_char(char c)
{
switch (c) {
case 'b': return PyArray_BOOL;
case 'i': return PyArray_LONG;
case 'f': return PyArray_DOUBLE;
case 'c': return PyArray_CDOUBLE;
default:
PyErr_SetString(PyExc_TypeError, "signature value not in 'ifc'");
return -1;
}
}
static int
last_opcode(PyObject *program_object) {
Py_ssize_t n;
unsigned char *program;
PyString_AsStringAndSize(program_object, (char **)&program, &n);
return program[n-4];
}
static int
get_reduction_axis(PyObject* program) {
char last_opcode, sig;
int end = PyString_Size(program);
int axis = ((unsigned char *)PyString_AS_STRING(program))[end-1];
if (axis != 255 && axis >= MAX_DIMS)
axis = MAX_DIMS - axis;
return axis;
}
static int
check_program(NumExprObject *self)
{
unsigned char *program;
Py_ssize_t prog_len, n_buffers, n_inputs;
int rno, pc, arg, argloc, argno, sig;
char *fullsig, *signature;
if (PyString_AsStringAndSize(self->program, (char **)&program,
&prog_len) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read program");
return -1;
}
if (prog_len % 4 != 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: prog_len mod 4 != 0");
return -1;
}
if (PyString_AsStringAndSize(self->fullsig, (char **)&fullsig,
&n_buffers) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read fullsig");
return -1;
}
if (PyString_AsStringAndSize(self->signature, (char **)&signature,
&n_inputs) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read signature");
return -1;
}
if (n_buffers > 255) {
PyErr_Format(PyExc_RuntimeError, "invalid program: too many buffers");
return -1;
}
for (rno = n_inputs+1; rno < n_buffers; rno++) {
char *bufend = self->mem[rno] + BLOCK_SIZE1 * size_from_char(fullsig[rno]);
if ( (bufend - self->rawmem) > self->rawmemsize) {
PyErr_Format(PyExc_RuntimeError, "invalid program: too many buffers");
return -1;
}
}
for (pc = 0; pc < prog_len; pc += 4) {
unsigned int op = program[pc];
if (op == OP_NOOP) {
continue;
}
if ((op >= OP_REDUCTION) && pc != prog_len-4) {
PyErr_Format(PyExc_RuntimeError,
"invalid program: reduction operations must occur last");
return -1;
}
for (argno = 0; ; argno++) {
sig = op_signature(op, argno);
if (sig == -1) {
PyErr_Format(PyExc_RuntimeError, "invalid program: illegal opcode at %i (%c)", pc, op);
return -1;
}
if (sig == 0) break;
if (argno < 3) {
argloc = pc+argno+1;
}
if (argno >= 3) {
if (pc + 1 >= prog_len) {
PyErr_Format(PyExc_RuntimeError, "invalid program: double opcode (%c) at end (%i)", pc, sig);
return -1;
}
argloc = pc+argno+2;
}
arg = program[argloc];
if (sig != 'n' && (arg >= n_buffers) || (arg < 0)) {
PyErr_Format(PyExc_RuntimeError, "invalid program: buffer out of range (%i) at %i", arg, argloc);
return -1;
}
if (sig == 'n') {
if (op == OP_FUNC_FF) {
if (arg < 0 || arg >= FUNC_FF_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_FFF) {
if (arg < 0 || arg >= FUNC_FFF_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_CC) {
if (arg < 0 || arg >= FUNC_CC_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_CCC) {
if (arg < 0 || arg >= FUNC_CCC_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op >= OP_REDUCTION) {
;
} else {
PyErr_Format(PyExc_RuntimeError, "invalid program: internal checker errror processing %i", argloc);
return -1;
}
} else if (sig != fullsig[arg]) {
PyErr_Format(PyExc_RuntimeError,
"invalid : opcode signature doesn't match buffer (%c vs %c) at %i", sig, fullsig[arg], argloc);
return -1;
}
}
}
return 0;
}
static int
NumExpr_init(NumExprObject *self, PyObject *args, PyObject *kwds)
{
int i, j, mem_offset;
int n_constants, n_inputs, n_temps;
PyObject *signature = NULL, *tempsig = NULL, *constsig = NULL;
PyObject *fullsig = NULL, *program = NULL, *constants = NULL;
PyObject *input_names = NULL, *o_constants = NULL;
char **mem = NULL, *rawmem = NULL;
intp *memsteps;
int rawmemsize;
static char *kwlist[] = {"signature", "tempsig",
"program", "constants",
"input_names", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "SSS|OO", kwlist,
&signature,
&tempsig,
&program, &o_constants,
&input_names)) {
return -1;
}
n_inputs = PyString_Size(signature);
n_temps = PyString_Size(tempsig);
if (o_constants) {
if (!PySequence_Check(o_constants) ) {
PyErr_SetString(PyExc_TypeError, "constants must be a sequence");
return -1;
}
n_constants = PySequence_Length(o_constants);
if (!(constants = PyTuple_New(n_constants)))
return -1;
if (!(constsig = PyString_FromStringAndSize(NULL, n_constants))) {
Py_DECREF(constants);
return -1;
}
for (i = 0; i < n_constants; i++) {
PyObject *o;
if (!(o = PySequence_GetItem(o_constants, i))) { /* new reference */
Py_DECREF(constants);
Py_DECREF(constsig);
return -1;
}
PyTuple_SET_ITEM(constants, i, o); /* steals reference */
if (PyBool_Check(o)) {
PyString_AS_STRING(constsig)[i] = 'b';
continue;
}
if (PyInt_Check(o)) {
PyString_AS_STRING(constsig)[i] = 'i';
continue;
}
if (PyFloat_Check(o)) {
PyString_AS_STRING(constsig)[i] = 'f';
continue;
}
if (PyComplex_Check(o)) {
PyString_AS_STRING(constsig)[i] = 'c';
continue;
}
PyErr_SetString(PyExc_TypeError, "constants must be of type int/float/complex");
Py_DECREF(constsig);
Py_DECREF(constants);
return -1;
}
} else {
n_constants = 0;
if (!(constants = PyTuple_New(0)))
return -1;
if (!(constsig = PyString_FromString(""))) {
Py_DECREF(constants);
return -1;
}
}
fullsig = PyString_FromFormat("%c%s%s%s", get_return_sig(program),
PyString_AS_STRING(signature), PyString_AS_STRING(constsig),
PyString_AS_STRING(tempsig));
if (!fullsig) {
Py_DECREF(constants);
Py_DECREF(constsig);
}
if (!input_names) {
input_names = Py_None;
}
rawmemsize = BLOCK_SIZE1 * (size_from_sig(constsig) + size_from_sig(tempsig));
mem = PyMem_New(char *, 1 + n_inputs + n_constants + n_temps);
rawmem = PyMem_New(char, rawmemsize);
memsteps = PyMem_New(int, 1 + n_inputs + n_constants + n_temps);
if (!mem || !rawmem || !memsteps) {
Py_DECREF(constants);
Py_DECREF(constsig);
Py_DECREF(fullsig);
PyMem_Del(mem);
PyMem_Del(rawmem);
PyMem_Del(memsteps);
return -1;
}
/*
0 -> output
[1, n_inputs+1) -> inputs
[n_inputs+1, n_inputs+n_consts+1) -> constants
[n_inputs+n_consts+1, n_inputs+n_consts+n_temps+1) -> temps
*/
/* Fill in 'mem' and 'rawmem' for constants */
mem_offset = 0;
for (i = 0; i < n_constants; i++) {
char c = PyString_AS_STRING(constsig)[i];
int size = size_from_char(c);
mem[i+n_inputs+1] = rawmem + mem_offset;
mem_offset += BLOCK_SIZE1 * size;
memsteps[i+n_inputs+1] = size;
/* fill in the constants */
if (c == 'b') {
char *bmem = (char*)mem[i+n_inputs+1];
char value = (char)PyInt_AS_LONG(PyTuple_GET_ITEM(constants, i));
for (j = 0; j < BLOCK_SIZE1; j++) {
bmem[j] = value;
}
} else if (c == 'i') {
long *imem = (long*)mem[i+n_inputs+1];
long value = PyInt_AS_LONG(PyTuple_GET_ITEM(constants, i));
for (j = 0; j < BLOCK_SIZE1; j++) {
imem[j] = value;
}
} else if (c == 'f') {
double *dmem = (double*)mem[i+n_inputs+1];
double value = PyFloat_AS_DOUBLE(PyTuple_GET_ITEM(constants, i));
for (j = 0; j < BLOCK_SIZE1; j++) {
dmem[j] = value;
}
} else if (c == 'c') {
double *cmem = (double*)mem[i+n_inputs+1];
Py_complex value = PyComplex_AsCComplex(PyTuple_GET_ITEM(constants, i));
for (j = 0; j < 2*BLOCK_SIZE1; j+=2) {
cmem[j] = value.real;
cmem[j+1] = value.imag;
}
}
}
/* Fill in 'mem' for temps */
for (i = 0; i < n_temps; i++) {
int size = size_from_char(PyString_AS_STRING(tempsig)[i]);
mem[i+n_inputs+n_constants+1] = rawmem + mem_offset;
mem_offset += BLOCK_SIZE1 * size;
memsteps[i+n_inputs+n_constants+1] = size;
}
/* See if any errors occured (e.g., in size_from_char) or if mem_offset is wrong */
if (PyErr_Occurred() || mem_offset != rawmemsize) {
if (mem_offset != rawmemsize) {
PyErr_Format(PyExc_RuntimeError, "mem_offset does not match rawmemsize");
}
Py_DECREF(constants);
Py_DECREF(constsig);
Py_DECREF(fullsig);
PyMem_Del(mem);
PyMem_Del(rawmem);
PyMem_Del(memsteps);
return -1;
}
#define REPLACE_OBJ(arg) \
{PyObject *tmp = self->arg; \
self->arg = arg; \
Py_XDECREF(tmp);}
#define INCREF_REPLACE_OBJ(arg) {Py_INCREF(arg); REPLACE_OBJ(arg);}
#define REPLACE_MEM(arg) {PyMem_Del(self->arg); self->arg=arg;}
INCREF_REPLACE_OBJ(signature);
INCREF_REPLACE_OBJ(tempsig);
REPLACE_OBJ(constsig);
REPLACE_OBJ(fullsig);
INCREF_REPLACE_OBJ(program);
REPLACE_OBJ(constants);
INCREF_REPLACE_OBJ(input_names);
REPLACE_MEM(mem);
REPLACE_MEM(rawmem);
REPLACE_MEM(memsteps);
self->rawmemsize = rawmemsize;
#undef REPLACE_OBJ
#undef INCREF_REPLACE_OBJ
#undef REPLACE_MEM
return check_program(self);
}
static PyMemberDef NumExpr_members[] = {
{"signature", T_OBJECT_EX, offsetof(NumExprObject, signature), READONLY, NULL},
{"constsig", T_OBJECT_EX, offsetof(NumExprObject, constsig), READONLY, NULL},
{"tempsig", T_OBJECT_EX, offsetof(NumExprObject, tempsig), READONLY, NULL},
{"fullsig", T_OBJECT_EX, offsetof(NumExprObject, fullsig), READONLY, NULL},
{"program", T_OBJECT_EX, offsetof(NumExprObject, program), READONLY, NULL},
{"constants", T_OBJECT_EX, offsetof(NumExprObject, constants),
READONLY, NULL},
{"input_names", T_OBJECT, offsetof(NumExprObject, input_names), 0, NULL},
{NULL},
};
struct index_data {
int count;
int size;
int findex;
int *shape;
int *strides;
int *index;
char *buffer;
};
struct vm_params {
int prog_len;
unsigned char *program;
unsigned int n_inputs;
unsigned int r_end;
char *output;
char **inputs;
char **mem;
intp *memsteps;
struct index_data *index_data;
};
static inline unsigned int
flat_index(struct index_data *id, unsigned int j) {
int i, k = id->count - 1;
unsigned int findex = id->findex;
if (k < 0) return 0;
if (findex == -1) {
findex = 0;
for (i = 0; i < id->count; i++)
findex += id->strides[i] * id->index[i];
}
id->index[k] += 1;
if (id->index[k] >= id->shape[k]) {
while (id->index[k] >= id->shape[k]) {
id->index[k] -= id->shape[k];
if (k < 1) break;
id->index[--k] += 1;
}
id->findex = -1;
} else {
id->findex = findex + id->strides[k];
}
return findex;
}
#define DO_BOUNDS_CHECK 1
#if DO_BOUNDS_CHECK
#define BOUNDS_CHECK(arg) if ((arg) >= params.r_end) { \
*pc_error = pc; \
return -2; \
}
#else
#define BOUNDS_CHECK(arg)
#endif
static inline int
vm_engine_1(int start, int blen, struct vm_params params, int *pc_error)
{
unsigned int index;
for (index = start; index < blen; index += BLOCK_SIZE1) {
#define VECTOR_SIZE BLOCK_SIZE1
#include "interp_body.c"
#undef VECTOR_SIZE
}
return 0;
}
static inline int
vm_engine_2(int start, int blen, struct vm_params params, int *pc_error)
{
unsigned int index;
for (index = start; index < blen; index += BLOCK_SIZE2) {
#define VECTOR_SIZE BLOCK_SIZE2
#include "interp_body.c"
#undef VECTOR_SIZE
}
return 0;
}
static inline int
vm_engine_rest(int start, int blen, struct vm_params params, int *pc_error)
{
unsigned int index = start;
unsigned int rest = blen - start;
#define VECTOR_SIZE rest
#include "interp_body.c"
#undef VECTOR_SIZE
return 0;
}
static int
run_interpreter(NumExprObject *self, int len, char *output, char **inputs,
struct index_data *index_data, int *pc_error)
{
int r;
Py_ssize_t plen;
unsigned int blen1, blen2;
struct vm_params params;
*pc_error = -1;
if (PyString_AsStringAndSize(self->program, (char **)&(params.program),
&plen) < 0) {
return -1;
}
params.prog_len = plen;
if ((params.n_inputs = PyObject_Length(self->signature)) == -1)
return -1;
params.output = output;
params.inputs = inputs;
params.index_data = index_data;
params.mem = self->mem;
params.memsteps = self->memsteps;
params.r_end = PyString_Size(self->fullsig);
blen1 = len - len % BLOCK_SIZE1;
r = vm_engine_1(0, blen1, params, pc_error);
if (r < 0) return r;
if (len != blen1) {
blen2 = len - len % BLOCK_SIZE2;
r = vm_engine_2(blen1, blen2, params, pc_error);
if (r < 0) return r;
if (len != blen2) {
r = vm_engine_rest(blen2, len, params, pc_error);
if (r < 0) return r;
}
}
return 0;
}
/* keyword arguments are ignored! */
static PyObject *
NumExpr_run(NumExprObject *self, PyObject *args, PyObject *kwds)
{
PyObject *output = NULL, *a_inputs = NULL;
struct index_data *inddata = NULL;
unsigned int n_inputs, n_dimensions = 0;
int shape[MAX_DIMS];
int i, j, size, r, pc_error;
char **inputs = NULL;
intp strides[MAX_DIMS]; /* clean up XXX */
n_inputs = PyTuple_Size(args);
if (PyString_Size(self->signature) != n_inputs) {
return PyErr_Format(PyExc_ValueError,
"number of inputs doesn't match program");
}
if (kwds && PyObject_Length(kwds) > 0) {
return PyErr_Format(PyExc_ValueError,
"keyword arguments are not accepted");
}
/* This is overkill - we shouldn't need to allocate all of this space,
but this makes it easier figure out */
a_inputs = PyTuple_New(3*n_inputs);
if (!a_inputs) goto cleanup_and_exit;
inputs = PyMem_New(char *, n_inputs);
if (!inputs) goto cleanup_and_exit;
inddata = PyMem_New(struct index_data, n_inputs+1);
if (!inddata) goto cleanup_and_exit;
for (i = 0; i < n_inputs+1; i++)
inddata[i].count = 0;
/* First, make sure everything is some sort of array so that we can work
with their shapes. Count dimensions concurrently. */
for (i = 0; i < n_inputs; i++) {
PyObject *o = PyTuple_GET_ITEM(args, i); /* borrowed ref */
PyObject *a;
char c = PyString_AS_STRING(self->signature)[i];
int typecode = typecode_from_char(c);
if (typecode == -1) goto cleanup_and_exit;
/* Convert it just in case of a non-swapped array */
a = PyArray_FROM_OTF(o, typecode, NOTSWAPPED);
if (!a) goto cleanup_and_exit;
PyTuple_SET_ITEM(a_inputs, i, a); /* steals reference */
if (PyArray_NDIM(a) > n_dimensions)
n_dimensions = PyArray_NDIM(a);
}
/* Broadcast all of the inputs to determine the output shape (this will
require some modifications if we later allow a final reduction
operation). If an array has too few dimensions it's shape is padded
with ones fromthe left. All array dimensions must match, or be one. */
for (i = 0; i < n_dimensions; i++)
shape[i] = 1;
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i);
unsigned int ndims = PyArray_NDIM(a);
int delta = n_dimensions - ndims;
for (j = 0; j < ndims; j++) {
unsigned int n = PyArray_DIM(a, j);
if (n == 1 || n == shape[delta+j]) continue;
if (shape[delta+j] == 1)
shape[delta+j] = n;
else {
PyErr_SetString(PyExc_ValueError,
"cannot broadcast inputs to common shape");
goto cleanup_and_exit;
}
}
}
size = PyArray_MultiplyList(shape, n_dimensions);
/* Broadcast indices of all of the arrays. We could improve efficiency
by keeping track of what needs to be broadcast above */
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i);
PyObject *b;
int strides[MAX_DIMS];
int delta = n_dimensions - PyArray_NDIM(a);
if (PyArray_NDIM(a)) {
for (j = 0; j < n_dimensions; j++)
strides[j] = (j < delta || PyArray_DIM(a, j-delta) == 1) ?
0 : PyArray_STRIDE(a, j-delta);
Py_INCREF(PyArray_DESCR(a));
b = PyArray_NewFromDescr(a->ob_type,
PyArray_DESCR(a),
n_dimensions, shape,
strides, PyArray_DATA(a), 0, a);
if (!b) goto cleanup_and_exit;
} else { /* Leave scalars alone */
b = a;
Py_INCREF(b);
}
/* Store b so that it stays alive till we're done */
PyTuple_SET_ITEM(a_inputs, i+n_inputs, b);
}
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i+n_inputs);
PyObject *b;
char c = PyString_AS_STRING(self->signature)[i];
int typecode = typecode_from_char(c);
if (PyArray_NDIM(a) == 0) {
/* Broadcast scalars */
intp dims[1] = {BLOCK_SIZE1};
b = PyArray_SimpleNew(1, dims, typecode);
if (!b) goto cleanup_and_exit;
self->memsteps[i+1] = 0;
PyTuple_SET_ITEM(a_inputs, i+2*n_inputs, b); /* steals reference */
inputs[i] = PyArray_DATA(b);
if (typecode == PyArray_LONG) {
long value = ((long*)PyArray_DATA(a))[0];
for (j = 0; j < BLOCK_SIZE1; j++)
((long*)PyArray_DATA(b))[j] = value;
} else if (typecode == PyArray_DOUBLE) {
double value = ((double*)PyArray_DATA(a))[0];
for (j = 0; j < BLOCK_SIZE1; j++)
((double*)PyArray_DATA(b))[j] = value;
} else if (typecode == PyArray_CDOUBLE) {
double rvalue = ((double*)PyArray_DATA(a))[0];
double ivalue = ((double*)PyArray_DATA(a))[1];
for (j = 0; j < 2*BLOCK_SIZE1; j+=2) {
((double*)PyArray_DATA(b))[j] = rvalue;
((double*)PyArray_DATA(b))[j+1] = ivalue;
}
} else {
PyErr_SetString(PyExc_RuntimeError, "illegal typecode value");
goto cleanup_and_exit;
}
} else {
PyObject *origA = a;
int inner_size = -1;
/* Check array is contiguous */
for (j = PyArray_NDIM(a)-1; j >= 0; j--) {
if ((inner_size == -1 && PyArray_STRIDE(a, j) % PyArray_ITEMSIZE(a)) ||
(inner_size != -1 && PyArray_STRIDE(a, j) != inner_size)) {
intp dims[1] = {BLOCK_SIZE1};
inddata[i+1].count = PyArray_NDIM(a);
inddata[i+1].findex = -1;
inddata[i+1].size = PyArray_ITEMSIZE(a);
inddata[i+1].shape = PyArray_DIMS(a);
inddata[i+1].strides = PyArray_STRIDES(a);
inddata[i+1].buffer = PyArray_BYTES(a);
inddata[i+1].index = PyMem_New(int, inddata[i+1].count);
for (j = 0; j < inddata[i+1].count; j++)
inddata[i+1].index[j] = 0;
a = PyArray_SimpleNew(1, dims, typecode);
PyTuple_SET_ITEM(a_inputs, i+2*n_inputs, a); /* steals reference */
break;
}
inner_size = PyArray_STRIDE(a, j) * PyArray_DIM(a, j);
}
self->memsteps[i+1] = PyArray_STRIDE(a, PyArray_NDIM(a)-1);
inputs[i] = PyArray_DATA(a);
}
}
if (last_opcode(self->program) > OP_REDUCTION) {
char retsig = get_return_sig(self->program);
int axis = get_reduction_axis(self->program);
self->memsteps[0] = 0; /*size_from_char(retsig);*/
if (axis == 255) {
intp dims[1];
for (i = 0; i < n_dimensions; i++)
strides[i] = 0;
output = PyArray_SimpleNew(0, dims, typecode_from_char(retsig));
if (!output) goto cleanup_and_exit;
} else {
intp dims[MAX_DIMS];
if (axis < 0)
axis = n_dimensions + axis;
if (axis < 0 || axis >= n_dimensions) {
PyErr_SetString(PyExc_ValueError, "axis out of range");
goto cleanup_and_exit;
}
for (i = j = 0; i < n_dimensions; i++) {
if (i != axis) {
dims[j] = shape[i];
j += 1;
}
}
output = PyArray_SimpleNew(n_dimensions-1, dims,
typecode_from_char(retsig));
if (!output) goto cleanup_and_exit;
for (i = j = 0; i < n_dimensions; i++) {
if (i != axis) {
strides[i] = PyArray_STRIDES(output)[j];
j += 1;
} else {
strides[i] = 0;
}
}
}
/* TODO optimize strides -- in this and other inddata cases, strides and
shape can be tweaked to minimize the amount of looping */
inddata[0].count = n_dimensions;
inddata[0].findex = -1;
inddata[0].size = PyArray_ITEMSIZE(output);
inddata[0].shape = shape;
inddata[0].strides = strides;
inddata[0].buffer = PyArray_BYTES(output);
inddata[0].index = PyMem_New(int, n_dimensions);
for (j = 0; j < inddata[0].count; j++)
inddata[0].index[j] = 0;
if (last_opcode(self->program) >= OP_SUM &&
last_opcode(self->program) < OP_PROD) {
PyObject *zero = PyInt_FromLong(0);
PyArray_FillWithScalar((PyArrayObject *)output, zero);
Py_DECREF(zero);
} else {
PyObject *one = PyInt_FromLong(1);
PyArray_FillWithScalar((PyArrayObject *)output, one);
Py_DECREF(one);
}
}
else {
char retsig = get_return_sig(self->program);
self->memsteps[0] = size_from_char(retsig);
output = PyArray_SimpleNew(n_dimensions,
shape,
typecode_from_char(retsig));
if (!output) goto cleanup_and_exit;
}
r = run_interpreter(self, size, PyArray_DATA(output), inputs, inddata, &pc_error);
if (r < 0) {
Py_XDECREF(output);
output = NULL;
if (r == -1) {
PyErr_SetString(PyExc_RuntimeError,
"an error occurred while running the program");
} else if (r == -2) {
PyErr_Format(PyExc_RuntimeError,
"bad argument at pc=%d", pc_error);
} else if (r == -3) {
PyErr_Format(PyExc_RuntimeError,
"bad opcode at pc=%d", pc_error);
} else {
PyErr_SetString(PyExc_RuntimeError,
"unknown error occurred while running the program");
}
}
cleanup_and_exit:
Py_XDECREF(a_inputs);
PyMem_Del(inputs);
if (inddata) {
for (i = 0; i < n_inputs+1; i++) {
if (inddata[i].count) {
PyMem_Del(inddata[i].index);
}
}
}
PyMem_Del(inddata);
return output;
}
static PyMethodDef NumExpr_methods[] = {
{"run", (PyCFunction) NumExpr_run, METH_VARARGS|METH_KEYWORDS, NULL},
{NULL, NULL}
};
static PyTypeObject NumExprType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"numexpr.NumExpr", /*tp_name*/
sizeof(NumExprObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)NumExpr_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
(ternaryfunc)NumExpr_run, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"NumExpr objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
NumExpr_methods, /* tp_methods */
NumExpr_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)NumExpr_init, /* tp_init */
0, /* tp_alloc */
NumExpr_new, /* tp_new */
};
static PyMethodDef module_methods[] = {
{NULL}
};
void
initinterpreter(void)
{
PyObject *m, *d, *o;
int r;
if (PyType_Ready(&NumExprType) < 0)
return;
m = Py_InitModule3("interpreter", module_methods, NULL);
if (m == NULL)
return;
Py_INCREF(&NumExprType);
PyModule_AddObject(m, "NumExpr", (PyObject *)&NumExprType);
import_array();
d = PyDict_New();
if (!d) return;
#define add_op(sname, name) o = PyInt_FromLong(name); \
r = PyDict_SetItemString(d, sname, o); \
Py_XDECREF(o); \
if (r < 0) {PyErr_SetString(PyExc_RuntimeError, "add_op"); return;}
add_op("noop", OP_NOOP);
add_op("copy_bb", OP_COPY_BB);
add_op("invert_bb", OP_INVERT_BB);
add_op("and_bbb", OP_AND_BBB);
add_op("or_bbb", OP_OR_BBB);
add_op("gt_bii", OP_GT_BII);
add_op("ge_bii", OP_GE_BII);
add_op("eq_bii", OP_EQ_BII);
add_op("ne_bii", OP_NE_BII);
add_op("gt_bff", OP_GT_BFF);
add_op("ge_bff", OP_GE_BFF);
add_op("eq_bff", OP_EQ_BFF);
add_op("ne_bff", OP_NE_BFF);
add_op("cast_ib", OP_CAST_IB);
add_op("ones_like_ii", OP_ONES_LIKE_II);
add_op("copy_ii", OP_COPY_II);
add_op("neg_ii", OP_NEG_II);
add_op("add_iii", OP_ADD_III);
add_op("sub_iii", OP_SUB_III);
add_op("mul_iii", OP_MUL_III);
add_op("div_iii", OP_DIV_III);
add_op("pow_iii", OP_POW_III);
add_op("mod_iii", OP_MOD_III);
add_op("where_ifii", OP_WHERE_IFII);
add_op("cast_fb", OP_CAST_FB);
add_op("cast_fi", OP_CAST_FI);
add_op("copy_ff", OP_COPY_FF);
add_op("ones_like_ff", OP_ONES_LIKE_FF);
add_op("neg_cc", OP_NEG_CC);
add_op("neg_ff", OP_NEG_FF);
add_op("add_fff", OP_ADD_FFF);
add_op("sub_fff", OP_SUB_FFF);
add_op("mul_fff", OP_MUL_FFF);
add_op("div_fff", OP_DIV_FFF);
add_op("pow_fff", OP_POW_FFF);
add_op("mod_fff", OP_MOD_FFF);
add_op("sin_ff", OP_SIN_FF);
add_op("cos_ff", OP_COS_FF);
add_op("tan_ff", OP_TAN_FF);
add_op("sqrt_ff", OP_SQRT_FF);
add_op("arctan2_fff", OP_ARCTAN2_FFF);
add_op("where_ffff", OP_WHERE_FFFF);
add_op("func_ff", OP_FUNC_FF);
add_op("func_fff", OP_FUNC_FFF);
add_op("eq_bcc", OP_EQ_BCC);
add_op("ne_bcc", OP_NE_BCC);
add_op("cast_cb", OP_CAST_CB);
add_op("cast_ci", OP_CAST_CI);
add_op("cast_cf", OP_CAST_CF);
add_op("copy_cc", OP_COPY_CC);
add_op("ones_like_cc", OP_ONES_LIKE_CC);
add_op("neg_cc", OP_NEG_CC);
add_op("add_ccc", OP_ADD_CCC);
add_op("sub_ccc", OP_SUB_CCC);
add_op("mul_ccc", OP_MUL_CCC);
add_op("div_ccc", OP_DIV_CCC);
add_op("where_cfcc", OP_WHERE_CFCC);
add_op("func_cc", OP_FUNC_CC);
add_op("func_ccc", OP_FUNC_CCC);
add_op("real_fc", OP_REAL_FC);
add_op("imag_fc", OP_IMAG_FC);
add_op("complex_cff", OP_COMPLEX_CFF);
add_op("sum_iin", OP_SUM_IIN);
add_op("sum_ffn", OP_SUM_FFN);
add_op("sum_ccn", OP_SUM_CCN);
add_op("prod_iin", OP_PROD_IIN);
add_op("prod_ffn", OP_PROD_FFN);
add_op("prod_ccn", OP_PROD_CCN);
#undef add_op
if (PyModule_AddObject(m, "opcodes", d) < 0) return;
d = PyDict_New();
if (!d) return;
#define add_func(sname, name) o = PyInt_FromLong(name); \
r = PyDict_SetItemString(d, sname, o); \
Py_XDECREF(o); \
if (r < 0) {PyErr_SetString(PyExc_RuntimeError, "add_func"); return;}
add_func("sqrt_ff", FUNC_SQRT_FF);
add_func("sin_ff", FUNC_SIN_FF);
add_func("cos_ff", FUNC_COS_FF);
add_func("tan_ff", FUNC_TAN_FF);
add_func("arcsin_ff", FUNC_ARCSIN_FF);
add_func("arccos_ff", FUNC_ARCCOS_FF);
add_func("arctan_ff", FUNC_ARCTAN_FF);
add_func("sinh_ff", FUNC_SINH_FF);
add_func("cosh_ff", FUNC_COSH_FF);
add_func("tanh_ff", FUNC_TANH_FF);
add_func("fmod_fff", FUNC_FMOD_FFF);
add_func("sqrt_cc", FUNC_SQRT_CC);
add_func("sin_cc", FUNC_SIN_CC);
add_func("cos_cc", FUNC_COS_CC);
add_func("tan_cc", FUNC_TAN_CC);
add_func("arcsin_cc", FUNC_ARCSIN_CC);
add_func("arccos_cc", FUNC_ARCCOS_CC);
add_func("arctan_cc", FUNC_ARCTAN_CC);
add_func("sinh_cc", FUNC_SINH_CC);
add_func("cosh_cc", FUNC_COSH_CC);
add_func("tanh_cc", FUNC_TANH_CC);
add_func("pow_ccc", FUNC_POW_CCC);
#undef add_func
if (PyModule_AddObject(m, "funccodes", d) < 0) return;
if (PyModule_AddObject(m, "allaxes", PyInt_FromLong(255)) < 0) return;
if (PyModule_AddObject(m, "maxdims", PyInt_FromLong(MAX_DIMS)) < 0) return;
}
|