1 |
schoenebeck |
803 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
6 |
|
|
* <cuse@users.sourceforge.net> * |
7 |
|
|
* * |
8 |
|
|
* This library is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This library is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this library; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef __LIBGIG_HELPER_H__ |
25 |
|
|
#define __LIBGIG_HELPER_H__ |
26 |
|
|
|
27 |
schoenebeck |
809 |
#include <string.h> |
28 |
schoenebeck |
803 |
#include <string> |
29 |
|
|
#include <sstream> |
30 |
|
|
|
31 |
|
|
// *************** Helper Functions ************** |
32 |
|
|
// * |
33 |
|
|
|
34 |
|
|
template<class T> inline std::string ToString(T o) { |
35 |
|
|
std::stringstream ss; |
36 |
|
|
ss << o; |
37 |
|
|
return ss.str(); |
38 |
|
|
} |
39 |
|
|
|
40 |
schoenebeck |
809 |
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 |
schoenebeck |
803 |
#endif // __LIBGIG_HELPER_H__ |