tools.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. #include <arpa/inet.h>
  8. #include <sys/time.h>
  9. #include "tools.h"
  10. int pcilib_isnumber(const char *str) {
  11. int i = 0;
  12. for (i = 0; str[i]; i++)
  13. if (!isdigit(str[i])) return 0;
  14. return 1;
  15. }
  16. int pcilib_isxnumber(const char *str) {
  17. int i = 0;
  18. if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
  19. for (; str[i]; i++)
  20. if (!isxdigit(str[i])) return 0;
  21. return 1;
  22. }
  23. int pcilib_isnumber_n(const char *str, size_t len) {
  24. int i = 0;
  25. for (i = 0; (str[i])&&(i < len); i++)
  26. if (!isdigit(str[i])) return 0;
  27. return 1;
  28. }
  29. int pcilib_isxnumber_n(const char *str, size_t len) {
  30. int i = 0;
  31. if ((len > 1)&&(str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
  32. for (; (str[i])&&(i < len); i++)
  33. if (!isxdigit(str[i])) return 0;
  34. return 1;
  35. }
  36. uint16_t pcilib_swap16(uint16_t x) {
  37. return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
  38. }
  39. uint32_t pcilib_swap32(uint32_t x) {
  40. return ((x & 0xFF) << 24) | \
  41. ((x & 0xFF00) << 8) | \
  42. ((x & 0xFF0000) >> 8) | \
  43. ((x & 0xFF000000) >> 24);
  44. }
  45. uint64_t pcilib_swap64(uint64_t x) {
  46. return (((uint64_t)(x) << 56) | \
  47. (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
  48. (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
  49. (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
  50. (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
  51. (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
  52. (((uint64_t)(x) >> 40) & 0xff00ULL) | \
  53. ((uint64_t)(x) >> 56));
  54. }
  55. void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
  56. int i;
  57. switch (size) {
  58. case 1:
  59. if (src != dst) memcpy(dst, src, n);
  60. break;
  61. case 2:
  62. for (i = 0; i < n; i++) {
  63. ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
  64. }
  65. break;
  66. case 4:
  67. for (i = 0; i < n; i++) {
  68. ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
  69. }
  70. break;
  71. case 8:
  72. for (i = 0; i < n; i++) {
  73. ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
  74. }
  75. break;
  76. default:
  77. pcilib_error("Invalid word size: %i", size);
  78. }
  79. }
  80. void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
  81. int i;
  82. for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
  83. return dst;
  84. }
  85. void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
  86. uint32_t * plDst = (uint32_t *) dst;
  87. uint32_t const * plSrc = (uint32_t const *) src;
  88. while (len >= 4) {
  89. // *plDst = ntohl(*plSrc);
  90. *plDst = *plSrc;
  91. plSrc++;
  92. plDst++;
  93. len -= 4;
  94. }
  95. char * pcDst = (char *) plDst;
  96. char const * pcSrc = (char const *) plSrc;
  97. while (len--) {
  98. *pcDst++ = *pcSrc++;
  99. }
  100. return (dst);
  101. }
  102. void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
  103. uint64_t * plDst = (uint64_t *) dst;
  104. uint64_t const * plSrc = (uint64_t const *) src;
  105. while (len >= 8) {
  106. *plDst++ = *plSrc++;
  107. len -= 8;
  108. }
  109. char * pcDst = (char *) plDst;
  110. char const * pcSrc = (char const *) plSrc;
  111. while (len--) {
  112. *pcDst++ = *pcSrc++;
  113. }
  114. return (dst);
  115. }
  116. /*
  117. void *memcpy128(void * dst, void const * src, size_t len) {
  118. long pos = - (len>>2);
  119. char * plDst = (char *) dst - 4 * pos;
  120. char const * plSrc = (char const *) src - 4 * pos;
  121. if (pos) {
  122. __asm__ __volatile__ (
  123. "1: \n\t"
  124. "mov (%0,%2,4), %%edi \n\t"
  125. "mov %%edi, (%1,%2,4) \n\t"
  126. "inc %2 \n\t"
  127. "jnz 1b \n\t"
  128. :
  129. : "r" (plSrc), "r" (plDst), "r" (pos)
  130. : "%edi"
  131. );
  132. }
  133. long pos = - ((len>>4)<<4);
  134. char * plDst = (char *) dst - pos;
  135. char const * plSrc = (char const *) src - pos;
  136. if (pos) {
  137. __asm__ __volatile__ (
  138. "1: \n\t"
  139. // "movdqa (%0,%2), %%xmm0 \n\t"
  140. "mov (%0,%2), %%esi \n\t"
  141. "movd %%esi, %%xmm0 \n\t"
  142. "mov 4(%0,%2), %%esi \n\t"
  143. "movd %%esi, %%xmm1 \n\t"
  144. "mov 8(%0,%2), %%esi \n\t"
  145. "movd %%esi, %%xmm2 \n\t"
  146. "mov 12(%0,%2), %%esi \n\t"
  147. "movd %%esi, %%xmm3 \n\t"
  148. "pslldq $4, %%xmm1 \n\t"
  149. "por %%xmm1, %%xmm0 \n\t"
  150. "pslldq $8, %%xmm2 \n\t"
  151. "por %%xmm2, %%xmm0 \n\t"
  152. "pslldq $12, %%xmm3 \n\t"
  153. "por %%xmm3, %%xmm0 \n\t"
  154. "movntdq %%xmm0, (%1,%2) \n\t"
  155. "add $16, %2 \n\t"
  156. "jnz 1b \n\t"
  157. :
  158. : "r" (plSrc), "r" (plDst), "r" (pos)
  159. : "%rsi"
  160. );
  161. }
  162. len &= 0x3;
  163. char * pcDst = (char *) plDst;
  164. char const * pcSrc = (char const *) plSrc;
  165. while (len--) {
  166. *pcDst++ = *pcSrc++;
  167. }
  168. return (dst);
  169. }
  170. */
  171. void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
  172. uint32_t * plDst = (uint32_t *) dst;
  173. uint32_t const * plSrc = (uint32_t const *) src;
  174. int swap = 0;
  175. if (endianess)
  176. swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
  177. assert(size == 4); // only 32 bit at the moment
  178. if (swap) {
  179. while (n > 0) {
  180. *plDst = ntohl(*plSrc);
  181. ++plSrc;
  182. ++plDst;
  183. --n;
  184. }
  185. } else {
  186. while (n > 0) {
  187. *plDst = *plSrc;
  188. ++plSrc;
  189. ++plDst;
  190. --n;
  191. }
  192. }
  193. }
  194. int pcilib_get_page_mask() {
  195. int pagesize,pagemask,temp;
  196. pagesize = sysconf(_SC_PAGESIZE);
  197. for( pagemask=0, temp = pagesize; temp != 1; ) {
  198. temp = (temp >> 1);
  199. pagemask = (pagemask << 1)+1;
  200. }
  201. return pagemask;
  202. }
  203. int calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
  204. gettimeofday(tv, NULL);
  205. tv->tv_usec += timeout%1000000;
  206. if (tv->tv_usec > 999999) {
  207. tv->tv_usec -= 1000000;
  208. tv->tv_sec = 1 + timeout/1000000;
  209. } else {
  210. tv->tv_sec = timeout/1000000;
  211. }
  212. return 0;
  213. }
  214. int check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
  215. int64_t res;
  216. struct timeval tvs;
  217. if (!tve->tv_sec) return 0;
  218. gettimeofday(&tvs, NULL);
  219. res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
  220. if (res < timeout) return 1;
  221. return 0;
  222. }
  223. pcilib_timeout_t calc_time_to_deadline(struct timeval *tve) {
  224. int64_t res;
  225. struct timeval tvs;
  226. gettimeofday(&tvs, NULL);
  227. res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
  228. if (res < 0) return 0;
  229. return res;
  230. }