Debian Codegraph @

src/common/swap_bytes_impl.h

raw
1 /*
2 swap_bytes: Swap byte order of samples in a buffer.
3
4 copyright 2018 by the mpg123 project
5 licensed under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7
8 initially written by Thomas Orgis
9
10 This is C source to include in your code to get the function named
11 swap_bytes. It is serves intentional duplication in libmpg123 and
12 libsyn123 to avoid introducing a dependency between them. This function
13 is too small for that.
14 */
15
16 /* Other headers are already included! */
17
18 /* Optionally use platform-specific byteswap macros. */
19 #ifdef HAVE_BYTESWAP_H
20 #include <byteswap.h>
21 #endif
22
23 /* Plain stupid swapping of elements in a byte array. */
24 /* This is the fallback when there is no native bswap macro. */
25 #define SWAP(a,b) tmp = p[a]; p[a] = p[b]; p[b] = tmp;
26
27 /* Convert samplecount elements of samplesize bytes each in buffer buf. */
28 static void swap_bytes(void *buf, size_t samplesize, size_t samplecount)
29 {
30 unsigned char *p = buf;
31 unsigned char *pend = (unsigned char*)buf+samplesize*samplecount;
32 unsigned char tmp;
33
34 if(samplesize < 2)
35 return;
36 switch(samplesize)
37 {
38 case 2: /* AB -> BA */
39 #ifdef HAVE_BYTESWAP_H
40 {
41 uint16_t* pp = (uint16_t*)p;
42 for(; pp<(uint16_t*)pend; ++pp)
43 *pp = bswap_16(*pp);
44 }
45 #else
46 for(; p<pend; p+=2)
47 {
48 SWAP(0,1)
49 }
50 #endif
51 break;
52 case 3: /* ABC -> CBA */
53 for(; p<pend; p+=3)
54 {
55 SWAP(0,2)
56 }
57 break;
58 case 4: /* ABCD -> DCBA */
59 #ifdef HAVE_BYTESWAP_H
60 {
61 uint32_t* pp = (uint32_t*)p;
62 for(; pp<(uint32_t*)pend; ++pp)
63 *pp = bswap_32(*pp);
64 }
65 #else
66 for(; p<pend; p+=4)
67 {
68 SWAP(0,3)
69 SWAP(1,2)
70 }
71 #endif
72 break;
73 case 8: /* ABCDEFGH -> HGFEDCBA */
74 #ifdef HAVE_BYTESWAP_H
75 {
76 uint64_t* pp = (uint64_t*)p;
77 for(; pp<(uint64_t*)pend; ++pp)
78 *pp = bswap_64(*pp);
79 }
80 #else
81 for(; p<pend; p+=8)
82 {
83 SWAP(0,7)
84 SWAP(1,6)
85 SWAP(2,5)
86 SWAP(3,4)
87 }
88 #endif
89 break;
90 /* All the weird choices with the full nested loop. */
91 default:
92 for(; p<pend; p+=samplesize)
93 {
94 size_t j;
95 for(j=0; j<samplesize/2; ++j)
96 {
97 SWAP(j, samplesize-j-1)
98 }
99 }
100 }
101 }
102
103 #undef SWAP