24 |
#ifndef __LIBGIG_HELPER_H__ |
#ifndef __LIBGIG_HELPER_H__ |
25 |
#define __LIBGIG_HELPER_H__ |
#define __LIBGIG_HELPER_H__ |
26 |
|
|
27 |
|
#include <string.h> |
28 |
#include <string> |
#include <string> |
29 |
#include <sstream> |
#include <sstream> |
30 |
|
|
37 |
return ss.str(); |
return ss.str(); |
38 |
} |
} |
39 |
|
|
40 |
|
inline long Min(long A, long B) { |
41 |
|
return (A > B) ? B : A; |
42 |
|
} |
43 |
|
|
44 |
|
inline long Abs(long val) { |
45 |
|
return (val > 0) ? val : -val; |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
|
* Swaps the order of the data words in the given memory area |
50 |
|
* with a granularity given by \a WordSize. |
51 |
|
* |
52 |
|
* @param pData - pointer to the memory area to be swapped |
53 |
|
* @param AreaSize - size of the memory area to be swapped (in bytes) |
54 |
|
* @param WordSize - size of the data words (in bytes) |
55 |
|
*/ |
56 |
|
inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) { |
57 |
|
switch (WordSize) { // TODO: unefficient |
58 |
|
case 1: { |
59 |
|
uint8_t* pDst = (uint8_t*) pData; |
60 |
|
uint8_t cache; |
61 |
|
unsigned long lo = 0, hi = AreaSize - 1; |
62 |
|
for (; lo < hi; hi--, lo++) { |
63 |
|
cache = pDst[lo]; |
64 |
|
pDst[lo] = pDst[hi]; |
65 |
|
pDst[hi] = cache; |
66 |
|
} |
67 |
|
break; |
68 |
|
} |
69 |
|
case 2: { |
70 |
|
uint16_t* pDst = (uint16_t*) pData; |
71 |
|
uint16_t cache; |
72 |
|
unsigned long lo = 0, hi = (AreaSize >> 1) - 1; |
73 |
|
for (; lo < hi; hi--, lo++) { |
74 |
|
cache = pDst[lo]; |
75 |
|
pDst[lo] = pDst[hi]; |
76 |
|
pDst[hi] = cache; |
77 |
|
} |
78 |
|
break; |
79 |
|
} |
80 |
|
case 4: { |
81 |
|
uint32_t* pDst = (uint32_t*) pData; |
82 |
|
uint32_t cache; |
83 |
|
unsigned long lo = 0, hi = (AreaSize >> 2) - 1; |
84 |
|
for (; lo < hi; hi--, lo++) { |
85 |
|
cache = pDst[lo]; |
86 |
|
pDst[lo] = pDst[hi]; |
87 |
|
pDst[hi] = cache; |
88 |
|
} |
89 |
|
break; |
90 |
|
} |
91 |
|
default: { |
92 |
|
uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient |
93 |
|
unsigned long lo = 0, hi = AreaSize - WordSize; |
94 |
|
for (; lo < hi; hi -= WordSize, lo += WordSize) { |
95 |
|
memcpy(pCache, (uint8_t*) pData + lo, WordSize); |
96 |
|
memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize); |
97 |
|
memcpy((uint8_t*) pData + hi, pCache, WordSize); |
98 |
|
} |
99 |
|
delete[] pCache; |
100 |
|
break; |
101 |
|
} |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
#endif // __LIBGIG_HELPER_H__ |
#endif // __LIBGIG_HELPER_H__ |