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
|
/* General Numerical routines support functions, */
/* and common Argyll support functions. */
/* (Perhaps these should be moved out of numlib at some stange ?) */
/*
* Copyright 1997 - 2010 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#if defined (NT)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef UNIX
#include <unistd.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <pthread.h>
#endif
#ifndef SALONEINSTLIB
#include "aconfig.h"
#else
#include "sa_config.h"
#endif
#define NUMSUP_C
#include "numsup.h"
/*
* TODO: Should probably break all the non-numlib stuff out into
* a separate library, so that it can be ommitted.
* Or enhance it so that numerical callers of error()
* can get a callback on out of memory etc. ???
*
*/
/* Globals */
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
char *exe_path = "\000"; /* Directory executable resides in ('/' dir separator) */
//char *error_program = "Unknown"; /* Name to report as responsible for an error */
static int g_log_init = 0; /* Initialised ? */
static int g_deb_init = 0; /* Debug output Initialised ? */
extern a1log default_log;
extern a1log *g_log;
/* Should Vector/Matrix Support functions return NULL on error, */
/* or call error() ? */
int ret_null_on_malloc_fail = 0; /* Call error() */
/******************************************************************/
/* Executable path routine. Sets default error_program too. */
/******************************************************************/
/* Pass in argv[0] from main() */
/* Sets the error_program name too */
void set_exe_path(char *argv0) {
int i;
g_log->tag = argv0;
i = strlen(argv0);
if ((exe_path = malloc(i + 5)) == NULL) {
a1loge(g_log, 1, "set_exe_path: malloc %d bytes failed\n",i+5);
return;
}
strcpy(exe_path, argv0);
#ifdef NT /* CMD.EXE doesn't give us the full path in argv[0] :-( */
/* so we need to fix this */
{
HMODULE mh;
char *tpath = NULL;
int pl;
/* Add an .exe extension if it is missing */
if (i < 4 || _stricmp(exe_path +i -4, ".exe") != 0)
strcat(exe_path, ".exe");
if ((mh = GetModuleHandle(exe_path)) == NULL) {
a1loge(g_log, 1, "set_exe_path: GetModuleHandle '%s' failed with%d\n",
exe_path,GetLastError());
exe_path[0] = '\000';
return;
}
/* Retry until we don't truncate the returned path */
for (pl = 100; ; pl *= 2) {
if (tpath != NULL)
free(tpath);
if ((tpath = malloc(pl)) == NULL) {
a1loge(g_log, 1, "set_exe_path: malloc %d bytes failed\n",pl);
exe_path[0] = '\000';
return;
}
if ((i = GetModuleFileName(mh, tpath, pl)) == 0) {
a1loge(g_log, 1, "set_exe_path: GetModuleFileName '%s' failed with%d\n",
tpath,GetLastError());
exe_path[0] = '\000';
return;
}
if (i < pl) /* There was enough space */
break;
}
free(exe_path);
exe_path = tpath;
/* Convert from MSWindows to UNIX file separator convention */
for (i = 0; ;i++) {
if (exe_path[i] == '\000')
break;
if (exe_path[i] == '\\')
exe_path[i] = '/';
}
}
#else /* Neither does UNIX */
if (*exe_path != '/') { /* Not already absolute */
char *p, *cp;
if (strchr(exe_path, '/') != 0) { /* relative path */
cp = ".:"; /* Fake a relative PATH */
} else {
cp = getenv("PATH");
}
if (cp != NULL) {
int found = 0;
while((p = strchr(cp,':')) != NULL
|| *cp != '\000') {
char b1[PATH_MAX], b2[PATH_MAX];
int ll;
if (p == NULL)
ll = strlen(cp);
else
ll = p - cp;
if ((ll + 1 + strlen(exe_path) + 1) > PATH_MAX) {
a1loge(g_log, 1, "set_exe_path: Search path exceeds PATH_MAX\n");
exe_path[0] = '\000';
return;
}
strncpy(b1, cp, ll); /* Element of path to search */
b1[ll] = '\000';
strcat(b1, "/");
strcat(b1, exe_path); /* Construct path */
if (realpath(b1, b2)) {
if (access(b2, 0) == 0) { /* See if exe exits */
found = 1;
free(exe_path);
if ((exe_path = malloc(strlen(b2)+1)) == NULL) {
a1loge(g_log, 1, "set_exe_path: malloc %d bytes failed\n",strlen(b2)+1);
exe_path[0] = '\000';
return;
}
strcpy(exe_path, b2);
break;
}
}
if (p == NULL)
break;
cp = p + 1;
}
if (found == 0)
exe_path[0] = '\000';
}
}
#endif
/* strip the executable path to the base */
for (i = strlen(exe_path)-1; i >= 0; i--) {
if (exe_path[i] == '/') {
char *tpath;
if ((tpath = malloc(strlen(exe_path + i))) == NULL) {
a1loge(g_log, 1, "set_exe_path: malloc %d bytes failed\n",strlen(exe_path + i));
exe_path[0] = '\000';
return;
}
strcpy(tpath, exe_path + i + 1);
g_log->tag = tpath; /* Set g_log->tag to base name */
exe_path[i+1] = '\000'; /* (The malloc never gets free'd) */
break;
}
}
/* strip off any .exe from the g_log->tag to be more readable */
i = strlen(g_log->tag);
if (i >= 4
&& g_log->tag[i-4] == '.'
&& (g_log->tag[i-3] == 'e' || g_log->tag[i-3] == 'E')
&& (g_log->tag[i-2] == 'x' || g_log->tag[i-2] == 'X')
&& (g_log->tag[i-1] == 'e' || g_log->tag[i-1] == 'E'))
g_log->tag[i-4] = '\000';
// a1logd(g_log, 1, "exe_path = '%s'\n",exe_path);
// a1logd(g_log, 1, "g_log->tag = '%s'\n",g_log->tag);
}
/******************************************************************/
/* Check "ARGYLL_NOT_INTERACTIVE" environment variable. */
/******************************************************************/
/* Check if the "ARGYLL_NOT_INTERACTIVE" environment variable is */
/* set, and set cr_char to '\n' if it is. */
int not_interactive = 0;
char cr_char = '\r';
void check_if_not_interactive() {
char *ev;
if ((ev = getenv("ARGYLL_NOT_INTERACTIVE")) != NULL) {
not_interactive = 1;
cr_char = '\n';
} else {
not_interactive = 0;
cr_char = '\r';
}
}
/******************************************************************/
/* Default verbose/debug/error loger */
/* It's values can be overridden to redirect these messages. */
/******************************************************************/
static void va_loge(a1log *p, char *fmt, ...);
#ifdef NT
/* Get a string describing the MWin operating system */
typedef struct {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
WCHAR szCSDVersion[128];
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} osversioninfoexw;
#ifndef VER_NT_DOMAIN_CONTROLLER
# define VER_NT_DOMAIN_CONTROLLER 0x0000002
# define VER_NT_SERVER 0x0000003
# define VER_NT_WORKSTATION 0x0000001
#endif
static char *get_sys_info() {
static char sysinfo[100] = { "Unknown" };
LONG (WINAPI *pfnRtlGetVersion)(osversioninfoexw*);
*(FARPROC *)&pfnRtlGetVersion
= GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlGetVersion");
if (pfnRtlGetVersion != NULL) {
osversioninfoexw ver = { 0 };
ver.dwOSVersionInfoSize = sizeof(ver);
if (pfnRtlGetVersion(&ver) == 0) {
if (ver.dwMajorVersion > 6 || (ver.dwMajorVersion == 6 && ver.dwMinorVersion > 3)) {
if (ver.wProductType == VER_NT_WORKSTATION)
sprintf(sysinfo,"Windows V%d.%d SP %d",
ver.dwMajorVersion,ver.dwMinorVersion,
ver.wServicePackMajor);
else
sprintf(sysinfo,"Windows Server 2016 V%d.%d SP %d",
ver.dwMajorVersion,ver.dwMinorVersion,
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 6 && ver.dwMinorVersion == 3) {
if (ver.wProductType == VER_NT_WORKSTATION)
sprintf(sysinfo,"Windows V8.1 SP %d",
ver.wServicePackMajor);
else
sprintf(sysinfo,"Windows Server 2012 R2 SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 6 && ver.dwMinorVersion == 2) {
if (ver.wProductType == VER_NT_WORKSTATION)
sprintf(sysinfo,"Windows V8 SP %d",
ver.wServicePackMajor);
else
sprintf(sysinfo,"Windows Server SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 6 && ver.dwMinorVersion == 1) {
if (ver.wProductType == VER_NT_WORKSTATION)
sprintf(sysinfo,"Windows V7 SP %d",
ver.wServicePackMajor);
else
sprintf(sysinfo,"Windows Server 2008 R2 SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 6 && ver.dwMinorVersion == 0) {
if (ver.wProductType == VER_NT_WORKSTATION)
sprintf(sysinfo,"Windows Vista SP %d",
ver.wServicePackMajor);
else
sprintf(sysinfo,"Windows Server 2008 SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 5 && ver.dwMinorVersion == 2) {
// Actually could be Server 2003, Home Server, Server 2003 R2
sprintf(sysinfo,"Windows XP Pro64 SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 5 && ver.dwMinorVersion == 1) {
sprintf(sysinfo,"Windows XP SP %d",
ver.wServicePackMajor);
} else if (ver.dwMajorVersion == 5 && ver.dwMinorVersion == 0) {
sprintf(sysinfo,"Windows XP SP %d",
ver.wServicePackMajor);
} else {
sprintf(sysinfo,"Windows Maj %d Min %d SP %d",
ver.dwMajorVersion,ver.dwMinorVersion,
ver.wServicePackMajor);
}
}
}
return sysinfo;
}
# define A1LOG_LOCK(log, deb) \
if (g_log_init == 0) { \
InitializeCriticalSection(&log->lock); \
EnterCriticalSection(&log->lock); \
g_log_init = 1; \
} else { \
EnterCriticalSection(&log->lock); \
} \
if (deb && !g_deb_init) { \
va_loge(log, "Argyll 'V%s' Build '%s' System '%s'\n",ARGYLL_VERSION_STR,ARGYLL_BUILD_STR, get_sys_info()); \
g_deb_init = 1; \
}
# define A1LOG_UNLOCK(log) LeaveCriticalSection(&log->lock)
#endif
#ifdef UNIX
static char *get_sys_info() {
static char sysinfo[200] = { "Unknown" };
struct utsname ver;
if (uname(&ver) == 0)
sprintf(sysinfo,"%s %s %s %s",ver.sysname, ver.version, ver.release, ver.machine);
return sysinfo;
}
# define A1LOG_LOCK(log, deb) \
if (g_log_init == 0) { \
pthread_mutex_init(&log->lock, NULL); \
pthread_mutex_lock(&log->lock); \
g_log_init = 1; \
} else { \
pthread_mutex_lock(&log->lock); \
} \
if (deb && !g_deb_init) { \
va_loge(log, "Argyll 'V%s' Build '%s' System '%s'\n",ARGYLL_VERSION_STR,ARGYLL_BUILD_STR, get_sys_info()); \
g_deb_init = 1; \
}
# define A1LOG_UNLOCK(log) pthread_mutex_unlock(&log->lock)
#endif
/* Default verbose logging function - print to stdtout */
static void a1_default_v_log(void *cntx, a1log *p, char *fmt, va_list args) {
vfprintf(stdout, fmt, args);
fflush(stdout);
}
/* Default debug & error logging function - print to stderr */
static void a1_default_de_log(void *cntx, a1log *p, char *fmt, va_list args) {
vfprintf(stderr, fmt, args);
fflush(stderr);
}
#define a1_default_d_log a1_default_de_log
#define a1_default_e_log a1_default_de_log
/* Call log->loge() with variags */
static void va_loge(a1log *p, char *fmt, ...) {
va_list args;
va_start(args, fmt);
p->loge(p->cntx, p, fmt, args);
va_end(args);
}
/* Global log */
a1log default_log = {
1, /* Refcount of 1 because this is not allocated or free'd */
"argyll", /* Default tag */
0, /* Vebose off */
0, /* Debug off */
NULL, /* Context */
&a1_default_v_log, /* Default verbose to stdout */
&a1_default_d_log, /* Default debug to stderr */
&a1_default_e_log, /* Default error to stderr */
0, /* error code 0 */
{ '\000' } /* No error message */
};
a1log *g_log = &default_log;
/* If log NULL, allocate a new log and return it, */
/* otherwise increment reference count and return existing log, */
/* exit() if malloc fails. */
a1log *new_a1log(
a1log *log, /* Existing log to reference, NULL if none */
int verb, /* Verbose level to set */
int debug, /* Debug level to set */
void *cntx, /* Function context value */
/* Vebose log function to call - stdout if NULL */
void (*logv)(void *cntx, a1log *p, char *fmt, va_list args),
/* Debug log function to call - stderr if NULL */
void (*logd)(void *cntx, a1log *p, char *fmt, va_list args),
/* Warning/error Log function to call - stderr if NULL */
void (*loge)(void *cntx, a1log *p, char *fmt, va_list args)
) {
if (log != NULL) {
log->refc++;
return log;
}
if ((log = (a1log *)calloc(sizeof(a1log), 1)) == NULL) {
a1loge(g_log, 1, "new_a1log: malloc of a1log failed, calling exit(1)\n");
exit(1);
}
log->refc = 1;
log->verb = verb;
log->debug = debug;
log->cntx = cntx;
if (logv != NULL)
log->logv = logv;
else
log->logv = a1_default_v_log;
if (logd != NULL)
log->logd = logd;
else
log->logd = a1_default_d_log;
if (loge != NULL)
log->loge = loge;
else
log->loge = a1_default_e_log;
log->errc = 0;
log->errm[0] = '\000';
return log;
}
/* Same as above but set default functions */
a1log *new_a1log_d(a1log *log) {
return new_a1log(log, 0, 0, NULL, NULL, NULL, NULL);
}
/* Decrement reference count and free log. */
/* Returns NULL */
a1log *del_a1log(a1log *log) {
if (log != NULL) {
if (--log->refc <= 0) {
#ifdef NT
DeleteCriticalSection(&log->lock);
#endif
#ifdef UNIX
pthread_mutex_destroy(&log->lock);
#endif
free(log);
}
}
return NULL;
}
/* Set the tag. Note that the tage string is NOT copied, just referenced */
void a1log_tag(a1log *log, char *tag) {
log->tag = tag;
}
/* Log a verbose message if level >= verb */
void a1logv(a1log *log, int level, char *fmt, ...) {
if (log != NULL) {
if (log->verb >= level) {
va_list args;
A1LOG_LOCK(log, 0);
va_start(args, fmt);
log->logv(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
}
}
/* Log a debug message if level >= debug */
void a1logd(a1log *log, int level, char *fmt, ...) {
if (log != NULL) {
if (log->debug >= level) {
va_list args;
A1LOG_LOCK(log, 1);
va_start(args, fmt);
log->loge(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
}
}
/* log a warning message to the verbose, debug and error output, */
void a1logw(a1log *log, char *fmt, ...) {
if (log != NULL) {
va_list args;
/* log to all the outputs, but only log once */
A1LOG_LOCK(log, 0);
va_start(args, fmt);
log->loge(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
if (log->logd != log->loge) {
A1LOG_LOCK(log, 1);
va_start(args, fmt);
log->logd(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
if (log->logv != log->loge && log->logv != log->logd) {
A1LOG_LOCK(log, 0);
va_start(args, fmt);
log->logv(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
}
}
/* log an error message to the verbose, debug and error output, */
/* and latch the error if it is the first. */
/* ecode = system, icoms or instrument error */
void a1loge(a1log *log, int ecode, char *fmt, ...) {
if (log != NULL) {
va_list args;
if (log->errc == 0) {
A1LOG_LOCK(log, 0);
log->errc = ecode;
va_start(args, fmt);
vsnprintf(log->errm, A1_LOG_BUFSIZE, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
va_start(args, fmt);
/* log to all the outputs, but only log once */
A1LOG_LOCK(log, 0);
va_start(args, fmt);
log->loge(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
if (log->logd != log->loge) {
A1LOG_LOCK(log, 1);
va_start(args, fmt);
log->logd(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
if (log->logv != log->loge && log->logv != log->logd) {
A1LOG_LOCK(log, 0);
va_start(args, fmt);
log->logv(log->cntx, log, fmt, args);
va_end(args);
A1LOG_UNLOCK(log);
}
}
}
/* Unlatch an error message. */
/* This just resets errc and errm */
void a1logue(a1log *log) {
if (log != NULL) {
log->errc = 0;
log->errm[0] = '\000';
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Print bytes as hex to debug log */
/* base is the base of the displayed offset */
void adump_bytes(a1log *log, char *pfx, unsigned char *buf, int base, int len) {
int i, j, ii;
char oline[200] = { '\000' }, *bp = oline;
for (i = j = 0; i < len; i++) {
if ((i % 16) == 0)
bp += sprintf(bp,"%s%04x:",pfx,base+i);
bp += sprintf(bp," %02x",buf[i]);
if ((i+1) >= len || ((i+1) % 16) == 0) {
for (ii = i; ((ii+1) % 16) != 0; ii++)
bp += sprintf(bp," ");
bp += sprintf(bp," ");
for (; j <= i; j++) {
if (!(buf[j] & 0x80) && isprint(buf[j]))
bp += sprintf(bp,"%c",buf[j]);
else
bp += sprintf(bp,".");
}
bp += sprintf(bp,"\n");
a1logd(log,0,"%s",oline);
bp = oline;
}
}
}
/******************************************************************/
/* Default verbose/warning/error output routines */
/* These fall through to, and can be re-director using the */
/* above log class. */
/******************************************************************/
/* Some utilities to allow us to format output to log functions */
/* (Caller aquires lock) */
static void g_logv(char *fmt, ...) {
va_list args;
va_start(args, fmt);
g_log->logv(g_log->cntx, g_log, fmt, args);
va_end(args);
}
static void g_loge(char *fmt, ...) {
va_list args;
va_start(args, fmt);
g_log->loge(g_log->cntx, g_log, fmt, args);
va_end(args);
}
void
verbose(int level, char *fmt, ...) {
if (g_log->verb >= level) {
va_list args;
A1LOG_LOCK(g_log, 0);
g_logv("%s: ",g_log->tag);
va_start(args, fmt);
g_log->logv(g_log->cntx, g_log, fmt, args);
va_end(args);
g_logv("\n");
A1LOG_UNLOCK(g_log);
}
}
void
warning(char *fmt, ...) {
va_list args;
A1LOG_LOCK(g_log, 0);
g_loge("%s: Warning - ",g_log->tag);
va_start(args, fmt);
g_log->loge(g_log->cntx, g_log, fmt, args);
va_end(args);
g_loge("\n");
A1LOG_UNLOCK(g_log);
}
ATTRIBUTE_NORETURN void
error(char *fmt, ...) {
va_list args;
A1LOG_LOCK(g_log, 0);
g_loge("%s: Error - ",g_log->tag);
va_start(args, fmt);
g_log->loge(g_log->cntx, g_log, fmt, args);
va_end(args);
g_loge("\n");
A1LOG_UNLOCK(g_log);
exit(1);
}
/******************************************************************/
/* Suplimental allcation functions */
/******************************************************************/
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t)(-1))
#endif
/* a * b */
static size_t ssat_mul(size_t a, size_t b) {
size_t c;
if (a == 0 || b == 0)
return 0;
if (a > (SIZE_MAX/b))
return SIZE_MAX;
else
return a * b;
}
/* reallocate and clear new allocation */
void *recalloc( /* Return new address */
void *ptr, /* Current address */
size_t cnum, /* Current number and unit size */
size_t csize,
size_t nnum, /* New number and unit size */
size_t nsize
) {
int ind = 0;
size_t ctot, ntot;
if (ptr == NULL)
return calloc(nnum, nsize);
if ((ntot = ssat_mul(nnum, nsize)) == SIZE_MAX)
return NULL; /* Overflow */
if ((ctot = ssat_mul(cnum, csize)) == SIZE_MAX)
return NULL; /* Overflow */
ptr = realloc(ptr, ntot);
if (ptr != NULL && ntot > ctot)
memset((char *)ptr + ctot, 0, ntot - ctot); /* Clear the new region */
return ptr;
}
/******************************************************************/
/* OS X App Nap fixes */
/******************************************************************/
#if defined(__APPLE__)
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
# include <objc/runtime.h>
# include <objc/message.h>
#else
# include <objc/objc-runtime.h>
#endif
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
# include <objc/objc-auto.h>
#endif
/*
OS X 10.9+ App Nap problems bug:
<http://stackoverflow.com/questions/22784886/what-can-make-nanosleep-drift-with-exactly-10-sec-on-mac-os-x-10-9>
NSProcessInfo variables:
<https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSProcessInfo_Class/#//apple_ref/c/tdef/NSActivityOptions>
typedef enum : uint64_t { NSActivityIdleDisplaySleepDisabled = (1ULL << 40),
NSActivityIdleSystemSleepDisabled = (1ULL << 20),
NSActivitySuddenTerminationDisabled = (1ULL << 14),
NSActivityAutomaticTerminationDisabled = (1ULL << 15),
NSActivityUserInitiated = (0x00FFFFFFULL | NSActivityIdleSystemSleepDisabled ),
NSActivityUserInitiatedAllowingIdleSystemSleep =
(NSActivityUserInitiated & ~NSActivityIdleSystemSleepDisabled ),
NSActivityBackground = 0x000000FFULL,
NSActivityLatencyCritical = 0xFF00000000ULL,
} NSActivityOptions;
See <http://stackoverflow.com/questions/19847293/disable-app-nap-in-macos-10-9-mavericks-application>:
@property (strong) id activity;
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"receiving OSC messages"];
}
<http://stackoverflow.com/questions/19671197/disabling-app-nap-with-beginactivitywithoptions>
NSProcessInfo = interface(NSObject)['{B96935F6-3809-4A49-AD4F-CBBAB0F2C961}']
function beginActivityWithOptions(options: NSActivityOptions; reason: NSString): NSObject; cdecl;
<http://stackoverflow.com/questions/22164571/weird-behaviour-of-dispatch-after>
*/
static int osx_userinitiated_cnt = 0;
static id osx_userinitiated_activity = nil;
/* Tell App Nap that this is user initiated */
void osx_userinitiated_start() {
Class pic; /* Process info class */
SEL pis; /* Process info selector */
SEL bawo; /* Begin Activity With Options selector */
id pi; /* Process info */
id str;
if (osx_userinitiated_cnt++ != 0)
return;
a1logd(g_log, 7, "OS X - User Initiated Activity start\n");
/* We have to be conservative to avoid triggering an exception when run on older OS X, */
/* since beginActivityWithOptions is only available in >= 10.9 */
if ((pic = (Class)objc_getClass("NSProcessInfo")) == nil) {
return;
}
if (class_getClassMethod(pic, (pis = sel_getUid("processInfo"))) == NULL) {
return;
}
if (class_getInstanceMethod(pic, (bawo = sel_getUid("beginActivityWithOptions:reason:"))) == NULL) {
a1logd(g_log, 7, "OS X - beginActivityWithOptions not supported\n");
return;
}
/* Get the process instance */
if ((pi = objc_msgSend((id)pic, pis)) == nil) {
return;
}
/* Create a reason string */
str = objc_msgSend(objc_getClass("NSString"), sel_getUid("alloc"));
str = objc_msgSend(str, sel_getUid("initWithUTF8String:"), "ArgyllCMS");
/* Start activity that tells App Nap to mind its own business. */
/* NSActivityUserInitiatedAllowingIdleSystemSleep */
osx_userinitiated_activity = objc_msgSend(pi, bawo, 0x00FFFFFFULL, str);
}
/* Done with user initiated */
void osx_userinitiated_end() {
if (osx_userinitiated_cnt > 0) {
osx_userinitiated_cnt--;
if (osx_userinitiated_cnt == 0 && osx_userinitiated_activity != nil) {
a1logd(g_log, 7, "OS X - User Initiated Activity end");
objc_msgSend(
objc_msgSend(objc_getClass("NSProcessInfo"), sel_getUid("processInfo")),
sel_getUid("endActivity:"), osx_userinitiated_activity);
osx_userinitiated_activity = nil;
}
}
}
static int osx_latencycritical_cnt = 0;
static id osx_latencycritical_activity = nil;
/* Tell App Nap that this is latency critical */
void osx_latencycritical_start() {
Class pic; /* Process info class */
SEL pis; /* Process info selector */
SEL bawo; /* Begin Activity With Options selector */
id pi; /* Process info */
id str;
if (osx_latencycritical_cnt++ != 0)
return;
a1logd(g_log, 7, "OS X - Latency Critical Activity start\n");
/* We have to be conservative to avoid triggering an exception when run on older OS X */
if ((pic = (Class)objc_getClass("NSProcessInfo")) == nil) {
return;
}
if (class_getClassMethod(pic, (pis = sel_getUid("processInfo"))) == NULL) {
return;
}
if (class_getInstanceMethod(pic, (bawo = sel_getUid("beginActivityWithOptions:reason:"))) == NULL) {
a1logd(g_log, 7, "OS X - beginActivityWithOptions not supported\n");
return;
}
/* Get the process instance */
if ((pi = objc_msgSend((id)pic, pis)) == nil) {
return;
}
/* Create a reason string */
str = objc_msgSend(objc_getClass("NSString"), sel_getUid("alloc"));
str = objc_msgSend(str, sel_getUid("initWithUTF8String:"), "Measuring Color");
/* Start activity that tells App Nap to mind its own business. */
/* NSActivityUserInitiatedAllowingIdleSystemSleep | NSActivityLatencyCritical */
osx_latencycritical_activity = objc_msgSend(pi, bawo, 0x00FFFFFFULL | 0xFF00000000ULL, str);
}
/* Done with latency critical */
void osx_latencycritical_end() {
if (osx_latencycritical_cnt > 0) {
osx_latencycritical_cnt--;
if (osx_latencycritical_cnt == 0 && osx_latencycritical_activity != nil) {
a1logd(g_log, 7, "OS X - Latency Critical Activity end");
objc_msgSend(
objc_msgSend(objc_getClass("NSProcessInfo"), sel_getUid("processInfo")),
sel_getUid("endActivity:"), osx_latencycritical_activity);
osx_latencycritical_activity = nil;
}
}
}
#endif /* __APPLE__ */
/******************************************************************/
/* Numerical Recipes Vector/Matrix Support functions */
/******************************************************************/
/* Note the z suffix versions return zero'd vectors/matricies */
/* Double Vector malloc/free */
double *dvector(
int nl, /* Lowest index */
int nh /* Highest index */
) {
double *v;
if ((v = (double *) malloc((nh-nl+1) * sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dvector()");
}
return v-nl;
}
double *dvectorz(
int nl, /* Lowest index */
int nh /* Highest index */
) {
double *v;
if ((v = (double *) calloc(nh-nl+1, sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dvector()");
}
return v-nl;
}
void free_dvector(
double *v,
int nl, /* Lowest index */
int nh /* Highest index */
) {
if (v == NULL)
return;
free((char *) (v+nl));
}
/* --------------------- */
/* 2D Double vector malloc/free */
double **dmatrix(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
double **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (double **) malloc((rows + 1) * sizeof(double *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (double *) malloc(rows * cols * sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
double **dmatrixz(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
double **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (double **) malloc((rows + 1) * sizeof(double *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (double *) calloc(rows * cols, sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
void free_dmatrix(
double **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
free((char *)(m[nrl-1]));
free((char *)(m+nrl-1));
}
/* --------------------- */
/* 2D diagonal half matrix vector malloc/free */
/* A half matrix must have equal rows and columns, */
/* and the column address must always be >= than the row. */
double **dhmatrix(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i, j;
int rows, cols;
double **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if (rows != cols) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("dhmatrix() given unequal rows and columns");
}
if ((m = (double **) malloc((rows + 1) * sizeof(double *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dhmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (double *) malloc((rows * rows + rows)/2 * sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dhmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1, j = 1; i <= nrh; i++, j++) /* Set subsequent row addresses */
m[i] = m[i-1] + j; /* Start with 1 entry and increment */
return m;
}
double **dhmatrixz(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i, j;
int rows, cols;
double **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if (rows != cols) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("dhmatrix() given unequal rows and columns");
}
if ((m = (double **) malloc((rows + 1) * sizeof(double *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dhmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (double *) calloc((rows * rows + rows)/2, sizeof(double))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dhmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1, j = 1; i <= nrh; i++, j++) /* Set subsequent row addresses */
m[i] = m[i-1] + j; /* Start with 1 entry and increment */
return m;
}
void free_dhmatrix(
double **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
free((char *)(m[nrl-1]));
free((char *)(m+nrl-1));
}
/* --------------------- */
/* 2D vector copy */
void copy_dmatrix(
double **dst,
double **src,
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i, j;
for (j = nrl; j <= nrh; j++)
for (i = ncl; i <= nch; i++)
dst[j][i] = src[j][i];
}
/* Copy a matrix to a 3x3 standard C array */
void copy_dmatrix_to3x3(
double dst[3][3],
double **src,
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i, j;
if ((nrh - nrl) > 2)
nrh = nrl + 2;
if ((nch - ncl) > 2)
nch = ncl + 2;
for (j = nrl; j <= nrh; j++)
for (i = ncl; i <= nch; i++)
dst[j][i] = src[j][i];
}
/* -------------------------------------------------------------- */
/* Convert standard C type 2D array into an indirect referenced array */
double **convert_dmatrix(
double *a, /* base address of normal C array, ie &a[0][0] */
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i, j, nrow = nrh-nrl+1, ncol = nch-ncl+1;
double **m;
/* Allocate pointers to rows */
if ((m = (double **) malloc(nrow * sizeof(double*))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in convert_dmatrix()");
}
m -= nrl;
m[nrl] = a - ncl;
for(i=1, j = nrl+1; i < nrow; i++, j++)
m[j] = m[j-1] + ncol;
/* return pointer to array of pointers */
return m;
}
/* Free the indirect array reference (but not array) */
void free_convert_dmatrix(
double **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
free((char*) (m+nrl));
}
/* -------------------------- */
/* Float vector malloc/free */
float *fvector(
int nl, /* Lowest index */
int nh /* Highest index */
) {
float *v;
if ((v = (float *) malloc((nh-nl+1) * sizeof(float))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in fvector()");
}
return v-nl;
}
float *fvectorz(
int nl, /* Lowest index */
int nh /* Highest index */
) {
float *v;
if ((v = (float *) calloc(nh-nl+1, sizeof(float))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in fvector()");
}
return v-nl;
}
void free_fvector(
float *v,
int nl, /* Lowest index */
int nh /* Highest index */
) {
if (v == NULL)
return;
free((char *) (v+nl));
}
/* --------------------- */
/* 2D Float matrix malloc/free */
float **fmatrix(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
float **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (float **) malloc((rows + 1) * sizeof(float *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (float *) malloc(rows * cols * sizeof(float))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
float **fmatrixz(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
float **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (float **) malloc((rows + 1) * sizeof(float *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (float *) calloc(rows * cols, sizeof(float))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in dmatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
void free_fmatrix(
float **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
free((char *)(m[nrl-1]));
free((char *)(m+nrl-1));
}
/* ------------------ */
/* Integer vector malloc/free */
int *ivector(
int nl, /* Lowest index */
int nh /* Highest index */
) {
int *v;
if ((v = (int *) malloc((nh-nl+1) * sizeof(int))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in ivector()");
}
return v-nl;
}
int *ivectorz(
int nl, /* Lowest index */
int nh /* Highest index */
) {
int *v;
if ((v = (int *) calloc(nh-nl+1, sizeof(int))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in ivector()");
}
return v-nl;
}
void free_ivector(
int *v,
int nl, /* Lowest index */
int nh /* Highest index */
) {
if (v == NULL)
return;
free((char *) (v+nl));
}
/* ------------------------------ */
/* 2D integer matrix malloc/free */
int **imatrix(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
int **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (int **) malloc((rows + 1) * sizeof(int *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in imatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (int *) malloc(rows * cols * sizeof(int))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in imatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
int **imatrixz(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
int **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (int **) malloc((rows + 1) * sizeof(int *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in imatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (int *) calloc(rows * cols, sizeof(int))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in imatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
void free_imatrix(
int **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
free((char *)(m[nrl-1]));
free((char *)(m+nrl-1));
}
/* ------------------ */
/* Short vector malloc/free */
short *svector(
int nl, /* Lowest index */
int nh /* Highest index */
) {
short *v;
if ((v = (short *) malloc((nh-nl+1) * sizeof(short))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in svector()");
}
return v-nl;
}
short *svectorz(
int nl, /* Lowest index */
int nh /* Highest index */
) {
short *v;
if ((v = (short *) calloc(nh-nl+1, sizeof(short))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in svector()");
}
return v-nl;
}
void free_svector(
short *v,
int nl, /* Lowest index */
int nh /* Highest index */
) {
if (v == NULL)
return;
free((char *) (v+nl));
}
/* ------------------------------ */
/* 2D short vector malloc/free */
short **smatrix(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
short **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (short **) malloc((rows + 1) * sizeof(short *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in smatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (short *) malloc(rows * cols * sizeof(short))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in smatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
short **smatrixz(
int nrl, /* Row low index */
int nrh, /* Row high index */
int ncl, /* Col low index */
int nch /* Col high index */
) {
int i;
int rows, cols;
short **m;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
rows = nrh - nrl + 1;
cols = nch - ncl + 1;
if ((m = (short **) malloc((rows + 1) * sizeof(short *))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in smatrix(), pointers");
}
m -= nrl; /* Offset to nrl */
m += 1; /* Make nrl-1 pointer to main allocation, in case rows get swaped */
if ((m[nrl-1] = (short *) calloc(rows * cols, sizeof(short))) == NULL) {
if (ret_null_on_malloc_fail)
return NULL;
else
error("Malloc failure in smatrix(), array");
}
m[nrl] = m[nrl-1] - ncl; /* Set first row address, offset to ncl */
for(i = nrl+1; i <= nrh; i++) /* Set subsequent row addresses */
m[i] = m[i-1] + cols;
return m;
}
void free_smatrix(
short **m,
int nrl,
int nrh,
int ncl,
int nch
) {
if (m == NULL)
return;
if (nrh < nrl) /* Prevent failure for 0 dimension */
nrh = nrl;
if (nch < ncl)
nch = ncl;
free((char *)(m[nrl-1]));
free((char *)(m+nrl-1));
}
/***************************/
/* Basic matrix operations */
/***************************/
/* Transpose a 0 base matrix */
void matrix_trans(double **d, double **s, int nr, int nc) {
int i, j;
for (i = 0; i < nr; i++) {
for (j = 0; j < nc; j++) {
d[j][i] = s[i][j];
}
}
}
/* Multiply two 0 based matricies */
/* Return nz on matching error */
int matrix_mult(
double **d, int nr, int nc,
double **s1, int nr1, int nc1,
double **s2, int nr2, int nc2
) {
int i, j, k;
/* s1 and s2 must mesh */
if (nc1 != nr2)
return 1;
/* Output rows = s1 rows */
if (nr != nr1)
return 2;
/* Output colums = s2 columns */
if (nc != nc2)
return 2;
for (i = 0; i < nr1; i++) {
for (j = 0; j < nc2; j++) {
d[i][j] = 0.0;
for (k = 0; k < nc1; k++) {
d[i][j] += s1[i][k] * s2[k][j];
}
}
}
return 0;
}
/* Diagnostic - print to g_log debug */
void matrix_print(char *c, double **a, int nr, int nc) {
int i, j;
a1logd(g_log, 0, "%s, %d x %d\n",c,nr,nc);
for (j = 0; j < nr; j++) {
a1logd(g_log, 0, " ");
for (i = 0; i < nc; i++) {
a1logd(g_log, 0, " %.2f",a[j][i]);
}
a1logd(g_log, 0, "\n");
}
}
/*******************************************/
/* Platform independent IEE754 conversions */
/*******************************************/
/* Convert a native double to an IEEE754 encoded single precision value, */
/* in a platform independent fashion. (ie. This works even */
/* on the rare platforms that don't use IEEE 754 floating */
/* point for their C implementation) */
ORD32 doubletoIEEE754(double d) {
ORD32 sn = 0, ep = 0, ma;
ORD32 id;
/* Convert double to IEEE754 single precision. */
/* This would be easy if we're running on an IEEE754 architecture, */
/* but isn't generally portable, so we use ugly code: */
if (d < 0.0) {
sn = 1;
d = -d;
}
if (d != 0.0) {
int ee;
ee = (int)floor(log(d)/log(2.0));
if (ee < -126) /* Allow for denormalized */
ee = -126;
d *= pow(0.5, (double)(ee - 23));
ee += 127;
if (ee < 1) /* Too small */
ee = 0; /* Zero or denormalised */
else if (ee > 254) { /* Too large */
ee = 255; /* Infinity */
d = 0.0;
}
ep = ee;
} else {
ep = 0; /* Zero */
}
ma = ((ORD32)d) & ((1 << 23)-1);
id = (sn << 31) | (ep << 23) | ma;
return id;
}
/* Convert a an IEEE754 encoded single precision value to a native double, */
/* in a platform independent fashion. (ie. This works even */
/* on the rare platforms that don't use IEEE 754 floating */
/* point for their C implementation) */
double IEEE754todouble(ORD32 ip) {
double op;
ORD32 sn = 0, ep = 0, ma;
sn = (ip >> 31) & 0x1;
ep = (ip >> 23) & 0xff;
ma = ip & 0x7fffff;
if (ep == 0) { /* Zero or denormalised */
op = (double)ma/(double)(1 << 23);
op *= pow(2.0, (-126.0));
} else {
op = (double)(ma | (1 << 23))/(double)(1 << 23);
op *= pow(2.0, (((int)ep)-127.0));
}
if (sn)
op = -op;
return op;
}
/* Convert a native double to an IEEE754 encoded double precision value, */
/* in a platform independent fashion. (ie. This works even */
/* on the rare platforms that don't use IEEE 754 floating */
/* point for their C implementation) */
ORD64 doubletoIEEE754_64(double d) {
ORD32 sn = 0, ep = 0;
ORD64 ma, id;
/* Convert double to IEEE754 double precision. */
/* This would be easy if we know we're running on an IEEE754 architecture, */
/* but isn't generally portable, so we use ugly code: */
if (d < 0.0) {
sn = 1;
d = -d;
}
if (d != 0.0) {
int ee;
ee = (int)floor(log(d)/log(2.0));
if (ee < -1022) /* Allow for denormalized */
ee = -1022;
d *= pow(0.5, (double)(ee - 52));
ee += 1023; /* Exponent bias */
if (ee < 1) /* Too small */
ee = 0; /* Zero or denormalised */
else if (ee > 2046) { /* Too large */
ee = 2047; /* Infinity */
d = 0.0;
}
ep = ee;
} else {
ep = 0; /* Zero */
}
ma = ((ORD64)d) & (((ORD64)1 << 52)-1);
id = ((ORD64)sn << 63) | ((ORD64)ep << 52) | ma;
return id;
}
/* Convert a an IEEE754 encode double precision value to a native double, */
/* in a platform independent fashion. (ie. This works even */
/* on the rare platforms that don't use IEEE 754 floating */
/* point for their C implementation) */
double IEEE754_64todouble(ORD64 ip) {
double op;
ORD32 sn = 0, ep = 0;
INR64 ma;
sn = (ip >> 63) & 0x1;
ep = (ip >> 52) & 0x7ff;
ma = ip & (((INR64)1 << 52)-1);
if (ep == 0) { /* Zero or denormalised */
op = (double)ma/(double)((INR64)1 << 52);
op *= pow(2.0, -1022.0);
} else {
op = (double)(ma | ((INR64)1 << 52))/(double)((INR64)1 << 52);
op *= pow(2.0, (((int)ep)-1023.0));
}
if (sn)
op = -op;
return op;
}
/* Return a string representation of a 32 bit ctime. */
/* A static buffer is used. There is no \n at the end */
char *ctime_32(const INR32 *timer) {
char *rv;
#if defined(_MSC_VER) && __MSVCRT_VERSION__ >= 0x0601
rv = _ctime32((const __time32_t *)timer);
#else
time_t timerv = (time_t) *timer; /* May case to 64 bit */
rv = ctime(&timerv);
#endif
if (rv != NULL)
rv[strlen(rv)-1] = '\000';
return rv;
}
/* Return a string representation of a 64 bit ctime. */
/* A static buffer is used. There is no \n at the end */
char *ctime_64(const INR64 *timer) {
char *rv;
#if defined(_MSC_VER) && __MSVCRT_VERSION__ >= 0x0601
rv = _ctime64((const __time64_t *)timer);
#else
time_t timerv;
if (sizeof(time_t) == 4 && *timer > 0x7fffffff)
return NULL;
timerv = (time_t) *timer; /* May truncate to 32 bits */
rv = ctime(&timerv);
#endif
if (rv != NULL)
rv[strlen(rv)-1] = '\000';
return rv;
}
/*******************************************/
/* Native to/from byte buffer functions */
/*******************************************/
/* No overflow detection is done - */
/* numbers are clipped or truncated. */
/* be = Big Endian */
/* le = Little Endian */
/* - - - - - - - - */
/* Unsigned 8 bit */
unsigned int read_ORD8(ORD8 *p) {
unsigned int rv;
rv = ((unsigned int)p[0]);
return rv;
}
void write_ORD8(ORD8 *p, unsigned int d) {
if (d > 0xff)
d = 0xff;
p[0] = (ORD8)(d);
}
/* - - - - - - - - */
/* Signed 8 bit */
int read_INR8(ORD8 *p) {
int rv;
rv = (int)(INR8)p[0];
return rv;
}
void write_INR8(ORD8 *p, int d) {
if (d > 0x7f)
d = 0x7f;
else if (d < -0x80)
d = -0x80;
p[0] = (ORD8)(d);
}
/* - - - - - - - - */
/* Unsigned 16 bit */
unsigned int read_ORD16_be(ORD8 *p) {
unsigned int rv;
rv = (((unsigned int)p[0]) << 8)
+ (((unsigned int)p[1]));
return rv;
}
unsigned int read_ORD16_le(ORD8 *p) {
unsigned int rv;
rv = (((unsigned int)p[0]))
+ (((unsigned int)p[1]) << 8);
return rv;
}
void write_ORD16_be(ORD8 *p, unsigned int d) {
if (d > 0xffff)
d = 0xffff;
p[0] = (ORD8)(d >> 8);
p[1] = (ORD8)(d);
}
void write_ORD16_le(ORD8 *p, unsigned int d) {
if (d > 0xffff)
d = 0xffff;
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
}
/* - - - - - - - - */
/* Signed 16 bit */
int read_INR16_be(ORD8 *p) {
int rv;
rv = (((int)(INR8)p[0]) << 8)
+ (((int)p[1]));
return rv;
}
int read_INR16_le(ORD8 *p) {
int rv;
rv = (((int)p[0]))
+ (((int)(INR8)p[1]) << 8);
return rv;
}
void write_INR16_be(ORD8 *p, int d) {
if (d > 0x7fff)
d = 0x7fff;
else if (d < -0x8000)
d = -0x8000;
p[0] = (ORD8)(d >> 8);
p[1] = (ORD8)(d);
}
void write_INR16_le(ORD8 *p, int d) {
if (d > 0x7fff)
d = 0x7fff;
else if (d < -0x8000)
d = -0x8000;
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
}
/* - - - - - - - - */
/* Unsigned 32 bit */
unsigned int read_ORD32_be(ORD8 *p) {
unsigned int rv;
rv = (((unsigned int)p[0]) << 24)
+ (((unsigned int)p[1]) << 16)
+ (((unsigned int)p[2]) << 8)
+ (((unsigned int)p[3]));
return rv;
}
unsigned int read_ORD32_le(ORD8 *p) {
unsigned int rv;
rv = (((unsigned int)p[0]))
+ (((unsigned int)p[1]) << 8)
+ (((unsigned int)p[2]) << 16)
+ (((unsigned int)p[3]) << 24);
return rv;
}
void write_ORD32_be(ORD8 *p, unsigned int d) {
p[0] = (ORD8)(d >> 24);
p[1] = (ORD8)(d >> 16);
p[2] = (ORD8)(d >> 8);
p[3] = (ORD8)(d);
}
void write_ORD32_le(ORD8 *p, unsigned int d) {
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
p[2] = (ORD8)(d >> 16);
p[3] = (ORD8)(d >> 24);
}
/* - - - - - - - - */
/* Signed 32 bit */
int read_INR32_be(ORD8 *p) {
int rv;
rv = (((int)(INR8)p[0]) << 24)
+ (((int)p[1]) << 16)
+ (((int)p[2]) << 8)
+ (((int)p[3]));
return rv;
}
int read_INR32_le(ORD8 *p) {
int rv;
rv = (((int)p[0]))
+ (((int)p[1]) << 8)
+ (((int)p[2]) << 16)
+ (((int)(INR8)p[3]) << 24);
return rv;
}
void write_INR32_be(ORD8 *p, int d) {
p[0] = (ORD8)(d >> 24);
p[1] = (ORD8)(d >> 16);
p[2] = (ORD8)(d >> 8);
p[3] = (ORD8)(d);
}
void write_INR32_le(ORD8 *p, int d) {
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
p[2] = (ORD8)(d >> 16);
p[3] = (ORD8)(d >> 24);
}
/* - - - - - - - - */
/* Unsigned 64 bit */
ORD64 read_ORD64_be(ORD8 *p) {
ORD64 rv;
rv = (((ORD64)p[0]) << 56)
+ (((ORD64)p[1]) << 48)
+ (((ORD64)p[2]) << 40)
+ (((ORD64)p[3]) << 32)
+ (((ORD64)p[4]) << 24)
+ (((ORD64)p[5]) << 16)
+ (((ORD64)p[6]) << 8)
+ (((ORD64)p[7]));
return rv;
}
ORD64 read_ORD64_le(ORD8 *p) {
ORD64 rv;
rv = (((ORD64)p[0]))
+ (((ORD64)p[1]) << 8)
+ (((ORD64)p[2]) << 16)
+ (((ORD64)p[3]) << 24)
+ (((ORD64)p[4]) << 32)
+ (((ORD64)p[5]) << 40)
+ (((ORD64)p[6]) << 48)
+ (((ORD64)p[7]) << 56);
return rv;
}
void write_ORD64_be(ORD8 *p, ORD64 d) {
p[0] = (ORD8)(d >> 56);
p[1] = (ORD8)(d >> 48);
p[2] = (ORD8)(d >> 40);
p[3] = (ORD8)(d >> 32);
p[4] = (ORD8)(d >> 24);
p[5] = (ORD8)(d >> 16);
p[6] = (ORD8)(d >> 8);
p[7] = (ORD8)(d);
}
void write_ORD64_le(ORD8 *p, ORD64 d) {
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
p[2] = (ORD8)(d >> 16);
p[3] = (ORD8)(d >> 24);
p[4] = (ORD8)(d >> 32);
p[5] = (ORD8)(d >> 40);
p[6] = (ORD8)(d >> 48);
p[7] = (ORD8)(d >> 56);
}
/* - - - - - - - - */
/* Signed 64 bit */
INR64 read_INR64_be(ORD8 *p) {
INR64 rv;
rv = (((INR64)(INR8)p[0]) << 56)
+ (((INR64)p[1]) << 48)
+ (((INR64)p[2]) << 40)
+ (((INR64)p[3]) << 32)
+ (((INR64)p[4]) << 24)
+ (((INR64)p[5]) << 16)
+ (((INR64)p[6]) << 8)
+ (((INR64)p[7]));
return rv;
}
INR64 read_INR64_le(ORD8 *p) {
INR64 rv;
rv = (((INR64)p[0]))
+ (((INR64)p[1]) << 8)
+ (((INR64)p[2]) << 16)
+ (((INR64)p[3]) << 24)
+ (((INR64)p[4]) << 32)
+ (((INR64)p[5]) << 40)
+ (((INR64)p[6]) << 48)
+ (((INR64)(INR8)p[7]) << 56);
return rv;
}
void write_INR64_be(ORD8 *p, INR64 d) {
p[0] = (ORD8)(d >> 56);
p[1] = (ORD8)(d >> 48);
p[2] = (ORD8)(d >> 40);
p[3] = (ORD8)(d >> 32);
p[4] = (ORD8)(d >> 24);
p[5] = (ORD8)(d >> 16);
p[6] = (ORD8)(d >> 8);
p[7] = (ORD8)(d);
}
void write_INR64_le(ORD8 *p, INR64 d) {
p[0] = (ORD8)(d);
p[1] = (ORD8)(d >> 8);
p[2] = (ORD8)(d >> 16);
p[3] = (ORD8)(d >> 24);
p[4] = (ORD8)(d >> 32);
p[5] = (ORD8)(d >> 40);
p[6] = (ORD8)(d >> 48);
p[7] = (ORD8)(d >> 56);
}
/*******************************/
/* System independent timing */
#ifdef NT
/* Sleep for the given number of msec */
void msec_sleep(unsigned int msec) {
Sleep(msec);
}
/* Return the current time in msec since */
/* the first invokation of msec_time() */
/* (Is this based on timeGetTime() ? ) */
unsigned int msec_time() {
unsigned int rv;
static unsigned int startup = 0;
rv = GetTickCount();
if (startup == 0)
startup = rv;
return rv - startup;
}
/* Return the current time in usec */
/* since the first invokation of usec_time() */
/* Return -1.0 if not available */
double usec_time() {
double rv;
LARGE_INTEGER val;
static double scale = 0.0;
static LARGE_INTEGER startup;
if (scale == 0.0) {
if (QueryPerformanceFrequency(&val) == 0)
return -1.0;
scale = 1000000.0/val.QuadPart;
QueryPerformanceCounter(&val);
startup.QuadPart = val.QuadPart;
} else {
QueryPerformanceCounter(&val);
}
val.QuadPart -= startup.QuadPart;
rv = val.QuadPart * scale;
return rv;
}
#endif /* NT */
#if defined(UNIX)
/* Sleep for the given number of msec */
/* (Note that OS X 10.9+ App Nap can wreck this, unless */
/* it is turned off.) */
void msec_sleep(unsigned int msec) {
#ifdef NEVER
if (msec > 1000) {
unsigned int secs;
secs = msec / 1000;
msec = msec % 1000;
sleep(secs);
}
usleep(msec * 1000);
#else
struct timespec ts;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
nanosleep(&ts, NULL);
#endif
}
#if defined(__APPLE__) && !defined(CLOCK_MONOTONIC)
#include <mach/mach_time.h>
unsigned int msec_time() {
mach_timebase_info_data_t timebase;
static uint64_t startup = 0;
uint64_t time;
double msec;
time = mach_absolute_time();
if (startup == 0)
startup = time;
mach_timebase_info(&timebase);
time -= startup;
msec = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e6);
return (unsigned int)floor(msec + 0.5);
}
/* Return the current time in usec */
/* since the first invokation of usec_time() */
double usec_time() {
mach_timebase_info_data_t timebase;
static uint64_t startup = 0;
uint64_t time;
double usec;
time = mach_absolute_time();
if (startup == 0)
startup = time;
mach_timebase_info(&timebase);
time -= startup;
usec = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e3);
return usec;
}
#else
/* Return the current time in msec */
/* since the first invokation of msec_time() */
unsigned int msec_time() {
unsigned int rv;
static struct timespec startup = { 0, 0 };
struct timespec cv;
clock_gettime(CLOCK_MONOTONIC, &cv);
/* Set time to 0 on first invocation */
if (startup.tv_sec == 0 && startup.tv_nsec == 0)
startup = cv;
/* Subtract, taking care of carry */
cv.tv_sec -= startup.tv_sec;
if (startup.tv_nsec > cv.tv_nsec) {
cv.tv_sec--;
cv.tv_nsec += 1000000000;
}
cv.tv_nsec -= startup.tv_nsec;
/* Convert nsec to msec */
rv = cv.tv_sec * 1000 + cv.tv_nsec / 1000000;
return rv;
}
/* Return the current time in usec */
/* since the first invokation of usec_time() */
double usec_time() {
double rv;
static struct timespec startup = { 0, 0 };
struct timespec cv;
clock_gettime(CLOCK_MONOTONIC, &cv);
/* Set time to 0 on first invocation */
if (startup.tv_sec == 0 && startup.tv_nsec == 0)
startup = cv;
/* Subtract, taking care of carry */
cv.tv_sec -= startup.tv_sec;
if (startup.tv_nsec > cv.tv_nsec) {
cv.tv_sec--;
cv.tv_nsec += 1000000000;
}
cv.tv_nsec -= startup.tv_nsec;
/* Convert to usec */
rv = cv.tv_sec * 1000000.0 + cv.tv_nsec/1000;
return rv;
}
#endif
#endif /* UNIX */
/*******************************/
/* Debug convenience functions */
/*******************************/
#define DEB_MAX_CHAN 15
/* Print an int vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *debPiv(int di, int *p) {
static char buf[5][DEB_MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > DEB_MAX_CHAN)
di = DEB_MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%d", p[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Print a double color vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *debPdv(int di, double *p) {
static char buf[5][DEB_MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > DEB_MAX_CHAN)
di = DEB_MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%.8f", p[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Print a float color vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *debPfv(int di, float *p) {
static char buf[5][DEB_MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > DEB_MAX_CHAN)
di = DEB_MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%.8f", p[e]); bp += strlen(bp);
}
return buf[ix];
}
#undef DEB_MAX_CHAN
|