tools.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #define _POSIX_C_SOURCE 200112L
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <sched.h>
  11. #include <arpa/inet.h>
  12. #include <sys/time.h>
  13. #include "tools.h"
  14. #include "error.h"
  15. int pcilib_isnumber(const char *str) {
  16. int i = 0;
  17. for (i = 0; str[i]; i++)
  18. if (!isdigit(str[i])) return 0;
  19. return 1;
  20. }
  21. int pcilib_isxnumber(const char *str) {
  22. int i = 0;
  23. if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
  24. for (; str[i]; i++)
  25. if (!isxdigit(str[i])) return 0;
  26. return 1;
  27. }
  28. int pcilib_isnumber_n(const char *str, size_t len) {
  29. int i = 0;
  30. for (i = 0; (str[i])&&(i < len); i++)
  31. if (!isdigit(str[i])) return 0;
  32. return 1;
  33. }
  34. int pcilib_isxnumber_n(const char *str, size_t len) {
  35. int i = 0;
  36. if ((len > 1)&&(str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
  37. for (; (str[i])&&(i < len); i++)
  38. if (!isxdigit(str[i])) return 0;
  39. return 1;
  40. }
  41. uint16_t pcilib_swap16(uint16_t x) {
  42. return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
  43. }
  44. uint32_t pcilib_swap32(uint32_t x) {
  45. return ((x & 0xFF) << 24) | \
  46. ((x & 0xFF00) << 8) | \
  47. ((x & 0xFF0000) >> 8) | \
  48. ((x & 0xFF000000) >> 24);
  49. }
  50. uint64_t pcilib_swap64(uint64_t x) {
  51. return (((uint64_t)(x) << 56) | \
  52. (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
  53. (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
  54. (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
  55. (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
  56. (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
  57. (((uint64_t)(x) >> 40) & 0xff00ULL) | \
  58. ((uint64_t)(x) >> 56));
  59. }
  60. void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
  61. int i;
  62. switch (size) {
  63. case 1:
  64. if (src != dst) memcpy(dst, src, n);
  65. break;
  66. case 2:
  67. for (i = 0; i < n; i++) {
  68. ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
  69. }
  70. break;
  71. case 4:
  72. for (i = 0; i < n; i++) {
  73. ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
  74. }
  75. break;
  76. case 8:
  77. for (i = 0; i < n; i++) {
  78. ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
  79. }
  80. break;
  81. default:
  82. pcilib_error("Invalid word size: %i", size);
  83. }
  84. }
  85. void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
  86. int i;
  87. for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
  88. return dst;
  89. }
  90. void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
  91. uint32_t * plDst = (uint32_t *) dst;
  92. uint32_t const * plSrc = (uint32_t const *) src;
  93. while (len >= 4) {
  94. // *plDst = ntohl(*plSrc);
  95. *plDst = *plSrc;
  96. plSrc++;
  97. plDst++;
  98. len -= 4;
  99. }
  100. char * pcDst = (char *) plDst;
  101. char const * pcSrc = (char const *) plSrc;
  102. while (len--) {
  103. *pcDst++ = *pcSrc++;
  104. }
  105. return (dst);
  106. }
  107. void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
  108. uint64_t * plDst = (uint64_t *) dst;
  109. uint64_t const * plSrc = (uint64_t const *) src;
  110. while (len >= 8) {
  111. *plDst++ = *plSrc++;
  112. len -= 8;
  113. }
  114. char * pcDst = (char *) plDst;
  115. char const * pcSrc = (char const *) plSrc;
  116. while (len--) {
  117. *pcDst++ = *pcSrc++;
  118. }
  119. return (dst);
  120. }
  121. /*
  122. void *memcpy128(void * dst, void const * src, size_t len) {
  123. long pos = - (len>>2);
  124. char * plDst = (char *) dst - 4 * pos;
  125. char const * plSrc = (char const *) src - 4 * pos;
  126. if (pos) {
  127. __asm__ __volatile__ (
  128. "1: \n\t"
  129. "mov (%0,%2,4), %%edi \n\t"
  130. "mov %%edi, (%1,%2,4) \n\t"
  131. "inc %2 \n\t"
  132. "jnz 1b \n\t"
  133. :
  134. : "r" (plSrc), "r" (plDst), "r" (pos)
  135. : "%edi"
  136. );
  137. }
  138. long pos = - ((len>>4)<<4);
  139. char * plDst = (char *) dst - pos;
  140. char const * plSrc = (char const *) src - pos;
  141. if (pos) {
  142. __asm__ __volatile__ (
  143. "1: \n\t"
  144. // "movdqa (%0,%2), %%xmm0 \n\t"
  145. "mov (%0,%2), %%esi \n\t"
  146. "movd %%esi, %%xmm0 \n\t"
  147. "mov 4(%0,%2), %%esi \n\t"
  148. "movd %%esi, %%xmm1 \n\t"
  149. "mov 8(%0,%2), %%esi \n\t"
  150. "movd %%esi, %%xmm2 \n\t"
  151. "mov 12(%0,%2), %%esi \n\t"
  152. "movd %%esi, %%xmm3 \n\t"
  153. "pslldq $4, %%xmm1 \n\t"
  154. "por %%xmm1, %%xmm0 \n\t"
  155. "pslldq $8, %%xmm2 \n\t"
  156. "por %%xmm2, %%xmm0 \n\t"
  157. "pslldq $12, %%xmm3 \n\t"
  158. "por %%xmm3, %%xmm0 \n\t"
  159. "movntdq %%xmm0, (%1,%2) \n\t"
  160. "add $16, %2 \n\t"
  161. "jnz 1b \n\t"
  162. :
  163. : "r" (plSrc), "r" (plDst), "r" (pos)
  164. : "%rsi"
  165. );
  166. }
  167. len &= 0x3;
  168. char * pcDst = (char *) plDst;
  169. char const * pcSrc = (char const *) plSrc;
  170. while (len--) {
  171. *pcDst++ = *pcSrc++;
  172. }
  173. return (dst);
  174. }
  175. */
  176. void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
  177. uint32_t * plDst = (uint32_t *) dst;
  178. uint32_t const * plSrc = (uint32_t const *) src;
  179. int swap = 0;
  180. if (endianess)
  181. swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
  182. assert(size == 4); // only 32 bit at the moment
  183. if (swap) {
  184. while (n > 0) {
  185. *plDst = ntohl(*plSrc);
  186. ++plSrc;
  187. ++plDst;
  188. --n;
  189. }
  190. } else {
  191. while (n > 0) {
  192. *plDst = *plSrc;
  193. ++plSrc;
  194. ++plDst;
  195. --n;
  196. }
  197. }
  198. return dst;
  199. }
  200. int pcilib_get_page_mask() {
  201. int pagesize,pagemask,temp;
  202. pagesize = sysconf(_SC_PAGESIZE);
  203. for( pagemask=0, temp = pagesize; temp != 1; ) {
  204. temp = (temp >> 1);
  205. pagemask = (pagemask << 1)+1;
  206. }
  207. return pagemask;
  208. }
  209. int pcilib_get_cpu_count() {
  210. int err;
  211. int cpu_count;
  212. cpu_set_t mask;
  213. err = sched_getaffinity(getpid(), sizeof(mask), &mask);
  214. if (err) return 1;
  215. #ifdef CPU_COUNT
  216. cpu_count = CPU_COUNT(&mask);
  217. #else
  218. for (cpu_count = 0; cpu_count < CPU_SETSIZE; cpu_count++) {
  219. if (!CPU_ISSET(cpu_count, &mask)) break;
  220. }
  221. #endif
  222. if (!cpu_count) cpu_count = PCILIB_DEFAULT_CPU_COUNT;
  223. return cpu_count;
  224. }
  225. int pcilib_add_timeout(struct timeval *tv, pcilib_timeout_t timeout) {
  226. tv->tv_usec += timeout%1000000;
  227. if (tv->tv_usec > 999999) {
  228. tv->tv_usec -= 1000000;
  229. tv->tv_sec += 1 + timeout/1000000;
  230. } else {
  231. tv->tv_sec += timeout/1000000;
  232. }
  233. return 0;
  234. }
  235. int pcilib_calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
  236. gettimeofday(tv, NULL);
  237. pcilib_add_timeout(tv, timeout);
  238. return 0;
  239. }
  240. int pcilib_check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
  241. int64_t res;
  242. struct timeval tvs;
  243. if (!tve->tv_sec) return 0;
  244. gettimeofday(&tvs, NULL);
  245. res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
  246. // Hm... Some problems comparing signed and unsigned. So, sign check first
  247. if ((res < 0)||(res < timeout)) {
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. pcilib_timeout_t pcilib_calc_time_to_deadline(struct timeval *tve) {
  253. int64_t res;
  254. struct timeval tvs;
  255. gettimeofday(&tvs, NULL);
  256. res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
  257. if (res < 0) return 0;
  258. return res;
  259. }
  260. int pcilib_sleep_until_deadline(struct timeval *tv) {
  261. struct timespec wait;
  262. pcilib_timeout_t duration;
  263. duration = pcilib_calc_time_to_deadline(tv);
  264. wait.tv_sec = duration / 1000000;
  265. wait.tv_nsec = 1000 * (duration % 1000000);
  266. nanosleep(&wait, NULL);
  267. return 0;
  268. }
  269. pcilib_timeout_t pcilib_timediff(struct timeval *tvs, struct timeval *tve) {
  270. return ((tve->tv_sec - tvs->tv_sec)*1000000 + (tve->tv_usec - tvs->tv_usec));
  271. }
  272. int pcilib_timecmp(struct timeval *tv1, struct timeval *tv2) {
  273. if (tv1->tv_sec > tv2->tv_sec) return 1;
  274. else if (tv1->tv_sec > tv2->tv_sec) return -1;
  275. else if (tv1->tv_usec > tv2->tv_usec) return 1;
  276. else if (tv1->tv_usec < tv2->tv_usec) return -1;
  277. return 0;
  278. }