1
#define _POSIX_C_SOURCE 200112L
12
#include <arpa/inet.h>
18
int pcilib_isnumber(const char *str) {
20
for (i = 0; str[i]; i++)
21
if (!isdigit(str[i])) return 0;
25
int pcilib_isxnumber(const char *str) {
28
if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
31
if (!isxdigit(str[i])) return 0;
36
int pcilib_isnumber_n(const char *str, size_t len) {
38
for (i = 0; (str[i])&&(i < len); i++)
39
if (!isdigit(str[i])) return 0;
43
int pcilib_isxnumber_n(const char *str, size_t len) {
46
if ((len > 1)&&(str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
48
for (; (str[i])&&(i < len); i++)
49
if (!isxdigit(str[i])) return 0;
55
uint16_t pcilib_swap16(uint16_t x) {
56
return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
59
uint32_t pcilib_swap32(uint32_t x) {
60
return ((x & 0xFF) << 24) | \
61
((x & 0xFF00) << 8) | \
62
((x & 0xFF0000) >> 8) | \
63
((x & 0xFF000000) >> 24);
66
uint64_t pcilib_swap64(uint64_t x) {
67
return (((uint64_t)(x) << 56) | \
68
(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
69
(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
70
(((uint64_t)(x) << 8) & 0xff00000000ULL) | \
71
(((uint64_t)(x) >> 8) & 0xff000000ULL) | \
72
(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
73
(((uint64_t)(x) >> 40) & 0xff00ULL) | \
74
((uint64_t)(x) >> 56));
77
void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
81
if (src != dst) memcpy(dst, src, n);
84
for (i = 0; i < n; i++) {
85
((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
89
for (i = 0; i < n; i++) {
90
((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
94
for (i = 0; i < n; i++) {
95
((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
99
pcilib_error("Invalid word size: %i", size);
103
void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
105
for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
109
void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
110
uint32_t * plDst = (uint32_t *) dst;
111
uint32_t const * plSrc = (uint32_t const *) src;
114
// *plDst = ntohl(*plSrc);
121
char * pcDst = (char *) plDst;
122
char const * pcSrc = (char const *) plSrc;
132
void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
133
uint64_t * plDst = (uint64_t *) dst;
134
uint64_t const * plSrc = (uint64_t const *) src;
141
char * pcDst = (char *) plDst;
142
char const * pcSrc = (char const *) plSrc;
152
void *memcpy128(void * dst, void const * src, size_t len) {
154
long pos = - (len>>2);
155
char * plDst = (char *) dst - 4 * pos;
156
char const * plSrc = (char const *) src - 4 * pos;
159
__asm__ __volatile__ (
161
"mov (%0,%2,4), %%edi \n\t"
162
"mov %%edi, (%1,%2,4) \n\t"
166
: "r" (plSrc), "r" (plDst), "r" (pos)
173
long pos = - ((len>>4)<<4);
174
char * plDst = (char *) dst - pos;
175
char const * plSrc = (char const *) src - pos;
178
__asm__ __volatile__ (
180
// "movdqa (%0,%2), %%xmm0 \n\t"
181
"mov (%0,%2), %%esi \n\t"
182
"movd %%esi, %%xmm0 \n\t"
183
"mov 4(%0,%2), %%esi \n\t"
184
"movd %%esi, %%xmm1 \n\t"
185
"mov 8(%0,%2), %%esi \n\t"
186
"movd %%esi, %%xmm2 \n\t"
187
"mov 12(%0,%2), %%esi \n\t"
188
"movd %%esi, %%xmm3 \n\t"
189
"pslldq $4, %%xmm1 \n\t"
190
"por %%xmm1, %%xmm0 \n\t"
191
"pslldq $8, %%xmm2 \n\t"
192
"por %%xmm2, %%xmm0 \n\t"
193
"pslldq $12, %%xmm3 \n\t"
194
"por %%xmm3, %%xmm0 \n\t"
196
"movntdq %%xmm0, (%1,%2) \n\t"
200
: "r" (plSrc), "r" (plDst), "r" (pos)
209
char * pcDst = (char *) plDst;
210
char const * pcSrc = (char const *) plSrc;
220
void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
221
uint32_t * plDst = (uint32_t *) dst;
222
uint32_t const * plSrc = (uint32_t const *) src;
227
swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
229
assert(size == 4); // only 32 bit at the moment
233
*plDst = ntohl(*plSrc);
250
int pcilib_get_page_mask() {
251
int pagesize,pagemask,temp;
253
pagesize = sysconf(_SC_PAGESIZE);
255
for( pagemask=0, temp = pagesize; temp != 1; ) {
257
pagemask = (pagemask << 1)+1;
262
int pcilib_get_cpu_count() {
268
err = sched_getaffinity(getpid(), sizeof(mask), &mask);
272
cpu_count = CPU_COUNT(&mask);
274
for (cpu_count = 0; cpu_count < CPU_SETSIZE; cpu_count++) {
275
if (!CPU_ISSET(cpu_count, &mask)) break;
279
if (!cpu_count) cpu_count = PCILIB_DEFAULT_CPU_COUNT;
284
int pcilib_add_timeout(struct timeval *tv, pcilib_timeout_t timeout) {
285
tv->tv_usec += timeout%1000000;
286
if (tv->tv_usec > 999999) {
287
tv->tv_usec -= 1000000;
288
tv->tv_sec += 1 + timeout/1000000;
290
tv->tv_sec += timeout/1000000;
296
int pcilib_calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
297
gettimeofday(tv, NULL);
298
pcilib_add_timeout(tv, timeout);
303
int pcilib_check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
307
if (!tve->tv_sec) return 0;
309
gettimeofday(&tvs, NULL);
310
res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
311
// Hm... Some problems comparing signed and unsigned. So, sign check first
312
if ((res < 0)||(res < timeout)) {
319
pcilib_timeout_t pcilib_calc_time_to_deadline(struct timeval *tve) {
323
gettimeofday(&tvs, NULL);
324
res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
326
if (res < 0) return 0;
330
int pcilib_sleep_until_deadline(struct timeval *tv) {
331
struct timespec wait;
332
pcilib_timeout_t duration;
334
duration = pcilib_calc_time_to_deadline(tv);
336
wait.tv_sec = duration / 1000000;
337
wait.tv_nsec = 1000 * (duration % 1000000);
338
nanosleep(&wait, NULL);
344
pcilib_timeout_t pcilib_timediff(struct timeval *tvs, struct timeval *tve) {
345
return ((tve->tv_sec - tvs->tv_sec)*1000000 + (tve->tv_usec - tvs->tv_usec));
348
int pcilib_timecmp(struct timeval *tv1, struct timeval *tv2) {
349
if (tv1->tv_sec > tv2->tv_sec) return 1;
350
else if (tv1->tv_sec < tv2->tv_sec) return -1;
351
else if (tv1->tv_usec > tv2->tv_usec) return 1;
352
else if (tv1->tv_usec < tv2->tv_usec) return -1;