tools.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "tools.h"
  9. int pcilib_isnumber(const char *str) {
  10. int i = 0;
  11. for (i = 0; str[i]; i++)
  12. if (!isdigit(str[i])) return 0;
  13. return 1;
  14. }
  15. int pcilib_isxnumber(const char *str) {
  16. int i = 0;
  17. if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
  18. for (; str[i]; i++)
  19. if (!isxdigit(str[i])) return 0;
  20. return 1;
  21. }
  22. uint16_t pcilib_swap16(uint16_t x) {
  23. return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
  24. }
  25. uint32_t pcilib_swap32(uint32_t x) {
  26. return ((x & 0xFF) << 24) | \
  27. ((x & 0xFF00) << 8) | \
  28. ((x & 0xFF0000) >> 8) | \
  29. ((x & 0xFF000000) >> 24);
  30. }
  31. uint64_t pcilib_swap64(uint64_t x) {
  32. return (((uint64_t)(x) << 56) | \
  33. (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
  34. (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
  35. (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
  36. (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
  37. (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
  38. (((uint64_t)(x) >> 40) & 0xff00ULL) | \
  39. ((uint64_t)(x) >> 56));
  40. }
  41. void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
  42. int i;
  43. switch (size) {
  44. case 1:
  45. if (src != dst) memcpy(dst, src, n);
  46. break;
  47. case 2:
  48. for (i = 0; i < n; i++) {
  49. ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
  50. }
  51. break;
  52. case 4:
  53. for (i = 0; i < n; i++) {
  54. ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
  55. }
  56. break;
  57. case 8:
  58. for (i = 0; i < n; i++) {
  59. ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
  60. }
  61. break;
  62. default:
  63. pcilib_error("Invalid word size: %i", size);
  64. }
  65. }
  66. void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
  67. int i;
  68. for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
  69. return dst;
  70. }
  71. void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
  72. uint32_t * plDst = (uint32_t *) dst;
  73. uint32_t const * plSrc = (uint32_t const *) src;
  74. while (len >= 4) {
  75. // *plDst = ntohl(*plSrc);
  76. *plDst = *plSrc;
  77. plSrc++;
  78. plDst++;
  79. len -= 4;
  80. }
  81. char * pcDst = (char *) plDst;
  82. char const * pcSrc = (char const *) plSrc;
  83. while (len--) {
  84. *pcDst++ = *pcSrc++;
  85. }
  86. return (dst);
  87. }
  88. void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
  89. uint64_t * plDst = (uint64_t *) dst;
  90. uint64_t const * plSrc = (uint64_t const *) src;
  91. while (len >= 8) {
  92. *plDst++ = *plSrc++;
  93. len -= 8;
  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. /*
  103. void *memcpy128(void * dst, void const * src, size_t len) {
  104. long pos = - (len>>2);
  105. char * plDst = (char *) dst - 4 * pos;
  106. char const * plSrc = (char const *) src - 4 * pos;
  107. if (pos) {
  108. __asm__ __volatile__ (
  109. "1: \n\t"
  110. "mov (%0,%2,4), %%edi \n\t"
  111. "mov %%edi, (%1,%2,4) \n\t"
  112. "inc %2 \n\t"
  113. "jnz 1b \n\t"
  114. :
  115. : "r" (plSrc), "r" (plDst), "r" (pos)
  116. : "%edi"
  117. );
  118. }
  119. long pos = - ((len>>4)<<4);
  120. char * plDst = (char *) dst - pos;
  121. char const * plSrc = (char const *) src - pos;
  122. if (pos) {
  123. __asm__ __volatile__ (
  124. "1: \n\t"
  125. // "movdqa (%0,%2), %%xmm0 \n\t"
  126. "mov (%0,%2), %%esi \n\t"
  127. "movd %%esi, %%xmm0 \n\t"
  128. "mov 4(%0,%2), %%esi \n\t"
  129. "movd %%esi, %%xmm1 \n\t"
  130. "mov 8(%0,%2), %%esi \n\t"
  131. "movd %%esi, %%xmm2 \n\t"
  132. "mov 12(%0,%2), %%esi \n\t"
  133. "movd %%esi, %%xmm3 \n\t"
  134. "pslldq $4, %%xmm1 \n\t"
  135. "por %%xmm1, %%xmm0 \n\t"
  136. "pslldq $8, %%xmm2 \n\t"
  137. "por %%xmm2, %%xmm0 \n\t"
  138. "pslldq $12, %%xmm3 \n\t"
  139. "por %%xmm3, %%xmm0 \n\t"
  140. "movntdq %%xmm0, (%1,%2) \n\t"
  141. "add $16, %2 \n\t"
  142. "jnz 1b \n\t"
  143. :
  144. : "r" (plSrc), "r" (plDst), "r" (pos)
  145. : "%rsi"
  146. );
  147. }
  148. len &= 0x3;
  149. char * pcDst = (char *) plDst;
  150. char const * pcSrc = (char const *) plSrc;
  151. while (len--) {
  152. *pcDst++ = *pcSrc++;
  153. }
  154. return (dst);
  155. }
  156. */
  157. void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
  158. uint32_t * plDst = (uint32_t *) dst;
  159. uint32_t const * plSrc = (uint32_t const *) src;
  160. int swap = 0;
  161. if (endianess)
  162. swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
  163. assert(size == 4); // only 32 bit at the moment
  164. if (swap) {
  165. while (n > 0) {
  166. *plDst = ntohl(*plSrc);
  167. ++plSrc;
  168. ++plDst;
  169. --n;
  170. }
  171. } else {
  172. while (n > 0) {
  173. *plDst = *plSrc;
  174. ++plSrc;
  175. ++plDst;
  176. --n;
  177. }
  178. }
  179. }
  180. int pcilib_get_page_mask() {
  181. int pagesize,pagemask,temp;
  182. pagesize = sysconf(_SC_PAGESIZE);
  183. for( pagemask=0, temp = pagesize; temp != 1; ) {
  184. temp = (temp >> 1);
  185. pagemask = (pagemask << 1)+1;
  186. }
  187. return pagemask;
  188. }