summaryrefslogtreecommitdiff
path: root/numpy/numarray/_capi.c
blob: 0323795151acab579e94c21720017ee74ead4514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
#include <Python.h>

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _libnumarray_MODULE
#include "include/numpy/libnumarray.h"
#include "numpy/npy_3kcompat.h"
#include <float.h>

#if (defined(__unix__) || defined(unix)) && !defined(USG)
#include <sys/param.h>
#endif

#if defined(__GLIBC__) || defined(__APPLE__) || defined(__MINGW32__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 502114))
#include <fenv.h>
#elif defined(__CYGWIN__)
#include "numpy/fenv/fenv.h"
#include "numpy/fenv/fenv.c"
#endif

static PyObject *pCfuncClass;
static PyTypeObject CfuncType;
static PyObject *pHandleErrorFunc;

static int
deferred_libnumarray_init(void)
{
static int initialized=0;

    if (initialized) return 0;

    pCfuncClass = (PyObject *) &CfuncType;
    Py_INCREF(pCfuncClass);

    pHandleErrorFunc =
        NA_initModuleGlobal("numpy.numarray.util", "handleError");

    if (!pHandleErrorFunc) goto _fail;


    /* _exit: */
    initialized = 1;
    return 0;

_fail:
    initialized = 0;
    return -1;
}



/**********************************************************************/
/*  Buffer Utility Functions                                          */
/**********************************************************************/

static PyObject *
getBuffer( PyObject *obj)
{
    if (!obj) return PyErr_Format(PyExc_RuntimeError,
            "NULL object passed to getBuffer()");
    if (((PyObject*)obj)->ob_type->tp_as_buffer == NULL) {
        return PyObject_CallMethod(obj, "__buffer__", NULL);
    } else {
        Py_INCREF(obj);  /* Since CallMethod returns a new object when it
                            succeeds, We'll need to DECREF later to free it.
                            INCREF ordinary buffers here so we don't have to
                            remember where the buffer came from at DECREF time.
                            */
        return obj;
    }
}

/* Either it defines the buffer API, or it is an instance which returns
   a buffer when obj.__buffer__() is called */
static int
isBuffer (PyObject *obj)
{
    PyObject *buf = getBuffer(obj);
    int ans = 0;
    if (buf) {
        ans = buf->ob_type->tp_as_buffer != NULL;
        Py_DECREF(buf);
    } else {
        PyErr_Clear();
    }
    return ans;
}

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

static int
getWriteBufferDataPtr(PyObject *buffobj, void **buff)
{
#if defined(NPY_PY3K)
    /* FIXME: XXX - needs implementation */
    PyErr_SetString(PyExc_RuntimeError,
                    "XXX: getWriteBufferDataPtr is not implemented");
    return -1;
#else
    int rval = -1;
    PyObject *buff2;
    if ((buff2 = getBuffer(buffobj)))
    {
        if (buff2->ob_type->tp_as_buffer->bf_getwritebuffer)
            rval = buff2->ob_type->tp_as_buffer->bf_getwritebuffer(buff2,
                    0, buff);
        Py_DECREF(buff2);
    }
    return rval;
#endif
}

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

static int
isBufferWriteable (PyObject *buffobj)
{
    void *ptr;
    int rval = -1;
    rval = getWriteBufferDataPtr(buffobj, &ptr);
    if (rval == -1)
        PyErr_Clear(); /* Since we're just "testing", it's not really an error */
    return rval != -1;
}

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

static int
getReadBufferDataPtr(PyObject *buffobj, void **buff)
{
#if defined(NPY_PY3K)
    /* FIXME: XXX - needs implementation */
    PyErr_SetString(PyExc_RuntimeError,
                    "XXX: getWriteBufferDataPtr is not implemented");
    return -1;
#else
    int rval = -1;
    PyObject *buff2;
    if ((buff2 = getBuffer(buffobj))) {
        if (buff2->ob_type->tp_as_buffer->bf_getreadbuffer)
            rval = buff2->ob_type->tp_as_buffer->bf_getreadbuffer(buff2,
                    0, buff);
        Py_DECREF(buff2);
    }
    return rval;
#endif
}

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

static int
getBufferSize(PyObject *buffobj)
{
#if defined(NPY_PY3K)
    /* FIXME: XXX - needs implementation */
    PyErr_SetString(PyExc_RuntimeError,
                    "XXX: getWriteBufferDataPtr is not implemented");
    return -1;
#else
    Py_ssize_t size=0;
    PyObject *buff2;
    if ((buff2 = getBuffer(buffobj)))
    {
        (void) buff2->ob_type->tp_as_buffer->bf_getsegcount(buff2, &size);
        Py_DECREF(buff2);
    }
    else
        size = -1;
    return size;
#endif
}


static double numarray_zero = 0.0;

static double raiseDivByZero(void)
{
    return 1.0/numarray_zero;
}

static double raiseNegDivByZero(void)
{
    return -1.0/numarray_zero;
}

static double num_log(double x)
{
    if (x == 0.0)
        return raiseNegDivByZero();
    else
        return log(x);
}

static double num_log10(double x)
{
    if (x == 0.0)
        return raiseNegDivByZero();
    else
        return log10(x);
}

static double num_pow(double x, double y)
{
    int z = (int) y;
    if ((x < 0.0) && (y != z))
        return raiseDivByZero();
    else
        return pow(x, y);
}

/* Inverse hyperbolic trig functions from Numeric */
static double num_acosh(double x)
{
    return log(x + sqrt((x-1.0)*(x+1.0)));
}

static double num_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 num_atanh(double x)
{
    return 0.5*log((1.0+x)/(1.0-x));
}

/* NUM_CROUND (in numcomplex.h) also calls num_round */
static double num_round(double x)
{
    return (x >= 0) ? floor(x+0.5) : ceil(x-0.5);
}


/* The following routine is used in the event of a detected integer *
 ** divide by zero so that a floating divide by zero is generated.   *
 ** This is done since numarray uses the floating point exception    *
 ** sticky bits to detect errors. The last bit is an attempt to      *
 ** prevent optimization of the divide by zero away, the input value *
 ** should always be 0                                               *
 */

static int int_dividebyzero_error(long NPY_UNUSED(value), long NPY_UNUSED(unused)) {
    double dummy;
    dummy = 1./numarray_zero;
    if (dummy) /* to prevent optimizer from eliminating expression */
        return 0;
    else
        return 1;
}

/* Likewise for Integer overflows */
#if defined(__GLIBC__) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 502114))
static int int_overflow_error(Float64 value) { /* For x86_64 */
    feraiseexcept(FE_OVERFLOW);
    return (int) value;
}
#else
static int int_overflow_error(Float64 value) {
    double dummy;
    dummy = pow(1.e10, fabs(value/2));
    if (dummy) /* to prevent optimizer from eliminating expression */
        return (int) value;
    else
        return 1;
}
#endif

static int umult64_overflow(UInt64 a, UInt64 b)
{
    UInt64 ah, al, bh, bl, w, x, y, z;

    ah = (a >> 32);
    al = (a & 0xFFFFFFFFL);
    bh = (b >> 32);
    bl = (b & 0xFFFFFFFFL);

    /* 128-bit product:  z*2**64 + (x+y)*2**32 + w  */
    w = al*bl;
    x = bh*al;
    y = ah*bl;
    z = ah*bh;

    /* *c = ((x + y)<<32) + w; */
    return z || (x>>32) || (y>>32) ||
        (((x & 0xFFFFFFFFL) + (y & 0xFFFFFFFFL) + (w >> 32)) >> 32);
}

static int smult64_overflow(Int64 a0, Int64 b0)
{
    UInt64 a, b;
    UInt64 ah, al, bh, bl, w, x, y, z;

    /* Convert to non-negative quantities */
    if (a0 < 0) { a = -a0; } else { a = a0; }
    if (b0 < 0) { b = -b0; } else { b = b0; }

    ah = (a >> 32);
    al = (a & 0xFFFFFFFFL);
    bh = (b >> 32);
    bl = (b & 0xFFFFFFFFL);

    w = al*bl;
    x = bh*al;
    y = ah*bl;
    z = ah*bh;

    /*
       UInt64 c = ((x + y)<<32) + w;
       if ((a0 < 0) ^ (b0 < 0))
     *c = -c;
     else
     *c = c
     */

    return z || (x>>31) || (y>>31) ||
        (((x & 0xFFFFFFFFL) + (y & 0xFFFFFFFFL) + (w >> 32)) >> 31);
}


static void
NA_Done(void)
{
    return;
}

static PyArrayObject *
NA_NewAll(int ndim, maybelong *shape, NumarrayType type,
        void *buffer, maybelong byteoffset, maybelong bytestride,
        int byteorder, int aligned, int writeable)
{
    PyArrayObject *result = NA_NewAllFromBuffer(
            ndim, shape, type, Py_None, byteoffset, bytestride,
            byteorder, aligned, writeable);

    if (result) {
        if (!NA_NumArrayCheck((PyObject *) result)) {
            PyErr_Format( PyExc_TypeError,
                    "NA_NewAll: non-NumArray result");
            result = NULL;
        } else {
            if (buffer) {
                memcpy(PyArray_DATA(result), buffer, NA_NBYTES(result));
            } else {
                memset(PyArray_DATA(result), 0, NA_NBYTES(result));
            }
        }
    }
    return  result;
}

static PyArrayObject *
NA_NewAllStrides(int ndim, maybelong *shape, maybelong *strides,
        NumarrayType type, void *buffer, maybelong byteoffset,
        int byteorder, int aligned, int writeable)
{
    int i;
    PyArrayObject *result = NA_NewAll(ndim, shape, type, buffer,
            byteoffset, 0,
            byteorder, aligned, writeable);
    for(i=0; i<ndim; i++)
        PyArray_STRIDES(result)[i] = strides[i];
    return result;
}


static PyArrayObject *
NA_New(void *buffer, NumarrayType type, int ndim, ...)
{
    int i;
    maybelong shape[MAXDIM];
    va_list ap;
    va_start(ap, ndim);
    for(i=0; i<ndim; i++)
        shape[i] = va_arg(ap, int);
    va_end(ap);
    return NA_NewAll(ndim, shape, type, buffer, 0, 0,
            NA_ByteOrder(), 1, 1);
}

static PyArrayObject *
NA_Empty(int ndim, maybelong *shape, NumarrayType type)
{
    return NA_NewAll(ndim, shape, type, NULL, 0, 0,
            NA_ByteOrder(), 1, 1);
}


/* Create a new numarray which is initially a C_array, or which
   references a C_array: aligned, !byteswapped, contiguous, ...
   Call with buffer==NULL to allocate storage.
   */
static PyArrayObject *
NA_vNewArray(void *buffer, NumarrayType type, int ndim, maybelong *shape)
{
    return (PyArrayObject *) NA_NewAll(ndim, shape, type, buffer, 0, 0,
            NA_ByteOrder(), 1, 1);
}

static PyArrayObject *
NA_NewArray(void *buffer, NumarrayType type, int ndim, ...)
{
    int i;
    maybelong shape[MAXDIM];
    va_list ap;
    va_start(ap, ndim);
    for(i=0; i<ndim; i++)
        shape[i] = va_arg(ap, int);  /* literals will still be ints */
    va_end(ap);
    return NA_vNewArray(buffer, type, ndim, shape);
}


static PyObject*
NA_ReturnOutput(PyObject *out, PyArrayObject *shadow)
{
    if ((out == Py_None) || (out == NULL)) {
        /* default behavior: return shadow array as the result */
        return (PyObject *) shadow;
    } else {
        PyObject *rval;
        /* specified output behavior: return None */
        /* del(shadow) --> out.copyFrom(shadow) */
        Py_DECREF(shadow);
        Py_INCREF(Py_None);
        rval = Py_None;
        return rval;
    }
}

static long NA_getBufferPtrAndSize(PyObject *buffobj, int readonly, void **ptr)
{
    long rval;
    if (readonly)
        rval = getReadBufferDataPtr(buffobj, ptr);
    else
        rval = getWriteBufferDataPtr(buffobj, ptr);
    return rval;
}


static int NA_checkIo(char *name,
        int wantIn, int wantOut, int gotIn, int gotOut)
{
    if (wantIn != gotIn) {
        PyErr_Format(_Error,
                "%s: wrong # of input buffers. Expected %d.  Got %d.",
                name, wantIn, gotIn);
        return -1;
    }
    if (wantOut != gotOut) {
        PyErr_Format(_Error,
                "%s: wrong # of output buffers. Expected %d.  Got %d.",
                name, wantOut, gotOut);
        return -1;
    }
    return 0;
}

static int NA_checkOneCBuffer(char *name, long niter,
        void *buffer, long bsize, size_t typesize)
{
    Int64 lniter = niter, ltypesize = typesize;

    if (lniter*ltypesize > bsize) {
        PyErr_Format(_Error,
                "%s: access out of buffer. niter=%d typesize=%d bsize=%d",
                name, (int) niter, (int) typesize, (int) bsize);
        return -1;
    }
    if ((typesize <= sizeof(Float64)) && (((long) buffer) % typesize)) {
        PyErr_Format(_Error,
                "%s: buffer not aligned on %d byte boundary.",
                name, (int) typesize);
        return -1;
    }
    return 0;
}


static int NA_checkNCBuffers(char *name, int N, long niter,
        void **buffers, long *bsizes,
        Int8 *typesizes, Int8 *iters)
{
    int i;
    for (i=0; i<N; i++)
        if (NA_checkOneCBuffer(name, iters[i] ? iters[i] : niter,
                    buffers[i], bsizes[i], typesizes[i]))
            return -1;
    return 0;
}

static int NA_checkOneStriding(char *name, long dim, maybelong *shape,
        long offset, maybelong *stride, long buffersize, long itemsize,
        int align)
{
    int i;
    long omin=offset, omax=offset;
    long alignsize = ((size_t)itemsize <= sizeof(Float64) ? (size_t)itemsize : sizeof(Float64));

    if (align && (offset % alignsize)) {
        PyErr_Format(_Error,
                "%s: buffer not aligned on %d byte boundary.",
                name, (int) alignsize);
        return -1;
    }
    for(i=0; i<dim; i++) {
        long strideN = stride[i] * (shape[i]-1);
        long tmax = omax + strideN;
        long tmin = omin + strideN;
        if (shape[i]-1 >= 0) {  /* Skip dimension == 0. */
            omax = MAX(omax, tmax);
            omin = MIN(omin, tmin);
            if (align && (ABS(stride[i]) % alignsize)) {
                PyErr_Format(_Error,
                        "%s: stride %d not aligned on %d byte boundary.",
                        name, (int) stride[i], (int) alignsize);
                return -1;
            }
            if (omax + itemsize > buffersize) {
                PyErr_Format(_Error,
                        "%s: access beyond buffer. offset=%d buffersize=%d",
                        name, (int) (omax+itemsize-1), (int) buffersize);
                return -1;
            }
            if (omin < 0) {
                PyErr_Format(_Error,
                        "%s: access before buffer. offset=%d buffersize=%d",
                        name, (int) omin, (int) buffersize);
                return -1;
            }
        }
    }
    return 0;
}

/* Function to call standard C Ufuncs
 **
 ** The C Ufuncs expect contiguous 1-d data numarray, input and output numarray
 ** iterate with standard increments of one data element over all numarray.
 ** (There are some exceptions like arrayrangexxx which use one or more of
 ** the data numarray as parameter or other sources of information and do not
 ** iterate over every buffer).
 **
 ** Arguments:
 **
 **   Number of iterations (simple integer value).
 **   Number of input numarray.
 **   Number of output numarray.
 **   Tuple of tuples, one tuple per input/output array. Each of these
 **     tuples consists of a buffer object and a byte offset to start.
 **
 ** Returns None
 */


static PyObject *
NA_callCUFuncCore(PyObject *self,
        long niter, long ninargs, long noutargs,
        PyObject **BufferObj, long *offset)
{
    CfuncObject *me = (CfuncObject *) self;
    char *buffers[MAXARGS];
    long bsizes[MAXARGS];
    long i, pnargs = ninargs + noutargs;
    UFUNC ufuncptr;

    if (pnargs > MAXARGS)
        return PyErr_Format(PyExc_RuntimeError, "NA_callCUFuncCore: too many parameters");

    if (!PyObject_IsInstance(self, (PyObject *) &CfuncType)
            || me->descr.type != CFUNC_UFUNC)
        return PyErr_Format(PyExc_TypeError,
                "NA_callCUFuncCore: problem with cfunc.");

    for (i=0; i<pnargs; i++) {
        int readonly = (i < ninargs);
        if (offset[i] < 0)
            return PyErr_Format(_Error,
                    "%s: invalid negative offset:%d for buffer[%d]",
                    me->descr.name, (int) offset[i], (int) i);
        if ((bsizes[i] = NA_getBufferPtrAndSize(BufferObj[i], readonly,
                        (void *) &buffers[i])) < 0)
            return PyErr_Format(_Error,
                    "%s: Problem with %s buffer[%d].",
                    me->descr.name,
                    readonly ? "read" : "write", (int) i);
        buffers[i] += offset[i];
        bsizes[i]  -= offset[i]; /* "shorten" buffer size by offset. */
    }

    ufuncptr = (UFUNC) me->descr.fptr;

    /* If it's not a self-checking ufunc, check arg count match,
       buffer size, and alignment for all buffers */
    if (!me->descr.chkself &&
            (NA_checkIo(me->descr.name,
                        me->descr.wantIn, me->descr.wantOut, ninargs, noutargs) ||
             NA_checkNCBuffers(me->descr.name, pnargs,
                 niter, (void **) buffers, bsizes,
                 me->descr.sizes, me->descr.iters)))
        return NULL;

    /* Since the parameters are valid, call the C Ufunc */
    if (!(*ufuncptr)(niter, ninargs, noutargs, (void **)buffers, bsizes)) {
        Py_INCREF(Py_None);
        return Py_None;
    } else {
        return NULL;
    }
}

static PyObject *
callCUFunc(PyObject *self, PyObject *args) {
    PyObject *DataArgs, *ArgTuple;
    long pnargs, ninargs, noutargs, niter, i;
    CfuncObject *me = (CfuncObject *) self;
    PyObject *BufferObj[MAXARGS];
    long     offset[MAXARGS];

    if (!PyArg_ParseTuple(args, "lllO",
                &niter, &ninargs, &noutargs, &DataArgs))
        return PyErr_Format(_Error,
                "%s: Problem with argument list", me->descr.name);

    /* check consistency of stated inputs/outputs and supplied buffers */
    pnargs = PyObject_Length(DataArgs);
    if ((pnargs != (ninargs+noutargs)) || (pnargs > MAXARGS))
        return PyErr_Format(_Error,
                "%s: wrong buffer count for function", me->descr.name);

    /* Unpack buffers and offsets, get data pointers */
    for (i=0; i<pnargs; i++) {
        ArgTuple = PySequence_GetItem(DataArgs, i);
        Py_DECREF(ArgTuple);
        if (!PyArg_ParseTuple(ArgTuple, "Ol", &BufferObj[i], &offset[i]))
            return PyErr_Format(_Error,
                    "%s: Problem with buffer/offset tuple", me->descr.name);
    }
    return NA_callCUFuncCore(self, niter, ninargs, noutargs, BufferObj, offset);
}

static PyObject *
callStrideConvCFunc(PyObject *self, PyObject *args) {
    PyObject *inbuffObj, *outbuffObj, *shapeObj;
    PyObject *inbstridesObj, *outbstridesObj;
    CfuncObject *me = (CfuncObject *) self;
    int  nshape, ninbstrides, noutbstrides;
    maybelong shape[MAXDIM], inbstrides[MAXDIM],
              outbstrides[MAXDIM], *outbstrides1 = outbstrides;
    long inboffset, outboffset, nbytes=0;

    if (!PyArg_ParseTuple(args, "OOlOOlO|l",
                &shapeObj, &inbuffObj,  &inboffset, &inbstridesObj,
                &outbuffObj, &outboffset, &outbstridesObj,
                &nbytes)) {
        return PyErr_Format(_Error,
                "%s: Problem with argument list",
                me->descr.name);
    }

    nshape = NA_maybeLongsFromIntTuple(MAXDIM, shape, shapeObj);
    if (nshape < 0) return NULL;

    ninbstrides = NA_maybeLongsFromIntTuple(MAXDIM, inbstrides, inbstridesObj);
    if (ninbstrides < 0) return NULL;

    noutbstrides=  NA_maybeLongsFromIntTuple(MAXDIM, outbstrides, outbstridesObj);
    if (noutbstrides < 0) return NULL;

    if (nshape && (nshape != ninbstrides)) {
        return PyErr_Format(_Error,
                "%s: Missmatch between input iteration and strides tuples",
                me->descr.name);
    }

    if (nshape && (nshape != noutbstrides)) {
        if (noutbstrides < 1 ||
                outbstrides[ noutbstrides - 1 ])/* allow 0 for reductions. */
            return PyErr_Format(_Error,
                    "%s: Missmatch between output "
                    "iteration and strides tuples",
                    me->descr.name);
    }

    return NA_callStrideConvCFuncCore(
            self, nshape, shape,
            inbuffObj,  inboffset,  ninbstrides, inbstrides,
            outbuffObj, outboffset, noutbstrides, outbstrides1, nbytes);
}

static int
_NA_callStridingHelper(PyObject *aux, long dim,
        long nnumarray, PyArrayObject *numarray[], char *data[],
        CFUNC_STRIDED_FUNC f)
{
    int i, j, status=0;
    dim -= 1;
    for(i=0; i<PyArray_DIMS(numarray[0])[dim]; i++) {
        for (j=0; j<nnumarray; j++)
            data[j] += PyArray_STRIDES(numarray[j])[dim]*i;
        if (dim == 0)
            status |= f(aux, nnumarray, numarray, data);
        else
            status |= _NA_callStridingHelper(
                    aux, dim, nnumarray, numarray, data, f);
        for (j=0; j<nnumarray; j++)
            data[j] -= PyArray_STRIDES(numarray[j])[dim]*i;
    }
    return status;
}


static PyObject *
callStridingCFunc(PyObject *self, PyObject *args) {
    CfuncObject *me = (CfuncObject *) self;
    PyObject *aux;
    PyArrayObject *numarray[MAXARRAYS];
    char *data[MAXARRAYS];
    CFUNC_STRIDED_FUNC f;
    int i;

    int nnumarray = PySequence_Length(args)-1;
    if ((nnumarray < 1) || (nnumarray > MAXARRAYS))
        return PyErr_Format(_Error, "%s, too many or too few numarray.",
                me->descr.name);

    aux = PySequence_GetItem(args, 0);
    if (!aux)
        return NULL;

    for(i=0; i<nnumarray; i++) {
        PyObject *otemp = PySequence_GetItem(args, i+1);
        if (!otemp)
            return PyErr_Format(_Error, "%s couldn't get array[%d]",
                    me->descr.name, i);
        if (!NA_NDArrayCheck(otemp))
            return PyErr_Format(PyExc_TypeError,
                    "%s arg[%d] is not an array.",
                    me->descr.name, i);
        numarray[i] = (PyArrayObject *) otemp;
        data[i] = PyArray_DATA(numarray[i]);
        Py_DECREF(otemp);
        if (!NA_updateDataPtr(numarray[i]))
            return NULL;
    }

    /* Cast function pointer and perform stride operation */
    f = (CFUNC_STRIDED_FUNC) me->descr.fptr;

    if (_NA_callStridingHelper(aux, PyArray_NDIM(numarray[0]),
                nnumarray, numarray, data, f)) {
        return NULL;
    } else {
        Py_INCREF(Py_None);
        return Py_None;
    }
}

/* Convert a standard C numeric value to a Python numeric value.
 **
 ** Handles both nonaligned and/or byteswapped C data.
 **
 ** Input arguments are:
 **
 **   Buffer object that contains the C numeric value.
 **   Offset (in bytes) into the buffer that the data is located at.
 **   The size of the C numeric data item in bytes.
 **   Flag indicating if the C data is byteswapped from the processor's
 **     natural representation.
 **
 **   Returns a Python numeric value.
 */

static PyObject *
NumTypeAsPyValue(PyObject *self, PyObject *args) {
    PyObject *bufferObj;
    long offset, itemsize, byteswap, i, buffersize;
    Py_complex temp;  /* to hold copies of largest possible type */
    void *buffer;
    char *tempptr;
    CFUNCasPyValue funcptr;
    CfuncObject *me = (CfuncObject *) self;

    if (!PyArg_ParseTuple(args, "Olll",
                &bufferObj, &offset, &itemsize, &byteswap))
        return PyErr_Format(_Error,
                "NumTypeAsPyValue: Problem with argument list");

    if ((buffersize = NA_getBufferPtrAndSize(bufferObj, 1, &buffer)) < 0)
        return PyErr_Format(_Error,
                "NumTypeAsPyValue: Problem with array buffer");

    if (offset < 0)
        return PyErr_Format(_Error,
                "NumTypeAsPyValue: invalid negative offset: %d", (int) offset);

    /* Guarantee valid buffer pointer */
    if (offset+itemsize > buffersize)
        return PyErr_Format(_Error,
                "NumTypeAsPyValue: buffer too small for offset and itemsize.");

    /* Do byteswapping.  Guarantee double alignment by using temp. */
    tempptr = (char *) &temp;
    if (!byteswap) {
        for (i=0; i<itemsize; i++)
            *(tempptr++) = *(((char *) buffer)+offset+i);
    } else {
        tempptr += itemsize-1;
        for (i=0; i<itemsize; i++)
            *(tempptr--) = *(((char *) buffer)+offset+i);
    }

    funcptr = (CFUNCasPyValue) me->descr.fptr;

    /* Call function to build PyObject.  Bad parameters to this function
       may render call meaningless, but "temp" guarantees that its safe.  */
    return (*funcptr)((void *)(&temp));
}

/* Convert a Python numeric value to a standard C numeric value.
 **
 ** Handles both nonaligned and/or byteswapped C data.
 **
 ** Input arguments are:
 **
 **   The Python numeric value to be converted.
 **   Buffer object to contain the C numeric value.
 **   Offset (in bytes) into the buffer that the data is to be copied to.
 **   The size of the C numeric data item in bytes.
 **   Flag indicating if the C data is byteswapped from the processor's
 **     natural representation.
 **
 **   Returns None
 */

static PyObject *
NumTypeFromPyValue(PyObject *self, PyObject *args) {
    PyObject *bufferObj, *valueObj;
    long offset, itemsize, byteswap, i, buffersize;
    Py_complex temp;  /* to hold copies of largest possible type */
    void *buffer;
    char *tempptr;
    CFUNCfromPyValue funcptr;
    CfuncObject *me = (CfuncObject *) self;

    if (!PyArg_ParseTuple(args, "OOlll",
                &valueObj, &bufferObj, &offset, &itemsize, &byteswap))
        return PyErr_Format(_Error,
                "%s: Problem with argument list", me->descr.name);

    if ((buffersize = NA_getBufferPtrAndSize(bufferObj, 0, &buffer)) < 0)
        return PyErr_Format(_Error,
                "%s: Problem with array buffer (read only?)", me->descr.name);

    funcptr = (CFUNCfromPyValue) me->descr.fptr;

    /* Convert python object into "temp". Always safe. */
    if (!((*funcptr)(valueObj, (void *)( &temp))))
        return PyErr_Format(_Error,
                "%s: Problem converting value", me->descr.name);

    /* Check buffer offset. */
    if (offset < 0)
        return PyErr_Format(_Error,
                "%s: invalid negative offset: %d", me->descr.name, (int) offset);

    if (offset+itemsize > buffersize)
        return PyErr_Format(_Error,
                "%s: buffer too small(%d) for offset(%d) and itemsize(%d)",
                me->descr.name, (int) buffersize, (int) offset, (int) itemsize);

    /* Copy "temp" to array buffer. */
    tempptr = (char *) &temp;
    if (!byteswap) {
        for (i=0; i<itemsize; i++)
            *(((char *) buffer)+offset+i) = *(tempptr++);
    } else {
        tempptr += itemsize-1;
        for (i=0; i<itemsize; i++)
            *(((char *) buffer)+offset+i) = *(tempptr--);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

/* Handle "calling" the cfunc object at the python level.
   Dispatch the call to the appropriate python-c wrapper based
   on the cfunc type.  Do this dispatch to avoid having to
   check that python code didn't somehow create a mismatch between
   cfunc and wrapper.
   */
static PyObject *
cfunc_call(PyObject *self, PyObject *argsTuple, PyObject *NPY_UNUSED(argsDict))
{
    CfuncObject *me = (CfuncObject *) self;
    switch(me->descr.type) {
    case CFUNC_UFUNC:
        return callCUFunc(self, argsTuple);
        break;
    case CFUNC_STRIDING:
        return callStrideConvCFunc(self, argsTuple);
        break;
    case CFUNC_NSTRIDING:
        return callStridingCFunc(self, argsTuple);
    case CFUNC_FROM_PY_VALUE:
        return NumTypeFromPyValue(self, argsTuple);
        break;
    case CFUNC_AS_PY_VALUE:
        return NumTypeAsPyValue(self, argsTuple);
        break;
    default:
        return PyErr_Format( _Error,
                "cfunc_call: Can't dispatch cfunc '%s' with type: %d.",
                me->descr.name, me->descr.type);
    }
}

static PyTypeObject CfuncType;

static void
cfunc_dealloc(PyObject* self)
{
    PyObject_Del(self);
}

static PyObject *
cfunc_repr(PyObject *self)
{
    char buf[256];
    CfuncObject *me = (CfuncObject *) self;
    sprintf(buf, "<cfunc '%s' at %08lx check-self:%d align:%d  io:(%d, %d)>",
            me->descr.name, (unsigned long ) me->descr.fptr,
            me->descr.chkself, me->descr.align,
            me->descr.wantIn, me->descr.wantOut);
    return PyUString_FromString(buf);
}

static PyTypeObject CfuncType = {
#if defined(NPY_PY3K)
    PyVarObject_HEAD_INIT(0,0)
#else
    PyObject_HEAD_INIT(0)
    0,                                          /* ob_size */
#endif
    "Cfunc",
    sizeof(CfuncObject),
    0,
    cfunc_dealloc,                              /* tp_dealloc */
    0,                                          /* tp_print */
    0,                                          /* tp_getattr */
    0,                                          /* tp_setattr */
    0,                                          /* tp_compare */
    cfunc_repr,                                 /* tp_repr */
    0,                                          /* tp_as_number */
    0,                                          /* tp_as_sequence */
    0,                                          /* tp_as_mapping */
    0,                                          /* tp_hash */
    cfunc_call,                                 /* tp_call */
    0,                                          /* tp_str */
    0,                                          /* tp_getattro */
    0,                                          /* tp_setattro */
    0,                                          /* tp_as_buffer */
    0,                                          /* tp_flags */
    0,                                          /* tp_doc */
    0,                                          /* tp_traverse */
    0,                                          /* tp_clear */
    0,                                          /* tp_richcompare */
    0,                                          /* tp_weaklistoffset */
    0,                                          /* tp_iter */
    0,                                          /* tp_iternext */
    0,                                          /* tp_methods */
    0,                                          /* tp_members */
    0,                                          /* tp_getset */
    0,                                          /* tp_base */
    0,                                          /* tp_dict */
    0,                                          /* tp_descr_get */
    0,                                          /* tp_descr_set */
    0,                                          /* tp_dictoffset */
    0,                                          /* tp_init */
    0,                                          /* tp_alloc */
    0,                                          /* tp_new */
    0,                                          /* tp_free */
    0,                                          /* tp_is_gc */
    0,                                          /* tp_bases */
    0,                                          /* tp_mro */
    0,                                          /* tp_cache */
    0,                                          /* tp_subclasses */
    0,                                          /* tp_weaklist */
    0,                                          /* tp_del */
#if PY_VERSION_HEX >= 0x02060000
    0,                                          /* tp_version_tag */
#endif
    };

/* CfuncObjects are created at the c-level only.  They ensure that each
   cfunc is called via the correct python-c-wrapper as defined by its
   CfuncDescriptor.  The wrapper, in turn, does conversions and buffer size
   and alignment checking.  Allowing these to be created at the python level
   would enable them to be created *wrong* at the python level, and thereby
   enable python code to *crash* python.
   */
static PyObject*
NA_new_cfunc(CfuncDescriptor *cfd)
{
    CfuncObject* cfunc;

    /* Should be done once at init.
       Do now since there is no init. */
    ((PyObject*)&CfuncType)->ob_type = &PyType_Type;

    cfunc = PyObject_New(CfuncObject, &CfuncType);

    if (!cfunc) {
        return PyErr_Format(_Error,
                "NA_new_cfunc: failed creating '%s'",
                cfd->name);
    }

    cfunc->descr = *cfd;

    return (PyObject*)cfunc;
}

static int NA_add_cfunc(PyObject *dict, char *keystr, CfuncDescriptor *descr)
{
    PyObject *c = (PyObject *) NA_new_cfunc(descr);
    if (!c) return -1;
    return PyDict_SetItemString(dict, keystr, c);
}

static PyArrayObject*
NA_InputArray(PyObject *a, NumarrayType t, int requires)
{
    PyArray_Descr *descr;
    if (t == tAny) descr = NULL;
    else descr = PyArray_DescrFromType(t);
    return (PyArrayObject *)                                    \
        PyArray_CheckFromAny(a, descr, 0, 0, requires, NULL);
}

/* satisfies ensures that 'a' meets a set of requirements and matches
   the specified type.
   */
static int
satisfies(PyArrayObject *a, int requirements, NumarrayType t)
{
    int type_ok = (PyArray_DESCR(a)->type_num == t) || (t == tAny);

    if (PyArray_ISCARRAY(a))
        return type_ok;
    if (PyArray_ISBYTESWAPPED(a) && (requirements & NUM_NOTSWAPPED))
        return 0;
    if (!PyArray_ISALIGNED(a) && (requirements & NUM_ALIGNED))
        return 0;
    if (!PyArray_ISCONTIGUOUS(a) && (requirements & NUM_CONTIGUOUS))
        return 0;
    if (!PyArray_ISWRITABLE(a) && (requirements & NUM_WRITABLE))
        return 0;
    if (requirements & NUM_COPY)
        return 0;
    return type_ok;
}


static PyArrayObject *
NA_OutputArray(PyObject *a, NumarrayType t, int requires)
{
    PyArray_Descr *dtype;
    PyArrayObject *ret;

    if (!PyArray_Check(a)) {
        PyErr_Format(PyExc_TypeError,
                "NA_OutputArray: only arrays work for output.");
        return NULL;
    }
    if (PyArray_FailUnlessWriteable((PyArrayObject *)a, "output array") < 0) {
        return NULL;
    }

    if (satisfies((PyArrayObject *)a, requires, t)) {
        Py_INCREF(a);
        return (PyArrayObject *)a;
    }
    if (t == tAny) {
        dtype = PyArray_DESCR((PyArrayObject *)a);
        Py_INCREF(dtype);
    }
    else {
        dtype = PyArray_DescrFromType(t);
    }
    ret = (PyArrayObject *)PyArray_Empty(PyArray_NDIM((PyArrayObject *)a),
                                        PyArray_DIMS((PyArrayObject *)a),
                                        dtype, 0);
    Py_INCREF(a);
    if (PyArray_SetUpdateIfCopyBase(ret, a) < 0) {
        Py_DECREF(ret);
        return NULL;
    }
    return ret;
}


/* NA_IoArray is a combination of NA_InputArray and NA_OutputArray.

   Unlike NA_OutputArray, if a temporary is required it is initialized to a copy
   of the input array.

   Unlike NA_InputArray, deallocating any resulting temporary array results in a
   copy from the temporary back to the original.
   */
static PyArrayObject *
NA_IoArray(PyObject *a, NumarrayType t, int requires)
{
    PyArrayObject *shadow = NA_InputArray(a, t,
                                requires | NPY_ARRAY_UPDATEIFCOPY );

    if (!shadow) return NULL;

    /* Guard against non-writable, but otherwise satisfying requires.
       In this case,  shadow == a.
       */
    if (!PyArray_FailUnlessWriteable(shadow, "input/output array")) {
        PyArray_XDECREF_ERR(shadow);
        return NULL;
    }

    return shadow;
}

/* NA_OptionalOutputArray works like NA_OutputArray, but handles the case
   where the output array 'optional' is omitted entirely at the python level,
   resulting in 'optional'==Py_None.  When 'optional' is Py_None, the return
   value is cloned (but with NumarrayType 't') from 'master', typically an input
   array with the same shape as the output array.
   */
static PyArrayObject *
NA_OptionalOutputArray(PyObject *optional, NumarrayType t, int requires,
        PyArrayObject *master)
{
    if ((optional == Py_None) || (optional == NULL)) {
        PyObject *rval;
        PyArray_Descr *descr;
        if (t == tAny) descr=NULL;
        else descr = PyArray_DescrFromType(t);
        rval = PyArray_FromArray(
                master, descr, NUM_C_ARRAY | NUM_COPY | NUM_WRITABLE);
        return (PyArrayObject *)rval;
    } else {
        return NA_OutputArray(optional, t, requires);
    }
}

Complex64 NA_get_Complex64(PyArrayObject *a, long offset)
{
    Complex32 v0;
    Complex64 v;

    switch(PyArray_DESCR(a)->type_num) {
    case tComplex32:
        v0 = NA_GETP(a, Complex32, (NA_PTR(a)+offset));
        v.r = v0.r;
        v.i = v0.i;
        break;
    case tComplex64:
        v = NA_GETP(a, Complex64, (NA_PTR(a)+offset));
        break;
    default:
        v.r = NA_get_Float64(a, offset);
        v.i = 0;
        break;
    }
    return v;
}

void NA_set_Complex64(PyArrayObject *a, long offset, Complex64 v)
{
    Complex32 v0;

    switch(PyArray_DESCR(a)->type_num) {
    case tComplex32:
        v0.r = v.r;
        v0.i = v.i;
        NA_SETP(a, Complex32, (NA_PTR(a)+offset), v0);
        break;
    case tComplex64:
        NA_SETP(a, Complex64, (NA_PTR(a)+offset), v);
        break;
    default:
        NA_set_Float64(a, offset, v.r);
        break;
    }
}

Int64 NA_get_Int64(PyArrayObject *a, long offset)
{
    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        return NA_GETP(a, Bool, (NA_PTR(a)+offset)) != 0;
    case tInt8:
        return NA_GETP(a, Int8, (NA_PTR(a)+offset));
    case tUInt8:
        return NA_GETP(a, UInt8, (NA_PTR(a)+offset));
    case tInt16:
        return NA_GETP(a, Int16, (NA_PTR(a)+offset));
    case tUInt16:
        return NA_GETP(a, UInt16, (NA_PTR(a)+offset));
    case tInt32:
        return NA_GETP(a, Int32, (NA_PTR(a)+offset));
    case tUInt32:
        return NA_GETP(a, UInt32, (NA_PTR(a)+offset));
    case tInt64:
        return NA_GETP(a, Int64, (NA_PTR(a)+offset));
    case tUInt64:
        return NA_GETP(a, UInt64, (NA_PTR(a)+offset));
    case tFloat32:
        return NA_GETP(a, Float32, (NA_PTR(a)+offset));
    case tFloat64:
        return NA_GETP(a, Float64, (NA_PTR(a)+offset));
    case tComplex32:
        return NA_GETP(a, Float32, (NA_PTR(a)+offset));
    case tComplex64:
        return NA_GETP(a, Float64, (NA_PTR(a)+offset));
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_get_Int64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
    }
    return 0; /* suppress warning */
}

void NA_set_Int64(PyArrayObject *a, long offset, Int64 v)
{
    Bool b;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        b = (v != 0);
        NA_SETP(a, Bool, (NA_PTR(a)+offset), b);
        break;
    case tInt8:    NA_SETP(a, Int8, (NA_PTR(a)+offset), v);
                   break;
    case tUInt8:   NA_SETP(a, UInt8, (NA_PTR(a)+offset), v);
                   break;
    case tInt16:   NA_SETP(a, Int16, (NA_PTR(a)+offset), v);
                   break;
    case tUInt16:  NA_SETP(a, UInt16, (NA_PTR(a)+offset), v);
                   break;
    case tInt32:   NA_SETP(a, Int32, (NA_PTR(a)+offset), v);
                   break;
    case tUInt32:   NA_SETP(a, UInt32, (NA_PTR(a)+offset), v);
                    break;
    case tInt64:   NA_SETP(a, Int64, (NA_PTR(a)+offset), v);
                   break;
    case tUInt64:   NA_SETP(a, UInt64, (NA_PTR(a)+offset), v);
                    break;
    case tFloat32:
                    NA_SETP(a, Float32, (NA_PTR(a)+offset), v);
                    break;
    case tFloat64:
                    NA_SETP(a, Float64, (NA_PTR(a)+offset), v);
                    break;
    case tComplex32:
                    NA_SETP(a, Float32, (NA_PTR(a)+offset), v);
                    NA_SETP(a, Float32, (NA_PTR(a)+offset+sizeof(Float32)), 0);
                    break;
    case tComplex64:
                    NA_SETP(a, Float64, (NA_PTR(a)+offset), v);
                    NA_SETP(a, Float64, (NA_PTR(a)+offset+sizeof(Float64)), 0);
                    break;
    default:
                    PyErr_Format( PyExc_TypeError,
                            "Unknown type %d in NA_set_Int64",
                            PyArray_DESCR(a)->type_num);
                    PyErr_Print();
    }
}

/*  NA_get_offset computes the offset specified by the set of indices.
    If N > 0, the indices are taken from the outer dimensions of the array.
    If N < 0, the indices are taken from the inner dimensions of the array.
    If N == 0, the offset is 0.
    */
long NA_get_offset(PyArrayObject *a, int N, ...)
{
    int i;
    long offset = 0;
    va_list ap;
    va_start(ap, N);
    if (N > 0) { /* compute offset of "outer" indices. */
        for(i=0; i<N; i++)
            offset += va_arg(ap, long) * PyArray_STRIDES(a)[i];
    } else {   /* compute offset of "inner" indices. */
        N = -N;
        for(i=0; i<N; i++)
            offset += va_arg(ap, long) * PyArray_STRIDES(a)[PyArray_NDIM(a)-N+i];
    }
    va_end(ap);
    return offset;
}

Float64 NA_get_Float64(PyArrayObject *a, long offset)
{
    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        return NA_GETP(a, Bool, (NA_PTR(a)+offset)) != 0;
    case tInt8:
        return NA_GETP(a, Int8, (NA_PTR(a)+offset));
    case tUInt8:
        return NA_GETP(a, UInt8, (NA_PTR(a)+offset));
    case tInt16:
        return NA_GETP(a, Int16, (NA_PTR(a)+offset));
    case tUInt16:
        return NA_GETP(a, UInt16, (NA_PTR(a)+offset));
    case tInt32:
        return NA_GETP(a, Int32, (NA_PTR(a)+offset));
    case tUInt32:
        return NA_GETP(a, UInt32, (NA_PTR(a)+offset));
    case tInt64:
        return NA_GETP(a, Int64, (NA_PTR(a)+offset));
#if HAS_UINT64
    case tUInt64:
        return NA_GETP(a, UInt64, (NA_PTR(a)+offset));
#endif
    case tFloat32:
        return NA_GETP(a, Float32, (NA_PTR(a)+offset));
    case tFloat64:
        return NA_GETP(a, Float64, (NA_PTR(a)+offset));
    case tComplex32:  /* Since real value is first */
        return NA_GETP(a, Float32, (NA_PTR(a)+offset));
    case tComplex64:  /* Since real value is first */
        return NA_GETP(a, Float64, (NA_PTR(a)+offset));
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_get_Float64",
                PyArray_DESCR(a)->type_num);
    }
    return 0; /* suppress warning */
}

void NA_set_Float64(PyArrayObject *a, long offset, Float64 v)
{
    Bool b;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        b = (v != 0);
        NA_SETP(a, Bool, (NA_PTR(a)+offset), b);
        break;
    case tInt8:    NA_SETP(a, Int8, (NA_PTR(a)+offset), v);
                   break;
    case tUInt8:   NA_SETP(a, UInt8, (NA_PTR(a)+offset), v);
                   break;
    case tInt16:   NA_SETP(a, Int16, (NA_PTR(a)+offset), v);
                   break;
    case tUInt16:  NA_SETP(a, UInt16, (NA_PTR(a)+offset), v);
                   break;
    case tInt32:   NA_SETP(a, Int32, (NA_PTR(a)+offset), v);
                   break;
    case tUInt32:   NA_SETP(a, UInt32, (NA_PTR(a)+offset), v);
                    break;
    case tInt64:   NA_SETP(a, Int64, (NA_PTR(a)+offset), v);
                   break;
#if HAS_UINT64
    case tUInt64:   NA_SETP(a, UInt64, (NA_PTR(a)+offset), v);
                    break;
#endif
    case tFloat32:
                    NA_SETP(a, Float32, (NA_PTR(a)+offset), v);
                    break;
    case tFloat64:
                    NA_SETP(a, Float64, (NA_PTR(a)+offset), v);
                    break;
    case tComplex32: {
                         NA_SETP(a, Float32, (NA_PTR(a)+offset), v);
                         NA_SETP(a, Float32, (NA_PTR(a)+offset+sizeof(Float32)), 0);
                         break;
                     }
    case tComplex64: {
                         NA_SETP(a, Float64, (NA_PTR(a)+offset), v);
                         NA_SETP(a, Float64, (NA_PTR(a)+offset+sizeof(Float64)), 0);
                         break;
                     }
    default:
                     PyErr_Format( PyExc_TypeError,
                             "Unknown type %d in NA_set_Float64",
                             PyArray_DESCR(a)->type_num );
                     PyErr_Print();
    }
}


Float64 NA_get1_Float64(PyArrayObject *a, long i)
{
    long offset = i * PyArray_STRIDES(a)[0];
    return NA_get_Float64(a, offset);
}

Float64 NA_get2_Float64(PyArrayObject *a, long i, long j)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    return NA_get_Float64(a, offset);
}

Float64 NA_get3_Float64(PyArrayObject *a, long i, long j, long k)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    return NA_get_Float64(a, offset);
}

void NA_set1_Float64(PyArrayObject *a, long i, Float64 v)
{
    long offset = i * PyArray_STRIDES(a)[0];
    NA_set_Float64(a, offset, v);
}

void NA_set2_Float64(PyArrayObject *a, long i, long j, Float64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    NA_set_Float64(a, offset, v);
}

void NA_set3_Float64(PyArrayObject *a, long i, long j, long k, Float64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    NA_set_Float64(a, offset, v);
}

Complex64 NA_get1_Complex64(PyArrayObject *a, long i)
{
    long offset = i * PyArray_STRIDES(a)[0];
    return NA_get_Complex64(a, offset);
}

Complex64 NA_get2_Complex64(PyArrayObject *a, long i, long j)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    return NA_get_Complex64(a, offset);
}

Complex64 NA_get3_Complex64(PyArrayObject *a, long i, long j, long k)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    return NA_get_Complex64(a, offset);
}

void NA_set1_Complex64(PyArrayObject *a, long i, Complex64 v)
{
    long offset = i * PyArray_STRIDES(a)[0];
    NA_set_Complex64(a, offset, v);
}

void NA_set2_Complex64(PyArrayObject *a, long i, long j, Complex64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    NA_set_Complex64(a, offset, v);
}

void NA_set3_Complex64(PyArrayObject *a, long i, long j, long k, Complex64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    NA_set_Complex64(a, offset, v);
}

Int64 NA_get1_Int64(PyArrayObject *a, long i)
{
    long offset = i * PyArray_STRIDES(a)[0];
    return NA_get_Int64(a, offset);
}

Int64 NA_get2_Int64(PyArrayObject *a, long i, long j)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    return NA_get_Int64(a, offset);
}

Int64 NA_get3_Int64(PyArrayObject *a, long i, long j, long k)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    return NA_get_Int64(a, offset);
}

void NA_set1_Int64(PyArrayObject *a, long i, Int64 v)
{
    long offset = i * PyArray_STRIDES(a)[0];
    NA_set_Int64(a, offset, v);
}

void NA_set2_Int64(PyArrayObject *a, long i, long j, Int64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1];
    NA_set_Int64(a, offset, v);
}

void NA_set3_Int64(PyArrayObject *a, long i, long j, long k, Int64 v)
{
    long offset  = i * PyArray_STRIDES(a)[0]
        + j * PyArray_STRIDES(a)[1]
        + k * PyArray_STRIDES(a)[2];
    NA_set_Int64(a, offset, v);
}

/* SET_CMPLX could be made faster by factoring it into 3 seperate loops.
*/
#define NA_SET_CMPLX(a, type, base, cnt, in)                                  \
{                                                                             \
    int i;                                                                \
    int stride = PyArray_STRIDES(a)[ PyArray_NDIM(a) - 1];                                  \
    NA_SET1D(a, type, base, cnt, in);                                     \
    base = NA_PTR(a) + offset + sizeof(type);                             \
    for(i=0; i<cnt; i++) {                                                \
        NA_SETP(a, Float32, base, 0);                                 \
        base += stride;                                               \
    }                                                                     \
}

static int
NA_get1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*out)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        NA_GET1D(a, Bool, base, cnt, out);
        break;
    case tInt8:
        NA_GET1D(a, Int8, base, cnt, out);
        break;
    case tUInt8:
        NA_GET1D(a, UInt8, base, cnt, out);
        break;
    case tInt16:
        NA_GET1D(a, Int16, base, cnt, out);
        break;
    case tUInt16:
        NA_GET1D(a, UInt16, base, cnt, out);
        break;
    case tInt32:
        NA_GET1D(a, Int32, base, cnt, out);
        break;
    case tUInt32:
        NA_GET1D(a, UInt32, base, cnt, out);
        break;
    case tInt64:
        NA_GET1D(a, Int64, base, cnt, out);
        break;
#if HAS_UINT64
    case tUInt64:
        NA_GET1D(a, UInt64, base, cnt, out);
        break;
#endif
    case tFloat32:
        NA_GET1D(a, Float32, base, cnt, out);
        break;
    case tFloat64:
        NA_GET1D(a, Float64, base, cnt, out);
        break;
    case tComplex32:
        NA_GET1D(a, Float32, base, cnt, out);
        break;
    case tComplex64:
        NA_GET1D(a, Float64, base, cnt, out);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_get1D_Float64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}

static Float64 *
NA_alloc1D_Float64(PyArrayObject *a, long offset, int cnt)
{
    Float64 *result = PyMem_New(Float64, (size_t)cnt);
    if (!result) return NULL;
    if (NA_get1D_Float64(a, offset, cnt, result) < 0) {
        PyMem_Free(result);
        return NULL;
    }
    return result;
}

static int
NA_set1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*in)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        NA_SET1D(a, Bool, base, cnt, in);
        break;
    case tInt8:
        NA_SET1D(a, Int8, base, cnt, in);
        break;
    case tUInt8:
        NA_SET1D(a, UInt8, base, cnt, in);
        break;
    case tInt16:
        NA_SET1D(a, Int16, base, cnt, in);
        break;
    case tUInt16:
        NA_SET1D(a, UInt16, base, cnt, in);
        break;
    case tInt32:
        NA_SET1D(a, Int32, base, cnt, in);
        break;
    case tUInt32:
        NA_SET1D(a, UInt32, base, cnt, in);
        break;
    case tInt64:
        NA_SET1D(a, Int64, base, cnt, in);
        break;
#if HAS_UINT64
    case tUInt64:
        NA_SET1D(a, UInt64, base, cnt, in);
        break;
#endif
    case tFloat32:
        NA_SET1D(a, Float32, base, cnt, in);
        break;
    case tFloat64:
        NA_SET1D(a, Float64, base, cnt, in);
        break;
    case tComplex32:
        NA_SET_CMPLX(a, Float32, base, cnt, in);
        break;
    case tComplex64:
        NA_SET_CMPLX(a, Float64, base, cnt, in);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_set1D_Float64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}

static int
NA_get1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*out)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        NA_GET1D(a, Bool, base, cnt, out);
        break;
    case tInt8:
        NA_GET1D(a, Int8, base, cnt, out);
        break;
    case tUInt8:
        NA_GET1D(a, UInt8, base, cnt, out);
        break;
    case tInt16:
        NA_GET1D(a, Int16, base, cnt, out);
        break;
    case tUInt16:
        NA_GET1D(a, UInt16, base, cnt, out);
        break;
    case tInt32:
        NA_GET1D(a, Int32, base, cnt, out);
        break;
    case tUInt32:
        NA_GET1D(a, UInt32, base, cnt, out);
        break;
    case tInt64:
        NA_GET1D(a, Int64, base, cnt, out);
        break;
    case tUInt64:
        NA_GET1D(a, UInt64, base, cnt, out);
        break;
    case tFloat32:
        NA_GET1D(a, Float32, base, cnt, out);
        break;
    case tFloat64:
        NA_GET1D(a, Float64, base, cnt, out);
        break;
    case tComplex32:
        NA_GET1D(a, Float32, base, cnt, out);
        break;
    case tComplex64:
        NA_GET1D(a, Float64, base, cnt, out);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_get1D_Int64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}

static Int64 *
NA_alloc1D_Int64(PyArrayObject *a, long offset, int cnt)
{
    Int64 *result = PyMem_New(Int64, (size_t)cnt);
    if (!result) return NULL;
    if (NA_get1D_Int64(a, offset, cnt, result) < 0) {
        PyMem_Free(result);
        return NULL;
    }
    return result;
}

static int
NA_set1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*in)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        NA_SET1D(a, Bool, base, cnt, in);
        break;
    case tInt8:
        NA_SET1D(a, Int8, base, cnt, in);
        break;
    case tUInt8:
        NA_SET1D(a, UInt8, base, cnt, in);
        break;
    case tInt16:
        NA_SET1D(a, Int16, base, cnt, in);
        break;
    case tUInt16:
        NA_SET1D(a, UInt16, base, cnt, in);
        break;
    case tInt32:
        NA_SET1D(a, Int32, base, cnt, in);
        break;
    case tUInt32:
        NA_SET1D(a, UInt32, base, cnt, in);
        break;
    case tInt64:
        NA_SET1D(a, Int64, base, cnt, in);
        break;
    case tUInt64:
        NA_SET1D(a, UInt64, base, cnt, in);
        break;
    case tFloat32:
        NA_SET1D(a, Float32, base, cnt, in);
        break;
    case tFloat64:
        NA_SET1D(a, Float64, base, cnt, in);
        break;
    case tComplex32:
        NA_SET_CMPLX(a, Float32, base, cnt, in);
        break;
    case tComplex64:
        NA_SET_CMPLX(a, Float64, base, cnt, in);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_set1D_Int64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}

static int
NA_get1D_Complex64(PyArrayObject *a, long offset, int cnt, Complex64*out)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tComplex64:
        NA_GET1D(a, Complex64, base, cnt, out);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unsupported type %d in NA_get1D_Complex64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}

static int
NA_set1D_Complex64(PyArrayObject *a, long offset, int cnt, Complex64*in)
{
    char *base = NA_PTR(a) + offset;

    switch(PyArray_DESCR(a)->type_num) {
    case tComplex64:
        NA_SET1D(a, Complex64, base, cnt, in);
        break;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unsupported type %d in NA_set1D_Complex64",
                PyArray_DESCR(a)->type_num);
        PyErr_Print();
        return -1;
    }
    return 0;
}


/* NA_ShapeEqual returns 1 if 'a' and 'b' have the same shape, 0 otherwise.
*/
static int
NA_ShapeEqual(PyArrayObject *a, PyArrayObject *b)
{
    int i;

    if (!NA_NDArrayCheck((PyObject *) a) ||
            !NA_NDArrayCheck((PyObject*) b)) {
        PyErr_Format(
                PyExc_TypeError,
                "NA_ShapeEqual: non-array as parameter.");
        return -1;
    }
    if (PyArray_NDIM(a) != PyArray_NDIM(b))
        return 0;
    for(i=0; i<PyArray_NDIM(a); i++)
        if (PyArray_DIMS(a)[i] != PyArray_DIMS(b)[i])
            return 0;
    return 1;
}

/* NA_ShapeLessThan returns 1 if a.shape[i] < b.shape[i] for all i, else 0.
   If they have a different number of dimensions, it compares the innermost
   overlapping dimensions of each.
   */
static int
NA_ShapeLessThan(PyArrayObject *a, PyArrayObject *b)
{
    int i;
    int mindim, aoff, boff;
    if (!NA_NDArrayCheck((PyObject *) a) ||
            !NA_NDArrayCheck((PyObject *) b)) {
        PyErr_Format(PyExc_TypeError,
                "NA_ShapeLessThan: non-array as parameter.");
        return -1;
    }
    mindim = MIN(PyArray_NDIM(a), PyArray_NDIM(b));
    aoff = PyArray_NDIM(a) - mindim;
    boff = PyArray_NDIM(b) - mindim;
    for(i=0; i<mindim; i++)
        if (PyArray_DIMS(a)[i+aoff] >=  PyArray_DIMS(b)[i+boff])
            return 0;
    return 1;
}

static int
NA_ByteOrder(void)
{
    unsigned long byteorder_test;
    byteorder_test = 1;
    if (*((char *) &byteorder_test))
        return NUM_LITTLE_ENDIAN;
    else
        return NUM_BIG_ENDIAN;
}

static Bool
NA_IeeeSpecial32( Float32 *f, Int32 *mask)
{
    return NA_IeeeMask32(*f, *mask);
}

static Bool
NA_IeeeSpecial64( Float64 *f, Int32 *mask)
{
    return NA_IeeeMask64(*f, *mask);
}

static PyArrayObject *
NA_updateDataPtr(PyArrayObject *me)
{
    return me;
}


#define ELEM(x) (sizeof(x)/sizeof(x[0]))

typedef struct
{
    char *name;
    int typeno;
} NumarrayTypeNameMapping;

static NumarrayTypeNameMapping NumarrayTypeNameMap[] = {
    {"Any", tAny},
    {"Bool", tBool},
    {"Int8", tInt8},
    {"UInt8", tUInt8},
    {"Int16", tInt16},
    {"UInt16", tUInt16},
    {"Int32", tInt32},
    {"UInt32", tUInt32},
    {"Int64", tInt64},
    {"UInt64", tUInt64},
    {"Float32", tFloat32},
    {"Float64", tFloat64},
    {"Complex32", tComplex32},
    {"Complex64", tComplex64},
    {"Object", tObject},
    {"Long", tLong},
};


/* Convert NumarrayType 'typeno' into the string of the type's name. */
static char *
NA_typeNoToName(int typeno)
{
    size_t i;
    PyObject *typeObj;
    int typeno2;

    for(i=0; i<ELEM(NumarrayTypeNameMap); i++)
        if (typeno == NumarrayTypeNameMap[i].typeno)
            return NumarrayTypeNameMap[i].name;

    /* Handle Numeric typecodes */
    typeObj = NA_typeNoToTypeObject(typeno);
    if (!typeObj) return 0;
    typeno2 = NA_typeObjectToTypeNo(typeObj);
    Py_DECREF(typeObj);

    return NA_typeNoToName(typeno2);
}

/* Look up the NumarrayType which corresponds to typename */

static int
NA_nameToTypeNo(char *typename)
{
    size_t i;
    for(i=0; i<ELEM(NumarrayTypeNameMap); i++)
        if (!strcmp(typename, NumarrayTypeNameMap[i].name))
            return NumarrayTypeNameMap[i].typeno;
    return -1;
}

static PyObject *
getTypeObject(NumarrayType type)
{
    return (PyObject *)PyArray_DescrFromType(type);
}


static PyObject *
NA_typeNoToTypeObject(int typeno)
{
    PyObject *o;
    o = getTypeObject(typeno);
    if (o) Py_INCREF(o);
    return o;
}


static PyObject *
NA_intTupleFromMaybeLongs(int len, maybelong *Longs)
{
    return PyArray_IntTupleFromIntp(len, Longs);
}

static long
NA_maybeLongsFromIntTuple(int len, maybelong *arr, PyObject *sequence)
{
    return PyArray_IntpFromSequence(sequence, arr, len);
}


static int
NA_intTupleProduct(PyObject  *shape, long *prod)
{
    int i, nshape, rval = -1;

    if (!PySequence_Check(shape)) {
        PyErr_Format(PyExc_TypeError,
                "NA_intSequenceProduct: object is not a sequence.");
        goto _exit;
    }
    nshape = PySequence_Size(shape);

    for(i=0, *prod=1;  i<nshape; i++) {
        PyObject *obj = PySequence_GetItem(shape, i);
        if (!obj || !(PyInt_Check(obj) || PyLong_Check(obj))) {
            PyErr_Format(PyExc_TypeError,
                    "NA_intTupleProduct: non-integer in shape.");
            Py_XDECREF(obj);
            goto _exit;
        }
        *prod *= PyInt_AsLong(obj);
        Py_DECREF(obj);
        if (PyErr_Occurred())
            goto _exit;
    }
    rval = 0;
_exit:
    return rval;
}

static long
NA_isIntegerSequence(PyObject *sequence)
{
    PyObject *o;
    long i, size, isInt = 1;
    if (!sequence) {
        isInt = -1;
        goto _exit;
    }
    if (!PySequence_Check(sequence)) {
        isInt = 0;
        goto _exit;
    }
    if ((size = PySequence_Length(sequence)) < 0) {
        isInt = -1;
        goto _exit;
    }
    for(i=0; i<size; i++) {
        o = PySequence_GetItem(sequence, i);
        if (!PyInt_Check(o) && !PyLong_Check(o)) {
            isInt = 0;
            Py_XDECREF(o);
            goto _exit;
        }
        Py_XDECREF(o);
    }
_exit:
    return isInt;
}

static int
getShape(PyObject *a, maybelong *shape, int dims)
{
    long slen;
    if (PyBytes_Check(a)) {
        PyErr_Format(PyExc_TypeError,
                "getShape: numerical sequences can't contain strings.");
        return -1;
    }

    if (!PySequence_Check(a) ||
            (NA_NDArrayCheck(a) && (PyArray_NDIM(PyArray(a)) == 0)))
        return dims;
    slen = PySequence_Length(a);
    if (slen < 0) {
        PyErr_Format(_Error,
                "getShape: couldn't get sequence length.");
        return -1;
    }
    if (!slen) {
        *shape = 0;
        return dims+1;
    } else if (dims < MAXDIM) {
        PyObject *item0 = PySequence_GetItem(a, 0);
        if (item0) {
            *shape = PySequence_Length(a);
            dims = getShape(item0, ++shape, dims+1);
            Py_DECREF(item0);
        } else {
            PyErr_Format(_Error,
                    "getShape: couldn't get sequence item.");
            return -1;
        }
    } else {
        PyErr_Format(_Error,
                "getShape: sequence object nested more than MAXDIM deep.");
        return -1;
    }
    return dims;
}



typedef enum {
    NOTHING,
    NUMBER,
    SEQUENCE
} SequenceConstraint;

static int
setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, long offset)
{
    SequenceConstraint mustbe = NOTHING;
    int i, seqlen=-1, slen = PySequence_Length(s);

    if (dim > PyArray_NDIM(a)) {
        PyErr_Format(PyExc_ValueError,
                "setArrayFromSequence: sequence/array dimensions mismatch.");
        return -1;
    }

    if (slen != PyArray_DIMS(a)[dim]) {
        PyErr_Format(PyExc_ValueError,
                "setArrayFromSequence: sequence/array shape mismatch.");
        return -1;
    }

    for(i=0; i<slen; i++) {
        PyObject *o = PySequence_GetItem(s, i);
        if (!o) {
            PyErr_SetString(_Error,
                    "setArrayFromSequence: Can't get a sequence item");
            return -1;
        } else if ((NA_isPythonScalar(o) ||
                    (NA_NumArrayCheck(o) && PyArray_NDIM(PyArray(o)) == 0)) &&
                ((mustbe == NOTHING) || (mustbe == NUMBER))) {
            if (NA_setFromPythonScalar(a, offset, o) < 0)
                return -2;
            mustbe = NUMBER;
        } else if (PyBytes_Check(o)) {
            PyErr_SetString( PyExc_ValueError,
                    "setArrayFromSequence: strings can't define numeric numarray.");
            return -3;
        } else if (PySequence_Check(o)) {

            if ((mustbe == NOTHING) || (mustbe == SEQUENCE)) {
                if (mustbe == NOTHING) {
                    mustbe = SEQUENCE;
                    seqlen = PySequence_Length(o);
                } else if (PySequence_Length(o) != seqlen) {
                    PyErr_SetString(
                            PyExc_ValueError,
                            "Nested sequences with different lengths.");
                    return -5;
                }
                setArrayFromSequence(a, o, dim+1, offset);
            } else {
                PyErr_SetString(PyExc_ValueError,
                        "Nested sequences with different lengths.");
                return -4;
            }
        } else {
            PyErr_SetString(PyExc_ValueError, "Invalid sequence.");
            return -6;
        }
        Py_DECREF(o);
        offset += PyArray_STRIDES(a)[dim];
    }
    return 0;
}

static PyObject *
NA_setArrayFromSequence(PyArrayObject *a, PyObject *s)
{
    maybelong shape[MAXDIM];

    if (!PySequence_Check(s))
        return PyErr_Format( PyExc_TypeError,
                "NA_setArrayFromSequence: (array, seq) expected.");

    if (getShape(s, shape, 0) < 0)
        return NULL;

    if (!NA_updateDataPtr(a))
        return NULL;

    if (setArrayFromSequence(a, s, 0, 0) < 0)
        return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}

enum {
    BOOL_SCALAR,
    INT_SCALAR,
    LONG_SCALAR,
    FLOAT_SCALAR,
    COMPLEX_SCALAR
};


static int
_NA_maxType(PyObject *seq, int limit)
{
    if (limit > MAXDIM) {
        PyErr_Format( PyExc_ValueError,
                "NA_maxType: sequence nested too deep." );
        return -1;
    }
    if (NA_NumArrayCheck(seq)) {
        switch(PyArray_DESCR(PyArray(seq))->type_num) {
        case tBool:
            return BOOL_SCALAR;
        case tInt8:
        case tUInt8:
        case tInt16:
        case tUInt16:
        case tInt32:
        case tUInt32:
            return INT_SCALAR;
        case tInt64:
        case tUInt64:
            return LONG_SCALAR;
        case tFloat32:
        case tFloat64:
            return FLOAT_SCALAR;
        case tComplex32:
        case tComplex64:
            return COMPLEX_SCALAR;
        default:
            PyErr_Format(PyExc_TypeError,
                    "Expecting a python numeric type, got something else.");
            return -1;
        }
    } else if (PySequence_Check(seq) && !PyBytes_Check(seq)) {
        long i, maxtype=BOOL_SCALAR, slen;

        slen = PySequence_Length(seq);
        if (slen < 0) return -1;

        if (slen == 0) return INT_SCALAR;

        for(i=0; i<slen; i++) {
            long newmax;
            PyObject *o = PySequence_GetItem(seq, i);
            if (!o) return -1;
            newmax = _NA_maxType(o, limit+1);
            if (newmax  < 0)
                return -1;
            else if (newmax > maxtype) {
                maxtype = newmax;
            }
            Py_DECREF(o);
        }
        return maxtype;
    } else {
#if PY_VERSION_HEX >= 0x02030000
        if (PyBool_Check(seq))
            return BOOL_SCALAR;
        else
#endif
#if defined(NPY_PY3K)
            if (PyInt_Check(seq))
                return INT_SCALAR;
            else if (PyLong_Check(seq))
#else
            if (PyLong_Check(seq))
#endif
                return LONG_SCALAR;
            else if (PyFloat_Check(seq))
                return FLOAT_SCALAR;
            else if (PyComplex_Check(seq))
                return COMPLEX_SCALAR;
            else {
                PyErr_Format(PyExc_TypeError,
                        "Expecting a python numeric type, got something else.");
                return -1;
            }
    }
}

static int
NA_maxType(PyObject *seq)
{
    int rval;
    rval = _NA_maxType(seq, 0);
    return rval;
}

static int
NA_isPythonScalar(PyObject *o)
{
    int rval;
    rval =  PyInt_Check(o) ||
        PyLong_Check(o) ||
        PyFloat_Check(o) ||
        PyComplex_Check(o) ||
        (PyBytes_Check(o) && (PyBytes_Size(o) == 1));
    return rval;
}

#if (NPY_SIZEOF_INTP == 8)
#define PlatBigInt PyInt_FromLong
#define PlatBigUInt PyLong_FromUnsignedLong
#else
#define PlatBigInt PyLong_FromLongLong
#define PlatBigUInt PyLong_FromUnsignedLongLong
#endif


static PyObject *
NA_getPythonScalar(PyArrayObject *a, long offset)
{
    int type = PyArray_DESCR(a)->type_num;
    PyObject *rval = NULL;

    switch(type) {
    case tBool:
    case tInt8:
    case tUInt8:
    case tInt16:
    case tUInt16:
    case tInt32: {
                     Int64 v = NA_get_Int64(a, offset);
                     rval = PyInt_FromLong(v);
                     break;
                 }
    case tUInt32: {
                      Int64 v = NA_get_Int64(a, offset);
                      rval = PlatBigUInt(v);
                      break;
                  }
    case tInt64: {
                     Int64 v = NA_get_Int64(a, offset);
                     rval = PlatBigInt( v);
                     break;
                 }
    case tUInt64: {
                      Int64 v = NA_get_Int64(a, offset);
                      rval = PlatBigUInt( v);
                      break;
                  }
    case tFloat32:
    case tFloat64: {
                       Float64 v = NA_get_Float64(a, offset);
                       rval = PyFloat_FromDouble( v );
                       break;
                   }
    case tComplex32:
    case tComplex64:
                   {
                       Complex64 v = NA_get_Complex64(a, offset);
                       rval = PyComplex_FromDoubles(v.r, v.i);
                       break;
                   }
    default:
                   rval = PyErr_Format(PyExc_TypeError,
                           "NA_getPythonScalar: bad type %d\n",
                           type);
    }
    return rval;
}

static int
NA_overflow(PyArrayObject *a, Float64 v)
{
    if ((PyArray_FLAGS(a) & CHECKOVERFLOW) == 0) return 0;

    switch(PyArray_DESCR(a)->type_num) {
    case tBool:
        return 0;
    case tInt8:
        if ((v < -128) || (v > 127))      goto _fail;
        return 0;
    case tUInt8:
        if ((v < 0) || (v > 255))         goto _fail;
        return 0;
    case tInt16:
        if ((v < -32768) || (v > 32767))  goto _fail;
        return 0;
    case tUInt16:
        if ((v < 0) || (v > 65535))       goto _fail;
        return 0;
    case tInt32:
        if ((v < -2147483648.) ||
                (v > 2147483647.))           goto _fail;
        return 0;
    case tUInt32:
        if ((v < 0) || (v > 4294967295.)) goto _fail;
        return 0;
    case tInt64:
        if ((v < -9223372036854775808.) ||
                (v > 9223372036854775807.))    goto _fail;
        return 0;
#if HAS_UINT64
    case tUInt64:
        if ((v < 0) ||
                (v > 18446744073709551615.))    goto _fail;
        return 0;
#endif
    case tFloat32:
        if ((v < -FLT_MAX) || (v > FLT_MAX)) goto _fail;
        return 0;
    case tFloat64:
        return 0;
    case tComplex32:
        if ((v < -FLT_MAX) || (v > FLT_MAX)) goto _fail;
        return 0;
    case tComplex64:
        return 0;
    default:
        PyErr_Format( PyExc_TypeError,
                "Unknown type %d in NA_overflow",
                PyArray_DESCR(a)->type_num );
        PyErr_Print();
        return -1;
    }
_fail:
    PyErr_Format(PyExc_OverflowError, "value out of range for array");
    return -1;
}

static int
_setFromPythonScalarCore(PyArrayObject *a, long offset, PyObject*value, int entries)
{
    Int64 v;
    if (entries >= 100) {
        PyErr_Format(PyExc_RuntimeError,
                "NA_setFromPythonScalar: __tonumtype__ conversion chain too long");
        return -1;
    } else if (PyInt_Check(value)) {
        v = PyInt_AsLong(value);
        if (NA_overflow(a, v) < 0)
            return -1;
        NA_set_Int64(a, offset, v);
    } else if (PyLong_Check(value)) {
        if (PyArray_DESCR(a)->type_num == tInt64) {
            v = (Int64) PyLong_AsLongLong( value );
        } else if (PyArray_DESCR(a)->type_num == tUInt64) {
            v = (UInt64) PyLong_AsUnsignedLongLong( value );
        } else if (PyArray_DESCR(a)->type_num == tUInt32) {
            v = PyLong_AsUnsignedLong(value);
        } else {
            v = PyLong_AsLongLong(value);
        }
        if (PyErr_Occurred())
            return -1;
        if (NA_overflow(a, v) < 0)
            return -1;
        NA_set_Int64(a, offset, v);
    } else if (PyFloat_Check(value)) {
        Float64 v = PyFloat_AsDouble(value);
        if (NA_overflow(a, v) < 0)
            return -1;
        NA_set_Float64(a, offset, v);
    } else if (PyComplex_Check(value)) {
        Complex64 vc;
        vc.r = PyComplex_RealAsDouble(value);
        vc.i = PyComplex_ImagAsDouble(value);
        if (NA_overflow(a, vc.r) < 0)
            return -1;
        if (NA_overflow(a, vc.i) < 0)
            return -1;
        NA_set_Complex64(a, offset, vc);
    } else if (PyObject_HasAttrString(value, "__tonumtype__")) {
        int rval;
        PyObject *type = NA_typeNoToTypeObject(PyArray_DESCR(a)->type_num);
        if (!type) return -1;
        value = PyObject_CallMethod(
                value, "__tonumtype__", "(N)", type);
        if (!value) return -1;
        rval = _setFromPythonScalarCore(a, offset, value, entries+1);
        Py_DECREF(value);
        return rval;
    } else if (PyBytes_Check(value)) {
        long size = PyBytes_Size(value);
        if ((size <= 0) || (size > 1)) {
            PyErr_Format( PyExc_ValueError,
                    "NA_setFromPythonScalar: len(string) must be 1.");
            return -1;
        }
        NA_set_Int64(a, offset, *PyBytes_AsString(value));
    } else {
        PyErr_Format(PyExc_TypeError,
                "NA_setFromPythonScalar: bad value type.");
        return -1;
    }
    return 0;
}

static int
NA_setFromPythonScalar(PyArrayObject *a, long offset, PyObject *value)
{
    if (PyArray_FailUnlessWriteable(a, "array") < 0) {
        return -1;
    }
    return _setFromPythonScalarCore(a, offset, value, 0);
}


static int
NA_NDArrayCheck(PyObject *obj) {
    return PyArray_Check(obj);
}

static int
NA_NumArrayCheck(PyObject *obj) {
    return PyArray_Check(obj);
}

static int
NA_ComplexArrayCheck(PyObject *a)
{
    int rval = NA_NumArrayCheck(a);
    if (rval > 0) {
        PyArrayObject *arr = (PyArrayObject *) a;
        switch(PyArray_DESCR(arr)->type_num) {
        case tComplex64: case tComplex32:
            return 1;
        default:
            return 0;
        }
    }
    return rval;
}

static unsigned long
NA_elements(PyArrayObject  *a)
{
    int i;
    unsigned long n = 1;
    for(i = 0; i<PyArray_NDIM(a); i++)
        n *= PyArray_DIMS(a)[i];
    return n;
}

static int
NA_typeObjectToTypeNo(PyObject *typeObj)
{
    PyArray_Descr *dtype;
    int i;
    if (PyArray_DescrConverter(typeObj, &dtype) == NPY_FAIL) i=-1;
    else i=dtype->type_num;
    return i;
}

static int
NA_copyArray(PyArrayObject *to, const PyArrayObject *from)
{
    return PyArray_CopyInto(to, (PyArrayObject *)from);
}

static PyArrayObject *
NA_copy(PyArrayObject *from)
{
    return (PyArrayObject *)PyArray_NewCopy(from, 0);
}


static PyObject *
NA_getType( PyObject *type)
{
    PyArray_Descr *typeobj = NULL;
    if (!type && PyArray_DescrConverter(type, &typeobj) == NPY_FAIL) {
        PyErr_Format(PyExc_ValueError, "NA_getType: unknown type.");
        typeobj = NULL;
    }
    return (PyObject *)typeobj;
}


/* Call a standard "stride" function
 **
 ** Stride functions always take one input and one output array.
 ** They can handle n-dimensional data with arbitrary strides (of
 ** either sign) for both the input and output numarray. Typically
 ** these functions are used to copy data, byteswap, or align data.
 **
 **
 ** It expects the following arguments:
 **
 **   Number of iterations for each dimension as a tuple
 **   Input Buffer Object
 **   Offset in bytes for input buffer
 **   Input strides (in bytes) for each dimension as a tuple
 **   Output Buffer Object
 **   Offset in bytes for output buffer
 **   Output strides (in bytes) for each dimension as a tuple
 **   An integer (Optional), typically the number of bytes to copy per
 *       element.
 **
 ** Returns None
 **
 ** The arguments expected by the standard stride functions that this
 ** function calls are:
 **
 **   Number of dimensions to iterate over
 **   Long int value (from the optional last argument to
 **      callStrideConvCFunc)
 **      often unused by the C Function
 **   An array of long ints. Each is the number of iterations for each
 **      dimension. NOTE: the previous argument as well as the stride
 **      arguments are reversed in order with respect to how they are
 **      used in Python. Fastest changing dimension is the first element
 **      in the numarray!
 **   A void pointer to the input data buffer.
 **   The starting offset for the input data buffer in bytes (long int).
 **   An array of long int input strides (in bytes) [reversed as with
 **      the iteration array]
 **   A void pointer to the output data buffer.
 **   The starting offset for the output data buffer in bytes (long int).
 **   An array of long int output strides (in bytes) [also reversed]
 */


static PyObject *
NA_callStrideConvCFuncCore(
        PyObject *self, int nshape, maybelong *shape,
        PyObject *inbuffObj,  long inboffset,
        int NPY_UNUSED(ninbstrides), maybelong *inbstrides,
        PyObject *outbuffObj, long outboffset,
        int NPY_UNUSED(noutbstrides), maybelong *outbstrides,
        long nbytes)
{
    CfuncObject *me = (CfuncObject *) self;
    CFUNC_STRIDE_CONV_FUNC funcptr;
    void *inbuffer, *outbuffer;
    long inbsize, outbsize;
    maybelong i, lshape[MAXDIM], in_strides[MAXDIM], out_strides[MAXDIM];
    maybelong shape_0, inbstr_0, outbstr_0;

    if (nshape == 0) {   /* handle rank-0 numarray. */
        nshape = 1;
        shape = &shape_0;
        inbstrides = &inbstr_0;
        outbstrides = &outbstr_0;
        shape[0] = 1;
        inbstrides[0] = outbstrides[0] = 0;
    }

    for(i=0; i<nshape; i++)
        lshape[i] = shape[nshape-1-i];
    for(i=0; i<nshape; i++)
        in_strides[i] = inbstrides[nshape-1-i];
    for(i=0; i<nshape; i++)
        out_strides[i] = outbstrides[nshape-1-i];

    if (!PyObject_IsInstance(self , (PyObject *) &CfuncType)
            || me->descr.type != CFUNC_STRIDING)
        return PyErr_Format(PyExc_TypeError,
                "NA_callStrideConvCFuncCore: problem with cfunc");

    if ((inbsize = NA_getBufferPtrAndSize(inbuffObj, 1, &inbuffer)) < 0)
        return PyErr_Format(_Error,
                "%s: Problem with input buffer", me->descr.name);

    if ((outbsize = NA_getBufferPtrAndSize(outbuffObj, 0, &outbuffer)) < 0)
        return PyErr_Format(_Error,
                "%s: Problem with output buffer (read only?)",
                me->descr.name);

    /* Check buffer alignment and bounds */
    if (NA_checkOneStriding(me->descr.name, nshape, lshape,
                inboffset, in_strides, inbsize,
                (me->descr.sizes[0] == -1) ?
                nbytes : me->descr.sizes[0],
                me->descr.align) ||
            NA_checkOneStriding(me->descr.name, nshape, lshape,
                outboffset, out_strides, outbsize,
                (me->descr.sizes[1] == -1) ?
                nbytes : me->descr.sizes[1],
                me->descr.align))
        return NULL;

    /* Cast function pointer and perform stride operation */
    funcptr = (CFUNC_STRIDE_CONV_FUNC) me->descr.fptr;
    if ((*funcptr)(nshape-1, nbytes, lshape,
                inbuffer,  inboffset, in_strides,
                outbuffer, outboffset, out_strides) == 0) {
        Py_INCREF(Py_None);
        return Py_None;
    } else {
        return NULL;
    }
}

static void
NA_stridesFromShape(int nshape, maybelong *shape, maybelong bytestride,
        maybelong *strides)
{
    int i;
    if (nshape > 0) {
        for(i=0; i<nshape; i++)
            strides[i] = bytestride;
        for(i=nshape-2; i>=0; i--)
            strides[i] = strides[i+1]*shape[i+1];
    }
}

static int
NA_OperatorCheck(PyObject *NPY_UNUSED(op)) {
    return 0;
}

static int
NA_ConverterCheck(PyObject *NPY_UNUSED(op)) {
    return 0;
}

static int
NA_UfuncCheck(PyObject *NPY_UNUSED(op)) {
    return 0;
}

static int
NA_CfuncCheck(PyObject *op) {
    return PyObject_TypeCheck(op, &CfuncType);
}

static int
NA_getByteOffset(PyArrayObject *NPY_UNUSED(array), int NPY_UNUSED(nindices),
        maybelong *NPY_UNUSED(indices), long *NPY_UNUSED(offset))
{
    return 0;
}

static int
NA_swapAxes(PyArrayObject *array, int x, int y)
{
    long temp;

    if (((PyObject *) array) == Py_None) return 0;

    if (PyArray_NDIM(array) < 2) return 0;

    if (x < 0) x += PyArray_NDIM(array);
    if (y < 0) y += PyArray_NDIM(array);

    if ((x < 0) || (x >= PyArray_NDIM(array)) ||
            (y < 0) || (y >= PyArray_NDIM(array))) {
        PyErr_Format(PyExc_ValueError,
                "Specified dimension does not exist");
        return -1;
    }

    temp = PyArray_DIMS(array)[x];
    PyArray_DIMS(array)[x] = PyArray_DIMS(array)[y];
    PyArray_DIMS(array)[y] = temp;

    temp = PyArray_STRIDES(array)[x];
    PyArray_STRIDES(array)[x] = PyArray_STRIDES(array)[y];
    PyArray_STRIDES(array)[y] = temp;

    PyArray_UpdateFlags(array, NPY_ARRAY_UPDATE_ALL);

    return 0;
}

static PyObject *
NA_initModuleGlobal(char *modulename, char *globalname)
{
    PyObject *module, *dict, *global = NULL;
    module = PyImport_ImportModule(modulename);
    if (!module) {
        PyErr_Format(PyExc_RuntimeError,
                "Can't import '%s' module",
                modulename);
        goto _exit;
    }
    dict = PyModule_GetDict(module);
    global = PyDict_GetItemString(dict, globalname);
    if (!global) {
        PyErr_Format(PyExc_RuntimeError,
                "Can't find '%s' global in '%s' module.",
                globalname, modulename);
        goto _exit;
    }
    Py_DECREF(module);
    Py_INCREF(global);
_exit:
    return global;
}

    NumarrayType
NA_NumarrayType(PyObject *seq)
{
    int maxtype = NA_maxType(seq);
    int rval;
    switch(maxtype) {
    case BOOL_SCALAR:
        rval = tBool;
        goto _exit;
    case INT_SCALAR:
    case LONG_SCALAR:
        rval = tLong; /* tLong corresponds to C long int,
                         not Python long int */
        goto _exit;
    case FLOAT_SCALAR:
        rval = tFloat64;
        goto _exit;
    case COMPLEX_SCALAR:
        rval = tComplex64;
        goto _exit;
    default:
        PyErr_Format(PyExc_TypeError,
                "expecting Python numeric scalar value; got something else.");
        rval = -1;
    }
_exit:
    return rval;
}

/* ignores bytestride */
static PyArrayObject *
NA_NewAllFromBuffer(int ndim, maybelong *shape, NumarrayType type,
        PyObject *bufferObject, maybelong byteoffset,
        maybelong NPY_UNUSED(bytestride), int byteorder,
        int NPY_UNUSED(aligned), int NPY_UNUSED(writeable))
{
    PyArrayObject *self = NULL;
    PyArray_Descr *dtype;

    if (type == tAny)
        type = tDefault;

    dtype = PyArray_DescrFromType(type);
    if (dtype == NULL) return NULL;

    if (byteorder != NA_ByteOrder()) {
        PyArray_Descr *temp;
        temp = PyArray_DescrNewByteorder(dtype, NPY_SWAP);
        Py_DECREF(dtype);
        if (temp == NULL) return NULL;
        dtype = temp;
    }

    if (bufferObject == Py_None || bufferObject == NULL) {
        self = (PyArrayObject *)        \
               PyArray_NewFromDescr(&PyArray_Type, dtype,
                       ndim, shape, NULL, NULL,
                       0, NULL);
    }
    else {
        npy_intp size = 1;
        int i;
        PyArrayObject *newself;
        PyArray_Dims newdims;
        for(i=0; i<ndim; i++) {
            size *= shape[i];
        }
        self = (PyArrayObject *)\
               PyArray_FromBuffer(bufferObject, dtype,
                       size, byteoffset);

        if (self == NULL) return self;
        newdims.len = ndim;
        newdims.ptr = shape;
        newself = (PyArrayObject *)\
                  PyArray_Newshape(self, &newdims, NPY_CORDER);
        Py_DECREF(self);
        self = newself;
    }

    return self;
}

static void
NA_updateAlignment(PyArrayObject *self)
{
    PyArray_UpdateFlags(self, NPY_ARRAY_ALIGNED);
}

static void
NA_updateContiguous(PyArrayObject *self)
{
    PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
}


static void
NA_updateStatus(PyArrayObject *self)
{
    PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL);
}

static int
NA_NumArrayCheckExact(PyObject *op) {
    return (op->ob_type == &PyArray_Type);
}

static int
NA_NDArrayCheckExact(PyObject *op) {
    return (op->ob_type == &PyArray_Type);
}

static int
NA_OperatorCheckExact(PyObject *NPY_UNUSED(op)) {
    return 0;
}

static int
NA_ConverterCheckExact(PyObject *NPY_UNUSED(op)) {
    return 0;
}

static int
NA_UfuncCheckExact(PyObject *NPY_UNUSED(op)) {
    return 0;
}


static int
NA_CfuncCheckExact(PyObject *op) {
    return op->ob_type == &CfuncType;
}

static char *
NA_getArrayData(PyArrayObject *obj)
{
    if (!NA_NDArrayCheck((PyObject *) obj)) {
        PyErr_Format(PyExc_TypeError,
                "expected an NDArray");
    }
    return PyArray_DATA(obj);
}

/* Byteswap is not a flag of the array --- it is implicit in the data-type */
static void
NA_updateByteswap(PyArrayObject *NPY_UNUSED(self))
{
    return;
}

static PyArray_Descr *
NA_DescrFromType(int type)
{
    if (type == tAny)
        type = tDefault;
    return PyArray_DescrFromType(type);
}

static PyObject *
NA_Cast(PyArrayObject *a, int type)
{
    return PyArray_Cast(a, type);
}


/* The following function has much platform dependent code since
 ** there is no platform-independent way of checking Floating Point
 ** status bits
 */

/*  OSF/Alpha (Tru64)  ---------------------------------------------*/
#if defined(__osf__) && defined(__alpha)

static int
NA_checkFPErrors(void)
{
    unsigned long fpstatus;
    int retstatus;

#include <machine/fpu.h>   /* Should migrate to global scope */

    fpstatus = ieee_get_fp_control();
    /* clear status bits as well as disable exception mode if on */
    ieee_set_fp_control( 0 );
    retstatus =
        pyFPE_DIVIDE_BY_ZERO* (int)((IEEE_STATUS_DZE  & fpstatus) != 0)
        + pyFPE_OVERFLOW      * (int)((IEEE_STATUS_OVF  & fpstatus) != 0)
        + pyFPE_UNDERFLOW     * (int)((IEEE_STATUS_UNF  & fpstatus) != 0)
        + pyFPE_INVALID       * (int)((IEEE_STATUS_INV  & fpstatus) != 0);

    return retstatus;
}

/* MS Windows -----------------------------------------------------*/
#elif defined(_MSC_VER)

#include <float.h>

static int
NA_checkFPErrors(void)
{
    int fpstatus = (int) _clear87();
    int retstatus =
        pyFPE_DIVIDE_BY_ZERO * ((SW_ZERODIVIDE & fpstatus) != 0)
        + pyFPE_OVERFLOW       * ((SW_OVERFLOW & fpstatus)   != 0)
        + pyFPE_UNDERFLOW      * ((SW_UNDERFLOW & fpstatus)  != 0)
        + pyFPE_INVALID        * ((SW_INVALID & fpstatus)    != 0);


    return retstatus;
}

/* Solaris --------------------------------------------------------*/
/* --------ignoring SunOS ieee_flags approach, someone else can
 **         deal with that! */
#elif defined(sun)
#include <ieeefp.h>

static int
NA_checkFPErrors(void)
{
    int fpstatus;
    int retstatus;

    fpstatus = (int) fpgetsticky();
    retstatus = pyFPE_DIVIDE_BY_ZERO * ((FP_X_DZ  & fpstatus) != 0)
        + pyFPE_OVERFLOW       * ((FP_X_OFL & fpstatus) != 0)
        + pyFPE_UNDERFLOW      * ((FP_X_UFL & fpstatus) != 0)
        + pyFPE_INVALID        * ((FP_X_INV & fpstatus) != 0);
    (void) fpsetsticky(0);

    return retstatus;
}

#elif defined(__GLIBC__) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 502114))

static int
NA_checkFPErrors(void)
{
    int fpstatus = (int) fetestexcept(
            FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID);
    int retstatus =
        pyFPE_DIVIDE_BY_ZERO * ((FE_DIVBYZERO  & fpstatus) != 0)
        + pyFPE_OVERFLOW       * ((FE_OVERFLOW   & fpstatus) != 0)
        + pyFPE_UNDERFLOW      * ((FE_UNDERFLOW  & fpstatus) != 0)
        + pyFPE_INVALID        * ((FE_INVALID    & fpstatus) != 0);
    (void) feclearexcept(FE_DIVBYZERO | FE_OVERFLOW |
            FE_UNDERFLOW | FE_INVALID);
    return retstatus;
}

#else

static int
NA_checkFPErrors(void)
{
    return 0;
}

#endif

static void
NA_clearFPErrors()
{
    NA_checkFPErrors();
}

/* Not supported yet */
static int
NA_checkAndReportFPErrors(char *name)
{
    int error = NA_checkFPErrors();
    if (error) {
        PyObject *ans;
        char msg[128];
        strcpy(msg, " in ");
        strncat(msg, name, 100);
        ans = PyObject_CallFunction(pHandleErrorFunc, "(is)", error, msg);
        if (!ans) return -1;
        Py_DECREF(ans); /* Py_None */
    }
    return 0;

}


#define WITHIN32(v, f) (((v) >= f##_MIN32) && ((v) <= f##_MAX32))
#define WITHIN64(v, f) (((v) >= f##_MIN64) && ((v) <= f##_MAX64))

static Bool
NA_IeeeMask32( Float32 f, Int32 mask)
{
    Int32 category;
    UInt32 v = *(UInt32 *) &f;

    if (v & BIT(31)) {
        if (WITHIN32(v, NEG_NORMALIZED)) {
            category = MSK_NEG_NOR;
        } else if (WITHIN32(v, NEG_DENORMALIZED)) {
            category = MSK_NEG_DEN;
        } else if (WITHIN32(v, NEG_SIGNAL_NAN)) {
            category = MSK_NEG_SNAN;
        } else if (WITHIN32(v, NEG_QUIET_NAN)) {
            category = MSK_NEG_QNAN;
        } else if (v == NEG_INFINITY_MIN32) {
            category = MSK_NEG_INF;
        } else if (v == NEG_ZERO_MIN32) {
            category = MSK_NEG_ZERO;
        } else if (v == INDETERMINATE_MIN32) {
            category = MSK_INDETERM;
        } else {
            category = MSK_BUG;
        }
    } else {
        if (WITHIN32(v, POS_NORMALIZED)) {
            category = MSK_POS_NOR;
        } else if (WITHIN32(v, POS_DENORMALIZED)) {
            category = MSK_POS_DEN;
        } else if (WITHIN32(v, POS_SIGNAL_NAN)) {
            category = MSK_POS_SNAN;
        } else if (WITHIN32(v, POS_QUIET_NAN)) {
            category = MSK_POS_QNAN;
        } else if (v == POS_INFINITY_MIN32) {
            category = MSK_POS_INF;
        } else if (v == POS_ZERO_MIN32) {
            category = MSK_POS_ZERO;
        } else {
            category = MSK_BUG;
        }
    }
    return (category & mask) != 0;
}

static Bool
NA_IeeeMask64( Float64 f, Int32 mask)
{
    Int32 category;
    UInt64 v = *(UInt64 *) &f;

    if (v & BIT(63)) {
        if (WITHIN64(v, NEG_NORMALIZED)) {
            category = MSK_NEG_NOR;
        } else if (WITHIN64(v, NEG_DENORMALIZED)) {
            category = MSK_NEG_DEN;
        } else if (WITHIN64(v, NEG_SIGNAL_NAN)) {
            category = MSK_NEG_SNAN;
        } else if (WITHIN64(v, NEG_QUIET_NAN)) {
            category = MSK_NEG_QNAN;
        } else if (v == NEG_INFINITY_MIN64) {
            category = MSK_NEG_INF;
        } else if (v == NEG_ZERO_MIN64) {
            category = MSK_NEG_ZERO;
        } else if (v == INDETERMINATE_MIN64) {
            category = MSK_INDETERM;
        } else {
            category = MSK_BUG;
        }
    } else {
        if (WITHIN64(v, POS_NORMALIZED)) {
            category = MSK_POS_NOR;
        } else if (WITHIN64(v, POS_DENORMALIZED)) {
            category = MSK_POS_DEN;
        } else if (WITHIN64(v, POS_SIGNAL_NAN)) {
            category = MSK_POS_SNAN;
        } else if (WITHIN64(v, POS_QUIET_NAN)) {
            category = MSK_POS_QNAN;
        } else if (v == POS_INFINITY_MIN64) {
            category = MSK_POS_INF;
        } else if (v == POS_ZERO_MIN64) {
            category = MSK_POS_ZERO;
        } else {
            category = MSK_BUG;
        }
    }
    return (category & mask) != 0;
}

static PyArrayObject *
NA_FromDimsStridesDescrAndData(int nd, maybelong *d, maybelong *s, PyArray_Descr *descr, char *data)
{
    return (PyArrayObject *)\
        PyArray_NewFromDescr(&PyArray_Type, descr, nd, d,
                s, data, 0, NULL);
}

static PyArrayObject *
NA_FromDimsTypeAndData(int nd, maybelong *d, int type, char *data)
{
    PyArray_Descr *descr = NA_DescrFromType(type);
    return NA_FromDimsStridesDescrAndData(nd, d, NULL, descr, data);
}

static PyArrayObject *
NA_FromDimsStridesTypeAndData(int nd, maybelong *shape, maybelong *strides,
        int type, char *data)
{
    PyArray_Descr *descr = NA_DescrFromType(type);
    return NA_FromDimsStridesDescrAndData(nd, shape, strides, descr, data);
}


typedef struct
{
    NumarrayType type_num;
    char suffix[5];
    int  itemsize;
} scipy_typestr;

static scipy_typestr scipy_descriptors[ ] = {
    { tAny,       "", 0},

    { tBool,      "b1", 1},

    { tInt8,      "i1", 1},
    { tUInt8,     "u1", 1},

    { tInt16,     "i2", 2},
    { tUInt16,    "u2", 2},

    { tInt32,     "i4", 4},
    { tUInt32,    "u4", 4},

    { tInt64,     "i8", 8},
    { tUInt64,    "u8", 8},

    { tFloat32,   "f4", 4},
    { tFloat64,   "f8", 8},

    { tComplex32, "c8", 8},
    { tComplex64, "c16", 16}
};


static int
NA_scipy_typestr(NumarrayType t, int byteorder, char *typestr)
{
    size_t i;
    if (byteorder)
        strcpy(typestr, ">");
    else
        strcpy(typestr, "<");
    for(i=0; i<ELEM(scipy_descriptors); i++) {
        scipy_typestr *ts = &scipy_descriptors[i];
        if (ts->type_num == t) {
            strncat(typestr, ts->suffix, 4);
            return 0;
        }
    }
    return -1;
}

static PyArrayObject *
NA_FromArrayStruct(PyObject *obj)
{
    return (PyArrayObject *)PyArray_FromStructInterface(obj);
}


static PyObject *_Error;

void *libnumarray_API[] = {
    (void*) getBuffer,
    (void*) isBuffer,
    (void*) getWriteBufferDataPtr,
    (void*) isBufferWriteable,
    (void*) getReadBufferDataPtr,
    (void*) getBufferSize,
    (void*) num_log,
    (void*) num_log10,
    (void*) num_pow,
    (void*) num_acosh,
    (void*) num_asinh,
    (void*) num_atanh,
    (void*) num_round,
    (void*) int_dividebyzero_error,
    (void*) int_overflow_error,
    (void*) umult64_overflow,
    (void*) smult64_overflow,
    (void*) NA_Done,
    (void*) NA_NewAll,
    (void*) NA_NewAllStrides,
    (void*) NA_New,
    (void*) NA_Empty,
    (void*) NA_NewArray,
    (void*) NA_vNewArray,
    (void*) NA_ReturnOutput,
    (void*) NA_getBufferPtrAndSize,
    (void*) NA_checkIo,
    (void*) NA_checkOneCBuffer,
    (void*) NA_checkNCBuffers,
    (void*) NA_checkOneStriding,
    (void*) NA_new_cfunc,
    (void*) NA_add_cfunc,
    (void*) NA_InputArray,
    (void*) NA_OutputArray,
    (void*) NA_IoArray,
    (void*) NA_OptionalOutputArray,
    (void*) NA_get_offset,
    (void*) NA_get_Float64,
    (void*) NA_set_Float64,
    (void*) NA_get_Complex64,
    (void*) NA_set_Complex64,
    (void*) NA_get_Int64,
    (void*) NA_set_Int64,
    (void*) NA_get1_Float64,
    (void*) NA_get2_Float64,
    (void*) NA_get3_Float64,
    (void*) NA_set1_Float64,
    (void*) NA_set2_Float64,
    (void*) NA_set3_Float64,
    (void*) NA_get1_Complex64,
    (void*) NA_get2_Complex64,
    (void*) NA_get3_Complex64,
    (void*) NA_set1_Complex64,
    (void*) NA_set2_Complex64,
    (void*) NA_set3_Complex64,
    (void*) NA_get1_Int64,
    (void*) NA_get2_Int64,
    (void*) NA_get3_Int64,
    (void*) NA_set1_Int64,
    (void*) NA_set2_Int64,
    (void*) NA_set3_Int64,
    (void*) NA_get1D_Float64,
    (void*) NA_set1D_Float64,
    (void*) NA_get1D_Int64,
    (void*) NA_set1D_Int64,
    (void*) NA_get1D_Complex64,
    (void*) NA_set1D_Complex64,
    (void*) NA_ShapeEqual,
    (void*) NA_ShapeLessThan,
    (void*) NA_ByteOrder,
    (void*) NA_IeeeSpecial32,
    (void*) NA_IeeeSpecial64,
    (void*) NA_updateDataPtr,
    (void*) NA_typeNoToName,
    (void*) NA_nameToTypeNo,
    (void*) NA_typeNoToTypeObject,
    (void*) NA_intTupleFromMaybeLongs,
    (void*) NA_maybeLongsFromIntTuple,
    (void*) NA_intTupleProduct,
    (void*) NA_isIntegerSequence,
    (void*) NA_setArrayFromSequence,
    (void*) NA_maxType,
    (void*) NA_isPythonScalar,
    (void*) NA_getPythonScalar,
    (void*) NA_setFromPythonScalar,
    (void*) NA_NDArrayCheck,
    (void*) NA_NumArrayCheck,
    (void*) NA_ComplexArrayCheck,
    (void*) NA_elements,
    (void*) NA_typeObjectToTypeNo,
    (void*) NA_copyArray,
    (void*) NA_copy,
    (void*) NA_getType,
    (void*) NA_callCUFuncCore,
    (void*) NA_callStrideConvCFuncCore,
    (void*) NA_stridesFromShape,
    (void*) NA_OperatorCheck,
    (void*) NA_ConverterCheck,
    (void*) NA_UfuncCheck,
    (void*) NA_CfuncCheck,
    (void*) NA_getByteOffset,
    (void*) NA_swapAxes,
    (void*) NA_initModuleGlobal,
    (void*) NA_NumarrayType,
    (void*) NA_NewAllFromBuffer,
    (void*) NA_alloc1D_Float64,
    (void*) NA_alloc1D_Int64,
    (void*) NA_updateAlignment,
    (void*) NA_updateContiguous,
    (void*) NA_updateStatus,
    (void*) NA_NumArrayCheckExact,
    (void*) NA_NDArrayCheckExact,
    (void*) NA_OperatorCheckExact,
    (void*) NA_ConverterCheckExact,
    (void*) NA_UfuncCheckExact,
    (void*) NA_CfuncCheckExact,
    (void*) NA_getArrayData,
    (void*) NA_updateByteswap,
    (void*) NA_DescrFromType,
    (void*) NA_Cast,
    (void*) NA_checkFPErrors,
    (void*) NA_clearFPErrors,
    (void*) NA_checkAndReportFPErrors,
    (void*) NA_IeeeMask32,
    (void*) NA_IeeeMask64,
    (void*) _NA_callStridingHelper,
    (void*) NA_FromDimsStridesDescrAndData,
    (void*) NA_FromDimsTypeAndData,
    (void*) NA_FromDimsStridesTypeAndData,
    (void*) NA_scipy_typestr,
    (void*) NA_FromArrayStruct
};

#if (!defined(METHOD_TABLE_EXISTS))
static PyMethodDef _libnumarrayMethods[] = {
    {NULL, NULL, 0, NULL}        /* Sentinel */
};
#endif

/* boiler plate API init */
#if defined(NPY_PY3K)

#define RETVAL m

static struct PyModuleDef moduledef = {
        PyModuleDef_HEAD_INIT,
        "_capi",
        NULL,
        -1,
        _libnumarrayMethods,
        NULL,
        NULL,
        NULL,
        NULL
};

PyMODINIT_FUNC PyInit__capi(void)
#else

#define RETVAL

PyMODINIT_FUNC init_capi(void)
#endif
{
    PyObject *m;
    PyObject *c_api_object;

    _Error = PyErr_NewException("numpy.numarray._capi.error", NULL, NULL);

    /* Create a CObject containing the API pointer array's address */
#if defined(NPY_PY3K)
    m = PyModule_Create(&moduledef);
#else
    m = Py_InitModule("_capi", _libnumarrayMethods);
#endif

#if defined(NPY_PY3K)
    c_api_object = PyCapsule_New((void *)libnumarray_API, NULL, NULL);
    if (c_api_object == NULL) {
        PyErr_Clear();
    }
#else
    c_api_object = PyCObject_FromVoidPtr((void *)libnumarray_API, NULL);
#endif

    if (c_api_object != NULL) {
        /* Create a name for this object in the module's namespace */
        PyObject *d = PyModule_GetDict(m);

        PyDict_SetItemString(d, "_C_API", c_api_object);
        PyDict_SetItemString(d, "error", _Error);
        Py_DECREF(c_api_object);
    }
    else {
        return RETVAL;
    }
    if (PyModule_AddObject(m, "__version__", PyUString_FromString("0.9")) < 0) {
        return RETVAL;
    }
    if (_import_array() < 0) {
        return RETVAL;
    }
    deferred_libnumarray_init();
    return RETVAL;
}